Skip to content

Commit

Permalink
ODB-538: Explicitly change column labelling in SQL statements
Browse files Browse the repository at this point in the history
  • Loading branch information
simondsmart committed Oct 17, 2023
1 parent ecd3690 commit 95658ad
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 36 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.6
1.5.0
10 changes: 10 additions & 0 deletions src/odc/ODBAPISettings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "eckit/log/Log.h"
#include "eckit/thread/ThreadSingleton.h"
#include "eckit/utils/StringTools.h"
#include "eckit/sql/LibEcKitSQL.h"

#include "odc/LibOdc.h"
#include "odc/ODBAPISettings.h"
Expand Down Expand Up @@ -88,12 +89,21 @@ string odc::ODBAPISettings::fileInHome(const string& fileName)

void odc::ODBAPISettings::treatIntegersAsDoubles(bool flag) {
integersAsDoubles_ = flag;
LibEcKitSQL::instance().treatIntegersAsDoubles(flag);
}

bool odc::ODBAPISettings::integersAsDoubles() const {
return integersAsDoubles_;
}

bool odc::ODBAPISettings::fullyQualifySQLColumnNames() const {
return LibEcKitSQL::instance().fullyQualifyColumnNames();
}

void odc::ODBAPISettings::fullyQualifySQLColumnNames(bool flag) {
LibEcKitSQL::instance().fullyQualifyColumnNames(flag);
}

void debugMeNow() {
Log::info() << "Debug me now" << endl;
odc::ODBAPISettings::debug = true;
Expand Down
3 changes: 3 additions & 0 deletions src/odc/ODBAPISettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class ODBAPISettings : private eckit::NonCopyable {
void treatIntegersAsDoubles(bool flag);
bool integersAsDoubles() const;

bool fullyQualifySQLColumnNames() const;
void fullyQualifySQLColumnNames(bool flag);

static bool debug;

private:
Expand Down
4 changes: 4 additions & 0 deletions src/odc/WriterBufferingIterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ WriterBufferingIterator::WriterBufferingIterator(Owner &owner, DataHandle *dh, b
columnOffsets_(0),
columnByteSizes_(0),
nrows_(0),
nrowsInFrame_(0),
path_(owner.path()),
initialisedColumns_(false),
properties_(),
Expand All @@ -63,6 +64,7 @@ WriterBufferingIterator::WriterBufferingIterator(Owner &owner, DataHandle &dh, b
columnOffsets_(0),
columnByteSizes_(0),
nrows_(0),
nrowsInFrame_(0),
path_(owner.path()),
initialisedColumns_(false),
properties_(),
Expand Down Expand Up @@ -155,6 +157,7 @@ void WriterBufferingIterator::allocRowsBuffer()
rowByteSize_ = rowDataSizeDoubles() * sizeof(double);
rowsBuffer_ = Buffer(rowsBufferSize_ * rowByteSize_);
nextRowInBuffer_ = reinterpret_cast<unsigned char*>(rowsBuffer_.data());
nrowsInFrame_ = 0;
}

void WriterBufferingIterator::writeHeader()
Expand Down Expand Up @@ -359,6 +362,7 @@ void WriterBufferingIterator::flush()
// Reset the write buffers

nextRowInBuffer_ = reinterpret_cast<unsigned char*>(rowsBuffer_.data());
nrowsInFrame_ = 0;

// This is a bad place to be. We need to reset the coders in the columns, not clone
// the existing ones (which have been optimised).
Expand Down
8 changes: 3 additions & 5 deletions src/odc/WriterBufferingIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class WriterBufferingIterator : public eckit::HandleHolder
std::vector<eckit::PathName> outputFiles();
bool next();

unsigned long long nrowsInFrame() const { return nrowsInFrame_; }

// If we are encoding strings, and the relevant string column size changes, we need
// to restart the encoding process
void flushAndResetColumnSizes(const std::map<std::string, size_t>& resetColumnSizeDoubles);
Expand All @@ -114,6 +116,7 @@ class WriterBufferingIterator : public eckit::HandleHolder
size_t* columnOffsets_; // in doubles
size_t* columnByteSizes_;
unsigned long long nrows_;
unsigned long long nrowsInFrame_;

eckit::PathName path_;

Expand Down Expand Up @@ -172,8 +175,6 @@ void WriterBufferingIterator::pass1init(T& it, const T& end)
template<typename T>
unsigned long WriterBufferingIterator::pass1(T& it, const T& end)
{
LOG_DEBUG_LIB(LibOdc) << "WriterBufferingIterator::pass1" << std::endl;

pass1init(it, end);
writeHeader();

Expand All @@ -182,7 +183,6 @@ unsigned long WriterBufferingIterator::pass1(T& it, const T& end)
{
if (it->isNewDataset() && it->columns() != columns())
{
LOG_DEBUG_LIB(LibOdc) << "WriterBufferingIterator::pass1: Change of input metadata." << std::endl;
flush();
pass1init(it, end);
writeHeader();
Expand All @@ -191,10 +191,8 @@ unsigned long WriterBufferingIterator::pass1(T& it, const T& end)
writeRow(it->data(), it->columns().size());
}

LOG_DEBUG_LIB(LibOdc) << "Flushing rest of the buffer..." << std::endl;
flush();

LOG_DEBUG_LIB(LibOdc) << "WriterBufferingIterator::pass1: processed " << nrows << " row(s)." << std::endl;
ASSERT(close() == 0);
return nrows;
}
Expand Down
12 changes: 12 additions & 0 deletions src/odc/api/Odb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,22 @@ const std::map<std::string, std::string>& Frame::properties() const {

//----------------------------------------------------------------------------------------------------------------------

bool Settings::treatIntegersAsDoubles() {
return odc::ODBAPISettings::instance().integersAsDoubles();
}

void Settings::treatIntegersAsDoubles(bool flag) {
odc::ODBAPISettings::instance().treatIntegersAsDoubles(flag);
}

bool Settings::fullyQualifySQLColumnNames() {
return odc::ODBAPISettings::instance().fullyQualifySQLColumnNames();
}

void Settings::fullyQualifySQLColumnNames(bool flag) {
odc::ODBAPISettings::instance().fullyQualifySQLColumnNames(flag);
}

void Settings::setIntegerMissingValue(long val) {
odc::MDI::integerMDI(val);
}
Expand Down
12 changes: 12 additions & 0 deletions src/odc/api/Odb.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,23 @@ namespace api {
class Settings {
public: // methods

/** Determine whether integers are treated as doubles across the interface and internally
*/
static bool treatIntegersAsDoubles();

/** Sets treatment of integers in ODB-2 data as doubles
* \param flag Whether to treat integers as doubles (*true*) or longs (*false*)
*/
static void treatIntegersAsDoubles(bool flag);

/** Determine if column names is SQL query results are fully qualified, or returned as queried
*/
static bool fullyQualifySQLColumnNames();

/** Set if column names is SQL query results are fully qualified, or returned as queried
*/
static void fullyQualifySQLColumnNames(bool flag);

/** Returns the value that identifies a missing integer
* \returns Missing integer value
*/
Expand Down
9 changes: 9 additions & 0 deletions src/odc/api/odc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ int odc_integer_behaviour(int integerBehaviour) {
});
}

int odc_sql_column_behaviour(int columnBehaviour) {
return wrapApiFunction([columnBehaviour] {
if (columnBehaviour != ODC_SQL_COLUMNS_FULLY_QUALIFIED && columnBehaviour != ODC_SQL_COLUMNS_AS_REFERENCED) {
throw SeriousBug("ODC column behaviour must be either ODC_SQL_COLUMNS_FULLY_QUALIFIED or ODC_SQL_COLUMNS_AS_REFERENCED", Here());
}
Settings::fullyQualifySQLColumnNames(columnBehaviour == ODC_SQL_COLUMNS_FULLY_QUALIFIED);
});
}

int odc_set_failure_handler(odc_failure_handler_t handler, void* context) {
return wrapApiFunction([handler, context] {
g_failure_handler = handler;
Expand Down
23 changes: 17 additions & 6 deletions src/odc/api/odc.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,35 @@ extern "C" {
/** @{ */

/** Represent integers as doubles in the API (default) */
const int ODC_INTEGERS_AS_DOUBLES = 1; // this is the default
const int ODC_INTEGERS_AS_DOUBLES = 1; /* this is the default */

/** Represent integers as 64-bit integers in the API */
const int ODC_INTEGERS_AS_LONGS = 2;

/** Always fully qualify column names, including table names, in SQL query results */
const int ODC_SQL_COLUMNS_FULLY_QUALIFIED = 1; /* this is the default */

/** Always fully qualify column names, including table names, in SQL query results */
const int ODC_SQL_COLUMNS_AS_REFERENCED = 2;

/** Initialises API, must be called before any other function
* \note This is only required if being used from a context where **eckit::Main()** is not otherwise initialised.
* \returns Return code (#OdcErrorValues)
*/
int odc_initialise_api();

/** Sets treatment of integers in the odc API
* \param integerBehaviour Desired integer behaviour (#ODC_INTEGERS_AS_DOUBLES #ODC_INTEGERS_AS_LONGS)
* \returns Return code (#OdcErrorValues)
*/
int odc_integer_behaviour(int integerBehaviour);

/** Sets behaviour of column naming in SQL queries
* \param columnBehaviour Desired column naming behaviour (#ODC_SQL_COLUMNS_FULLY_QUALIFIED #ODC_SQL_COLUMNS_AS_REFERENCED)
* \returns Return code (#OdcErrorValues)
*/
int odc_sql_column_behaviour(int columnBehaviour);

/** @} */


Expand Down Expand Up @@ -90,8 +103,6 @@ enum OdcErrorValues {
*/
const char* odc_error_string(int err);

// int odc_abort_on_failure(bool abort); ///< @todo to remove

/** Error handler callback function signature
* \param context Error handler context
* \param error_code Error code (#OdcErrorValues)
Expand Down Expand Up @@ -139,8 +150,8 @@ int odc_column_type_count(int* count);
*/
int odc_column_type_name(int type, const char** type_name);

/// @todo In the top CMakelists.txt assert that in this system C long is 64 bit
/// @todo In the top CMakelists.txt assert that in this system C double is 64 bit
/** @todo In the top CMakelists.txt assert that in this system C long is 64 bit */
/** @todo In the top CMakelists.txt assert that in this system C double is 64 bit */


/** Sets the value that identifies a missing integer in the API
Expand Down Expand Up @@ -529,7 +540,7 @@ int odc_encoder_set_rows_per_frame(odc_encoder_t* encoder, long rows_per_frame);
*/
int odc_encoder_set_data_array(odc_encoder_t* encoder, const void* data, long width, long height, int columnMajorWidth);

/// @todo implement the int columnMajorWidth in the above function
/** @todo implement the int columnMajorWidth in the above function */

/** Adds a data column to current encoder
* \param encoder Encoder instance
Expand Down
5 changes: 5 additions & 0 deletions src/odc/core/ThreadSharedDataHandle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ void ThreadSharedDataHandle::close() {
internal_->dh_->close();
}

eckit::Length ThreadSharedDataHandle::size() {
ASSERT(internal_);
return internal_->dh_->size();
}

eckit::Length ThreadSharedDataHandle::estimate() {
ASSERT(internal_);
return internal_->dh_->estimate();
Expand Down
1 change: 1 addition & 0 deletions src/odc/core/ThreadSharedDataHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class ThreadSharedDataHandle : public eckit::DataHandle {
void close() override;

eckit::Length estimate() override;
eckit::Length size() override;
eckit::Offset position() override;
eckit::Offset seek(const eckit::Offset&) override;

Expand Down
2 changes: 0 additions & 2 deletions src/odc/tools/ODAHeaderTool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class OffsetsPrinter : public MDPrinter {
Length length (tbl.nextPosition() - tbl.startPosition());
o << offset << " " << length << " " << tbl.rowCount() << " " << tbl.columnCount() << std::endl;
}
private:
unsigned long headerCount_;
};

class DDLPrinter : public MDPrinter {
Expand Down
14 changes: 7 additions & 7 deletions tests/api/odc_encode_custom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ int main(int argc, char** argv) {

// Define all column names, their types and sizes
std::vector<ColumnInfo> columns = {
ColumnInfo{std::string("expver"), ColumnType(STRING), 8},
ColumnInfo{std::string("date@hdr"), ColumnType(INTEGER), sizeof(int64_t)},
ColumnInfo{std::string("statid@hdr"), ColumnType(STRING), 8},
ColumnInfo{std::string("wigos@hdr"), ColumnType(STRING), 16},
ColumnInfo{std::string("obsvalue@body"), ColumnType(REAL), sizeof(double)},
ColumnInfo{std::string("integer_missing"), ColumnType(INTEGER), sizeof(int64_t)},
ColumnInfo{std::string("double_missing"), ColumnType(REAL), sizeof(double)},
ColumnInfo{std::string("expver"), ColumnType(STRING), 8, {}},
ColumnInfo{std::string("date@hdr"), ColumnType(INTEGER), sizeof(int64_t), {}},
ColumnInfo{std::string("statid@hdr"), ColumnType(STRING), 8, {}},
ColumnInfo{std::string("wigos@hdr"), ColumnType(STRING), 16, {}},
ColumnInfo{std::string("obsvalue@body"), ColumnType(REAL), sizeof(double), {}},
ColumnInfo{std::string("integer_missing"), ColumnType(INTEGER), sizeof(int64_t), {}},
ColumnInfo{std::string("double_missing"), ColumnType(REAL), sizeof(double), {}},
ColumnInfo{std::string("bitfield_column"), ColumnType(BITFIELD), sizeof(int64_t), bitfields},
};

Expand Down
Loading

0 comments on commit 95658ad

Please sign in to comment.