Skip to content

Commit

Permalink
[CALCITE-6727] Column uniqueness constrain should only apply to inner…
Browse files Browse the repository at this point in the history
… join
  • Loading branch information
joeyutong committed Dec 17, 2024
1 parent 041619f commit faa8abe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,16 @@ public Boolean areColumnsUnique(Intersect rel, RelMetadataQuery mq,
}

final JoinInfo joinInfo = rel.analyzeCondition();

// Joining with a singleton constrains the keys on the other table
final Double rightMaxRowCount = mq.getMaxRowCount(right);
if (rightMaxRowCount != null && rightMaxRowCount <= 1.0) {
leftColumns = leftColumns.union(joinInfo.leftSet());
}
final Double leftMaxRowCount = mq.getMaxRowCount(left);
if (leftMaxRowCount != null && leftMaxRowCount <= 1.0) {
rightColumns = rightColumns.union(joinInfo.rightSet());
if (rel.getJoinType() == JoinRelType.INNER) {
// Joining with a singleton constrains the keys on the other table
final Double rightMaxRowCount = mq.getMaxRowCount(right);
if (rightMaxRowCount != null && rightMaxRowCount <= 1.0) {
leftColumns = leftColumns.union(joinInfo.leftSet());
}
final Double leftMaxRowCount = mq.getMaxRowCount(left);
if (leftMaxRowCount != null && leftMaxRowCount <= 1.0) {
rightColumns = rightColumns.union(joinInfo.rightSet());
}
}

// If the original column mask contains columns from both the left and
Expand Down
21 changes: 21 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,27 @@ private void checkColumnUniquenessForFilterWithConstantColumns(String sql) {
.assertThatUniqueKeysAre(bitSetOf());
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6727">[CALCITE-6727]
* Column uniqueness constrain should only apply to inner join</a>. */
@Test void testColumnUniquenessForLeftJoinOnLimit1() {
final String sql = ""
+ "select A.empno as a_empno,\n"
+ " A.ename as a_ename,\n"
+ " B.empno as b_empno,\n"
+ " B.ename as b_ename\n"
+ "from emp A\n"
+ "left join (\n"
+ " select * from emp\n"
+ " limit 1) B\n"
+ "on A.empno = B.empno";
sql(sql)
.assertThatAreColumnsUnique(bitSetOf(0), is(true))
.assertThatAreColumnsUnique(bitSetOf(1), is(false))
.assertThatAreColumnsUnique(bitSetOf(2), is(false))
.assertThatAreColumnsUnique(bitSetOf(3), is(false));
}

@Test void testColumnUniquenessForJoinOnAggregation() {
final String sql = ""
+ "select *\n"
Expand Down

0 comments on commit faa8abe

Please sign in to comment.