Search in sources :

Example 1 with EnumerableInterpreter

use of org.apache.calcite.adapter.enumerable.EnumerableInterpreter in project calcite by apache.

the class ToLogicalConverter method visit.

@Override
public RelNode visit(RelNode relNode) {
    if (relNode instanceof Aggregate) {
        final Aggregate agg = (Aggregate) relNode;
        return relBuilder.push(visit(agg.getInput())).aggregate(relBuilder.groupKey(agg.getGroupSet(), agg.groupSets), agg.getAggCallList()).build();
    }
    if (relNode instanceof TableScan) {
        return visit((TableScan) relNode);
    }
    if (relNode instanceof Filter) {
        final Filter filter = (Filter) relNode;
        return relBuilder.push(visit(filter.getInput())).filter(filter.getCondition()).build();
    }
    if (relNode instanceof Project) {
        final Project project = (Project) relNode;
        return relBuilder.push(visit(project.getInput())).project(project.getProjects(), project.getRowType().getFieldNames()).build();
    }
    if (relNode instanceof Union) {
        final Union union = (Union) relNode;
        for (RelNode rel : union.getInputs()) {
            relBuilder.push(visit(rel));
        }
        return relBuilder.union(union.all, union.getInputs().size()).build();
    }
    if (relNode instanceof Intersect) {
        final Intersect intersect = (Intersect) relNode;
        for (RelNode rel : intersect.getInputs()) {
            relBuilder.push(visit(rel));
        }
        return relBuilder.intersect(intersect.all, intersect.getInputs().size()).build();
    }
    if (relNode instanceof Minus) {
        final Minus minus = (Minus) relNode;
        for (RelNode rel : minus.getInputs()) {
            relBuilder.push(visit(rel));
        }
        return relBuilder.minus(minus.all, minus.getInputs().size()).build();
    }
    if (relNode instanceof Join) {
        final Join join = (Join) relNode;
        return relBuilder.push(visit(join.getLeft())).push(visit(join.getRight())).join(join.getJoinType(), join.getCondition()).build();
    }
    if (relNode instanceof Correlate) {
        final Correlate corr = (Correlate) relNode;
        return relBuilder.push(visit(corr.getLeft())).push(visit(corr.getRight())).join(corr.getJoinType(), relBuilder.literal(true), corr.getVariablesSet()).build();
    }
    if (relNode instanceof Values) {
        final Values values = (Values) relNode;
        return relBuilder.values(values.tuples, values.getRowType()).build();
    }
    if (relNode instanceof Sort) {
        final Sort sort = (Sort) relNode;
        return LogicalSort.create(visit(sort.getInput()), sort.getCollation(), sort.offset, sort.fetch);
    }
    if (relNode instanceof Window) {
        final Window window = (Window) relNode;
        final RelNode input = visit(window.getInput());
        return LogicalWindow.create(input.getTraitSet(), input, window.constants, window.getRowType(), window.groups);
    }
    if (relNode instanceof Calc) {
        final Calc calc = (Calc) relNode;
        return LogicalCalc.create(visit(calc.getInput()), calc.getProgram());
    }
    if (relNode instanceof TableModify) {
        final TableModify tableModify = (TableModify) relNode;
        final RelNode input = visit(tableModify.getInput());
        return LogicalTableModify.create(tableModify.getTable(), tableModify.getCatalogReader(), input, tableModify.getOperation(), tableModify.getUpdateColumnList(), tableModify.getSourceExpressionList(), tableModify.isFlattened());
    }
    if (relNode instanceof EnumerableInterpreter || relNode instanceof JdbcToEnumerableConverter) {
        return visit(((SingleRel) relNode).getInput());
    }
    if (relNode instanceof EnumerableLimit) {
        final EnumerableLimit limit = (EnumerableLimit) relNode;
        RelNode logicalInput = visit(limit.getInput());
        RelCollation collation = RelCollations.of();
        if (logicalInput instanceof Sort) {
            collation = ((Sort) logicalInput).collation;
            logicalInput = ((Sort) logicalInput).getInput();
        }
        return LogicalSort.create(logicalInput, collation, limit.offset, limit.fetch);
    }
    if (relNode instanceof Uncollect) {
        final Uncollect uncollect = (Uncollect) relNode;
        final RelNode input = visit(uncollect.getInput());
        return Uncollect.create(input.getTraitSet(), input, uncollect.withOrdinality, Collections.emptyList());
    }
    throw new AssertionError("Need to implement logical converter for " + relNode.getClass().getName());
}
Also used : Window(org.apache.calcite.rel.core.Window) Uncollect(org.apache.calcite.rel.core.Uncollect) TableScan(org.apache.calcite.rel.core.TableScan) Correlate(org.apache.calcite.rel.core.Correlate) JdbcToEnumerableConverter(org.apache.calcite.adapter.jdbc.JdbcToEnumerableConverter) Values(org.apache.calcite.rel.core.Values) EnumerableInterpreter(org.apache.calcite.adapter.enumerable.EnumerableInterpreter) Join(org.apache.calcite.rel.core.Join) Calc(org.apache.calcite.rel.core.Calc) Union(org.apache.calcite.rel.core.Union) Project(org.apache.calcite.rel.core.Project) Intersect(org.apache.calcite.rel.core.Intersect) RelCollation(org.apache.calcite.rel.RelCollation) EnumerableLimit(org.apache.calcite.adapter.enumerable.EnumerableLimit) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) Sort(org.apache.calcite.rel.core.Sort) Aggregate(org.apache.calcite.rel.core.Aggregate) TableModify(org.apache.calcite.rel.core.TableModify) Minus(org.apache.calcite.rel.core.Minus)

Example 2 with EnumerableInterpreter

use of org.apache.calcite.adapter.enumerable.EnumerableInterpreter in project Mycat2 by MyCATApache.

the class ToLogicalConverter method visit.

@Override
public RelNode visit(RelNode relNode) {
    if (relNode instanceof Aggregate) {
        final Aggregate agg = (Aggregate) relNode;
        return relBuilder.push(visit(agg.getInput())).aggregate(relBuilder.groupKey(agg.getGroupSet(), (Iterable<ImmutableBitSet>) agg.groupSets), agg.getAggCallList()).build();
    }
    if (relNode instanceof TableScan) {
        return visit((TableScan) relNode);
    }
    if (relNode instanceof Filter) {
        final Filter filter = (Filter) relNode;
        return relBuilder.push(visit(filter.getInput())).filter(filter.getCondition()).build();
    }
    if (relNode instanceof Project) {
        final Project project = (Project) relNode;
        return relBuilder.push(visit(project.getInput())).project(project.getProjects(), project.getRowType().getFieldNames()).build();
    }
    if (relNode instanceof Union) {
        final Union union = (Union) relNode;
        for (RelNode rel : union.getInputs()) {
            relBuilder.push(visit(rel));
        }
        return relBuilder.union(union.all, union.getInputs().size()).build();
    }
    if (relNode instanceof Intersect) {
        final Intersect intersect = (Intersect) relNode;
        for (RelNode rel : intersect.getInputs()) {
            relBuilder.push(visit(rel));
        }
        return relBuilder.intersect(intersect.all, intersect.getInputs().size()).build();
    }
    if (relNode instanceof Minus) {
        final Minus minus = (Minus) relNode;
        for (RelNode rel : minus.getInputs()) {
            relBuilder.push(visit(rel));
        }
        return relBuilder.minus(minus.all, minus.getInputs().size()).build();
    }
    if (relNode instanceof Join) {
        final Join join = (Join) relNode;
        return relBuilder.push(visit(join.getLeft())).push(visit(join.getRight())).join(join.getJoinType(), join.getCondition()).build();
    }
    if (relNode instanceof Correlate) {
        final Correlate corr = (Correlate) relNode;
        return relBuilder.push(visit(corr.getLeft())).push(visit(corr.getRight())).join(corr.getJoinType(), relBuilder.literal(true), corr.getVariablesSet()).build();
    }
    if (relNode instanceof Values) {
        final Values values = (Values) relNode;
        return relBuilder.values(values.tuples, values.getRowType()).build();
    }
    if (relNode instanceof Sort) {
        final Sort sort = (Sort) relNode;
        return LogicalSort.create(visit(sort.getInput()), sort.getCollation(), sort.offset, sort.fetch);
    }
    if (relNode instanceof Window) {
        final Window window = (Window) relNode;
        final RelNode input = visit(window.getInput());
        return LogicalWindow.create(input.getTraitSet(), input, window.constants, window.getRowType(), window.groups);
    }
    if (relNode instanceof Calc) {
        final Calc calc = (Calc) relNode;
        return LogicalCalc.create(visit(calc.getInput()), calc.getProgram());
    }
    if (relNode instanceof TableModify) {
        final TableModify tableModify = (TableModify) relNode;
        final RelNode input = visit(tableModify.getInput());
        return LogicalTableModify.create(tableModify.getTable(), tableModify.getCatalogReader(), input, tableModify.getOperation(), tableModify.getUpdateColumnList(), tableModify.getSourceExpressionList(), tableModify.isFlattened());
    }
    // }
    if (relNode instanceof EnumerableInterpreter) {
        return visit(((SingleRel) relNode).getInput());
    }
    if (relNode instanceof EnumerableLimit) {
        final EnumerableLimit limit = (EnumerableLimit) relNode;
        RelNode logicalInput = visit(limit.getInput());
        RelCollation collation = RelCollations.of();
        if (logicalInput instanceof Sort) {
            collation = ((Sort) logicalInput).collation;
            logicalInput = ((Sort) logicalInput).getInput();
        }
        return LogicalSort.create(logicalInput, collation, limit.offset, limit.fetch);
    }
    if (relNode instanceof Uncollect) {
        final Uncollect uncollect = (Uncollect) relNode;
        final RelNode input = visit(uncollect.getInput());
        return Uncollect.create(input.getTraitSet(), input, uncollect.withOrdinality, Collections.emptyList());
    }
    throw new AssertionError("Need to implement logical converter for " + relNode.getClass().getName());
}
Also used : Window(org.apache.calcite.rel.core.Window) Uncollect(org.apache.calcite.rel.core.Uncollect) TableScan(org.apache.calcite.rel.core.TableScan) Correlate(org.apache.calcite.rel.core.Correlate) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) Values(org.apache.calcite.rel.core.Values) EnumerableInterpreter(org.apache.calcite.adapter.enumerable.EnumerableInterpreter) Join(org.apache.calcite.rel.core.Join) Calc(org.apache.calcite.rel.core.Calc) Union(org.apache.calcite.rel.core.Union) Project(org.apache.calcite.rel.core.Project) Intersect(org.apache.calcite.rel.core.Intersect) RelCollation(org.apache.calcite.rel.RelCollation) EnumerableLimit(org.apache.calcite.adapter.enumerable.EnumerableLimit) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) Sort(org.apache.calcite.rel.core.Sort) Aggregate(org.apache.calcite.rel.core.Aggregate) TableModify(org.apache.calcite.rel.core.TableModify) Minus(org.apache.calcite.rel.core.Minus)

Example 3 with EnumerableInterpreter

use of org.apache.calcite.adapter.enumerable.EnumerableInterpreter in project calcite by apache.

the class PigRelToSqlConverter method visit.

@Override
public Result visit(Aggregate e) {
    final boolean isProjectOutput = e.getInput() instanceof Project || (e.getInput() instanceof EnumerableInterpreter && ((EnumerableInterpreter) e.getInput()).getInput() instanceof Project);
    final Result x = visitInput(e, 0, isAnon(), isProjectOutput, ImmutableSet.of(Clause.GROUP_BY));
    final Builder builder = x.builder(e);
    final List<SqlNode> groupByList = Expressions.list();
    final List<SqlNode> selectList = new ArrayList<>();
    buildAggGroupList(e, builder, groupByList, selectList);
    final int groupSetSize = e.getGroupSets().size();
    SqlNodeList groupBy = new SqlNodeList(groupByList, POS);
    if (groupSetSize > 1) {
        // If there are multiple group sets, this should be a result of converting a
        // Pig CUBE/cube or Pig CUBE/rollup
        final List<SqlNode> cubeRollupList = Expressions.list();
        if (groupSetSize == groupByList.size() + 1) {
            cubeRollupList.add(SqlStdOperatorTable.ROLLUP.createCall(groupBy));
        } else {
            assert groupSetSize == Math.round(Math.pow(2, groupByList.size()));
            cubeRollupList.add(SqlStdOperatorTable.CUBE.createCall(groupBy));
        }
        groupBy = new SqlNodeList(cubeRollupList, POS);
    }
    return buildAggregate(e, builder, selectList, groupBy).result();
}
Also used : Project(org.apache.calcite.rel.core.Project) EnumerableInterpreter(org.apache.calcite.adapter.enumerable.EnumerableInterpreter) ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

EnumerableInterpreter (org.apache.calcite.adapter.enumerable.EnumerableInterpreter)3 Project (org.apache.calcite.rel.core.Project)3 EnumerableLimit (org.apache.calcite.adapter.enumerable.EnumerableLimit)2 RelCollation (org.apache.calcite.rel.RelCollation)2 RelNode (org.apache.calcite.rel.RelNode)2 Aggregate (org.apache.calcite.rel.core.Aggregate)2 Calc (org.apache.calcite.rel.core.Calc)2 Correlate (org.apache.calcite.rel.core.Correlate)2 Filter (org.apache.calcite.rel.core.Filter)2 Intersect (org.apache.calcite.rel.core.Intersect)2 Join (org.apache.calcite.rel.core.Join)2 Minus (org.apache.calcite.rel.core.Minus)2 Sort (org.apache.calcite.rel.core.Sort)2 TableModify (org.apache.calcite.rel.core.TableModify)2 TableScan (org.apache.calcite.rel.core.TableScan)2 Uncollect (org.apache.calcite.rel.core.Uncollect)2 Union (org.apache.calcite.rel.core.Union)2 Values (org.apache.calcite.rel.core.Values)2 Window (org.apache.calcite.rel.core.Window)2 ArrayList (java.util.ArrayList)1