org.apache.calcite.rel.logical.LogicalUnion Java Examples

The following examples show how to use org.apache.calcite.rel.logical.LogicalUnion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StreamRules.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Join join = call.rel(1);
  final RelNode left = join.getLeft();
  final RelNode right = join.getRight();

  final LogicalDelta rightWithDelta = LogicalDelta.create(right);
  final LogicalJoin joinL = LogicalJoin.create(left, rightWithDelta,
      join.getCondition(), join.getVariablesSet(), join.getJoinType(),
      join.isSemiJoinDone(),
      ImmutableList.copyOf(join.getSystemFieldList()));

  final LogicalDelta leftWithDelta = LogicalDelta.create(left);
  final LogicalJoin joinR = LogicalJoin.create(leftWithDelta, right,
      join.getCondition(), join.getVariablesSet(), join.getJoinType(),
      join.isSemiJoinDone(),
      ImmutableList.copyOf(join.getSystemFieldList()));

  List<RelNode> inputsToUnion = new ArrayList<>();
  inputsToUnion.add(joinL);
  inputsToUnion.add(joinR);

  final LogicalUnion newNode = LogicalUnion.create(inputsToUnion, true);
  call.transformTo(newNode);
}
 
Example #2
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testRuleClass() throws Exception {
  // Verify that an entire class of rules can be applied.

  HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addRuleClass(CoerceInputsRule.class);

  HepPlanner planner =
      new HepPlanner(
          programBuilder.build());

  planner.addRule(
      new CoerceInputsRule(LogicalUnion.class, false,
          RelFactories.LOGICAL_BUILDER));
  planner.addRule(
      new CoerceInputsRule(LogicalIntersect.class, false,
          RelFactories.LOGICAL_BUILDER));

  final String sql = "(select name from dept union select ename from emp)\n"
      + "intersect (select fname from customer.contact)";
  sql(sql).with(planner).check();
}
 
Example #3
Source File: DrillUnionAllRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final LogicalUnion union = call.rel(0);

  // This rule applies to Union-All only
  if(!union.all) {
    return;
  }

  final RelTraitSet traits = union.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
  final List<RelNode> convertedInputs = new ArrayList<>();
  for (RelNode input : union.getInputs()) {
    final RelNode convertedInput = convert(input, input.getTraitSet().plus(DrillRel.DRILL_LOGICAL).simplify());
    convertedInputs.add(convertedInput);
  }
  try {
    call.transformTo(new DrillUnionRel(union.getCluster(), traits, convertedInputs, union.all,
        true /* check compatibility */));
  } catch (InvalidRelException e) {
    tracer.warn(e.toString());
  }
}
 
Example #4
Source File: UnionAllRule.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final LogicalUnion union = (LogicalUnion) call.rel(0);

  // This rule applies to Union-All only
  if(!union.all) {
    return;
  }

  final RelTraitSet traits = union.getTraitSet().plus(Rel.LOGICAL);
  final List<RelNode> convertedInputs = new ArrayList<>();
  for (RelNode input : union.getInputs()) {
    final RelNode convertedInput = convert(input, input.getTraitSet().plus(Rel.LOGICAL).simplify());
    convertedInputs.add(convertedInput);
  }
  try {
    call.transformTo(new UnionRel(union.getCluster(), traits, convertedInputs, union.all,
        true /* check compatibility */));
  } catch (InvalidRelException e) {
    tracer.warn(e.toString()) ;
  }
}
 
Example #5
Source File: StreamRules.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Union union = call.rel(1);
  final List<RelNode> newInputs = new ArrayList<>();
  for (RelNode input : union.getInputs()) {
    final LogicalDelta newDelta =
        LogicalDelta.create(input);
    newInputs.add(newDelta);
  }
  final LogicalUnion newUnion = LogicalUnion.create(newInputs, union.all);
  call.transformTo(newUnion);
}
 
Example #6
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testTableReferencesUnionUnknownNode() {
  final String sql = "select * from emp limit 10";
  final RelNode node = convertSql(sql);
  final RelNode nodeWithUnknown = new DummyRelNode(
      node.getCluster(), node.getTraitSet(), node);
  // Union
  final LogicalUnion union =
      LogicalUnion.create(ImmutableList.of(nodeWithUnknown, node),
          true);
  final RelMetadataQuery mq = node.getCluster().getMetadataQuery();
  final Set<RelTableRef> tableReferences = mq.getTableReferences(union);
  assertNull(tableReferences);
}
 
Example #7
Source File: Bindables.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final SetOp setOp = (SetOp) rel;
  final BindableConvention out = BindableConvention.INSTANCE;
  final RelTraitSet traitSet = setOp.getTraitSet().replace(out);
  if (setOp instanceof LogicalUnion) {
    return new BindableUnion(rel.getCluster(), traitSet,
        convertList(setOp.getInputs(), out), setOp.all);
  } else if (setOp instanceof LogicalIntersect) {
    return new BindableIntersect(rel.getCluster(), traitSet,
        convertList(setOp.getInputs(), out), setOp.all);
  } else {
    return new BindableMinus(rel.getCluster(), traitSet,
        convertList(setOp.getInputs(), out), setOp.all);
  }
}
 
Example #8
Source File: EnumerableUnionRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalUnion union = (LogicalUnion) rel;
  final EnumerableConvention out = EnumerableConvention.INSTANCE;
  final RelTraitSet traitSet = rel.getCluster().traitSet().replace(out);
  final List<RelNode> newInputs = Lists.transform(
      union.getInputs(), n -> convert(n, traitSet));
  return new EnumerableUnion(rel.getCluster(), traitSet,
      newInputs, union.all);
}
 
Example #9
Source File: RelFactories.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode createSetOp(SqlKind kind, List<RelNode> inputs,
    boolean all) {
  switch (kind) {
  case UNION:
    return LogicalUnion.create(inputs, all);
  case EXCEPT:
    return LogicalMinus.create(inputs, all);
  case INTERSECT:
    return LogicalIntersect.create(inputs, all);
  default:
    throw new AssertionError("not a set op: " + kind);
  }
}
 
Example #10
Source File: StreamRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Join join = call.rel(1);
  final RelNode left = join.getLeft();
  final RelNode right = join.getRight();

  final LogicalDelta rightWithDelta = LogicalDelta.create(right);
  final LogicalJoin joinL = LogicalJoin.create(left,
      rightWithDelta,
      join.getHints(),
      join.getCondition(),
      join.getVariablesSet(),
      join.getJoinType(),
      join.isSemiJoinDone(),
      ImmutableList.copyOf(join.getSystemFieldList()));

  final LogicalDelta leftWithDelta = LogicalDelta.create(left);
  final LogicalJoin joinR = LogicalJoin.create(leftWithDelta,
      right,
      join.getHints(),
      join.getCondition(),
      join.getVariablesSet(),
      join.getJoinType(),
      join.isSemiJoinDone(),
      ImmutableList.copyOf(join.getSystemFieldList()));

  List<RelNode> inputsToUnion = new ArrayList<>();
  inputsToUnion.add(joinL);
  inputsToUnion.add(joinR);

  final LogicalUnion newNode = LogicalUnion.create(inputsToUnion, true);
  call.transformTo(newNode);
}
 
Example #11
Source File: StreamRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Delta delta = call.rel(0);
  Util.discard(delta);
  final Union union = call.rel(1);
  final List<RelNode> newInputs = new ArrayList<>();
  for (RelNode input : union.getInputs()) {
    final LogicalDelta newDelta =
        LogicalDelta.create(input);
    newInputs.add(newDelta);
  }
  final LogicalUnion newUnion = LogicalUnion.create(newInputs, union.all);
  call.transformTo(newUnion);
}
 
Example #12
Source File: PreProcessRel.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(LogicalUnion union) {
  for(RelNode child : union.getInputs()) {
    for(RelDataTypeField dataField : child.getRowType().getFieldList()) {
      if(dataField.getName().contains(StarColumnHelper.STAR_COLUMN)) {
        // see DRILL-2414
        unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.RELATIONAL,
            "Union-All over schema-less tables must specify the columns explicitly");
        throw new UnsupportedOperationException();
      }
    }
  }

  return visitChildren(union);
}
 
Example #13
Source File: CopyWithCluster.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(LogicalUnion union) {
  return new LogicalUnion(
    cluster,
    copyOf(union.getTraitSet()),
    visitAll(union.getInputs()),
    union.all
  );
}
 
Example #14
Source File: RelFactories.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelNode createSetOp(SqlKind kind, List<RelNode> inputs,
    boolean all) {
  switch (kind) {
  case UNION:
    return LogicalUnion.create(inputs, all);
  case EXCEPT:
    return LogicalMinus.create(inputs, all);
  case INTERSECT:
    return LogicalIntersect.create(inputs, all);
  default:
    throw new AssertionError("not a set op: " + kind);
  }
}
 
Example #15
Source File: PreProcessLogicalRel.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(LogicalUnion union) {
    for (RelNode child : union.getInputs()) {
        for (RelDataTypeField dataField : child.getRowType().getFieldList()) {
            if (dataField.getName().contains(SchemaPath.DYNAMIC_STAR)) {
                unsupportedOperatorCollector.setException(SqlUnsupportedException.ExceptionType.RELATIONAL,
                        "Union-All over schema-less tables must specify the columns explicitly\n"
                                + "See Apache Drill JIRA: DRILL-2414");
                throw new UnsupportedOperationException();
            }
        }
    }

    return visitChildren(union);
}
 
Example #16
Source File: MycatCalcitePlanner.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 测试单库分表与不同分片两个情况
 *
 * @param relBuilder
 * @param cache
 * @param margeList
 * @param bestExp2
 * @return
 */
private RelNode simplyAggreate(MycatRelBuilder relBuilder, IdentityHashMap<RelNode, Boolean> cache, IdentityHashMap<RelNode, List<String>> margeList, RelNode bestExp2) {
    RelNode parent = bestExp2;
    RelNode child = bestExp2 instanceof Aggregate ? bestExp2.getInput(0) : null;
    RelNode bestExp3 = parent;
    if (parent instanceof Aggregate && child instanceof Union) {
        Aggregate aggregate = (Aggregate) parent;
        if (aggregate.getAggCallList() != null && !aggregate.getAggCallList().isEmpty()) {//distinct会没有参数
            List<AggregateCall> aggCallList = aggregate.getAggCallList();
            boolean allMatch = aggregate.getRowType().getFieldCount() == 1 && aggCallList.stream().allMatch(new Predicate<AggregateCall>() {
                @Override
                public boolean test(AggregateCall aggregateCall) {
                    return SUPPORTED_AGGREGATES.getOrDefault(aggregateCall.getAggregation().getKind(), false)
                            &&
                            aggregate.getRowType().getFieldList().stream().allMatch(i -> i.getType().getSqlTypeName().getFamily() == SqlTypeFamily.NUMERIC);
                }
            });
            if (allMatch) {
                List<RelNode> inputs = child.getInputs();
                List<RelNode> resList = new ArrayList<>(inputs.size());
                boolean allCanPush = true;//是否聚合节点涉及不同分片
                String target = null;
                for (RelNode input : inputs) {
                    RelNode res;
                    if (cache.get(input)) {
                        res = LogicalAggregate.create(input, aggregate.getGroupSet(), aggregate.getGroupSets(), aggregate.getAggCallList());
                        cache.put(res, Boolean.TRUE);
                        List<String> strings = margeList.getOrDefault(input, Collections.emptyList());
                        Objects.requireNonNull(strings);
                        if (target == null && strings.size() > 0) {
                            target = strings.get(0);
                        } else if (target != null && strings.size() > 0) {
                            if (!target.equals(strings.get(0))) {
                                allCanPush = false;
                            }
                        }
                        margeList.put(res, strings);
                    } else {
                        res = input;
                        allCanPush = false;
                    }
                    resList.add(res);
                }

                LogicalUnion logicalUnion = LogicalUnion.create(resList, ((Union) child).all);

                //构造sum
                relBuilder.clear();
                relBuilder.push(logicalUnion);
                List<RexNode> fields = relBuilder.fields();
                if (fields == null) {
                    fields = Collections.emptyList();
                }

                RelBuilder.GroupKey groupKey = relBuilder.groupKey();
                List<RelBuilder.AggCall> aggCalls = fields.stream().map(i -> relBuilder.sum(i)).collect(Collectors.toList());
                relBuilder.aggregate(groupKey, aggCalls);
                bestExp3 = relBuilder.build();

                cache.put(logicalUnion, allCanPush);
                cache.put(bestExp3, allCanPush);
                if (target != null) {//是否聚合节点涉及不同分片
                    List<String> targetSingelList = Collections.singletonList(target);
                    margeList.put(logicalUnion, targetSingelList);
                    margeList.put(bestExp3, targetSingelList);
                }
            }
        }
    }
    return bestExp3;
}
 
Example #17
Source File: RelHomogeneousShuttle.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public RelNode visit(LogicalUnion union) {
  return visit((RelNode) union);
}
 
Example #18
Source File: RelShuttleImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
public RelNode visit(LogicalUnion union) {
  return visitChildren(union);
}
 
Example #19
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void rewriteRel(LogicalUnion rel) {
  rewriteGeneric(rel);
}
 
Example #20
Source File: EnumerableUnionRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
EnumerableUnionRule() {
  super(LogicalUnion.class, Convention.NONE, EnumerableConvention.INSTANCE,
      "EnumerableUnionRule");
}
 
Example #21
Source File: CalciteMaterializer.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode visit(LogicalUnion union) {
  return union;
}
 
Example #22
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void rewriteRel(LogicalUnion rel) {
    rewriteGeneric(rel);
}
 
Example #23
Source File: RelShuttleImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode visit(LogicalUnion union) {
  return visitChildren(union);
}
 
Example #24
Source File: RelHomogeneousShuttle.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public RelNode visit(LogicalUnion union) {
  return visit((RelNode) union);
}
 
Example #25
Source File: DrillUnionAllRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
private DrillUnionAllRule() {
  super(RelOptHelper.any(LogicalUnion.class, Convention.NONE),
      DrillRelFactories.LOGICAL_BUILDER, "DrillUnionRule");
}
 
Example #26
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public RelNode visit(LogicalUnion union) {
	checkCorConditionOfSetOpInputs(union);
	return super.visit(union);
}
 
Example #27
Source File: FindLimit0Visitor.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public RelNode visit(LogicalUnion union) {
  return union;
}
 
Example #28
Source File: UnionAllRule.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private UnionAllRule() {
  super(RelOptHelper.any(LogicalUnion.class, Convention.NONE), "UnionAllRule");
}
 
Example #29
Source File: JoinUtils.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public RelNode visit(LogicalUnion union) {
  return union;
}
 
Example #30
Source File: RoutingShuttle.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public RelNode visit(LogicalUnion union) {
  return visit((RelNode) union);
}