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());
}
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());
}
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());
}
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");
}
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());
}
Aggregations