Search in sources :

Example 41 with Union

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Union in project Mycat2 by MyCATApache.

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();
}
Also used : Project(org.apache.calcite.rel.core.Project) TableScan(org.apache.calcite.rel.core.TableScan) RexProgram(org.apache.calcite.rex.RexProgram) Correlate(org.apache.calcite.rel.core.Correlate) Filter(org.apache.calcite.rel.core.Filter) Join(org.apache.calcite.rel.core.Join) Union(org.apache.calcite.rel.core.Union) HashSet(java.util.HashSet) RexNode(org.apache.calcite.rex.RexNode) Intersect(org.apache.calcite.rel.core.Intersect) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) TableModify(org.apache.calcite.rel.core.TableModify) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) SingleRel(org.apache.calcite.rel.SingleRel) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ImmutableSet(com.google.common.collect.ImmutableSet) Minus(org.apache.calcite.rel.core.Minus) Linq4j(org.apache.calcite.linq4j.Linq4j) MycatView(io.mycat.calcite.logical.MycatView) Set(java.util.Set) RelNode(org.apache.calcite.rel.RelNode) Aggregate(org.apache.calcite.rel.core.Aggregate) JoinInfo(org.apache.calcite.rel.core.JoinInfo) Maps(com.google.common.collect.Maps) RexInputRef(org.apache.calcite.rex.RexInputRef) List(java.util.List) Sort(org.apache.calcite.rel.core.Sort) Calc(org.apache.calcite.rel.core.Calc) Util(org.apache.calcite.util.Util) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ImmutableSet(com.google.common.collect.ImmutableSet) RexInputRef(org.apache.calcite.rex.RexInputRef) List(java.util.List) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) RexNode(org.apache.calcite.rex.RexNode)

Example 42 with Union

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Union in project Mycat2 by MyCATApache.

the class MycatUnionRule method convert.

public RelNode convert(RelNode rel) {
    final Union union = (Union) rel;
    final RelTraitSet traitSet = union.getTraitSet().replace(out);
    return MycatUnion.create(traitSet, convertList(union.getInputs(), out), union.all);
}
Also used : RelTraitSet(org.apache.calcite.plan.RelTraitSet) Union(org.apache.calcite.rel.core.Union) MycatUnion(io.mycat.calcite.physical.MycatUnion)

Example 43 with Union

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Union in project ignite-3 by apache.

the class UnionPlannerTest method testUnion.

@Test
public void testUnion() throws Exception {
    IgniteSchema publicSchema = prepareSchema();
    String sql = "" + "SELECT * FROM table1 " + "UNION " + "SELECT * FROM table2 " + "UNION " + "SELECT * FROM table3 ";
    assertPlan(sql, publicSchema, isInstanceOf(IgniteReduceAggregateBase.class).and(hasChildThat(isInstanceOf(Union.class))));
    assertPlan(sql, publicSchema, isInstanceOf(IgniteReduceAggregateBase.class).and(hasChildThat(isInstanceOf(Union.class).and(input(0, isTableScan("TABLE1"))).and(input(1, isTableScan("TABLE2"))).and(input(2, isTableScan("TABLE3"))))));
}
Also used : IgniteSchema(org.apache.ignite.internal.sql.engine.schema.IgniteSchema) Union(org.apache.calcite.rel.core.Union) Test(org.junit.jupiter.api.Test)

Aggregations

Union (org.apache.calcite.rel.core.Union)41 RelNode (org.apache.calcite.rel.RelNode)28 Aggregate (org.apache.calcite.rel.core.Aggregate)15 Sort (org.apache.calcite.rel.core.Sort)13 ArrayList (java.util.ArrayList)12 RexNode (org.apache.calcite.rex.RexNode)12 Join (org.apache.calcite.rel.core.Join)11 Filter (org.apache.calcite.rel.core.Filter)10 Project (org.apache.calcite.rel.core.Project)10 RelBuilder (org.apache.calcite.tools.RelBuilder)10 TableScan (org.apache.calcite.rel.core.TableScan)9 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)9 RexBuilder (org.apache.calcite.rex.RexBuilder)9 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)9 Intersect (org.apache.calcite.rel.core.Intersect)8 Minus (org.apache.calcite.rel.core.Minus)8 Correlate (org.apache.calcite.rel.core.Correlate)7 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)7 Map (java.util.Map)6 Calc (org.apache.calcite.rel.core.Calc)6