Search in sources :

Example 26 with DataContext

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.DataContext in project calcite by apache.

the class TableScanNode method createQueryable.

private static TableScanNode createQueryable(Compiler compiler, TableScan rel, ImmutableList<RexNode> filters, ImmutableIntList projects, QueryableTable queryableTable) {
    final DataContext root = compiler.getDataContext();
    final RelOptTable relOptTable = rel.getTable();
    final Type elementType = queryableTable.getElementType();
    SchemaPlus schema = root.getRootSchema();
    for (String name : Util.skipLast(relOptTable.getQualifiedName())) {
        schema = schema.getSubSchema(name);
    }
    final Enumerable<Row> rowEnumerable;
    if (elementType instanceof Class) {
        // noinspection unchecked
        final Queryable<Object> queryable = Schemas.queryable(root, (Class) elementType, relOptTable.getQualifiedName());
        ImmutableList.Builder<Field> fieldBuilder = ImmutableList.builder();
        Class type = (Class) elementType;
        for (Field field : type.getFields()) {
            if (Modifier.isPublic(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) {
                fieldBuilder.add(field);
            }
        }
        final List<Field> fields = fieldBuilder.build();
        rowEnumerable = queryable.select(new Function1<Object, Row>() {

            public Row apply(Object o) {
                final Object[] values = new Object[fields.size()];
                for (int i = 0; i < fields.size(); i++) {
                    Field field = fields.get(i);
                    try {
                        values[i] = field.get(o);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                }
                return new Row(values);
            }
        });
    } else {
        rowEnumerable = Schemas.queryable(root, Row.class, relOptTable.getQualifiedName());
    }
    return createEnumerable(compiler, rel, rowEnumerable, null, filters, projects);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Function1(org.apache.calcite.linq4j.function.Function1) Field(java.lang.reflect.Field) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) DataContext(org.apache.calcite.DataContext) RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) RelOptTable(org.apache.calcite.plan.RelOptTable)

Example 27 with DataContext

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.DataContext in project calcite by apache.

the class TableScanNode method createFilterable.

private static TableScanNode createFilterable(Compiler compiler, TableScan rel, ImmutableList<RexNode> filters, ImmutableIntList projects, FilterableTable filterableTable) {
    final DataContext root = compiler.getDataContext();
    final List<RexNode> mutableFilters = Lists.newArrayList(filters);
    final Enumerable<Object[]> enumerable = filterableTable.scan(root, mutableFilters);
    for (RexNode filter : mutableFilters) {
        if (!filters.contains(filter)) {
            throw RESOURCE.filterableTableInventedFilter(filter.toString()).ex();
        }
    }
    final Enumerable<Row> rowEnumerable = Enumerables.toRow(enumerable);
    return createEnumerable(compiler, rel, rowEnumerable, null, mutableFilters, projects);
}
Also used : DataContext(org.apache.calcite.DataContext) RexNode(org.apache.calcite.rex.RexNode)

Example 28 with DataContext

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.DataContext in project calcite by apache.

the class RexImplicationChecker method implies2.

/**
 * Returns whether the predicate {@code first} (not a conjunction)
 * implies {@code second}.
 */
private boolean implies2(RexNode first, RexNode second) {
    if (second.isAlwaysFalse()) {
        // f cannot imply s
        return false;
    }
    // E.g. "x is null" implies "x is null".
    if (RexUtil.eq(first, second)) {
        return true;
    }
    // Several things imply "IS NOT NULL"
    switch(second.getKind()) {
        case IS_NOT_NULL:
            // Suppose we know that first is strong in second; that is,
            // the if second is null, then first will be null.
            // Then, first being not null implies that second is not null.
            // 
            // For example, first is "x > y", second is "x".
            // If we know that "x > y" is not null, we know that "x" is not null.
            final RexNode operand = ((RexCall) second).getOperands().get(0);
            final Strong strong = new Strong() {

                @Override
                public boolean isNull(RexNode node) {
                    return RexUtil.eq(node, operand) || super.isNull(node);
                }
            };
            if (strong.isNull(first)) {
                return true;
            }
    }
    final InputUsageFinder firstUsageFinder = new InputUsageFinder();
    final InputUsageFinder secondUsageFinder = new InputUsageFinder();
    RexUtil.apply(firstUsageFinder, ImmutableList.<RexNode>of(), first);
    RexUtil.apply(secondUsageFinder, ImmutableList.<RexNode>of(), second);
    // Check Support
    if (!checkSupport(firstUsageFinder, secondUsageFinder)) {
        LOGGER.warn("Support for checking {} => {} is not there", first, second);
        return false;
    }
    ImmutableList.Builder<Set<Pair<RexInputRef, RexNode>>> usagesBuilder = ImmutableList.builder();
    for (Map.Entry<RexInputRef, InputRefUsage<SqlOperator, RexNode>> entry : firstUsageFinder.usageMap.entrySet()) {
        ImmutableSet.Builder<Pair<RexInputRef, RexNode>> usageBuilder = ImmutableSet.builder();
        if (entry.getValue().usageList.size() > 0) {
            for (final Pair<SqlOperator, RexNode> pair : entry.getValue().usageList) {
                usageBuilder.add(Pair.of(entry.getKey(), pair.getValue()));
            }
            usagesBuilder.add(usageBuilder.build());
        }
    }
    final Set<List<Pair<RexInputRef, RexNode>>> usages = Sets.cartesianProduct(usagesBuilder.build());
    for (List<Pair<RexInputRef, RexNode>> usageList : usages) {
        // Get the literals from first conjunction and executes second conjunction
        // using them.
        // 
        // E.g., for
        // x > 30 &rArr; x > 10,
        // we will replace x by 30 in second expression and execute it i.e.,
        // 30 > 10
        // 
        // If it's true then we infer implication.
        final DataContext dataValues = VisitorDataContext.of(rowType, usageList);
        if (!isSatisfiable(second, dataValues)) {
            return false;
        }
    }
    return true;
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ImmutableList(com.google.common.collect.ImmutableList) SqlOperator(org.apache.calcite.sql.SqlOperator) DataContext(org.apache.calcite.DataContext) ImmutableSet(com.google.common.collect.ImmutableSet) RexInputRef(org.apache.calcite.rex.RexInputRef) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) RexNode(org.apache.calcite.rex.RexNode) Pair(org.apache.calcite.util.Pair)

Example 29 with DataContext

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.DataContext in project calcite by apache.

the class CalcitePrepareImpl method simplePrepare.

/**
 * Quickly prepares a simple SQL statement, circumventing the usual
 * preparation process.
 */
private <T> CalciteSignature<T> simplePrepare(Context context, String sql) {
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    final RelDataType x = typeFactory.builder().add(SqlUtil.deriveAliasFromOrdinal(0), SqlTypeName.INTEGER).build();
    @SuppressWarnings("unchecked") final List<T> list = (List) ImmutableList.of(1);
    final List<String> origin = null;
    final List<List<String>> origins = Collections.nCopies(x.getFieldCount(), origin);
    final List<ColumnMetaData> columns = getColumnMetaDataList(typeFactory, x, x, origins);
    final Meta.CursorFactory cursorFactory = Meta.CursorFactory.deduce(columns, null);
    return new CalciteSignature<>(sql, ImmutableList.<AvaticaParameter>of(), ImmutableMap.<String, Object>of(), x, columns, cursorFactory, context.getRootSchema(), ImmutableList.<RelCollation>of(), -1, new Bindable<T>() {

        public Enumerable<T> bind(DataContext dataContext) {
            return Linq4j.asEnumerable(list);
        }
    }, Meta.StatementType.SELECT);
}
Also used : Meta(org.apache.calcite.avatica.Meta) RelDataType(org.apache.calcite.rel.type.RelDataType) DataContext(org.apache.calcite.DataContext) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) Enumerable(org.apache.calcite.linq4j.Enumerable) ArrayList(java.util.ArrayList) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 30 with DataContext

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.DataContext in project calcite by apache.

the class Prepare method optimize.

/**
 * Optimizes a query plan.
 *
 * @param root Root of relational expression tree
 * @param materializations Tables known to be populated with a given query
 * @param lattices Lattices
 * @return an equivalent optimized relational expression
 */
protected RelRoot optimize(RelRoot root, final List<Materialization> materializations, final List<CalciteSchema.LatticeEntry> lattices) {
    final RelOptPlanner planner = root.rel.getCluster().getPlanner();
    final DataContext dataContext = context.getDataContext();
    planner.setExecutor(new RexExecutorImpl(dataContext));
    final List<RelOptMaterialization> materializationList = new ArrayList<>();
    for (Materialization materialization : materializations) {
        List<String> qualifiedTableName = materialization.materializedTable.path();
        materializationList.add(new RelOptMaterialization(materialization.tableRel, materialization.queryRel, materialization.starRelOptTable, qualifiedTableName));
    }
    final List<RelOptLattice> latticeList = new ArrayList<>();
    for (CalciteSchema.LatticeEntry lattice : lattices) {
        final CalciteSchema.TableEntry starTable = lattice.getStarTable();
        final JavaTypeFactory typeFactory = context.getTypeFactory();
        final RelOptTableImpl starRelOptTable = RelOptTableImpl.create(catalogReader, starTable.getTable().getRowType(typeFactory), starTable, null);
        latticeList.add(new RelOptLattice(lattice.getLattice(), starRelOptTable));
    }
    final RelTraitSet desiredTraits = getDesiredRootTraitSet(root);
    // Work around
    // [CALCITE-1774] Allow rules to be registered during planning process
    // by briefly creating each kind of physical table to let it register its
    // rules. The problem occurs when plans are created via RelBuilder, not
    // the usual process (SQL and SqlToRelConverter.Config.isConvertTableAccess
    // = true).
    final RelVisitor visitor = new RelVisitor() {

        @Override
        public void visit(RelNode node, int ordinal, RelNode parent) {
            if (node instanceof TableScan) {
                final RelOptCluster cluster = node.getCluster();
                final RelOptTable.ToRelContext context = RelOptUtil.getContext(cluster);
                final RelNode r = node.getTable().toRel(context);
                planner.registerClass(r);
            }
            super.visit(node, ordinal, parent);
        }
    };
    visitor.go(root.rel);
    final Program program = getProgram();
    final RelNode rootRel4 = program.run(planner, root.rel, desiredTraits, materializationList, latticeList);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Plan after physical tweaks: {}", RelOptUtil.toString(rootRel4, SqlExplainLevel.ALL_ATTRIBUTES));
    }
    return root.withRel(rootRel4);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) TableScan(org.apache.calcite.rel.core.TableScan) Program(org.apache.calcite.tools.Program) ArrayList(java.util.ArrayList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) LatticeEntry(org.apache.calcite.jdbc.CalciteSchema.LatticeEntry) RexExecutorImpl(org.apache.calcite.rex.RexExecutorImpl) RelOptMaterialization(org.apache.calcite.plan.RelOptMaterialization) DataContext(org.apache.calcite.DataContext) RelNode(org.apache.calcite.rel.RelNode) RelOptMaterialization(org.apache.calcite.plan.RelOptMaterialization) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelOptLattice(org.apache.calcite.plan.RelOptLattice) RelVisitor(org.apache.calcite.rel.RelVisitor) RelOptTable(org.apache.calcite.plan.RelOptTable)

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