diff --git a/src/rhydb/query_engine/scalar_expressions/at.test.cpp b/src/rhydb/query_engine/scalar_expressions/at.test.cpp index 3f42f9761..cfa14bbce 100644 --- a/src/rhydb/query_engine/scalar_expressions/at.test.cpp +++ b/src/rhydb/query_engine/scalar_expressions/at.test.cpp @@ -240,6 +240,13 @@ std::vector filterScenarios() { add("NOT_EQUALS_REFERENCE", "segment1[4] <> 'T'", {"id_3"}); add("NOT_EQUALS_INVALID_SYMBOL", "segment1[1] <> 'X'", {"id_0", "id_2", "id_3"}); add("NOT_EQUALS_OUT_OF_BOUNDS", "segment1[5] <> 'A'", {"id_0", "id_2", "id_3"}); + add("EQUALS_EMPTY_OUT_OF_BOUNDS", "segment1[5] = ''", {"id_0", "id_2", "id_3"}); + add("NOT_EQUALS_EMPTY_OUT_OF_BOUNDS", "segment1[5] <> ''", {}); + // A comparison that can never match is still null on a null sequence, so its negation must not + // select id_1. + add("NEGATED_EQUALS_OUT_OF_BOUNDS", "!(segment1[5] = 'A')", {"id_0", "id_2", "id_3"}); + add("NEGATED_EQUALS_INVALID_SYMBOL", "!(segment1[1] = 'X')", {"id_0", "id_2", "id_3"}); + add("NEGATED_NOT_EQUALS_EMPTY_OUT_OF_BOUNDS", "!(segment1[5] <> '')", {"id_0", "id_2", "id_3"}); add("AMINO_ACID_EQUALS", "gene1[1] = '*'", {"id_0"}); add("AMINO_ACID_EQUALS_ANY", "gene1[1] = 'X'", {"id_2"}); add("AMINO_ACID_NOT_EQUALS", "gene1[1] <> '*'", {"id_2"}); diff --git a/src/rhydb/query_engine/scalar_expressions/comparison.cpp b/src/rhydb/query_engine/scalar_expressions/comparison.cpp index f17b745a7..7c2dd2ed7 100644 --- a/src/rhydb/query_engine/scalar_expressions/comparison.cpp +++ b/src/rhydb/query_engine/scalar_expressions/comparison.cpp @@ -129,6 +129,16 @@ std::optional splitSequencePositionAndLiteral( }; } +/// False on rows that have a sequence and null on rows whose sequence is null - the value of a +/// position comparison that can never match. A plain `false` would be wrong under a negation, which +/// must not select the null rows. +std::unique_ptr falseOrNullIfNull(const ColumnIdentifier& column) { + ScalarExpressionVector children; + children.emplace_back(std::make_unique(column)); + children.emplace_back(std::make_unique(schema::DataType::BOOL)); + return std::make_unique(std::move(children)); +} + /// Rewrites `sequence.at(position) = 'X'` (or `<>`) into the symbol filters that `nucleotideEquals` /// / `aminoAcidEquals` use, so the predicate is answered from the sequence index instead of /// reconstructing every sequence. The result must match the Arrow evaluation of the comparison, @@ -154,13 +164,13 @@ std::unique_ptr rewriteSequencePositionComparison( if (comparator == Comparator::EQUALS) { return std::make_unique(std::make_unique(split.column)); } - return std::make_unique(false); + return falseOrNullIfNull(split.column); } if (!in_bounds || !is_decoded_symbol) { // The extracted character can never equal the literal, so `=` matches nothing and `<>` // matches every row that has a sequence. if (comparator == Comparator::EQUALS) { - return std::make_unique(false); + return falseOrNullIfNull(split.column); } return std::make_unique(std::make_unique(split.column)); }