Search in sources :

Example 1 with Collect

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

the class EnumerableCollectRule method convert.

public RelNode convert(RelNode rel) {
    final Collect collect = (Collect) rel;
    final RelTraitSet traitSet = collect.getTraitSet().replace(EnumerableConvention.INSTANCE);
    final RelNode input = collect.getInput();
    return new EnumerableCollect(rel.getCluster(), traitSet, convert(input, input.getTraitSet().replace(EnumerableConvention.INSTANCE)), collect.getFieldName());
}
Also used : RelNode(org.apache.calcite.rel.RelNode) Collect(org.apache.calcite.rel.core.Collect) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 2 with Collect

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

the class MutableRels method fromMutable.

public static RelNode fromMutable(MutableRel node, RelBuilder relBuilder) {
    switch(node.type) {
        case TABLE_SCAN:
        case VALUES:
            return ((MutableLeafRel) node).rel;
        case PROJECT:
            final MutableProject project = (MutableProject) node;
            relBuilder.push(fromMutable(project.input, relBuilder));
            relBuilder.project(project.projects, project.rowType.getFieldNames(), true);
            return relBuilder.build();
        case FILTER:
            final MutableFilter filter = (MutableFilter) node;
            relBuilder.push(fromMutable(filter.input, relBuilder));
            relBuilder.filter(filter.condition);
            return relBuilder.build();
        case AGGREGATE:
            final MutableAggregate aggregate = (MutableAggregate) node;
            relBuilder.push(fromMutable(aggregate.input, relBuilder));
            relBuilder.aggregate(relBuilder.groupKey(aggregate.groupSet, aggregate.groupSets), aggregate.aggCalls);
            return relBuilder.build();
        case SORT:
            final MutableSort sort = (MutableSort) node;
            return LogicalSort.create(fromMutable(sort.input, relBuilder), sort.collation, sort.offset, sort.fetch);
        case CALC:
            final MutableCalc calc = (MutableCalc) node;
            return LogicalCalc.create(fromMutable(calc.input, relBuilder), calc.program);
        case EXCHANGE:
            final MutableExchange exchange = (MutableExchange) node;
            return LogicalExchange.create(fromMutable(exchange.getInput(), relBuilder), exchange.distribution);
        case COLLECT:
            {
                final MutableCollect collect = (MutableCollect) node;
                final RelNode child = fromMutable(collect.getInput(), relBuilder);
                return new Collect(collect.cluster, child.getTraitSet(), child, collect.fieldName);
            }
        case UNCOLLECT:
            {
                final MutableUncollect uncollect = (MutableUncollect) node;
                final RelNode child = fromMutable(uncollect.getInput(), relBuilder);
                return Uncollect.create(child.getTraitSet(), child, uncollect.withOrdinality);
            }
        case WINDOW:
            {
                final MutableWindow window = (MutableWindow) node;
                final RelNode child = fromMutable(window.getInput(), relBuilder);
                return LogicalWindow.create(child.getTraitSet(), child, window.constants, window.rowType, window.groups);
            }
        case TABLE_MODIFY:
            final MutableTableModify modify = (MutableTableModify) node;
            return LogicalTableModify.create(modify.table, modify.catalogReader, fromMutable(modify.getInput(), relBuilder), modify.operation, modify.updateColumnList, modify.sourceExpressionList, modify.flattened);
        case SAMPLE:
            final MutableSample sample = (MutableSample) node;
            return new Sample(sample.cluster, fromMutable(sample.getInput(), relBuilder), sample.params);
        case TABLE_FUNCTION_SCAN:
            final MutableTableFunctionScan tableFunctionScan = (MutableTableFunctionScan) node;
            return LogicalTableFunctionScan.create(tableFunctionScan.cluster, fromMutables(tableFunctionScan.getInputs(), relBuilder), tableFunctionScan.rexCall, tableFunctionScan.elementType, tableFunctionScan.rowType, tableFunctionScan.columnMappings);
        case JOIN:
            final MutableJoin join = (MutableJoin) node;
            relBuilder.push(fromMutable(join.getLeft(), relBuilder));
            relBuilder.push(fromMutable(join.getRight(), relBuilder));
            relBuilder.join(join.joinType, join.condition, join.variablesSet);
            return relBuilder.build();
        case SEMIJOIN:
            final MutableSemiJoin semiJoin = (MutableSemiJoin) node;
            relBuilder.push(fromMutable(semiJoin.getLeft(), relBuilder));
            relBuilder.push(fromMutable(semiJoin.getRight(), relBuilder));
            relBuilder.semiJoin(semiJoin.condition);
            return relBuilder.build();
        case CORRELATE:
            final MutableCorrelate correlate = (MutableCorrelate) node;
            return LogicalCorrelate.create(fromMutable(correlate.getLeft(), relBuilder), fromMutable(correlate.getRight(), relBuilder), correlate.correlationId, correlate.requiredColumns, correlate.joinType);
        case UNION:
            final MutableUnion union = (MutableUnion) node;
            relBuilder.pushAll(MutableRels.fromMutables(union.inputs, relBuilder));
            relBuilder.union(union.all, union.inputs.size());
            return relBuilder.build();
        case MINUS:
            final MutableMinus minus = (MutableMinus) node;
            relBuilder.pushAll(MutableRels.fromMutables(minus.inputs, relBuilder));
            relBuilder.minus(minus.all, minus.inputs.size());
            return relBuilder.build();
        case INTERSECT:
            final MutableIntersect intersect = (MutableIntersect) node;
            relBuilder.pushAll(MutableRels.fromMutables(intersect.inputs, relBuilder));
            relBuilder.intersect(intersect.all, intersect.inputs.size());
            return relBuilder.build();
        default:
            throw new AssertionError(node.deep());
    }
}
Also used : Collect(org.apache.calcite.rel.core.Collect) Sample(org.apache.calcite.rel.core.Sample) RelNode(org.apache.calcite.rel.RelNode)

Example 3 with Collect

use of org.apache.calcite.rel.core.Collect 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) {
        return toMutable(Util.first(((RelSubset) rel).getBest(), ((RelSubset) rel).getOriginal()));
    }
    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 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 SemiJoin) {
        final SemiJoin semiJoin = (SemiJoin) rel;
        final MutableRel left = toMutable(semiJoin.getLeft());
        final MutableRel right = toMutable(semiJoin.getRight());
        return MutableSemiJoin.of(semiJoin.getRowType(), left, right, semiJoin.getCondition(), semiJoin.getLeftKeys(), semiJoin.getRightKeys());
    }
    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) 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) SemiJoin(org.apache.calcite.rel.core.SemiJoin) 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) SemiJoin(org.apache.calcite.rel.core.SemiJoin) 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) 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)

Example 4 with Collect

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

the class SqlToRelConverter method convertMultisets.

private RelNode convertMultisets(final List<SqlNode> operands, Blackboard bb) {
    // NOTE: Wael 2/04/05: this implementation is not the most efficient in
    // terms of planning since it generates XOs that can be reduced.
    final List<Object> joinList = new ArrayList<>();
    List<SqlNode> lastList = new ArrayList<>();
    for (int i = 0; i < operands.size(); i++) {
        SqlNode operand = operands.get(i);
        if (!(operand instanceof SqlCall)) {
            lastList.add(operand);
            continue;
        }
        final SqlCall call = (SqlCall) operand;
        final RelNode input;
        switch(call.getKind()) {
            case MULTISET_VALUE_CONSTRUCTOR:
            case ARRAY_VALUE_CONSTRUCTOR:
                final SqlNodeList list = new SqlNodeList(call.getOperandList(), call.getParserPosition());
                CollectNamespace nss = (CollectNamespace) validator.getNamespace(call);
                Blackboard usedBb;
                if (null != nss) {
                    usedBb = createBlackboard(nss.getScope(), null, false);
                } else {
                    usedBb = createBlackboard(new ListScope(bb.scope) {

                        public SqlNode getNode() {
                            return call;
                        }
                    }, null, false);
                }
                RelDataType multisetType = validator.getValidatedNodeType(call);
                ((SqlValidatorImpl) validator).setValidatedNodeType(list, multisetType.getComponentType());
                input = convertQueryOrInList(usedBb, list, null);
                break;
            case MULTISET_QUERY_CONSTRUCTOR:
            case ARRAY_QUERY_CONSTRUCTOR:
                final RelRoot root = convertQuery(call.operand(0), false, true);
                input = root.rel;
                break;
            default:
                lastList.add(operand);
                continue;
        }
        if (lastList.size() > 0) {
            joinList.add(lastList);
        }
        lastList = new ArrayList<>();
        Collect collect = new Collect(cluster, cluster.traitSetOf(Convention.NONE), input, validator.deriveAlias(call, i));
        joinList.add(collect);
    }
    if (joinList.size() == 0) {
        joinList.add(lastList);
    }
    for (int i = 0; i < joinList.size(); i++) {
        Object o = joinList.get(i);
        if (o instanceof List) {
            @SuppressWarnings("unchecked") List<SqlNode> projectList = (List<SqlNode>) o;
            final List<RexNode> selectList = new ArrayList<>();
            final List<String> fieldNameList = new ArrayList<>();
            for (int j = 0; j < projectList.size(); j++) {
                SqlNode operand = projectList.get(j);
                selectList.add(bb.convertExpression(operand));
                // REVIEW angel 5-June-2005: Use deriveAliasFromOrdinal
                // instead of deriveAlias to match field names from
                // SqlRowOperator. Otherwise, get error   Type
                // 'RecordType(INTEGER EMPNO)' has no field 'EXPR$0' when
                // doing   select * from unnest(     select multiset[empno]
                // from sales.emps);
                fieldNameList.add(SqlUtil.deriveAliasFromOrdinal(j));
            }
            relBuilder.push(LogicalValues.createOneRow(cluster)).projectNamed(selectList, fieldNameList, true);
            joinList.set(i, relBuilder.build());
        }
    }
    RelNode ret = (RelNode) joinList.get(0);
    for (int i = 1; i < joinList.size(); i++) {
        RelNode relNode = (RelNode) joinList.get(i);
        ret = RelFactories.DEFAULT_JOIN_FACTORY.createJoin(ret, relNode, rexBuilder.makeLiteral(true), ImmutableSet.<CorrelationId>of(), JoinRelType.INNER, false);
    }
    return ret;
}
Also used : Collect(org.apache.calcite.rel.core.Collect) SqlCall(org.apache.calcite.sql.SqlCall) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelRoot(org.apache.calcite.rel.RelRoot) NlsString(org.apache.calcite.util.NlsString) SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) RelNode(org.apache.calcite.rel.RelNode) SqlNodeList(org.apache.calcite.sql.SqlNodeList) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) CorrelationId(org.apache.calcite.rel.core.CorrelationId) CollectNamespace(org.apache.calcite.sql.validate.CollectNamespace) ListScope(org.apache.calcite.sql.validate.ListScope) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

Collect (org.apache.calcite.rel.core.Collect)4 RelNode (org.apache.calcite.rel.RelNode)3 Sample (org.apache.calcite.rel.core.Sample)2 ImmutableList (com.google.common.collect.ImmutableList)1 AbstractList (java.util.AbstractList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 RelTraitSet (org.apache.calcite.plan.RelTraitSet)1 HepRelVertex (org.apache.calcite.plan.hep.HepRelVertex)1 RelSubset (org.apache.calcite.plan.volcano.RelSubset)1 RelRoot (org.apache.calcite.rel.RelRoot)1 Aggregate (org.apache.calcite.rel.core.Aggregate)1 Calc (org.apache.calcite.rel.core.Calc)1 Correlate (org.apache.calcite.rel.core.Correlate)1 CorrelationId (org.apache.calcite.rel.core.CorrelationId)1 Exchange (org.apache.calcite.rel.core.Exchange)1 Filter (org.apache.calcite.rel.core.Filter)1 Intersect (org.apache.calcite.rel.core.Intersect)1 Join (org.apache.calcite.rel.core.Join)1 Minus (org.apache.calcite.rel.core.Minus)1