use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Union in project calcite by apache.
the class RelBuilder method setOp.
private RelBuilder setOp(boolean all, SqlKind kind, int n) {
List<RelNode> inputs = new ArrayList<>();
for (int i = 0; i < n; i++) {
inputs.add(0, build());
}
switch(kind) {
case UNION:
case INTERSECT:
case EXCEPT:
if (n < 1) {
throw new IllegalArgumentException("bad INTERSECT/UNION/EXCEPT input count");
}
break;
default:
throw new AssertionError("bad setOp " + kind);
}
if (n == 1) {
return push(inputs.get(0));
}
if (config.simplifyValues() && kind == UNION && inputs.stream().allMatch(r -> r instanceof Values)) {
List<RelDataType> inputTypes = Util.transform(inputs, RelNode::getRowType);
RelDataType rowType = getTypeFactory().leastRestrictive(inputTypes);
requireNonNull(rowType, () -> "leastRestrictive(" + inputTypes + ")");
final List<List<RexLiteral>> tuples = new ArrayList<>();
for (RelNode input : inputs) {
tuples.addAll(((Values) input).tuples);
}
final List<List<RexLiteral>> tuples2 = all ? tuples : Util.distinctList(tuples);
return values(tuples2, rowType);
}
return push(struct.setOpFactory.createSetOp(kind, inputs, all));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Union in project calcite by apache.
the class RelMdUniqueKeys method getProjectUniqueKeys.
private static Set<ImmutableBitSet> getProjectUniqueKeys(SingleRel rel, RelMetadataQuery mq, boolean ignoreNulls, List<RexNode> projExprs) {
// LogicalProject maps a set of rows to a different set;
// Without knowledge of the mapping function(whether it
// preserves uniqueness), it is only safe to derive uniqueness
// info from the child of a project when the mapping is f(a) => a.
//
// Further more, the unique bitset coming from the child needs
// to be mapped to match the output of the project.
// Single input can be mapped to multiple outputs
ImmutableMultimap.Builder<Integer, Integer> inToOutPosBuilder = ImmutableMultimap.builder();
ImmutableBitSet.Builder mappedInColumnsBuilder = ImmutableBitSet.builder();
// Build an input to output position map.
for (int i = 0; i < projExprs.size(); i++) {
RexNode projExpr = projExprs.get(i);
if (projExpr instanceof RexInputRef) {
int inputIndex = ((RexInputRef) projExpr).getIndex();
inToOutPosBuilder.put(inputIndex, i);
mappedInColumnsBuilder.set(inputIndex);
}
}
ImmutableBitSet inColumnsUsed = mappedInColumnsBuilder.build();
if (inColumnsUsed.isEmpty()) {
// return empty set.
return ImmutableSet.of();
}
Set<ImmutableBitSet> childUniqueKeySet = mq.getUniqueKeys(rel.getInput(), ignoreNulls);
if (childUniqueKeySet == null) {
return ImmutableSet.of();
}
Map<Integer, ImmutableBitSet> mapInToOutPos = Maps.transformValues(inToOutPosBuilder.build().asMap(), ImmutableBitSet::of);
ImmutableSet.Builder<ImmutableBitSet> resultBuilder = ImmutableSet.builder();
// projected.
for (ImmutableBitSet colMask : childUniqueKeySet) {
if (!inColumnsUsed.contains(colMask)) {
// colMask contains a column that is not projected as RexInput => the key is not unique
continue;
}
// colMask is mapped to output project, however, the column can be mapped more than once:
// select id, id, id, unique2, unique2
// the resulting unique keys would be {{0},{3}}, {{0},{4}}, {{0},{1},{4}}, ...
Iterable<List<ImmutableBitSet>> product = Linq4j.product(Util.transform(colMask, in -> Util.filter(requireNonNull(mapInToOutPos.get(in), () -> "no entry for column " + in + " in mapInToOutPos: " + mapInToOutPos).powerSet(), bs -> !bs.isEmpty())));
resultBuilder.addAll(Util.transform(product, ImmutableBitSet::union));
}
return resultBuilder.build();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Union in project calcite by apache.
the class MutableRels method toMutable.
public static MutableRel toMutable(RelNode rel) {
if (rel instanceof HepRelVertex) {
return toMutable(((HepRelVertex) rel).getCurrentRel());
}
if (rel instanceof RelSubset) {
RelSubset subset = (RelSubset) rel;
RelNode best = subset.getBest();
if (best == null) {
best = requireNonNull(subset.getOriginal(), () -> "subset.getOriginal() is null for " + subset);
}
return toMutable(best);
}
if (rel instanceof TableScan) {
return MutableScan.of((TableScan) rel);
}
if (rel instanceof Values) {
return MutableValues.of((Values) rel);
}
if (rel instanceof Project) {
final Project project = (Project) rel;
final MutableRel input = toMutable(project.getInput());
return MutableProject.of(input, project.getProjects(), project.getRowType().getFieldNames());
}
if (rel instanceof Filter) {
final Filter filter = (Filter) rel;
final MutableRel input = toMutable(filter.getInput());
return MutableFilter.of(input, filter.getCondition());
}
if (rel instanceof Aggregate) {
final Aggregate aggregate = (Aggregate) rel;
final MutableRel input = toMutable(aggregate.getInput());
return MutableAggregate.of(input, aggregate.getGroupSet(), aggregate.getGroupSets(), aggregate.getAggCallList());
}
if (rel instanceof Sort) {
final Sort sort = (Sort) rel;
final MutableRel input = toMutable(sort.getInput());
return MutableSort.of(input, sort.getCollation(), sort.offset, sort.fetch);
}
if (rel instanceof Calc) {
final Calc calc = (Calc) rel;
final MutableRel input = toMutable(calc.getInput());
return MutableCalc.of(input, calc.getProgram());
}
if (rel instanceof Exchange) {
final Exchange exchange = (Exchange) rel;
final MutableRel input = toMutable(exchange.getInput());
return MutableExchange.of(input, exchange.getDistribution());
}
if (rel instanceof Collect) {
final Collect collect = (Collect) rel;
final MutableRel input = toMutable(collect.getInput());
return MutableCollect.of(collect.getRowType(), input, collect.getFieldName());
}
if (rel instanceof Uncollect) {
final Uncollect uncollect = (Uncollect) rel;
final MutableRel input = toMutable(uncollect.getInput());
return MutableUncollect.of(uncollect.getRowType(), input, uncollect.withOrdinality);
}
if (rel instanceof Window) {
final Window window = (Window) rel;
final MutableRel input = toMutable(window.getInput());
return MutableWindow.of(window.getRowType(), input, window.groups, window.getConstants());
}
if (rel instanceof Match) {
final Match match = (Match) rel;
final MutableRel input = toMutable(match.getInput());
return MutableMatch.of(match.getRowType(), input, match.getPattern(), match.isStrictStart(), match.isStrictEnd(), match.getPatternDefinitions(), match.getMeasures(), match.getAfter(), match.getSubsets(), match.isAllRows(), match.getPartitionKeys(), match.getOrderKeys(), match.getInterval());
}
if (rel instanceof TableModify) {
final TableModify modify = (TableModify) rel;
final MutableRel input = toMutable(modify.getInput());
return MutableTableModify.of(modify.getRowType(), input, modify.getTable(), modify.getCatalogReader(), modify.getOperation(), modify.getUpdateColumnList(), modify.getSourceExpressionList(), modify.isFlattened());
}
if (rel instanceof Sample) {
final Sample sample = (Sample) rel;
final MutableRel input = toMutable(sample.getInput());
return MutableSample.of(input, sample.getSamplingParameters());
}
if (rel instanceof TableFunctionScan) {
final TableFunctionScan tableFunctionScan = (TableFunctionScan) rel;
final List<MutableRel> inputs = toMutables(tableFunctionScan.getInputs());
return MutableTableFunctionScan.of(tableFunctionScan.getCluster(), tableFunctionScan.getRowType(), inputs, tableFunctionScan.getCall(), tableFunctionScan.getElementType(), tableFunctionScan.getColumnMappings());
}
// is a sub-class of Join.
if (rel instanceof Join) {
final Join join = (Join) rel;
final MutableRel left = toMutable(join.getLeft());
final MutableRel right = toMutable(join.getRight());
return MutableJoin.of(join.getRowType(), left, right, join.getCondition(), join.getJoinType(), join.getVariablesSet());
}
if (rel instanceof Correlate) {
final Correlate correlate = (Correlate) rel;
final MutableRel left = toMutable(correlate.getLeft());
final MutableRel right = toMutable(correlate.getRight());
return MutableCorrelate.of(correlate.getRowType(), left, right, correlate.getCorrelationId(), correlate.getRequiredColumns(), correlate.getJoinType());
}
if (rel instanceof Union) {
final Union union = (Union) rel;
final List<MutableRel> inputs = toMutables(union.getInputs());
return MutableUnion.of(union.getRowType(), inputs, union.all);
}
if (rel instanceof Minus) {
final Minus minus = (Minus) rel;
final List<MutableRel> inputs = toMutables(minus.getInputs());
return MutableMinus.of(minus.getRowType(), inputs, minus.all);
}
if (rel instanceof Intersect) {
final Intersect intersect = (Intersect) rel;
final List<MutableRel> inputs = toMutables(intersect.getInputs());
return MutableIntersect.of(intersect.getRowType(), inputs, intersect.all);
}
throw new RuntimeException("cannot translate " + rel + " to MutableRel");
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Union in project druid by apache.
the class DruidUnionRule method onMatch.
@Override
public void onMatch(final RelOptRuleCall call) {
final Union unionRel = call.rel(0);
final DruidRel<?> someDruidRel = call.rel(1);
final List<RelNode> inputs = unionRel.getInputs();
// Can only do UNION ALL.
if (unionRel.all) {
call.transformTo(DruidUnionRel.create(someDruidRel.getPlannerContext(), unionRel.getRowType(), inputs, -1));
} else {
plannerContext.setPlanningError("SQL requires 'UNION' but only 'UNION ALL' is supported.");
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Union in project druid by apache.
the class DruidUnionRule method matches.
@Override
public boolean matches(RelOptRuleCall call) {
// Make DruidUnionRule and DruidUnionDataSourceRule mutually exclusive.
final Union unionRel = call.rel(0);
final DruidRel<?> firstDruidRel = call.rel(1);
final DruidRel<?> secondDruidRel = call.rel(2);
return !DruidUnionDataSourceRule.isCompatible(unionRel, firstDruidRel, secondDruidRel, null);
}
Aggregations