LakeShen commented on code in PR #3345: URL: https://github.com/apache/calcite/pull/3345#discussion_r1285783800
########## core/src/test/java/org/apache/calcite/test/RelMetadataTest.java: ########## @@ -971,6 +989,1025 @@ final RelMetadataFixture sql(String sql) { is(1D)); } + // ---------------------------------------------------------------------- + // Tests for getForeignKeys + // ---------------------------------------------------------------------- + + @Test void testForeignKeysAggregateEmpty() { + sql("select hiredate, sum(sal), count(deptno) from emp group by hiredate") + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysIgnoreNullsAggregateEmpty() { + sql("select hiredate, sum(sal), count(deptno) from empnullables group by hiredate") + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysAggregateKey() { + Set<RelOptForeignKey> foreignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(1), ImmutableBitSet.of())); + sql("select count(sal), deptno, count(deptno) from emp group by deptno") + .assertForeignKeys(equalTo(foreignKeys), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys(equalTo(foreignKeys), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysIgnoreNullsAggregateKey() { + Set<RelOptForeignKey> foreignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMPNULLABLES_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(1), ImmutableBitSet.of())); + sql("select count(sal), deptno, count(deptno) from empnullables group by deptno") + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(foreignKeys), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysTableOnly() { + Set<RelOptForeignKey> foreignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(), + InferredConstraintKey.of(EMP_QNAME, 0, false))), + ImmutableBitSet.of(), bitSetOf(0)), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(1), ImmutableBitSet.of()), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(3), ImmutableBitSet.of())); + sql("select empno, deptno, ename, deptno from emp") + .assertForeignKeys(equalTo(foreignKeys), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys(equalTo(foreignKeys), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysIgnoreNullsTableOnly() { + Set<RelOptForeignKey> foreignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMPNULLABLES_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(0), ImmutableBitSet.of())); + sql("select deptno, ename from empnullables") + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys(equalTo(foreignKeys), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysTableOnlyEmpty() { + sql("select ename, job from emp") + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysIgnoreNullsTableOnlyEmpty() { + sql("select ename, job from empnullables") + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysExpressionEmpty() { + sql("select deptno + 1, ename from emp") + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysIgnoreNullsExpressionEmpty() { + sql("select deptno + 1, ename from empnullables") + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysFilter() { + Set<RelOptForeignKey> foreignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(0), ImmutableBitSet.of())); + sql("select deptno, ename from emp where ename = 'lucy' and deptno = 1001") + .assertForeignKeys( + equalTo(foreignKeys), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(foreignKeys), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysIgnoreNullsFilter() { + Set<RelOptForeignKey> foreignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMPNULLABLES_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(0), ImmutableBitSet.of())); + sql("select deptno, ename from empnullables where ename = 'lucy' and deptno = 1001") + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys(equalTo(foreignKeys), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysFilterEmpty() { + sql("select ename from emp where deptno = 1001") + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysIgnoreNullsFilterEmpty() { + sql("select ename from empnullables where deptno = 1001") + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(EMPTY_FOREIGN_KEY_SET), equalTo(EMPTY_FOREIGN_KEY_SET), true); + } + + @Test void testForeignKeysInnerJoinLeft() { + Set<RelOptForeignKey> foreignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(0), ImmutableBitSet.of()), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(2), ImmutableBitSet.of()), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + ImmutableBitSet.of(), bitSetOf(3))); + Set<RelOptForeignKey> confirmedForeignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, true), + InferredConstraintKey.of(DEPT_QNAME, 0, true))), + bitSetOf(0), bitSetOf(3)), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, true), + InferredConstraintKey.of(DEPT_QNAME, 0, true))), + bitSetOf(2), bitSetOf(3))); + foreignKeys.addAll(confirmedForeignKeys); + sql("select emp.deptno, dept.name, emp.deptno, dept.deptno from emp, dept") + .assertForeignKeys( + equalTo(foreignKeys), equalTo(confirmedForeignKeys), true) + .assertForeignKeys( + equalTo(foreignKeys), equalTo(confirmedForeignKeys), false); + } + + @Test void testForeignKeysIgnoreNullsInnerJoinLeft() { + Set<RelOptForeignKey> foreignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + ImmutableBitSet.of(), bitSetOf(3))); + Set<RelOptForeignKey> confirmedForeignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMPNULLABLES_QNAME, 7, true), + InferredConstraintKey.of(DEPT_QNAME, 0, true))), + bitSetOf(0), bitSetOf(3)), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMPNULLABLES_QNAME, 7, true), + InferredConstraintKey.of(DEPT_QNAME, 0, true))), + bitSetOf(2), bitSetOf(3))); + Set<RelOptForeignKey> foreignKeysIgnoreNulls = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMPNULLABLES_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(0), ImmutableBitSet.of()), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMPNULLABLES_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(2), ImmutableBitSet.of())); + foreignKeysIgnoreNulls.addAll(confirmedForeignKeys); + foreignKeysIgnoreNulls.addAll(foreignKeys); + sql("select empnullables.deptno, dept.name, empnullables.deptno, dept.deptno " + + "from empnullables, dept") + .assertForeignKeys( + equalTo(foreignKeys), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(foreignKeysIgnoreNulls), equalTo(confirmedForeignKeys), true); + } + + @Test void testForeignKeysInnerJoinRight() { + Set<RelOptForeignKey> foreignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(1), ImmutableBitSet.of()), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(2), ImmutableBitSet.of()), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + ImmutableBitSet.of(), bitSetOf(3))); + Set<RelOptForeignKey> confirmedForeignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, true), + InferredConstraintKey.of(DEPT_QNAME, 0, true))), + bitSetOf(1), bitSetOf(3)), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, true), + InferredConstraintKey.of(DEPT_QNAME, 0, true))), + bitSetOf(2), bitSetOf(3))); + foreignKeys.addAll(confirmedForeignKeys); + sql("select dept.name, emp.deptno, emp.deptno, dept.deptno from dept, emp") + .assertForeignKeys( + equalTo(foreignKeys), equalTo(confirmedForeignKeys), false) + .assertForeignKeys( + equalTo(foreignKeys), equalTo(confirmedForeignKeys), true); + } + + @Test void testForeignKeysIgnoreNullsInnerJoinRight() { + Set<RelOptForeignKey> foreignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + ImmutableBitSet.of(), bitSetOf(2))); + Set<RelOptForeignKey> confirmedForeignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMPNULLABLES_QNAME, 7, true), + InferredConstraintKey.of(DEPT_QNAME, 0, true))), + bitSetOf(1), bitSetOf(2)), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMPNULLABLES_QNAME, 7, true), + InferredConstraintKey.of(DEPT_QNAME, 0, true))), + bitSetOf(3), bitSetOf(2))); + Set<RelOptForeignKey> foreignKeysIgnoreNull = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMPNULLABLES_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(1), ImmutableBitSet.of()), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMPNULLABLES_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(3), ImmutableBitSet.of())); + foreignKeysIgnoreNull.addAll(confirmedForeignKeys); + foreignKeysIgnoreNull.addAll(foreignKeys); + sql("select dept.name, empnullables.deptno, dept.deptno, empnullables.deptno " + + "from dept, empnullables") + .assertForeignKeys( + equalTo(foreignKeys), equalTo(EMPTY_FOREIGN_KEY_SET), false) + .assertForeignKeys( + equalTo(foreignKeysIgnoreNull), equalTo(confirmedForeignKeys), true); + } + + @Test void testForeignKeysLeftOuterJoin() { + Set<RelOptForeignKey> foreignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + ImmutableBitSet.of(), bitSetOf(3)), + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, false), + InferredConstraintKey.of(DEPT_QNAME, 0, false))), + bitSetOf(1), ImmutableBitSet.of())); + Set<RelOptForeignKey> confirmedForeignKeys = + Sets.newHashSet( + RelOptForeignKey.of( + Lists.newArrayList( + Pair.of( + InferredConstraintKey.of(EMP_QNAME, 7, true), + InferredConstraintKey.of(DEPT_QNAME, 0, true))), + bitSetOf(1), bitSetOf(3))); Review Comment: These two variables are used in almost every single test I see. They have too much duplicate code. You can define two methods that take different parameter types and then construct those two variables. This reduces code duplication. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@calcite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org