Search in sources :

Example 16 with DataContext

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

the class CalciteConnectionImpl method enumerable.

public <T> Enumerable<T> enumerable(Meta.StatementHandle handle, CalcitePrepare.CalciteSignature<T> signature) throws SQLException {
    Map<String, Object> map = Maps.newLinkedHashMap();
    AvaticaStatement statement = lookupStatement(handle);
    final List<TypedValue> parameterValues = TROJAN.getParameterValues(statement);
    if (MetaImpl.checkParameterValueHasNull(parameterValues)) {
        throw new SQLException("exception while executing query: unbound parameter");
    }
    for (Ord<TypedValue> o : Ord.zip(parameterValues)) {
        map.put("?" + o.i, o.e.toLocal());
    }
    map.putAll(signature.internalParameters);
    final AtomicBoolean cancelFlag;
    try {
        cancelFlag = getCancelFlag(handle);
    } catch (NoSuchStatementException e) {
        throw new RuntimeException(e);
    }
    map.put(DataContext.Variable.CANCEL_FLAG.camelName, cancelFlag);
    final DataContext dataContext = createDataContext(map, signature.rootSchema);
    return signature.enumerable(dataContext);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataContext(org.apache.calcite.DataContext) SQLException(java.sql.SQLException) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) TypedValue(org.apache.calcite.avatica.remote.TypedValue)

Example 17 with DataContext

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

the class Plc4xBaseTable method scan.

/**
 * if tableCutoff is positive, then the row gets limited to that.
 */
public Enumerable<Object[]> scan(DataContext root) {
    return new AbstractEnumerable<Object[]>() {

        @Override
        public Enumerator<Object[]> enumerator() {
            return new Enumerator<Object[]>() {

                private final AtomicLong counter = new AtomicLong(0);

                @Override
                public Object[] current() {
                    List<Object> objects = new ArrayList<>(Arrays.asList(new Timestamp(current.timestamp.toEpochMilli()), current.source));
                    List<Object> objects2 = names.stream().map(name -> current.values.get(name)).collect(Collectors.toList());
                    objects.addAll(objects2);
                    return objects.toArray();
                }

                @Override
                public boolean moveNext() {
                    try {
                        current = queue.take();
                        // If stream, simply return
                        if (tableCutoff <= 0L) {
                            return true;
                        }
                        // If table, return if below cutoff
                        return counter.getAndIncrement() < tableCutoff;
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                    return false;
                }

                @Override
                public void reset() {
                    counter.set(0);
                }

                @Override
                public void close() {
                // Unimplemented
                }
            };
        }
    };
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) java.util.concurrent(java.util.concurrent) Timestamp(java.sql.Timestamp) LoggerFactory(org.slf4j.LoggerFactory) DataContext(org.apache.calcite.DataContext) Enumerable(org.apache.calcite.linq4j.Enumerable) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ArrayList(java.util.ArrayList) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) Stream(java.util.stream.Stream) Enumerator(org.apache.calcite.linq4j.Enumerator) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) org.apache.calcite.rel(org.apache.calcite.rel) Collections(java.util.Collections) Statistic(org.apache.calcite.schema.Statistic) JobConfiguration(org.apache.plc4x.java.scraper.config.JobConfiguration) AtomicLong(java.util.concurrent.atomic.AtomicLong) Enumerator(org.apache.calcite.linq4j.Enumerator) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp)

Example 18 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, @Nullable 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();
        }
        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<@Nullable Object[]> enumerable1 = pfTable.scan(root, mutableFilters, projectInts);
        final Enumerable<Row> rowEnumerable = Enumerables.toRow(enumerable1);
        final ImmutableIntList rejectedProjects;
        if (originalProjects == null || originalProjects.equals(projects)) {
            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) Nullable(org.checkerframework.checker.nullness.qual.Nullable) RexNode(org.apache.calcite.rex.RexNode)

Example 19 with DataContext

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

the class CalciteMetaImpl method createResultSet.

@Override
protected MetaResultSet createResultSet(Map<String, Object> internalParameters, List<ColumnMetaData> columns, CursorFactory cursorFactory, final Frame firstFrame) {
    try {
        final CalciteConnectionImpl connection = getConnection();
        final AvaticaStatement statement = connection.createStatement();
        final CalcitePrepare.CalciteSignature<Object> signature = new CalcitePrepare.CalciteSignature<Object>("", ImmutableList.of(), internalParameters, null, columns, cursorFactory, null, ImmutableList.of(), -1, null, Meta.StatementType.SELECT) {

            @Override
            public Enumerable<Object> enumerable(DataContext dataContext) {
                return Linq4j.asEnumerable(firstFrame.rows);
            }
        };
        return MetaResultSet.create(connection.id, statement.getId(), true, signature, firstFrame);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : DataContext(org.apache.calcite.DataContext) SQLException(java.sql.SQLException) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement)

Example 20 with DataContext

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

the class RexProgramBuilderBase method setUp.

@BeforeEach
public void setUp() {
    typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    rexBuilder = new RexBuilder(typeFactory);
    final DataContext dataContext = DataContexts.of(ImmutableMap.of(DataContext.Variable.TIME_ZONE.camelName, TimeZone.getTimeZone("America/Los_Angeles"), DataContext.Variable.CURRENT_TIMESTAMP.camelName, 1311120000000L));
    executor = new RexExecutorImpl(dataContext);
    simplify = new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, executor).withParanoid(true);
    trueLiteral = rexBuilder.makeLiteral(true);
    falseLiteral = rexBuilder.makeLiteral(false);
    nonNullableInt = typeFactory.createSqlType(SqlTypeName.INTEGER);
    nullableInt = typeFactory.createTypeWithNullability(nonNullableInt, true);
    nullInt = rexBuilder.makeNullLiteral(nullableInt);
    nonNullableSmallInt = typeFactory.createSqlType(SqlTypeName.SMALLINT);
    nullableSmallInt = typeFactory.createTypeWithNullability(nonNullableSmallInt, true);
    nullSmallInt = rexBuilder.makeNullLiteral(nullableSmallInt);
    nonNullableBool = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
    nullableBool = typeFactory.createTypeWithNullability(nonNullableBool, true);
    nullBool = rexBuilder.makeNullLiteral(nullableBool);
    nonNullableVarchar = typeFactory.createSqlType(SqlTypeName.VARCHAR);
    nullableVarchar = typeFactory.createTypeWithNullability(nonNullableVarchar, true);
    nullVarchar = rexBuilder.makeNullLiteral(nullableVarchar);
    nonNullableDecimal = typeFactory.createSqlType(SqlTypeName.DECIMAL);
    nullableDecimal = typeFactory.createTypeWithNullability(nonNullableDecimal, true);
    nullDecimal = rexBuilder.makeNullLiteral(nullableDecimal);
    nonNullableVarbinary = typeFactory.createSqlType(SqlTypeName.VARBINARY);
    nullableVarbinary = typeFactory.createTypeWithNullability(nonNullableVarbinary, true);
    nullVarbinary = rexBuilder.makeNullLiteral(nullableVarbinary);
}
Also used : DataContext(org.apache.calcite.DataContext) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

DataContext (org.apache.calcite.DataContext)47 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)10 RexNode (org.apache.calcite.rex.RexNode)8 Nullable (org.checkerframework.checker.nullness.qual.Nullable)8 HashMap (java.util.HashMap)7 RelDataType (org.apache.calcite.rel.type.RelDataType)7 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)7 Values (org.apache.storm.tuple.Values)7 Context (org.apache.calcite.interpreter.Context)6 StormContext (org.apache.calcite.interpreter.StormContext)6 SQLException (java.sql.SQLException)5 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)5 TypedValue (org.apache.calcite.avatica.remote.TypedValue)5 Enumerable (org.apache.calcite.linq4j.Enumerable)5 ScannableTable (org.apache.calcite.schema.ScannableTable)5 URL (java.net.URL)4 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)4 Enumerator (org.apache.calcite.linq4j.Enumerator)4 RexBuilder (org.apache.calcite.rex.RexBuilder)4