Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/odc/api/Odb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -687,12 +687,45 @@ void encode(DataHandle& out, const std::vector<ColumnInfo>& 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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this also apply to other places?

Seems like a general problem of the SQL interface?


odc::Select odb(sql, in);
odc::Select::iterator it = odb.begin();
odc::Select::iterator end = odb.end();
Expand Down
4 changes: 4 additions & 0 deletions src/odc/api/Odb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -366,6 +368,8 @@ void encode(eckit::DataHandle& out, const std::vector<ColumnInfo>& 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
Expand Down
75 changes: 75 additions & 0 deletions tests/api/read.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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<int64_t> RESTRICTED{1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0};
std::vector<int64_t> 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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you set a global setting?


std::unique_ptr<eckit::DataHandle> dh(path.fileHandle());
dh->openForWrite(0);
eckit::AutoClose closer(*dh);

std::vector<odc::api::ColumnInfo> columns{
{"values@body", odc::api::ColumnType::INTEGER, sizeof(int64_t), {}},
{"restricted@hdr", odc::api::ColumnType::INTEGER, sizeof(int64_t), {}},
};
std::vector<odc::api::ConstStridedData> 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<long> RESTRICTED_VALUES{1, 2, 3, 4, 6, 9};
const std::set<long> UNRESTRICTED_VALUES{5, 7, 8, 10, 11, 12};

struct Query {
std::string sql;
std::set<long> expected;
};
const std::vector<Query> 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<eckit::DataHandle> 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<std::string>({"values", "restricted"}), /* onlyConst */ false);
EXPECT(s.getIntegerValues("values") == query.expected);
}

EXPECT(!reader.next());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it's never resetted?

}
}

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

// CASE("Decode an entire ODB file") {
//
// odc::api::Odb o("../2000010106-reduced.odb");
Expand Down
Loading