Search in sources :

Example 1 with Sample

use of org.apache.calcite.rel.core.Sample 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 2 with Sample

use of org.apache.calcite.rel.core.Sample 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 3 with Sample

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

the class SqlToRelConverter method convertFrom.

/**
 * Converts a FROM clause into a relational expression.
 *
 * @param bb   Scope within which to resolve identifiers
 * @param from FROM clause of a query. Examples include:
 *
 *             <ul>
 *             <li>a single table ("SALES.EMP"),
 *             <li>an aliased table ("EMP AS E"),
 *             <li>a list of tables ("EMP, DEPT"),
 *             <li>an ANSI Join expression ("EMP JOIN DEPT ON EMP.DEPTNO =
 *             DEPT.DEPTNO"),
 *             <li>a VALUES clause ("VALUES ('Fred', 20)"),
 *             <li>a query ("(SELECT * FROM EMP WHERE GENDER = 'F')"),
 *             <li>or any combination of the above.
 *             </ul>
 */
protected void convertFrom(Blackboard bb, SqlNode from) {
    if (from == null) {
        bb.setRoot(LogicalValues.createOneRow(cluster), false);
        return;
    }
    final SqlCall call;
    final SqlNode[] operands;
    switch(from.getKind()) {
        case MATCH_RECOGNIZE:
            convertMatchRecognize(bb, (SqlCall) from);
            return;
        case AS:
            call = (SqlCall) from;
            convertFrom(bb, call.operand(0));
            if (call.operandCount() > 2 && bb.root instanceof Values) {
                final List<String> fieldNames = new ArrayList<>();
                for (SqlNode node : Util.skip(call.getOperandList(), 2)) {
                    fieldNames.add(((SqlIdentifier) node).getSimple());
                }
                bb.setRoot(relBuilder.push(bb.root).rename(fieldNames).build(), true);
            }
            return;
        case WITH_ITEM:
            convertFrom(bb, ((SqlWithItem) from).query);
            return;
        case WITH:
            convertFrom(bb, ((SqlWith) from).body);
            return;
        case TABLESAMPLE:
            operands = ((SqlBasicCall) from).getOperands();
            SqlSampleSpec sampleSpec = SqlLiteral.sampleValue(operands[1]);
            if (sampleSpec instanceof SqlSampleSpec.SqlSubstitutionSampleSpec) {
                String sampleName = ((SqlSampleSpec.SqlSubstitutionSampleSpec) sampleSpec).getName();
                datasetStack.push(sampleName);
                convertFrom(bb, operands[0]);
                datasetStack.pop();
            } else if (sampleSpec instanceof SqlSampleSpec.SqlTableSampleSpec) {
                SqlSampleSpec.SqlTableSampleSpec tableSampleSpec = (SqlSampleSpec.SqlTableSampleSpec) sampleSpec;
                convertFrom(bb, operands[0]);
                RelOptSamplingParameters params = new RelOptSamplingParameters(tableSampleSpec.isBernoulli(), tableSampleSpec.getSamplePercentage(), tableSampleSpec.isRepeatable(), tableSampleSpec.getRepeatableSeed());
                bb.setRoot(new Sample(cluster, bb.root, params), false);
            } else {
                throw new AssertionError("unknown TABLESAMPLE type: " + sampleSpec);
            }
            return;
        case IDENTIFIER:
            convertIdentifier(bb, (SqlIdentifier) from, null);
            return;
        case EXTEND:
            call = (SqlCall) from;
            SqlIdentifier id = (SqlIdentifier) call.getOperandList().get(0);
            SqlNodeList extendedColumns = (SqlNodeList) call.getOperandList().get(1);
            convertIdentifier(bb, id, extendedColumns);
            return;
        case JOIN:
            final SqlJoin join = (SqlJoin) from;
            final SqlValidatorScope scope = validator.getJoinScope(from);
            final Blackboard fromBlackboard = createBlackboard(scope, null, false);
            SqlNode left = join.getLeft();
            SqlNode right = join.getRight();
            final boolean isNatural = join.isNatural();
            final JoinType joinType = join.getJoinType();
            final SqlValidatorScope leftScope = Util.first(validator.getJoinScope(left), ((DelegatingScope) bb.scope).getParent());
            final Blackboard leftBlackboard = createBlackboard(leftScope, null, false);
            final SqlValidatorScope rightScope = Util.first(validator.getJoinScope(right), ((DelegatingScope) bb.scope).getParent());
            final Blackboard rightBlackboard = createBlackboard(rightScope, null, false);
            convertFrom(leftBlackboard, left);
            RelNode leftRel = leftBlackboard.root;
            convertFrom(rightBlackboard, right);
            RelNode rightRel = rightBlackboard.root;
            JoinRelType convertedJoinType = convertJoinType(joinType);
            RexNode conditionExp;
            final SqlValidatorNamespace leftNamespace = validator.getNamespace(left);
            final SqlValidatorNamespace rightNamespace = validator.getNamespace(right);
            if (isNatural) {
                final RelDataType leftRowType = leftNamespace.getRowType();
                final RelDataType rightRowType = rightNamespace.getRowType();
                final List<String> columnList = SqlValidatorUtil.deriveNaturalJoinColumnList(leftRowType, rightRowType);
                conditionExp = convertUsing(leftNamespace, rightNamespace, columnList);
            } else {
                conditionExp = convertJoinCondition(fromBlackboard, leftNamespace, rightNamespace, join.getCondition(), join.getConditionType(), leftRel, rightRel);
            }
            final RelNode joinRel = createJoin(fromBlackboard, leftRel, rightRel, conditionExp, convertedJoinType);
            bb.setRoot(joinRel, false);
            return;
        case SELECT:
        case INTERSECT:
        case EXCEPT:
        case UNION:
            final RelNode rel = convertQueryRecursive(from, false, null).project();
            bb.setRoot(rel, true);
            return;
        case VALUES:
            convertValuesImpl(bb, (SqlCall) from, null);
            return;
        case UNNEST:
            call = (SqlCall) from;
            final List<SqlNode> nodes = call.getOperandList();
            final SqlUnnestOperator operator = (SqlUnnestOperator) call.getOperator();
            for (SqlNode node : nodes) {
                replaceSubQueries(bb, node, RelOptUtil.Logic.TRUE_FALSE_UNKNOWN);
            }
            final List<RexNode> exprs = new ArrayList<>();
            final List<String> fieldNames = new ArrayList<>();
            for (Ord<SqlNode> node : Ord.zip(nodes)) {
                exprs.add(bb.convertExpression(node.e));
                fieldNames.add(validator.deriveAlias(node.e, node.i));
            }
            RelNode child = (null != bb.root) ? bb.root : LogicalValues.createOneRow(cluster);
            relBuilder.push(child).projectNamed(exprs, fieldNames, false);
            Uncollect uncollect = new Uncollect(cluster, cluster.traitSetOf(Convention.NONE), relBuilder.build(), operator.withOrdinality);
            bb.setRoot(uncollect, true);
            return;
        case COLLECTION_TABLE:
            call = (SqlCall) from;
            // Dig out real call; TABLE() wrapper is just syntactic.
            assert call.getOperandList().size() == 1;
            final SqlCall call2 = call.operand(0);
            convertCollectionTable(bb, call2);
            return;
        default:
            throw new AssertionError("not a join operator " + from);
    }
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) Uncollect(org.apache.calcite.rel.core.Uncollect) Values(org.apache.calcite.rel.core.Values) LogicalValues(org.apache.calcite.rel.logical.LogicalValues) ArrayList(java.util.ArrayList) SqlUnnestOperator(org.apache.calcite.sql.SqlUnnestOperator) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) RelOptSamplingParameters(org.apache.calcite.plan.RelOptSamplingParameters) SqlNode(org.apache.calcite.sql.SqlNode) SqlCall(org.apache.calcite.sql.SqlCall) Sample(org.apache.calcite.rel.core.Sample) SqlSampleSpec(org.apache.calcite.sql.SqlSampleSpec) JoinType(org.apache.calcite.sql.JoinType) SemiJoinType(org.apache.calcite.sql.SemiJoinType) JoinRelType(org.apache.calcite.rel.core.JoinRelType) RelNode(org.apache.calcite.rel.RelNode) SqlJoin(org.apache.calcite.sql.SqlJoin) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlValidatorNamespace(org.apache.calcite.sql.validate.SqlValidatorNamespace) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

Sample (org.apache.calcite.rel.core.Sample)3 RelNode (org.apache.calcite.rel.RelNode)2 Collect (org.apache.calcite.rel.core.Collect)2 Uncollect (org.apache.calcite.rel.core.Uncollect)2 Values (org.apache.calcite.rel.core.Values)2 ArrayList (java.util.ArrayList)1 RelOptSamplingParameters (org.apache.calcite.plan.RelOptSamplingParameters)1 HepRelVertex (org.apache.calcite.plan.hep.HepRelVertex)1 RelSubset (org.apache.calcite.plan.volcano.RelSubset)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 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 JoinRelType (org.apache.calcite.rel.core.JoinRelType)1 Minus (org.apache.calcite.rel.core.Minus)1 Project (org.apache.calcite.rel.core.Project)1 SemiJoin (org.apache.calcite.rel.core.SemiJoin)1