diff --git a/src/odc/api/Odb.cc b/src/odc/api/Odb.cc index 86c282b8..74e5d391 100644 --- a/src/odc/api/Odb.cc +++ b/src/odc/api/Odb.cc @@ -687,12 +687,45 @@ void encode(DataHandle& out, const std::vector& columns, const std:: } +namespace { + +/// Temporarily overrides the (per-thread) treatIntegersAsDoubles setting, restoring it on destruction +class IntegersAsDoublesGuard { +public: + + explicit IntegersAsDoublesGuard(bool flag) : previous_(ODBAPISettings::instance().integersAsDoubles()) { + ODBAPISettings::instance().treatIntegersAsDoubles(flag); + } + + ~IntegersAsDoublesGuard() { ODBAPISettings::instance().treatIntegersAsDoubles(previous_); } + + IntegersAsDoublesGuard(const IntegersAsDoublesGuard&) = delete; + IntegersAsDoublesGuard& operator=(const IntegersAsDoublesGuard&) = delete; + +private: + + bool previous_; +}; + +} // namespace + size_t filter(const std::string& sql, eckit::DataHandle& in, eckit::DataHandle& out) { if (sql.empty()) { in.saveInto(out); } else { + // The SQL engine models every numeric value as a double, and evaluates expressions (comparisons, functions, + // arithmetic) on that basis. It is not aware of the int64_t in-memory representation of integer columns that + // is selected by treatIntegersAsDoubles(false). In that mode the integer 1 is held as the bit pattern 0x1, + // which is a denormal double. So 'x = 1' never matches, and 'x = 0' only matches by virtue of 0 and 0.0 sharing + // the same bits (and then matches everything if the platform flushes denormals to zero). + // + // The data crossing this function (in and out) is encoded ODB-2, where the in-memory representation does not + // matter, so we always run the query with integers-as-doubles. The (32-bit) integers are exactly representable + // as doubles, so nothing is lost. Frames read from the output are decoded according to the caller's setting. + const IntegersAsDoublesGuard guard(true); + odc::Select odb(sql, in); odc::Select::iterator it = odb.begin(); odc::Select::iterator end = odb.end(); diff --git a/src/odc/api/Odb.h b/src/odc/api/Odb.h index ae02300c..3f89498c 100644 --- a/src/odc/api/Odb.h +++ b/src/odc/api/Odb.h @@ -233,6 +233,8 @@ class Frame { /** Filters current frame according to an SQL-like query and returns another frame object * (which owns its own attached memory buffer) * buffer + * \note The query is evaluated independently of Settings::treatIntegersAsDoubles. The returned frame is decoded + * according to the setting in force when the frame is returned. * \param sql SQL query * \returns Frame object attached to a memory buffer */ @@ -366,6 +368,8 @@ void encode(eckit::DataHandle& out, const std::vector& columns, cons /** Filters ODB-2 data according to an SQL-like query and writes result into another data handle * \note Depending on the query, SQL filtering may not be appropriate to do on a per-Frame basis, as aggregate * values won't behave properly. This function allows filtering of an entire data stream. + * \note The query is evaluated independently of Settings::treatIntegersAsDoubles, as the data in and out are encoded + * ODB-2 streams. * \param sql SQL query * \param in Source data handle * \param out Target data handle diff --git a/tests/api/read.cc b/tests/api/read.cc index e164445c..71a8e683 100644 --- a/tests/api/read.cc +++ b/tests/api/read.cc @@ -14,6 +14,7 @@ #include "eckit/io/FileHandle.h" #include "eckit/testing/Test.h" +#include "odc/ODBAPISettings.h" #include "odc/api/Odb.h" #include "odc/api/odc.h" @@ -1283,6 +1284,80 @@ CASE("Test Span interface with and without treatIntegersAsDoubles") { // ------------------------------------------------------------------------------------------------------ +CASE("Test SQL filtering with and without treatIntegersAsDoubles") { + + // The SQL engine evaluates everything as doubles, so the query results must not depend on whether integers + // are decoded as int64_t (where the integer 1 is the denormal bit pattern 0x1) or as doubles. + + std::vector RESTRICTED{1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0}; + std::vector VALUES{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + + eckit::PathName path = "sql-filter-test-1"; + + { + odc::api::Settings::treatIntegersAsDoubles(false); + + std::unique_ptr dh(path.fileHandle()); + dh->openForWrite(0); + eckit::AutoClose closer(*dh); + + std::vector columns{ + {"values@body", odc::api::ColumnType::INTEGER, sizeof(int64_t), {}}, + {"restricted@hdr", odc::api::ColumnType::INTEGER, sizeof(int64_t), {}}, + }; + std::vector strides{ + {&VALUES[0], VALUES.size(), sizeof(int64_t), sizeof(int64_t)}, + {&RESTRICTED[0], RESTRICTED.size(), sizeof(int64_t), sizeof(int64_t)}, + }; + + odc::api::encode(*dh, columns, strides); + } + + const std::set RESTRICTED_VALUES{1, 2, 3, 4, 6, 9}; + const std::set UNRESTRICTED_VALUES{5, 7, 8, 10, 11, 12}; + + struct Query { + std::string sql; + std::set expected; + }; + const std::vector queries{ + {"select * where restricted = 1", RESTRICTED_VALUES}, + {"select * where restricted = 0", UNRESTRICTED_VALUES}, + {"select * where restricted <> 1", UNRESTRICTED_VALUES}, + {"select * where restricted > 0", RESTRICTED_VALUES}, + {"select * where restricted = 0 or restricted is null", UNRESTRICTED_VALUES}, + }; + + for (bool asDoubles : {false, true}) { + odc::api::Settings::treatIntegersAsDoubles(asDoubles); + + std::unique_ptr dh(path.fileHandle()); + dh->openForRead(); + eckit::AutoClose closer(*dh); + + odc::api::Reader reader(*dh, /* aggregated */ true); + odc::api::Frame frame = reader.next(); + EXPECT(frame.rowCount() == RESTRICTED.size()); + + for (const auto& query : queries) { + odc::api::Frame filtered = frame.filter(query.sql); + + // The setting of the caller must be unaffected, and the filtered frame decodes accordingly + EXPECT(odc::ODBAPISettings::instance().integersAsDoubles() == asDoubles); + + ASSERT(filtered); + EXPECT(filtered.rowCount() == query.expected.size()); + + odc::api::Span s = filtered.span(std::vector({"values", "restricted"}), /* onlyConst */ false); + EXPECT(s.getIntegerValues("values") == query.expected); + } + + EXPECT(!reader.next()); + } +} + +// ------------------------------------------------------------------------------------------------------ + // CASE("Decode an entire ODB file") { // // odc::api::Odb o("../2000010106-reduced.odb");