Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,27 @@ public Boolean areColumnsUnique(Intersect rel, RelMetadataQuery mq,
}
}
}
return mq.areColumnsUnique(rel.getInput(), inputColumns.build(), ignoreNulls);
final ImmutableBitSet mappedColumns = inputColumns.build();
final ImmutableBitSet nonGroupColumns = mappedColumns.except(rel.getGroupSet());
// Check the input columns together if their values come from the same row.
if (canPassThroughKey(rel, mq, nonGroupColumns)) {
return mq.areColumnsUnique(rel.getInput(), mappedColumns, ignoreNulls);
}

// Each aggregate value can still form a key with the grouping columns.
final ImmutableBitSet groupColumns = mappedColumns.intersect(rel.getGroupSet());
Boolean anyUnique = false;
for (int column : nonGroupColumns) {
Boolean unique =
mq.areColumnsUnique(rel.getInput(), groupColumns.set(column), ignoreNulls);
if (Boolean.TRUE.equals(unique)) {
return true;
}
if (unique == null) {
anyUnique = null;
}
}
return anyUnique;
}

final ImmutableBitSet commonKeys = columns.intersect(groupKey);
Expand All @@ -434,6 +454,19 @@ public Boolean areColumnsUnique(Intersect rel, RelMetadataQuery mq,
return null;
}

/** Returns whether a simple aggregate can pass through an input key
* using values from a single row in each group. Either condition suffices.
*
* <ul>
* <li>At most one input key column is outside the group set.
* <li>The grouping columns are known to be unique in the input.
* </ul> */
static boolean canPassThroughKey(Aggregate rel, RelMetadataQuery mq,
ImmutableBitSet nonGroupColumns) {
return nonGroupColumns.cardinality() <= 1
|| RelMdUtil.areColumnsDefinitelyUnique(mq, rel.getInput(), rel.getGroupSet());
}

public Boolean areColumnsUnique(Values rel, RelMetadataQuery mq,
ImmutableBitSet columns, boolean ignoreNulls) {
if (rel.tuples.size() < 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import static com.google.common.base.Preconditions.checkArgument;

import static org.apache.calcite.rel.metadata.RelMdColumnUniqueness.PASSTHROUGH_AGGREGATIONS;
import static org.apache.calcite.rel.metadata.RelMdColumnUniqueness.canPassThroughKey;
import static org.apache.calcite.rel.metadata.RelMdColumnUniqueness.getConstantColumnSet;

/**
Expand Down Expand Up @@ -368,12 +369,16 @@ public Set<ImmutableBitSet> getUniqueKeys(Aggregate rel, RelMetadataQuery mq,
: keysInGroupBy;
}

// If an input's unique column(s) value is returned (passed through) by an aggregation
// function, then the result of the function(s) is also unique.
// If each group returns the key of one of its input rows, that key is
// unique across groups. For example, MIN(x) and MAX(y) may take values
// from different rows, so their pair can repeat even if (x, y) is unique.
Set<ImmutableBitSet> keysBuilder = new HashSet<>();
if (inputUniqueKeys != null) {
outerLoop:
for (ImmutableBitSet inputKey : inputUniqueKeys) {
if (!canPassThroughKey(rel, mq, inputKey.except(groupKeys))) {
continue;
}
Iterable<List<Integer>> product =
Linq4j.product(Util.transform(inputKey, i -> getPassedThroughCols(i, rel)));
for (List<Integer> passKey : product) {
Expand Down
36 changes: 25 additions & 11 deletions core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,7 @@ private void checkColumnUniquenessForJoin(String sql) {
.assertThatAreColumnsUnique(bitSetOf(0), is(true))
.assertThatAreColumnsUnique(bitSetOf(1), is(false))
.assertThatAreColumnsUnique(bitSetOf(2), is(true))
.assertThatAreColumnsUnique(bitSetOf(1, 2), is(true))
.assertThatUniqueKeysAre(bitSetOf(0), bitSetOf(2));
}

Expand Down Expand Up @@ -2593,16 +2594,16 @@ private static RelNode applyAggregateProjectMerge(RelNode rel) {
.assertThatAreColumnsUnique(bitSetOf(0, 1), is(true))
.assertThatUniqueKeysAre(bitSetOf());

// both keys in passthrough functions, group by non-key
// [CALCITE-7978] The two aggregates can take their values from different rows.
sql("select value1, min(key1), max(key2) from s.composite_keys_table group by value1")
.withCatalogReaderFactory(factory)
.assertThatAreColumnsUnique(bitSetOf(0), is(true))
.assertThatAreColumnsUnique(bitSetOf(1), is(false))
.assertThatAreColumnsUnique(bitSetOf(2), is(false))
.assertThatAreColumnsUnique(bitSetOf(0, 1), is(true))
.assertThatAreColumnsUnique(bitSetOf(1, 2), is(true))
.assertThatAreColumnsUnique(bitSetOf(1, 2), is(false))
.assertThatAreColumnsUnique(bitSetOf(0, 1, 2), is(true))
.assertThatUniqueKeysAre(bitSetOf(0), bitSetOf(1, 2));
.assertThatUniqueKeysAre(bitSetOf(0));

// keys passed through multiple functions, group by non-key
sql("select min(key1), max(key1), avg(key1), min(key2), max(key2), avg(key2), value1\n"
Expand All @@ -2617,13 +2618,13 @@ private static RelNode applyAggregateProjectMerge(RelNode rel) {
.assertThatAreColumnsUnique(bitSetOf(6), is(true)) // group by
.assertThatAreColumnsUnique(bitSetOf(0, 1), is(false))
.assertThatAreColumnsUnique(bitSetOf(0, 2), is(false))
.assertThatAreColumnsUnique(bitSetOf(0, 3), is(true))
.assertThatAreColumnsUnique(bitSetOf(0, 4), is(true))
.assertThatAreColumnsUnique(bitSetOf(0, 3), is(false))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that @PaulJackson123 has added these tests, maybe he can provide a review?

.assertThatAreColumnsUnique(bitSetOf(0, 4), is(false))
.assertThatAreColumnsUnique(bitSetOf(0, 5), is(false))
.assertThatAreColumnsUnique(bitSetOf(0, 6), is(true)) // group by
.assertThatAreColumnsUnique(bitSetOf(1, 2), is(false))
.assertThatAreColumnsUnique(bitSetOf(1, 3), is(true))
.assertThatAreColumnsUnique(bitSetOf(1, 4), is(true))
.assertThatAreColumnsUnique(bitSetOf(1, 3), is(false))
.assertThatAreColumnsUnique(bitSetOf(1, 4), is(false))
.assertThatAreColumnsUnique(bitSetOf(1, 5), is(false))
.assertThatAreColumnsUnique(bitSetOf(1, 6), is(true)) // group by
.assertThatAreColumnsUnique(bitSetOf(2, 3), is(false))
Expand All @@ -2636,10 +2637,9 @@ private static RelNode applyAggregateProjectMerge(RelNode rel) {
.assertThatAreColumnsUnique(bitSetOf(4, 5), is(false))
.assertThatAreColumnsUnique(bitSetOf(4, 6), is(true)) // group by
.assertThatAreColumnsUnique(bitSetOf(5, 6), is(true)) // group by
.assertThatAreColumnsUnique(bitSetOf(0, 1, 2, 3), is(true))
.assertThatAreColumnsUnique(bitSetOf(1, 3, 4, 5), is(true))
.assertThatUniqueKeysAre(bitSetOf(0, 3), bitSetOf(0, 4), bitSetOf(1, 3), bitSetOf(1, 4),
bitSetOf(6));
.assertThatAreColumnsUnique(bitSetOf(0, 1, 2, 3), is(false))
.assertThatAreColumnsUnique(bitSetOf(1, 3, 4, 5), is(false))
.assertThatUniqueKeysAre(bitSetOf(6));

// one key in mutating function, group by non-key
sql("select value1, min(key1), count(key2) from s.composite_keys_table group by value1")
Expand Down Expand Up @@ -2999,6 +2999,20 @@ private static ImmutableBitSet bitSetOf(int... bits) {
.assertRowsUnique(is(true), "set query is always unique");
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7978">[CALCITE-7978]
* RelMdColumnUniqueness can incorrectly infer that an Aggregate preserves
* the uniqueness of input column combinations</a>. */
@Test void testCompositeKeyWithSingleRowGroups() {
// Both the passport pair and the national ID pair identify an input row.
sql("select min(p.nid), max(q.nid)\n"
+ "from s.passenger p cross join s.passenger q\n"
+ "group by p.passport, q.passport")
.withCatalogReaderFactory(COMPOSITE_FACTORY)
.assertThatAreColumnsUnique(bitSetOf(0, 1), is(true))
.assertThatUniqueKeysAre(bitSetOf(0, 1));
}

@Test void testBrokenCustomProviderWithMetadataFactory() {
final List<String> buf = new ArrayList<>();
ColTypeImpl.THREAD_LIST.set(buf);
Expand Down
14 changes: 14 additions & 0 deletions core/src/test/resources/sql/agg.iq
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ select distinct count(*) as c from emp group by deptno order by 1 desc;

!ok

# [CALCITE-7978] RelMdColumnUniqueness can incorrectly infer that an Aggregate
# preserves the uniqueness of input column combinations
select distinct min(price) as min_price, max(qty) as max_qty
from (values ('A', 1, 2), ('B', 1, 1), ('B', 2, 2)) as sales(shop, price, qty)
group by shop;
+-----------+---------+
| MIN_PRICE | MAX_QTY |
+-----------+---------+
| 1 | 2 |
+-----------+---------+
(1 row)

!ok

# [CALCITE-2192] RelBuilder wrongly skips creation of Aggregate that prunes
# columns if input is unique
select distinct deptno
Expand Down
Loading