org.apache.calcite.rel.core.Union Java Examples

The following examples show how to use org.apache.calcite.rel.core.Union. 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: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountUnion() {
  final String sql = "select ename from emp\n"
      + "union all\n"
      + "select name from dept";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 2);
  expected.put(Project.class, 2);
  expected.put(Union.class, 1);
  checkNodeTypeCount(sql, expected);
}
 
Example #2
Source File: RelMdRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Double getRowCount(Union rel, RelMetadataQuery mq) {
  double rowCount = 0.0;
  for (RelNode input : rel.getInputs()) {
    Double partialRowCount = mq.getRowCount(input);
    if (partialRowCount == null) {
      return null;
    }
    rowCount += partialRowCount;
  }
  return rowCount;
}
 
Example #3
Source File: SortUnionTransposeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final Union union = call.rel(1);
  List<RelNode> inputs = new ArrayList<>();
  // Thus we use 'ret' as a flag to identify if we have finished pushing the
  // sort past a union.
  boolean ret = true;
  final RelMetadataQuery mq = call.getMetadataQuery();
  for (RelNode input : union.getInputs()) {
    if (!RelMdUtil.checkInputForCollationAndLimit(mq, input,
        sort.getCollation(), sort.offset, sort.fetch)) {
      ret = false;
      Sort branchSort = sort.copy(sort.getTraitSet(), input,
          sort.getCollation(), sort.offset, sort.fetch);
      inputs.add(branchSort);
    } else {
      inputs.add(input);
    }
  }
  // there is nothing to change
  if (ret) {
    return;
  }
  // create new union and sort
  Union unionCopy = (Union) union
      .copy(union.getTraitSet(), inputs, union.all);
  Sort result = sort.copy(sort.getTraitSet(), unionCopy, sort.getCollation(),
      sort.offset, sort.fetch);
  call.transformTo(result);
}
 
Example #4
Source File: AggregateUnionAggregateRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a AggregateUnionAggregateRule.
 */
public AggregateUnionAggregateRule(Class<? extends Aggregate> aggregateClass,
    Class<? extends Union> unionClass,
    Class<? extends RelNode> firstUnionInputClass,
    Class<? extends RelNode> secondUnionInputClass,
    RelBuilderFactory relBuilderFactory,
    String desc) {
  super(
      operandJ(aggregateClass, null, Aggregate::isSimple,
          operand(unionClass,
              operand(firstUnionInputClass, any()),
              operand(secondUnionInputClass, any()))),
      relBuilderFactory, desc);
}
 
Example #5
Source File: AggregateUnionAggregateRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public AggregateUnionAggregateRule(Class<? extends Aggregate> aggregateClass,
    RelFactories.AggregateFactory aggregateFactory,
    Class<? extends Union> unionClass,
    RelFactories.SetOpFactory setOpFactory) {
  this(aggregateClass, unionClass, RelNode.class, RelNode.class,
      RelBuilder.proto(aggregateFactory, setOpFactory),
      "AggregateUnionAggregateRule");
}
 
Example #6
Source File: SortUnionTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public boolean matches(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final Union union = call.rel(1);
  // We only apply this rule if Union.all is true and Sort.offset is null.
  // There is a flag indicating if this rule should be applied when
  // Sort.fetch is null.
  return union.all
      && sort.offset == null
      && (matchNullFetch || sort.fetch != null);
}
 
Example #7
Source File: UnionDistinctPrel.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Union copy(RelTraitSet traitSet, List<RelNode> inputs, boolean all) {
  try {
    return new UnionDistinctPrel(this.getCluster(), traitSet, inputs,
        false /* don't check compatibility during copy */);
  }catch (InvalidRelException e) {
    throw new AssertionError(e);
  }
}
 
Example #8
Source File: UnionAllPrel.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Union copy(RelTraitSet traitSet, List<RelNode> inputs, boolean all) {
  try {
    return new UnionAllPrel(this.getCluster(), traitSet, inputs,
        false /* don't check compatibility during copy */);
  }catch (InvalidRelException e) {
    throw new AssertionError(e);
  }
}
 
Example #9
Source File: StreamRules.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a DeltaUnionTransposeRule.
 *
 * @param relBuilderFactory Builder for relational expressions
 */
public DeltaUnionTransposeRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(Delta.class,
          operand(Union.class, any())),
      relBuilderFactory, null);
}
 
Example #10
Source File: AggregateUnionAggregateRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a AggregateUnionAggregateRule.
 */
public AggregateUnionAggregateRule(Class<? extends Aggregate> aggregateClass,
    Class<? extends Union> unionClass,
    Class<? extends RelNode> firstUnionInputClass,
    Class<? extends RelNode> secondUnionInputClass,
    RelBuilderFactory relBuilderFactory,
    String desc) {
  super(
      operandJ(aggregateClass, null, Aggregate::isSimple,
          operand(unionClass,
              operand(firstUnionInputClass, any()),
              operand(secondUnionInputClass, any()))),
      relBuilderFactory, desc);
}
 
Example #11
Source File: GroupByPartitionRule.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
    final Aggregate aggregate = call.rel(0);
    final Union union = call.rel(1);
    final TableScan table = call.rel(2);
    if (!test(aggregate)) {
        return;
    }

    ImmutableBitSet groupSet = aggregate.getGroupSet();
    Integer integer = groupSet.asList().get(0);
    MycatPhysicalTable table1 = table.getTable().unwrap(MycatPhysicalTable.class);

    MycatLogicTable logicTable = table1.getLogicTable();
    TableHandler tableHandler = logicTable.getTable();
    if (tableHandler instanceof ShardingTable) {
        ShardingTable handler = (ShardingTable) tableHandler;
        List<SimpleColumnInfo> columns = handler.getColumns();
        if (integer < columns.size()) {
            if (handler.getNatureTableColumnInfo().getColumnInfo().equals(columns.get(integer))) {
                RelBuilder builder = call.builder();
                List<RelNode> inputs = union.getInputs();


            }
        }
    }

}
 
Example #12
Source File: SortLimitPushRule.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public SortLimitPushRule() {
    super(
            operandJ(Sort.class,null, (Predicate<Sort>) call -> {
                        if (call!=null) {
                            if ( call.isDistinct() ||
                                    (call.getChildExps() == null || call.getChildExps().isEmpty())) {
                                return false;
                            }
                        }
                        return true;
                    },
                    operand(Union.class, any())),
            RelFactories.LOGICAL_BUILDER, "SortLimitPushRule");
}
 
Example #13
Source File: CalciteUtls.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public static void collect(Union e, List<RelNode> unions) {
    for (RelNode input : e.getInputs()) {
        if (input instanceof Union){
            collect((Union)input,unions);
        }else {
            unions.add(input);
        }
    }
}
 
Example #14
Source File: UnionAllPrel.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public Union copy(RelTraitSet traitSet, List<RelNode> inputs, boolean all) {
  try {
    return new UnionAllPrel(this.getCluster(), traitSet, inputs,
        false /* don't check compatibility during copy */);
  }catch (InvalidRelException e) {
    throw new AssertionError(e);
  }
}
 
Example #15
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
public Result visitChild(int i, RelNode e) {
  if (e instanceof Union) {
    return visitUnion((Union) e);
  } else if (e instanceof Join) {
    return visitJoin((Join) e);
  } else if (e instanceof Filter) {
    return visitFilter((Filter) e);
  } else if (e instanceof Project) {
    return visitProject((Project) e);
  } else if (e instanceof Aggregate) {
    return visitAggregate((Aggregate) e);
  } else if (e instanceof TableScan) {
    return visitTableScan((TableScan) e);
  } else if (e instanceof Intersect) {
    return visitIntersect((Intersect) e);
  } else if (e instanceof Minus) {
    return visitMinus((Minus) e);
  } else if (e instanceof Calc) {
    return visitCalc((Calc) e);
  } else if (e instanceof Sort) {
    return visitSort((Sort) e);
  } else if (e instanceof TableModify) {
    return visitTableModify((TableModify) e);
  } else if (e instanceof Limit) {
    return visitLimit((Limit) e);
  } else {
    throw new AssertionError("Need to Implement for " + e.getClass().getName()); // TODO:
  }
}
 
Example #16
Source File: OLAPUnionRule.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode convert(RelNode rel) {
    final Union union = (Union) rel;
    final RelTraitSet traitSet = union.getTraitSet().replace(OLAPRel.CONVENTION);
    final List<RelNode> inputs = union.getInputs();
    return new OLAPUnionRel(rel.getCluster(), traitSet, convertList(inputs, OLAPRel.CONVENTION), union.all);
}
 
Example #17
Source File: StreamRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a DeltaUnionTransposeRule.
 *
 * @param relBuilderFactory Builder for relational expressions
 */
public DeltaUnionTransposeRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(Delta.class,
          operand(Union.class, any())),
      relBuilderFactory, null);
}
 
Example #18
Source File: RelMdDistinctRowCount.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getDistinctRowCount(Union rel, RelMetadataQuery mq,
    ImmutableBitSet groupKey, RexNode predicate) {
  double rowCount = 0.0;
  int[] adjustments = new int[rel.getRowType().getFieldCount()];
  RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  for (RelNode input : rel.getInputs()) {
    // convert the predicate to reference the types of the union child
    RexNode modifiedPred;
    if (predicate == null) {
      modifiedPred = null;
    } else {
      modifiedPred =
          predicate.accept(
              new RelOptUtil.RexInputConverter(
                  rexBuilder,
                  null,
                  input.getRowType().getFieldList(),
                  adjustments));
    }
    Double partialRowCount =
        mq.getDistinctRowCount(input, groupKey, modifiedPred);
    if (partialRowCount == null) {
      return null;
    }
    rowCount += partialRowCount;
  }
  return rowCount;
}
 
Example #19
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountUnionOnFinite() {
  final String sql = "select ename from (select * from emp limit 100)\n"
      + "union all\n"
      + "select name from (select * from dept limit 40)";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 2);
  expected.put(Union.class, 1);
  expected.put(Project.class, 4);
  expected.put(Sort.class, 2);
  checkNodeTypeCount(sql, expected);
}
 
Example #20
Source File: RelMdUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns an estimate of the number of rows returned by a {@link Union}
 * (before duplicates are eliminated). */
public static double getUnionAllRowCount(RelMetadataQuery mq, Union rel) {
  double rowCount = 0;
  for (RelNode input : rel.getInputs()) {
    rowCount += mq.getRowCount(input);
  }
  return rowCount;
}
 
Example #21
Source File: RelMdSelectivity.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getSelectivity(Union rel, RelMetadataQuery mq,
    RexNode predicate) {
  if ((rel.getInputs().size() == 0) || (predicate == null)) {
    return 1.0;
  }

  double sumRows = 0.0;
  double sumSelectedRows = 0.0;
  int[] adjustments = new int[rel.getRowType().getFieldCount()];
  RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  for (RelNode input : rel.getInputs()) {
    Double nRows = mq.getRowCount(input);
    if (nRows == null) {
      return null;
    }

    // convert the predicate to reference the types of the union child
    RexNode modifiedPred =
        predicate.accept(
            new RelOptUtil.RexInputConverter(
                rexBuilder,
                null,
                input.getRowType().getFieldList(),
                adjustments));
    double sel = mq.getSelectivity(input, modifiedPred);

    sumRows += nRows;
    sumSelectedRows += nRows * sel;
  }

  if (sumRows < 1.0) {
    sumRows = 1.0;
  }
  return sumSelectedRows / sumRows;
}
 
Example #22
Source File: RelMdMaxRowCount.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getMaxRowCount(Union rel, RelMetadataQuery mq) {
  double rowCount = 0.0;
  for (RelNode input : rel.getInputs()) {
    Double partialRowCount = mq.getMaxRowCount(input);
    if (partialRowCount == null) {
      return null;
    }
    rowCount += partialRowCount;
  }
  return rowCount;
}
 
Example #23
Source File: RelMdMinRowCount.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getMinRowCount(Union rel, RelMetadataQuery mq) {
  double rowCount = 0.0;
  for (RelNode input : rel.getInputs()) {
    Double partialRowCount = mq.getMinRowCount(input);
    if (partialRowCount != null) {
      rowCount += partialRowCount;
    }
  }

  if (rel.all) {
    return rowCount;
  } else {
    return Math.min(rowCount, 1d);
  }
}
 
Example #24
Source File: RelMdRowCount.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getRowCount(Union rel, RelMetadataQuery mq) {
  double rowCount = 0.0;
  for (RelNode input : rel.getInputs()) {
    Double partialRowCount = mq.getRowCount(input);
    if (partialRowCount == null) {
      return null;
    }
    rowCount += partialRowCount;
  }
  if (!rel.all) {
    rowCount *= 0.5;
  }
  return rowCount;
}
 
Example #25
Source File: RelMdPercentageOriginalRows.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getPercentageOriginalRows(Union rel, RelMetadataQuery mq) {
  double numerator = 0.0;
  double denominator = 0.0;

  // Ignore rel.isDistinct() because it's the same as an aggregate.

  // REVIEW jvs 28-Mar-2006: The original Broadbase formula was broken.
  // It was multiplying percentage into the numerator term rather than
  // than dividing it out of the denominator term, which would be OK if
  // there weren't summation going on.  Probably the cause of the error
  // was the desire to avoid division by zero, which I don't know how to
  // handle so I punt, meaning we return a totally wrong answer in the
  // case where a huge table has been completely filtered away.

  for (RelNode input : rel.getInputs()) {
    Double rowCount = mq.getRowCount(input);
    if (rowCount == null) {
      continue;
    }
    Double percentage = mq.getPercentageOriginalRows(input);
    if (percentage == null) {
      continue;
    }
    if (percentage != 0.0) {
      denominator += rowCount / percentage;
      numerator += rowCount;
    }
  }

  return quotientForPercentage(numerator, denominator);
}
 
Example #26
Source File: UnionPullUpConstantsRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a UnionPullUpConstantsRule. */
public UnionPullUpConstantsRule(Class<? extends Union> unionClass,
    RelBuilderFactory relBuilderFactory) {
  // If field count is 1, then there's no room for
  // optimization since we cannot create an empty Project
  // operator. If we created a Project with one column, this rule would
  // cycle.
  super(
      operandJ(unionClass, null, union -> union.getRowType().getFieldCount() > 1, any()),
      relBuilderFactory, null);
}
 
Example #27
Source File: AggregateUnionTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates an AggregateUnionTransposeRule. */
public AggregateUnionTransposeRule(Class<? extends Aggregate> aggregateClass,
    Class<? extends Union> unionClass, RelBuilderFactory relBuilderFactory) {
  super(
      operand(aggregateClass,
          operand(unionClass, any())),
      relBuilderFactory, null);
}
 
Example #28
Source File: AggregateUnionTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public AggregateUnionTransposeRule(Class<? extends Aggregate> aggregateClass,
    RelFactories.AggregateFactory aggregateFactory,
    Class<? extends Union> unionClass,
    RelFactories.SetOpFactory setOpFactory) {
  this(aggregateClass, unionClass,
      RelBuilder.proto(aggregateFactory, setOpFactory));
}
 
Example #29
Source File: UnionToDistinctRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a UnionToDistinctRule.
 */
public UnionToDistinctRule(Class<? extends Union> unionClazz,
    RelBuilderFactory relBuilderFactory) {
  super(
      operandJ(unionClazz, null, union -> !union.all, any()),
      relBuilderFactory, null);
}
 
Example #30
Source File: AggregateUnionAggregateRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public AggregateUnionAggregateRule(Class<? extends Aggregate> aggregateClass,
    RelFactories.AggregateFactory aggregateFactory,
    Class<? extends Union> unionClass,
    RelFactories.SetOpFactory setOpFactory) {
  this(aggregateClass, unionClass, RelNode.class, RelNode.class,
      RelBuilder.proto(aggregateFactory, setOpFactory),
      "AggregateUnionAggregateRule");
}