Skip to content

Commit

Permalink
[CALCITE-4239] RelMdUniqueKeys returns wrong unique keys for Aggregat…
Browse files Browse the repository at this point in the history
…e with grouping sets
  • Loading branch information
chunweilei committed Sep 14, 2020
1 parent 99c0fef commit 1ae20f3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,13 @@ public Boolean areColumnsUnique(Join rel, RelMetadataQuery mq,

public Boolean areColumnsUnique(Aggregate rel, RelMetadataQuery mq,
ImmutableBitSet columns, boolean ignoreNulls) {
columns = decorateWithConstantColumnsFromPredicates(columns, rel, mq);
// group by keys form a unique key
ImmutableBitSet groupKey = ImmutableBitSet.range(rel.getGroupCount());
return columns.contains(groupKey);
if (Aggregate.isSimple(rel) || ignoreNulls) {
columns = decorateWithConstantColumnsFromPredicates(columns, rel, mq);
// group by keys form a unique key
ImmutableBitSet groupKey = ImmutableBitSet.range(rel.getGroupCount());
return columns.contains(groupKey);
}
return null;
}

public Boolean areColumnsUnique(Values rel, RelMetadataQuery mq,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,14 @@ public Set<ImmutableBitSet> getUniqueKeys(Join rel, RelMetadataQuery mq,

public Set<ImmutableBitSet> getUniqueKeys(Aggregate rel, RelMetadataQuery mq,
boolean ignoreNulls) {
// group by keys form a unique key
return ImmutableSet.of(rel.getGroupSet());
if (Aggregate.isSimple(rel) || ignoreNulls) {
// group by keys form a unique key
return ImmutableSet.of(rel.getGroupSet());
} else {
// If the aggregate has grouping sets, all group by keys might be null which means group by
// keys do not form a unique key.
return ImmutableSet.of();
}
}

public Set<ImmutableBitSet> getUniqueKeys(Union rel, RelMetadataQuery mq,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,12 @@ private void checkColumnUniquenessForFilterWithConstantColumns(String sql) {
ImmutableSet.of(ImmutableBitSet.of(0)));
}

@Test void testGroupingSets() {
checkGetUniqueKeys("select deptno, sal, count(*) from emp\n"
+ "group by GROUPING SETS (deptno, sal)",
ImmutableSet.of());
}

@Test void testUnion() {
checkGetUniqueKeys("select deptno from emp\n"
+ "union\n"
Expand Down

0 comments on commit 1ae20f3

Please sign in to comment.