Skip to content

Commit

Permalink
Boolean not filter missing parentheses. (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
marikomedlock committed Sep 13, 2024
1 parent 4b66bb5 commit c29f1eb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ default String booleanAndOrFilterSql(
}

default String booleanNotFilterSql(String subFilterSql) {
return "NOT " + subFilterSql;
return "NOT (" + subFilterSql + ")";
}

default String logicalOperatorSql(BooleanAndOrFilter.LogicalOperator operator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,39 @@ void booleanNotFilter() throws IOException {
assertSqlMatchesWithTableNameOnly("booleanNotFilter", listQueryResult.getSql(), table);
}

@Test
void booleanNotFilterWithNestedBooleanAndFilter() throws IOException {
Entity entity = underlay.getPrimaryEntity();
AttributeFilter attributeFilter1 =
new AttributeFilter(
underlay,
entity,
entity.getAttribute("year_of_birth"),
BinaryOperator.NOT_EQUALS,
Literal.forInt64(1_956L));
AttributeFilter attributeFilter2 =
new AttributeFilter(
underlay,
entity,
entity.getAttribute("gender"),
BinaryOperator.EQUALS,
Literal.forInt64(8_532L));
BooleanNotFilter booleanNotFilter =
new BooleanNotFilter(
new BooleanAndOrFilter(
BooleanAndOrFilter.LogicalOperator.AND,
List.of(attributeFilter1, attributeFilter2)));
AttributeField simpleAttribute =
new AttributeField(underlay, entity, entity.getAttribute("year_of_birth"), false);
ListQueryResult listQueryResult =
bqQueryRunner.run(
ListQueryRequest.dryRunAgainstIndexData(
underlay, entity, List.of(simpleAttribute), booleanNotFilter, null, null));
BQTable table = underlay.getIndexSchema().getEntityMain(entity.getName()).getTablePointer();
assertSqlMatchesWithTableNameOnly(
"booleanNotFilterWithNestedBooleanAndFilter", listQueryResult.getSql(), table);
}

@Test
void booleanAndOrFilterWithSwapFields() throws IOException {
// e.g. SELECT occurrence BOOLEAN LOGIC FILTER ON condition (foreign key is on the occurrence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void booleanNotFilter() {
apiTranslator.binaryFilterSql(
field, BinaryOperator.LESS_THAN_OR_EQUAL, val, tableAlias, sqlParams);
String sql = apiTranslator.booleanNotFilterSql(subFilterSql);
assertEquals("NOT tableAlias.columnName <= @val0", sql);
assertEquals("NOT (tableAlias.columnName <= @val0)", sql);
assertEquals(ImmutableMap.of("val0", val), sqlParams.getParams());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
FROM
${ENT_person}
WHERE
NOT year_of_birth != @val0
NOT (year_of_birth != @val0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

SELECT
year_of_birth
FROM
${ENT_person}
WHERE
NOT ((year_of_birth != @val0)
AND (gender = @val1))
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
FROM
${ENT_conditionOccurrence}
WHERE
NOT condition IN (SELECT
NOT (condition IN (SELECT
descendant
FROM
${HAD_condition_default}
WHERE
ancestor = @val0
UNION
ALL SELECT
@val1)
@val1))

0 comments on commit c29f1eb

Please sign in to comment.