Skip to content

Commit

Permalink
[CALCITE-2392] Prevent columns permutation for NATURAL JOIN and JOIN …
Browse files Browse the repository at this point in the history
…USING when dynamic table is used

Close #752
  • Loading branch information
vvysotskyi committed Jul 5, 2018
1 parent 625edb8 commit 5bbc501
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
Expand Up @@ -474,9 +474,11 @@ private boolean expandStar(List<SqlNode> selectItems, Set<String> aliases,
final SqlParserPos startPosition = identifier.getParserPosition();
switch (identifier.names.size()) {
case 1:
boolean hasDynamicStruct = false;
for (ScopeChild child : scope.children) {
final int before = fields.size();
if (child.namespace.getRowType().isDynamicStruct()) {
hasDynamicStruct = true;
// don't expand star if the underneath table is dynamic.
// Treat this star as a special field in validation/conversion and
// wait until execution time to expand this star.
Expand Down Expand Up @@ -532,7 +534,9 @@ private boolean expandStar(List<SqlNode> selectItems, Set<String> aliases,
}
// If NATURAL JOIN or USING is present, move key fields to the front of
// the list.
new Permute(scope.getNode().getFrom(), 0).permute(selectItems, fields);
if (!hasDynamicStruct || Bug.CALCITE_2400_FIXED) {
new Permute(scope.getNode().getFrom(), 0).permute(selectItems, fields);
}
return true;

default:
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/org/apache/calcite/util/Bug.java
Expand Up @@ -168,6 +168,12 @@ public abstract class Bug {
* Decorrelate sub-queries in Project and Join</a> is fixed. */
public static final boolean CALCITE_1045_FIXED = false;

/** Whether
* <a href="https://issues.apache.org/jira/browse/CALCITE-2400">[CALCITE-2400]
* Allow standards-compliant column ordering for NATURAL JOIN and JOIN USING
* when dynamic tables are used</a> is fixed. */
public static final boolean CALCITE_2400_FIXED = false;

/**
* Use this to flag temporary code.
*/
Expand Down
Expand Up @@ -114,6 +114,14 @@ protected final void check(
sql(sql).ok();
}

@Test
public void testJoinUsingDynamicTable() {
final String sql = "select * from SALES.NATION t1\n"
+ "join SALES.NATION t2\n"
+ "using (n_nationkey)";
sql(sql).with(getTesterWithDynamicTable()).ok();
}

/**
* Tests that AND(x, AND(y, z)) gets flattened to AND(x, y, z).
*/
Expand Down
Expand Up @@ -1605,6 +1605,20 @@ LogicalSort(offset=[10])
LogicalSort(offset=[?0])
LogicalProject(EMPNO=[$0])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testJoinUsingDynamicTable">
<Resource name="sql">
<![CDATA[select * from SALES.NATION t1
join SALES.NATION t2 using (n_nationkey)]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(**=[$1], **0=[$3])
LogicalJoin(condition=[=($0, $2)], joinType=[inner])
LogicalTableScan(table=[[CATALOG, SALES, NATION]])
LogicalTableScan(table=[[CATALOG, SALES, NATION]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit 5bbc501

Please sign in to comment.