Search in sources :

Example 21 with Join

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Join in project flink by apache.

the class FlinkSemiAntiJoinProjectTransposeRule method onMatch.

// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    LogicalJoin join = call.rel(0);
    LogicalProject project = call.rel(1);
    // convert the semi/anti join condition to reflect the LHS with the project
    // pulled up
    RexNode newCondition = adjustCondition(project, join);
    Join newJoin = LogicalJoin.create(project.getInput(), join.getRight(), join.getHints(), newCondition, join.getVariablesSet(), join.getJoinType());
    // Create the new projection. Note that the projection expressions
    // are the same as the original because they only reference the LHS
    // of the semi/anti join and the semi/anti join only projects out the LHS
    final RelBuilder relBuilder = call.builder();
    relBuilder.push(newJoin);
    relBuilder.project(project.getProjects(), project.getRowType().getFieldNames());
    call.transformTo(relBuilder.build());
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) Join(org.apache.calcite.rel.core.Join) LogicalJoin(org.apache.calcite.rel.logical.LogicalJoin) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RexNode(org.apache.calcite.rex.RexNode)

Example 22 with Join

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Join in project beam by apache.

the class ArrayScanToJoinConverter method convert.

/**
 * Returns a LogicJoin.
 */
@Override
public RelNode convert(ResolvedArrayScan zetaNode, List<RelNode> inputs) {
    List<RexNode> projects = new ArrayList<>();
    RelNode leftInput = inputs.get(0);
    ResolvedColumnRef columnRef = (ResolvedColumnRef) zetaNode.getArrayExpr();
    CorrelationId correlationId = getCluster().createCorrel();
    getCluster().getQuery().mapCorrel(correlationId.getName(), leftInput);
    String columnName = String.format("%s%s", zetaNode.getElementColumn().getTableName(), zetaNode.getElementColumn().getName());
    projects.add(getCluster().getRexBuilder().makeFieldAccess(getCluster().getRexBuilder().makeCorrel(leftInput.getRowType(), correlationId), getExpressionConverter().indexOfProjectionColumnRef(columnRef.getColumn().getId(), zetaNode.getInputScan().getColumnList())));
    RelNode projectNode = LogicalProject.create(createOneRow(getCluster()), ImmutableList.of(), projects, ImmutableList.of(columnName));
    // Create an UnCollect
    boolean ordinality = (zetaNode.getArrayOffsetColumn() != null);
    // If they aren't true we need the Project to reorder columns.
    assert zetaNode.getElementColumn().getId() == 1;
    assert !ordinality || zetaNode.getArrayOffsetColumn().getColumn().getId() == 2;
    ZetaSqlUnnest uncollectNode = ZetaSqlUnnest.create(projectNode.getTraitSet(), projectNode, ordinality);
    List<RexInputRef> rightProjects = new ArrayList<>();
    List<String> rightNames = new ArrayList<>();
    rightProjects.add(getCluster().getRexBuilder().makeInputRef(uncollectNode, 0));
    rightNames.add(columnName);
    if (ordinality) {
        rightProjects.add(getCluster().getRexBuilder().makeInputRef(uncollectNode, 1));
        rightNames.add(String.format(zetaNode.getArrayOffsetColumn().getColumn().getTableName(), zetaNode.getArrayOffsetColumn().getColumn().getName()));
    }
    RelNode rightInput = LogicalProject.create(uncollectNode, ImmutableList.of(), rightProjects, rightNames);
    // Join condition should be a RexNode converted from join_expr.
    RexNode condition = getExpressionConverter().convertRexNodeFromResolvedExpr(zetaNode.getJoinExpr());
    JoinRelType joinRelType = zetaNode.getIsOuter() ? JoinRelType.LEFT : JoinRelType.INNER;
    return LogicalJoin.create(leftInput, rightInput, ImmutableList.of(), condition, ImmutableSet.of(), joinRelType, false, ImmutableList.of());
}
Also used : ArrayList(java.util.ArrayList) ResolvedColumnRef(com.google.zetasql.resolvedast.ResolvedNodes.ResolvedColumnRef) JoinRelType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.JoinRelType) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) ZetaSqlUnnest(org.apache.beam.sdk.extensions.sql.zetasql.unnest.ZetaSqlUnnest) RexInputRef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexInputRef) CorrelationId(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.CorrelationId) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Example 23 with Join

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Join in project beam by apache.

the class BeamSideInputJoinRelTest method testNodeStatsEstimation.

@Test
public void testNodeStatsEstimation() {
    String sql = "SELECT o1.order_id, o1.sum_site_id, o2.buyer FROM " + "(select order_id, sum(site_id) as sum_site_id FROM ORDER_DETAILS " + "          GROUP BY order_id, TUMBLE(order_time, INTERVAL '1' HOUR)) o1 " + " JOIN " + " ORDER_DETAILS1 o2 " + " on " + " o1.order_id=o2.order_id";
    RelNode root = env.parseQuery(sql);
    while (!(root instanceof BeamSideInputJoinRel)) {
        root = root.getInput(0);
    }
    NodeStats estimate = BeamSqlRelUtils.getNodeStats(root, ((BeamRelMetadataQuery) root.getCluster().getMetadataQuery()));
    NodeStats leftEstimate = BeamSqlRelUtils.getNodeStats(((BeamSideInputJoinRel) root).getLeft(), ((BeamRelMetadataQuery) root.getCluster().getMetadataQuery()));
    NodeStats rightEstimate = BeamSqlRelUtils.getNodeStats(((BeamSideInputJoinRel) root).getRight(), ((BeamRelMetadataQuery) root.getCluster().getMetadataQuery()));
    Assert.assertFalse(estimate.isUnknown());
    Assert.assertEquals(0d, estimate.getRowCount(), 0.01);
    Assert.assertNotEquals(0d, estimate.getRate(), 0.001);
    Assert.assertTrue(estimate.getRate() < leftEstimate.getRowCount() * rightEstimate.getWindow() + rightEstimate.getRowCount() * leftEstimate.getWindow());
    Assert.assertNotEquals(0d, estimate.getWindow(), 0.001);
    Assert.assertTrue(estimate.getWindow() < leftEstimate.getWindow() * rightEstimate.getWindow());
}
Also used : NodeStats(org.apache.beam.sdk.extensions.sql.impl.planner.NodeStats) BeamRelMetadataQuery(org.apache.beam.sdk.extensions.sql.impl.planner.BeamRelMetadataQuery) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) Test(org.junit.Test)

Example 24 with Join

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Join in project beam by apache.

the class ThreeTablesSchema method testBeamJoinAssociationRule.

@Test
public void testBeamJoinAssociationRule() throws Exception {
    RuleSet prepareRules = RuleSets.ofList(CoreRules.SORT_PROJECT_TRANSPOSE, EnumerableRules.ENUMERABLE_JOIN_RULE, EnumerableRules.ENUMERABLE_PROJECT_RULE, EnumerableRules.ENUMERABLE_SORT_RULE, EnumerableRules.ENUMERABLE_TABLE_SCAN_RULE);
    String sqlQuery = "select * from \"tt\".\"large_table\" as large_table " + " JOIN \"tt\".\"medium_table\" as medium_table on large_table.\"medium_key\" = medium_table.\"large_key\" " + " JOIN \"tt\".\"small_table\" as small_table on medium_table.\"small_key\" = small_table.\"medium_key\" ";
    RelNode originalPlan = transform(sqlQuery, prepareRules);
    RelNode optimizedPlan = transform(sqlQuery, RuleSets.ofList(ImmutableList.<RelOptRule>builder().addAll(prepareRules).add(BeamJoinAssociateRule.INSTANCE).build()));
    assertTopTableInJoins(originalPlan, "small_table");
    assertTopTableInJoins(optimizedPlan, "large_table");
}
Also used : RuleSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RuleSet) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) Test(org.junit.Test)

Example 25 with Join

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Join in project beam by apache.

the class BeamCoGBKJoinRelBoundedVsBoundedTest method testNodeStatsOfMoreConditions.

@Test
public void testNodeStatsOfMoreConditions() {
    String sql1 = "SELECT *  " + " FROM ORDER_DETAILS1 o1 " + " JOIN ORDER_DETAILS2 o2 " + " on " + " o1.order_id=o2.site_id ";
    String sql2 = "SELECT *  " + " FROM ORDER_DETAILS1 o1 " + " JOIN ORDER_DETAILS2 o2 " + " on " + " o1.order_id=o2.site_id AND o2.price=o1.site_id";
    RelNode root1 = env.parseQuery(sql1);
    while (!(root1 instanceof BeamCoGBKJoinRel)) {
        root1 = root1.getInput(0);
    }
    RelNode root2 = env.parseQuery(sql2);
    while (!(root2 instanceof BeamCoGBKJoinRel)) {
        root2 = root2.getInput(0);
    }
    NodeStats estimate1 = BeamSqlRelUtils.getNodeStats(root1, ((BeamRelMetadataQuery) root1.getCluster().getMetadataQuery()));
    NodeStats estimate2 = BeamSqlRelUtils.getNodeStats(root2, ((BeamRelMetadataQuery) root1.getCluster().getMetadataQuery()));
    Assert.assertNotEquals(0d, estimate2.getRowCount(), 0.001);
    // A join with two conditions should have lower estimate.
    Assert.assertTrue(estimate2.getRowCount() < estimate1.getRowCount());
}
Also used : NodeStats(org.apache.beam.sdk.extensions.sql.impl.planner.NodeStats) BeamRelMetadataQuery(org.apache.beam.sdk.extensions.sql.impl.planner.BeamRelMetadataQuery) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) Test(org.junit.Test)

Aggregations

Join (org.apache.calcite.rel.core.Join)73 RelNode (org.apache.calcite.rel.RelNode)45 RexNode (org.apache.calcite.rex.RexNode)40 ArrayList (java.util.ArrayList)31 LogicalJoin (org.apache.calcite.rel.logical.LogicalJoin)25 Project (org.apache.calcite.rel.core.Project)22 RexBuilder (org.apache.calcite.rex.RexBuilder)20 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)18 RelBuilder (org.apache.calcite.tools.RelBuilder)17 HiveJoin (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveJoin)14 Aggregate (org.apache.calcite.rel.core.Aggregate)13 Test (org.junit.Test)13 Filter (org.apache.calcite.rel.core.Filter)12 RelNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode)11 SemiJoin (org.apache.calcite.rel.core.SemiJoin)11 RelOptCluster (org.apache.calcite.plan.RelOptCluster)10 JoinRelType (org.apache.calcite.rel.core.JoinRelType)9 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)9 Mappings (org.apache.calcite.util.mapping.Mappings)9 List (java.util.List)8