Search in sources :

Example 36 with DataContext

use of 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 (first.equals(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 node.equals(operand) || super.isNull(node);
                }
            };
            if (strong.isNull(first)) {
                return true;
            }
            break;
        default:
            break;
    }
    final InputUsageFinder firstUsageFinder = new InputUsageFinder();
    final InputUsageFinder secondUsageFinder = new InputUsageFinder();
    RexUtil.apply(firstUsageFinder, ImmutableList.of(), first);
    RexUtil.apply(secondUsageFinder, ImmutableList.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, @Nullable RexNode>>> usagesBuilder = ImmutableList.builder();
    for (Map.Entry<RexInputRef, InputRefUsage<SqlOperator, @Nullable RexNode>> entry : firstUsageFinder.usageMap.entrySet()) {
        ImmutableSet.Builder<Pair<RexInputRef, @Nullable RexNode>> usageBuilder = ImmutableSet.builder();
        if (entry.getValue().usageList.size() > 0) {
            for (final Pair<SqlOperator, @Nullable RexNode> pair : entry.getValue().usageList) {
                usageBuilder.add(Pair.of(entry.getKey(), pair.getValue()));
            }
            usagesBuilder.add(usageBuilder.build());
        }
    }
    final Set<List<Pair<RexInputRef, @Nullable RexNode>>> usages = Sets.cartesianProduct(usagesBuilder.build());
    for (List<Pair<RexInputRef, @Nullable 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) Nullable(org.checkerframework.checker.nullness.qual.Nullable) RexNode(org.apache.calcite.rex.RexNode) Pair(org.apache.calcite.util.Pair)

Example 37 with DataContext

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

the class PsTableFunction method eval.

public static ScannableTable eval(boolean b) {
    return new ScannableTable() {

        @Override
        public Enumerable<@Nullable Object[]> scan(DataContext root) {
            JavaTypeFactory typeFactory = root.getTypeFactory();
            final RelDataType rowType = getRowType(typeFactory);
            final List<String> fieldNames = ImmutableList.copyOf(rowType.getFieldNames());
            final String[] args;
            final String osName = System.getProperty("os.name");
            final String osVersion = System.getProperty("os.version");
            Util.discard(osVersion);
            switch(osName) {
                case // tested on version 10.12.5
                "Mac OS X":
                    args = new String[] { "ps", "ax", "-o", "ppid=,pid=,pgid=,tpgid=,stat=," + "user=,pcpu=,pmem=,vsz=,rss=,tty=,start=,time=,uid=,ruid=," + "sess=,comm=" };
                    break;
                default:
                    args = new String[] { "ps", "--no-headers", "axo", "ppid,pid,pgrp," + "tpgid,stat,user,pcpu,pmem,vsz,rss,tty,start_time,time,euid," + "ruid,sess,comm" };
            }
            return Processes.processLines(args).select(new Function1<String, Object[]>() {

                @Override
                public Object[] apply(String line) {
                    final String[] fields = line.trim().split(" +");
                    final Object[] values = new Object[fieldNames.size()];
                    for (int i = 0; i < values.length; i++) {
                        try {
                            values[i] = field(fieldNames.get(i), fields[i]);
                        } catch (RuntimeException e) {
                            throw new RuntimeException("while parsing value [" + fields[i] + "] of field [" + fieldNames.get(i) + "] in line [" + line + "]");
                        }
                    }
                    return values;
                }

                private Object field(String field, String value) {
                    switch(field) {
                        case "pid":
                        case "ppid":
                        // linux only; macOS equivalent is "pgid"
                        case "pgrp":
                        // see "pgrp"
                        case "pgid":
                        case "tpgid":
                            return Integer.valueOf(value);
                        case "pcpu":
                        case "pmem":
                            return (int) (Float.valueOf(value) * 10f);
                        case "time":
                            final Matcher m1 = MINUTE_SECOND_MILLIS_PATTERN.matcher(value);
                            if (m1.matches()) {
                                final long h = Long.parseLong(m1.group(1));
                                final long m = Long.parseLong(m1.group(2));
                                final long s = Long.parseLong(m1.group(3));
                                return h * 3600000L + m * 60000L + s * 1000L;
                            }
                            final Matcher m2 = HOUR_MINUTE_SECOND_PATTERN.matcher(value);
                            if (m2.matches()) {
                                final long m = Long.parseLong(m2.group(1));
                                final long s = Long.parseLong(m2.group(2));
                                String g3 = m2.group(3);
                                while (g3.length() < 3) {
                                    g3 = g3 + "0";
                                }
                                final long millis = Long.parseLong(g3);
                                return m * 60000L + s * 1000L + millis;
                            }
                            return 0L;
                        // linux only; macOS version is "lstart"
                        case "start_time":
                        // see "start_time"
                        case "lstart":
                        // linux only; macOS equivalent is "uid"
                        case "euid":
                        // see "euid"
                        case "uid":
                        default:
                            return value;
                    }
                }
            });
        }

        @Override
        public RelDataType getRowType(RelDataTypeFactory typeFactory) {
            return typeFactory.builder().add("pid", SqlTypeName.INTEGER).add("ppid", SqlTypeName.INTEGER).add("pgrp", SqlTypeName.INTEGER).add("tpgid", SqlTypeName.INTEGER).add("stat", SqlTypeName.VARCHAR).add("user", SqlTypeName.VARCHAR).add("pcpu", SqlTypeName.DECIMAL, 3, 1).add("pmem", SqlTypeName.DECIMAL, 3, 1).add("vsz", SqlTypeName.INTEGER).add("rss", SqlTypeName.INTEGER).add("tty", SqlTypeName.VARCHAR).add("start_time", SqlTypeName.VARCHAR).add("time", TimeUnit.HOUR, -1, TimeUnit.SECOND, 0).add("euid", SqlTypeName.VARCHAR).add("ruid", SqlTypeName.VARCHAR).add("sess", SqlTypeName.VARCHAR).add("command", SqlTypeName.VARCHAR).build();
        }

        @Override
        public Statistic getStatistic() {
            return Statistics.of(1000d, ImmutableList.of(ImmutableBitSet.of(1)));
        }

        @Override
        public Schema.TableType getJdbcTableType() {
            return Schema.TableType.TABLE;
        }

        @Override
        public boolean isRolledUp(String column) {
            return false;
        }

        @Override
        public boolean rolledUpColumnValidInsideAgg(String column, SqlCall call, @Nullable SqlNode parent, @Nullable CalciteConnectionConfig config) {
            return true;
        }
    };
}
Also used : Matcher(java.util.regex.Matcher) SqlCall(org.apache.calcite.sql.SqlCall) CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) Schema(org.apache.calcite.schema.Schema) RelDataType(org.apache.calcite.rel.type.RelDataType) DataContext(org.apache.calcite.DataContext) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) ScannableTable(org.apache.calcite.schema.ScannableTable) Nullable(org.checkerframework.checker.nullness.qual.Nullable) SqlNode(org.apache.calcite.sql.SqlNode)

Example 38 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, @Nullable List<TypedValue> parameterValues0) throws SQLException {
    Map<String, Object> map = new LinkedHashMap<>();
    AvaticaStatement statement = lookupStatement(handle);
    final List<TypedValue> parameterValues;
    if (parameterValues0 == null || parameterValues0.isEmpty()) {
        parameterValues = TROJAN.getParameterValues(statement);
    } else {
        parameterValues = parameterValues0;
    }
    if (MetaImpl.checkParameterValueHasNull(parameterValues)) {
        throw new SQLException("exception while executing query: unbound parameter");
    }
    Ord.forEach(parameterValues, (e, i) -> map.put("?" + i, 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);
    int queryTimeout = statement.getQueryTimeout();
    // Avoid overflow
    if (queryTimeout > 0 && queryTimeout < Integer.MAX_VALUE / 1000) {
        map.put(DataContext.Variable.TIMEOUT.camelName, queryTimeout * 1000L);
    }
    final DataContext dataContext = createDataContext(map, signature.rootSchema);
    return signature.enumerable(dataContext);
}
Also used : SQLException(java.sql.SQLException) LinkedHashMap(java.util.LinkedHashMap) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataContext(org.apache.calcite.DataContext) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException) TypedValue(org.apache.calcite.avatica.remote.TypedValue)

Example 39 with DataContext

use of 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, @Nullable ImmutableIntList projects, FilterableTable filterableTable) {
    final DataContext root = compiler.getDataContext();
    final List<RexNode> mutableFilters = Lists.newArrayList(filters);
    final Enumerable<@Nullable 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) Nullable(org.checkerframework.checker.nullness.qual.Nullable) RexNode(org.apache.calcite.rex.RexNode)

Example 40 with DataContext

use of 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, @Nullable 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())) {
        requireNonNull(schema, () -> "schema is null while resolving " + name + " for table" + 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(o -> {
            @Nullable 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 : TableScan(org.apache.calcite.rel.core.TableScan) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) Enumerables(org.apache.calcite.runtime.Enumerables) Mappings(org.apache.calcite.util.mapping.Mappings) Iterables(com.google.common.collect.Iterables) RelOptUtil(org.apache.calcite.plan.RelOptUtil) RESOURCE(org.apache.calcite.util.Static.RESOURCE) RelOptTable(org.apache.calcite.plan.RelOptTable) RexUtil(org.apache.calcite.rex.RexUtil) Lists(com.google.common.collect.Lists) QueryableTable(org.apache.calcite.schema.QueryableTable) Schemas(org.apache.calcite.schema.Schemas) ImmutableList(com.google.common.collect.ImmutableList) RexNode(org.apache.calcite.rex.RexNode) Mapping(org.apache.calcite.util.mapping.Mapping) Objects.requireNonNull(java.util.Objects.requireNonNull) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) Queryable(org.apache.calcite.linq4j.Queryable) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelDataType(org.apache.calcite.rel.type.RelDataType) Function1(org.apache.calcite.linq4j.function.Function1) ScannableTable(org.apache.calcite.schema.ScannableTable) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) DataContext(org.apache.calcite.DataContext) Enumerable(org.apache.calcite.linq4j.Enumerable) FilterableTable(org.apache.calcite.schema.FilterableTable) Field(java.lang.reflect.Field) List(java.util.List) Type(java.lang.reflect.Type) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Modifier(java.lang.reflect.Modifier) Util(org.apache.calcite.util.Util) ImmutableList(com.google.common.collect.ImmutableList) SchemaPlus(org.apache.calcite.schema.SchemaPlus) 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)

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