Search in sources :

Example 1 with Match

use of org.apache.calcite.rel.core.Match in project beam by apache.

the class BeamMatchRule method convert.

@Override
public RelNode convert(RelNode rel) {
    Match match = (Match) rel;
    final RelNode input = match.getInput();
    return new BeamMatchRel(match.getCluster(), match.getTraitSet().replace(BeamLogicalConvention.INSTANCE), convert(input, input.getTraitSet().replace(BeamLogicalConvention.INSTANCE)), match.getRowType(), match.getPattern(), match.isStrictStart(), match.isStrictEnd(), match.getPatternDefinitions(), match.getMeasures(), match.getAfter(), match.getSubsets(), match.isAllRows(), match.getPartitionKeys(), match.getOrderKeys(), match.getInterval());
}
Also used : RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) BeamMatchRel(org.apache.beam.sdk.extensions.sql.impl.rel.BeamMatchRel) Match(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Match) LogicalMatch(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.logical.LogicalMatch)

Example 2 with Match

use of org.apache.calcite.rel.core.Match in project calcite by apache.

the class RelBuilder method unpivot.

/**
 * Creates an Unpivot.
 *
 * <p>To achieve the same effect as the SQL
 *
 * <blockquote><pre>{@code
 * SELECT *
 * FROM (SELECT deptno, job, sal, comm FROM emp)
 *   UNPIVOT INCLUDE NULLS (remuneration
 *     FOR remuneration_type IN (comm AS 'commission',
 *                               sal AS 'salary'))
 * }</pre></blockquote>
 *
 * <p>use the builder as follows:
 *
 * <blockquote><pre>{@code
 * RelBuilder b;
 * b.scan("EMP");
 * final List<String> measureNames = Arrays.asList("REMUNERATION");
 * final List<String> axisNames = Arrays.asList("REMUNERATION_TYPE");
 * final Map<List<RexLiteral>, List<RexNode>> axisMap =
 *     ImmutableMap.<List<RexLiteral>, List<RexNode>>builder()
 *         .put(Arrays.asList(b.literal("commission")),
 *             Arrays.asList(b.field("COMM")))
 *         .put(Arrays.asList(b.literal("salary")),
 *             Arrays.asList(b.field("SAL")))
 *         .build();
 * b.unpivot(false, measureNames, axisNames, axisMap);
 * }</pre></blockquote>
 *
 * <p>The query generates two columns: {@code remuneration_type} (an axis
 * column) and {@code remuneration} (a measure column). Axis columns contain
 * values to indicate the source of the row (in this case, {@code 'salary'}
 * if the row came from the {@code sal} column, and {@code 'commission'}
 * if the row came from the {@code comm} column).
 *
 * @param includeNulls Whether to include NULL values in the output
 * @param measureNames Names of columns to be generated to hold pivoted
 *                    measures
 * @param axisNames Names of columns to be generated to hold qualifying values
 * @param axisMap Mapping from the columns that hold measures to the values
 *           that the axis columns will hold in the generated rows
 * @return This RelBuilder
 */
public RelBuilder unpivot(boolean includeNulls, Iterable<String> measureNames, Iterable<String> axisNames, Iterable<? extends Map.Entry<? extends List<? extends RexLiteral>, ? extends List<? extends RexNode>>> axisMap) {
    // Make immutable copies of all arguments.
    final List<String> measureNameList = ImmutableList.copyOf(measureNames);
    final List<String> axisNameList = ImmutableList.copyOf(axisNames);
    final List<Pair<List<RexLiteral>, List<RexNode>>> map = StreamSupport.stream(axisMap.spliterator(), false).map(pair -> Pair.<List<RexLiteral>, List<RexNode>>of(ImmutableList.<RexLiteral>copyOf(pair.getKey()), ImmutableList.<RexNode>copyOf(pair.getValue()))).collect(Util.toImmutableList());
    // Check that counts match.
    Pair.forEach(map, (valueList, inputMeasureList) -> {
        if (inputMeasureList.size() != measureNameList.size()) {
            throw new IllegalArgumentException("Number of measures (" + inputMeasureList.size() + ") must match number of measure names (" + measureNameList.size() + ")");
        }
        if (valueList.size() != axisNameList.size()) {
            throw new IllegalArgumentException("Number of axis values (" + valueList.size() + ") match match number of axis names (" + axisNameList.size() + ")");
        }
    });
    final RelDataType leftRowType = peek().getRowType();
    final BitSet usedFields = new BitSet();
    Pair.forEach(map, (aliases, nodes) -> nodes.forEach(node -> {
        if (node instanceof RexInputRef) {
            usedFields.set(((RexInputRef) node).getIndex());
        }
    }));
    // Create "VALUES (('commission'), ('salary')) AS t (remuneration_type)"
    values(ImmutableList.copyOf(Pair.left(map)), axisNameList);
    join(JoinRelType.INNER);
    final ImmutableBitSet unusedFields = ImmutableBitSet.range(leftRowType.getFieldCount()).except(ImmutableBitSet.fromBitSet(usedFields));
    final List<RexNode> projects = new ArrayList<>(fields(unusedFields));
    Ord.forEach(axisNameList, (dimensionName, d) -> projects.add(alias(field(leftRowType.getFieldCount() + d), dimensionName)));
    final List<RexNode> conditions = new ArrayList<>();
    Ord.forEach(measureNameList, (measureName, m) -> {
        final List<RexNode> caseOperands = new ArrayList<>();
        Pair.forEach(map, (literals, nodes) -> {
            Ord.forEach(literals, (literal, d) -> conditions.add(equals(field(leftRowType.getFieldCount() + d), literal)));
            caseOperands.add(and(conditions));
            conditions.clear();
            caseOperands.add(nodes.get(m));
        });
        caseOperands.add(literal(null));
        projects.add(alias(call(SqlStdOperatorTable.CASE, caseOperands), measureName));
    });
    project(projects);
    if (!includeNulls) {
        // Add 'WHERE m1 IS NOT NULL OR m2 IS NOT NULL'
        final BitSet notNullFields = new BitSet();
        Ord.forEach(measureNameList, (measureName, m) -> {
            final int f = unusedFields.cardinality() + axisNameList.size() + m;
            conditions.add(isNotNull(field(f)));
            notNullFields.set(f);
        });
        filter(or(conditions));
        if (measureNameList.size() == 1) {
            // If there is one field, EXCLUDE NULLS will have converted it to NOT
            // NULL.
            final RelDataTypeFactory.Builder builder = getTypeFactory().builder();
            peek().getRowType().getFieldList().forEach(field -> {
                final RelDataType type = field.getType();
                builder.add(field.getName(), notNullFields.get(field.getIndex()) ? getTypeFactory().createTypeWithNullability(type, false) : type);
            });
            convert(builder.build(), false);
        }
        conditions.clear();
    }
    return this;
}
Also used : Values(org.apache.calcite.rel.core.Values) Mappings(org.apache.calcite.util.mapping.Mappings) Arrays(java.util.Arrays) UnaryOperator(java.util.function.UnaryOperator) RESOURCE(org.apache.calcite.util.Static.RESOURCE) Contexts(org.apache.calcite.plan.Contexts) Union(org.apache.calcite.rel.core.Union) BigDecimal(java.math.BigDecimal) CorrelationId(org.apache.calcite.rel.core.CorrelationId) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) Map(java.util.Map) TableFunctionReturnTypeInference(org.apache.calcite.sql.type.TableFunctionReturnTypeInference) EnumSet(java.util.EnumSet) RexWindowBound(org.apache.calcite.rex.RexWindowBound) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) Uncollect(org.apache.calcite.rel.core.Uncollect) SqlKind(org.apache.calcite.sql.SqlKind) Minus(org.apache.calcite.rel.core.Minus) SqlCountAggFunction(org.apache.calcite.sql.fun.SqlCountAggFunction) SqlQuantifyOperator(org.apache.calcite.sql.fun.SqlQuantifyOperator) Set(java.util.Set) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelCollation(org.apache.calcite.rel.RelCollation) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) RexCall(org.apache.calcite.rex.RexCall) RelOptTableImpl(org.apache.calcite.prepare.RelOptTableImpl) Iterables(com.google.common.collect.Iterables) Holder(org.apache.calcite.util.Holder) Correlate(org.apache.calcite.rel.core.Correlate) RepeatUnion(org.apache.calcite.rel.core.RepeatUnion) Ord(org.apache.calcite.linq4j.Ord) Filter(org.apache.calcite.rel.core.Filter) ListTransientTable(org.apache.calcite.schema.impl.ListTransientTable) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) Join(org.apache.calcite.rel.core.Join) TreeSet(java.util.TreeSet) RelOptTable(org.apache.calcite.plan.RelOptTable) ArrayList(java.util.ArrayList) RexFieldCollation(org.apache.calcite.rex.RexFieldCollation) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) Optionality(org.apache.calcite.util.Optionality) Lists(com.google.common.collect.Lists) ViewExpanders(org.apache.calcite.plan.ViewExpanders) RelColumnMapping(org.apache.calcite.rel.metadata.RelColumnMapping) StreamSupport(java.util.stream.StreamSupport) SqlWindow(org.apache.calcite.sql.SqlWindow) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) Aggregate(org.apache.calcite.rel.core.Aggregate) SqlReturnTypeInference(org.apache.calcite.sql.type.SqlReturnTypeInference) RelHomogeneousShuttle(org.apache.calcite.rel.RelHomogeneousShuttle) RexWindowBounds(org.apache.calcite.rex.RexWindowBounds) RelOptSchema(org.apache.calcite.plan.RelOptSchema) RelDistribution(org.apache.calcite.rel.RelDistribution) RexCallBinding(org.apache.calcite.rex.RexCallBinding) JoinRelType(org.apache.calcite.rel.core.JoinRelType) AggregateCall(org.apache.calcite.rel.core.AggregateCall) Preconditions(com.google.common.base.Preconditions) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) ImmutableSortedMultiset(com.google.common.collect.ImmutableSortedMultiset) ArrayDeque(java.util.ArrayDeque) Convention(org.apache.calcite.plan.Convention) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SortedSet(java.util.SortedSet) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) RelFactories(org.apache.calcite.rel.core.RelFactories) Match(org.apache.calcite.rel.core.Match) AbstractList(java.util.AbstractList) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) RexUtil(org.apache.calcite.rex.RexUtil) SqlLikeOperator(org.apache.calcite.sql.fun.SqlLikeOperator) SqlUtil(org.apache.calcite.sql.SqlUtil) RexNode(org.apache.calcite.rex.RexNode) RelHint(org.apache.calcite.rel.hint.RelHint) Locale(java.util.Locale) Intersect(org.apache.calcite.rel.core.Intersect) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) RelOptCluster(org.apache.calcite.plan.RelOptCluster) TableFunctionScan(org.apache.calcite.rel.core.TableFunctionScan) Litmus(org.apache.calcite.util.Litmus) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) ImmutableMap(com.google.common.collect.ImmutableMap) RexLiteral(org.apache.calcite.rex.RexLiteral) Context(org.apache.calcite.plan.Context) Nullness.castNonNull(org.apache.calcite.linq4j.Nullness.castNonNull) ImmutableNullableList(org.apache.calcite.util.ImmutableNullableList) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) RexInputRef(org.apache.calcite.rex.RexInputRef) Objects(java.util.Objects) List(java.util.List) Sort(org.apache.calcite.rel.core.Sort) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Spool(org.apache.calcite.rel.core.Spool) RexSimplify(org.apache.calcite.rex.RexSimplify) Hook(org.apache.calcite.runtime.Hook) Project(org.apache.calcite.rel.core.Project) TableScan(org.apache.calcite.rel.core.TableScan) Multiset(com.google.common.collect.Multiset) Hintable(org.apache.calcite.rel.hint.Hintable) TransientTable(org.apache.calcite.schema.TransientTable) HashMap(java.util.HashMap) Deque(java.util.Deque) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Function(java.util.function.Function) HashSet(java.util.HashSet) Snapshot(org.apache.calcite.rel.core.Snapshot) ImmutableList(com.google.common.collect.ImmutableList) Value(org.immutables.value.Value) Pair(org.apache.calcite.util.Pair) Mapping(org.apache.calcite.util.mapping.Mapping) Objects.requireNonNull(java.util.Objects.requireNonNull) SqlOperator(org.apache.calcite.sql.SqlOperator) RexExecutor(org.apache.calcite.rex.RexExecutor) Nullable(org.checkerframework.checker.nullness.qual.Nullable) RelCollations(org.apache.calcite.rel.RelCollations) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RexBuilder(org.apache.calcite.rex.RexBuilder) Experimental(org.apache.calcite.linq4j.function.Experimental) RexSubQuery(org.apache.calcite.rex.RexSubQuery) RelNode(org.apache.calcite.rel.RelNode) UNION(org.apache.calcite.sql.SqlKind.UNION) BitSet(java.util.BitSet) RexShuttle(org.apache.calcite.rex.RexShuttle) Util(org.apache.calcite.util.Util) Collections(java.util.Collections) TableSpool(org.apache.calcite.rel.core.TableSpool) RexLiteral(org.apache.calcite.rex.RexLiteral) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) RelHint(org.apache.calcite.rel.hint.RelHint) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexInputRef(org.apache.calcite.rex.RexInputRef) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) ImmutableNullableList(org.apache.calcite.util.ImmutableNullableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Pair(org.apache.calcite.util.Pair) RexNode(org.apache.calcite.rex.RexNode)

Example 3 with Match

use of org.apache.calcite.rel.core.Match 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");
}
Also used : Uncollect(org.apache.calcite.rel.core.Uncollect) Collect(org.apache.calcite.rel.core.Collect) Values(org.apache.calcite.rel.core.Values) Union(org.apache.calcite.rel.core.Union) Match(org.apache.calcite.rel.core.Match) LogicalMatch(org.apache.calcite.rel.logical.LogicalMatch) HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex) Intersect(org.apache.calcite.rel.core.Intersect) LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Sort(org.apache.calcite.rel.core.Sort) LogicalTableModify(org.apache.calcite.rel.logical.LogicalTableModify) TableModify(org.apache.calcite.rel.core.TableModify) RelSubset(org.apache.calcite.plan.volcano.RelSubset) LogicalWindow(org.apache.calcite.rel.logical.LogicalWindow) Window(org.apache.calcite.rel.core.Window) TableScan(org.apache.calcite.rel.core.TableScan) Correlate(org.apache.calcite.rel.core.Correlate) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) Sample(org.apache.calcite.rel.core.Sample) Join(org.apache.calcite.rel.core.Join) LogicalCalc(org.apache.calcite.rel.logical.LogicalCalc) Calc(org.apache.calcite.rel.core.Calc) Exchange(org.apache.calcite.rel.core.Exchange) LogicalExchange(org.apache.calcite.rel.logical.LogicalExchange) Project(org.apache.calcite.rel.core.Project) RelNode(org.apache.calcite.rel.RelNode) LogicalTableFunctionScan(org.apache.calcite.rel.logical.LogicalTableFunctionScan) TableFunctionScan(org.apache.calcite.rel.core.TableFunctionScan) Filter(org.apache.calcite.rel.core.Filter) Aggregate(org.apache.calcite.rel.core.Aggregate) Minus(org.apache.calcite.rel.core.Minus)

Aggregations

Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedMultiset (com.google.common.collect.ImmutableSortedMultiset)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 Multiset (com.google.common.collect.Multiset)1 Sets (com.google.common.collect.Sets)1 BigDecimal (java.math.BigDecimal)1 AbstractList (java.util.AbstractList)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 BitSet (java.util.BitSet)1 Collections (java.util.Collections)1 Deque (java.util.Deque)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1