Skip to content
Merged
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
7 changes: 7 additions & 0 deletions src/rhydb/query_engine/scalar_expressions/at.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ std::vector<QueryTestScenario> 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"});
Expand Down
14 changes: 12 additions & 2 deletions src/rhydb/query_engine/scalar_expressions/comparison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ std::optional<SequencePositionAndLiteral> 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<ScalarExpression> falseOrNullIfNull(const ColumnIdentifier& column) {
ScalarExpressionVector children;
children.emplace_back(std::make_unique<IsNull>(column));
children.emplace_back(std::make_unique<NullLiteral>(schema::DataType::BOOL));
return std::make_unique<And>(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,
Expand All @@ -154,13 +164,13 @@ std::unique_ptr<ScalarExpression> rewriteSequencePositionComparison(
if (comparator == Comparator::EQUALS) {
return std::make_unique<Negation>(std::make_unique<IsNull>(split.column));
}
return std::make_unique<BoolLiteral>(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<BoolLiteral>(false);
return falseOrNullIfNull(split.column);
}
return std::make_unique<Negation>(std::make_unique<IsNull>(split.column));
}
Expand Down
Loading