Skip to content

Conversation

@mihaibudiu
Copy link
Contributor

@mihaibudiu mihaibudiu commented Dec 27, 2025

…es of Correlate + Unnest to Unnest

Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
Copy link
Member

@xiedeyantu xiedeyantu left a 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();
}

Copy link
Member

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>

}
List<RexNode> shifted = RexUtil.shift(outerProject.getProjects(), -leftCount);
builder.project(shifted);
RelNode result = builder.build();
Copy link
Member

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>
@sonarqubecloud
Copy link

}

RexNode projected = projects.get(0);
if (projected instanceof RexFieldAccess) {
Copy link
Contributor

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants