Search in sources :

Example 1 with DataContext

use of org.apache.calcite.DataContext in project storm by apache.

the class QueryPlanner method compile.

public AbstractTridentProcessor compile(Map<String, ISqlTridentDataSource> sources, String query) throws Exception {
    TridentRel relNode = getPlan(query);
    TridentPlanCreator tridentPlanCreator = new TridentPlanCreator(sources, new RexBuilder(typeFactory));
    relNode.tridentPlan(tridentPlanCreator);
    final TridentTopology topology = tridentPlanCreator.getTopology();
    final IAggregatableStream lastStream = tridentPlanCreator.pop();
    final DataContext dc = tridentPlanCreator.getDataContext();
    final List<CompilingClassLoader> cls = tridentPlanCreator.getClassLoaders();
    return new AbstractTridentProcessor() {

        @Override
        public TridentTopology build() {
            return topology;
        }

        @Override
        public Stream outputStream() {
            return lastStream.toStream();
        }

        @Override
        public DataContext getDataContext() {
            return dc;
        }

        @Override
        public List<CompilingClassLoader> getClassLoaders() {
            return cls;
        }
    };
}
Also used : AbstractTridentProcessor(org.apache.storm.sql.AbstractTridentProcessor) IAggregatableStream(org.apache.storm.trident.fluent.IAggregatableStream) DataContext(org.apache.calcite.DataContext) CompilingClassLoader(org.apache.storm.sql.javac.CompilingClassLoader) TridentTopology(org.apache.storm.trident.TridentTopology) RexBuilder(org.apache.calcite.rex.RexBuilder) TridentRel(org.apache.storm.sql.planner.trident.rel.TridentRel)

Example 2 with DataContext

use of org.apache.calcite.DataContext in project storm by apache.

the class EvaluationFilter method isKeep.

@Override
public boolean isKeep(TridentTuple tuple) {
    Context calciteContext = new StormContext(dataContext);
    calciteContext.values = tuple.getValues().toArray();
    filterInstance.execute(calciteContext, outputValues);
    return (outputValues[0] != null && (boolean) outputValues[0]);
}
Also used : Context(org.apache.calcite.interpreter.Context) DataContext(org.apache.calcite.DataContext) TridentOperationContext(org.apache.storm.trident.operation.TridentOperationContext) StormContext(org.apache.calcite.interpreter.StormContext) StormContext(org.apache.calcite.interpreter.StormContext)

Example 3 with DataContext

use of org.apache.calcite.DataContext in project storm by apache.

the class EvaluationFunction method execute.

@Override
public Values execute(TridentTuple input) {
    Context calciteContext = new StormContext(dataContext);
    calciteContext.values = input.getValues().toArray();
    projectionInstance.execute(calciteContext, outputValues);
    return new Values(outputValues);
}
Also used : Context(org.apache.calcite.interpreter.Context) DataContext(org.apache.calcite.DataContext) TridentOperationContext(org.apache.storm.trident.operation.TridentOperationContext) StormContext(org.apache.calcite.interpreter.StormContext) Values(org.apache.storm.tuple.Values) StormContext(org.apache.calcite.interpreter.StormContext)

Example 4 with DataContext

use of org.apache.calcite.DataContext in project druid by druid-io.

the class DruidPlanner method planWithBindableConvention.

private PlannerResult planWithBindableConvention(final SqlExplain explain, final RelRoot root) throws RelConversionException {
    BindableRel bindableRel = (BindableRel) planner.transform(Rules.BINDABLE_CONVENTION_RULES, planner.getEmptyTraitSet().replace(BindableConvention.INSTANCE).plus(root.collation), root.rel);
    if (!root.isRefTrivial()) {
        // Add a projection on top to accommodate root.fields.
        final List<RexNode> projects = new ArrayList<>();
        final RexBuilder rexBuilder = bindableRel.getCluster().getRexBuilder();
        for (int field : Pair.left(root.fields)) {
            projects.add(rexBuilder.makeInputRef(bindableRel, field));
        }
        bindableRel = new Bindables.BindableProject(bindableRel.getCluster(), bindableRel.getTraitSet(), bindableRel, projects, root.validatedRowType);
    }
    if (explain != null) {
        return planExplanation(bindableRel, explain);
    } else {
        final BindableRel theRel = bindableRel;
        final DataContext dataContext = plannerContext.createDataContext((JavaTypeFactory) planner.getTypeFactory());
        final Supplier<Sequence<Object[]>> resultsSupplier = new Supplier<Sequence<Object[]>>() {

            @Override
            public Sequence<Object[]> get() {
                final Enumerable enumerable = theRel.bind(dataContext);
                return Sequences.simple(enumerable);
            }
        };
        return new PlannerResult(resultsSupplier, root.validatedRowType);
    }
}
Also used : ArrayList(java.util.ArrayList) BindableRel(org.apache.calcite.interpreter.BindableRel) Bindables(org.apache.calcite.interpreter.Bindables) Sequence(io.druid.java.util.common.guava.Sequence) DataContext(org.apache.calcite.DataContext) RexBuilder(org.apache.calcite.rex.RexBuilder) Enumerable(org.apache.calcite.linq4j.Enumerable) Supplier(com.google.common.base.Supplier) RexNode(org.apache.calcite.rex.RexNode)

Example 5 with DataContext

use of org.apache.calcite.DataContext in project calcite by apache.

the class TableScanNode method createProjectableFilterable.

private static TableScanNode createProjectableFilterable(Compiler compiler, TableScan rel, ImmutableList<RexNode> filters, ImmutableIntList projects, ProjectableFilterableTable pfTable) {
    final DataContext root = compiler.getDataContext();
    final ImmutableIntList originalProjects = projects;
    for (; ; ) {
        final List<RexNode> mutableFilters = Lists.newArrayList(filters);
        final int[] projectInts;
        if (projects == null || projects.equals(TableScan.identity(rel.getTable()))) {
            projectInts = null;
        } else {
            projectInts = projects.toIntArray();
        }
        final Enumerable<Object[]> enumerable1 = pfTable.scan(root, mutableFilters, projectInts);
        for (RexNode filter : mutableFilters) {
            if (!filters.contains(filter)) {
                throw RESOURCE.filterableTableInventedFilter(filter.toString()).ex();
            }
        }
        final ImmutableBitSet usedFields = RelOptUtil.InputFinder.bits(mutableFilters, null);
        if (projects != null) {
            int changeCount = 0;
            for (int usedField : usedFields) {
                if (!projects.contains(usedField)) {
                    // A field that is not projected is used in a filter that the
                    // table rejected. We won't be able to apply the filter later.
                    // Try again without any projects.
                    projects = ImmutableIntList.copyOf(Iterables.concat(projects, ImmutableList.of(usedField)));
                    ++changeCount;
                }
            }
            if (changeCount > 0) {
                continue;
            }
        }
        final Enumerable<Row> rowEnumerable = Enumerables.toRow(enumerable1);
        final ImmutableIntList rejectedProjects;
        if (Objects.equals(projects, originalProjects)) {
            rejectedProjects = null;
        } else {
            // We projected extra columns because they were needed in filters. Now
            // project the leading columns.
            rejectedProjects = ImmutableIntList.identity(originalProjects.size());
        }
        return createEnumerable(compiler, rel, rowEnumerable, projects, mutableFilters, rejectedProjects);
    }
}
Also used : DataContext(org.apache.calcite.DataContext) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

DataContext (org.apache.calcite.DataContext)34 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)7 Values (org.apache.storm.tuple.Values)7 HashMap (java.util.HashMap)6 Context (org.apache.calcite.interpreter.Context)6 StormContext (org.apache.calcite.interpreter.StormContext)6 RelDataType (org.apache.calcite.rel.type.RelDataType)6 RexNode (org.apache.calcite.rex.RexNode)6 ScannableTable (org.apache.calcite.schema.ScannableTable)5 Schema (org.apache.calcite.schema.Schema)5 CalciteConnectionConfig (org.apache.calcite.config.CalciteConnectionConfig)4 Enumerable (org.apache.calcite.linq4j.Enumerable)4 Enumerator (org.apache.calcite.linq4j.Enumerator)4 RexBuilder (org.apache.calcite.rex.RexBuilder)4 ImmutableList (com.google.common.collect.ImmutableList)3 AbstractEnumerable (org.apache.calcite.linq4j.AbstractEnumerable)3 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 SqlCall (org.apache.calcite.sql.SqlCall)3 SqlNode (org.apache.calcite.sql.SqlNode)3