Skip to content

Commit 7518da9

Browse files
authored
query uses grouping index with Is_Null predicate (#3327)
1 parent 16776ea commit 7518da9

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/expressions/GroupByExpression.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,7 @@ private BooleanWithConstraint groupingSubsumedBy(@Nonnull final Quantifier candi
471471
}
472472

473473
return comparisons.stream()
474-
.anyMatch(comparison -> comparison.getType().isEquality() &&
475-
comparison.getType() == Comparisons.Type.EQUALS);
474+
.anyMatch(comparison -> comparison.getType().isEquality());
476475
}
477476
return false;
478477
});

fdb-relational-core/src/test/java/com/apple/foundationdb/relational/recordlayer/query/GroupByQueryTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.net.URI;
3636

3737
import static com.apple.foundationdb.relational.recordlayer.query.QueryTestUtils.insertT1Record;
38+
import static com.apple.foundationdb.relational.recordlayer.query.QueryTestUtils.insertT1RecordColAIsNull;
3839

3940
public class GroupByQueryTests {
4041

@@ -46,6 +47,38 @@ public GroupByQueryTests() {
4647
Utils.enableCascadesDebugger();
4748
}
4849

50+
@Test
51+
void isNullPredicateUsesGroupIndex() throws Exception {
52+
final String schemaTemplate =
53+
"CREATE TABLE T1(pk bigint, a bigint, b bigint, c bigint, PRIMARY KEY(pk))\n" +
54+
"CREATE INDEX idx1 as select a, b, c from t1 order by a, b, c\n" +
55+
"CREATE INDEX sum_idx as select sum(c) from t1 group by a\n";
56+
try (var ddl = Ddl.builder().database(URI.create("/TEST/QT")).relationalExtension(relationalExtension).schemaTemplate(schemaTemplate).build()) {
57+
try (var statement = ddl.setSchemaAndGetConnection().createStatement()) {
58+
insertT1Record(statement, 2, 1, 1, 20);
59+
insertT1Record(statement, 3, 1, 2, 5);
60+
insertT1Record(statement, 4, 1, 2, 15);
61+
insertT1Record(statement, 5, 1, 2, 5);
62+
insertT1Record(statement, 6, 2, 1, 10);
63+
insertT1Record(statement, 7, 2, 1, 40);
64+
insertT1Record(statement, 8, 2, 1, 20);
65+
insertT1Record(statement, 9, 2, 1, 90);
66+
insertT1RecordColAIsNull(statement, 10, 3, 10);
67+
insertT1RecordColAIsNull(statement, 11, 4, 35);
68+
Assertions.assertTrue(statement.execute("EXPLAIN SELECT sum(c) FROM T1 WHERE a is null"), "Did not return a result set from a select statement!");
69+
try (final RelationalResultSet resultSet = statement.getResultSet()) {
70+
ResultSetAssert.assertThat(resultSet).hasNextRow()
71+
.hasColumn("PLAN", "AISCAN(SUM_IDX [[null],[null]] BY_GROUP -> [_0: KEY:[0], _1: VALUE:[0]]) | MAP ((_._1 AS _0) AS _0) | ON EMPTY NULL | MAP (_._0._0 AS _0)");
72+
}
73+
Assertions.assertTrue(statement.execute("EXPLAIN SELECT sum(c) FROM T1 WHERE a = 1"), "Did not return a result set from a select statement!");
74+
try (final RelationalResultSet resultSet = statement.getResultSet()) {
75+
ResultSetAssert.assertThat(resultSet).hasNextRow()
76+
.hasColumn("PLAN", "AISCAN(SUM_IDX [EQUALS promote(@c11 AS LONG)] BY_GROUP -> [_0: KEY:[0], _1: VALUE:[0]]) | MAP ((_._1 AS _0) AS _0) | ON EMPTY NULL | MAP (_._0._0 AS _0)");
77+
}
78+
}
79+
}
80+
}
81+
4982
@Test
5083
void groupByClauseWithPredicateWorks() throws Exception {
5184
final String schemaTemplate =

fdb-relational-core/src/test/java/com/apple/foundationdb/relational/recordlayer/query/QueryTestUtils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,15 @@ public static RelationalStruct insertT1Record(@Nonnull final RelationalStatement
4242
Assertions.assertEquals(1, cnt, "Incorrect insertion count");
4343
return result;
4444
}
45+
46+
public static RelationalStruct insertT1RecordColAIsNull(@Nonnull final RelationalStatement statement, long pk, long b, long c) throws SQLException {
47+
var result = EmbeddedRelationalStruct.newBuilder()
48+
.addLong("PK", pk)
49+
.addLong("B", b)
50+
.addLong("C", c)
51+
.build();
52+
int cnt = statement.executeInsert("T1", result);
53+
Assertions.assertEquals(1, cnt, "Incorrect insertion count");
54+
return result;
55+
}
4556
}

0 commit comments

Comments
 (0)