-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-7196] Create an optimization pass which can convert some cases of Correlate + Unnest to Unnest #4709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…es of Correlate + Unnest to Unnest Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
xiedeyantu
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know much about this, but the test results look good. So I'm just offering some simple suggestions for your reference. It would be great if someone else could review them.
| .withRule(CoreRules.UNNEST_DECORRELATE) | ||
| .check(); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can add a test case constructed in a non-Values manner. Like the one below, I tested your code and it looks good.
@Test void testUnnestDecorrelate4() {
final String sql = "select t2.ename\n"
+ "from DEPT_NESTED as t1,\n"
+ "unnest(t1.employees) as t2";
sql(sql)
.withPreRule(CoreRules.PROJECT_REMOVE)
.withRule(CoreRules.UNNEST_DECORRELATE)
.check();
}
Xml result:
<TestCase name="testUnnestDecorrelate4">
<Resource name="sql">
<![CDATA[select t2.ename
from DEPT_NESTED as t1,
unnest(t1.employees) as t2]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalProject(ENAME=[$5])
LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{3}])
LogicalTableScan(table=[[CATALOG, SALES, DEPT_NESTED]])
Uncollect
LogicalProject(EMPLOYEES=[$cor0.EMPLOYEES])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalProject(ENAME=[$1])
Uncollect
LogicalProject(EMPLOYEES=[$3])
LogicalTableScan(table=[[CATALOG, SALES, DEPT_NESTED]])
]]>
</Resource>
</TestCase>
core/src/main/java/org/apache/calcite/rel/rules/UnnestDecorrelateRule.java
Outdated
Show resolved
Hide resolved
| } | ||
| List<RexNode> shifted = RexUtil.shift(outerProject.getProjects(), -leftCount); | ||
| builder.project(shifted); | ||
| RelNode result = builder.build(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A small suggestion. It seems that all the operations related to the builder can be linked together in a point-like manner.
Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
|
core/src/main/java/org/apache/calcite/rel/rules/UnnestDecorrelateRule.java
Show resolved
Hide resolved
| } | ||
|
|
||
| RexNode projected = projects.get(0); | ||
| if (projected instanceof RexFieldAccess) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about embedded RexFieldAccess ?
final String sql = "WITH t1 AS (SELECT ROW(ARRAY[1, 2, 3]) as struct_with_array_field)\n"
+ "SELECT array_element.id\n"
+ "FROM t1, UNNEST(t1.struct_with_array_field.EXPR$0) AS array_element(id)";
sql(sql)
.withRule(CoreRules.UNNEST_PROJECT_DECORRELATE)
.check();



https://issues.apache.org/jira/browse/CALCITE-7196