Search in sources :

Example 6 with ScannableTable

use of org.apache.calcite.schema.ScannableTable in project calcite by apache.

the class RelOptTableImpl method toRel.

public RelNode toRel(ToRelContext context) {
    // RelOptTable by replacing with immutable RelRecordType using the same field list.
    if (this.getRowType().isDynamicStruct()) {
        final RelDataType staticRowType = new RelRecordType(getRowType().getFieldList());
        final RelOptTable relOptTable = this.copy(staticRowType);
        return relOptTable.toRel(context);
    }
    // If there are any virtual columns, create a copy of this table without
    // those virtual columns.
    final List<ColumnStrategy> strategies = getColumnStrategies();
    if (strategies.contains(ColumnStrategy.VIRTUAL)) {
        final RelDataTypeFactory.Builder b = context.getCluster().getTypeFactory().builder();
        for (RelDataTypeField field : rowType.getFieldList()) {
            if (strategies.get(field.getIndex()) != ColumnStrategy.VIRTUAL) {
                b.add(field.getName(), field.getType());
            }
        }
        final RelOptTable relOptTable = new RelOptTableImpl(this.schema, b.build(), this.names, this.table, this.expressionFunction, this.rowCount) {

            @Override
            public <T> T unwrap(Class<T> clazz) {
                if (clazz.isAssignableFrom(InitializerExpressionFactory.class)) {
                    return clazz.cast(NullInitializerExpressionFactory.INSTANCE);
                }
                return super.unwrap(clazz);
            }
        };
        return relOptTable.toRel(context);
    }
    if (table instanceof TranslatableTable) {
        return ((TranslatableTable) table).toRel(context, this);
    }
    final RelOptCluster cluster = context.getCluster();
    if (Hook.ENABLE_BINDABLE.get(false)) {
        return LogicalTableScan.create(cluster, this);
    }
    if (CalcitePrepareImpl.ENABLE_ENUMERABLE && table instanceof QueryableTable) {
        return EnumerableTableScan.create(cluster, this);
    }
    if (table instanceof ScannableTable || table instanceof FilterableTable || table instanceof ProjectableFilterableTable) {
        return LogicalTableScan.create(cluster, this);
    }
    if (CalcitePrepareImpl.ENABLE_ENUMERABLE) {
        return EnumerableTableScan.create(cluster, this);
    }
    throw new AssertionError();
}
Also used : ColumnStrategy(org.apache.calcite.schema.ColumnStrategy) RelOptCluster(org.apache.calcite.plan.RelOptCluster) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) FilterableTable(org.apache.calcite.schema.FilterableTable) RelDataType(org.apache.calcite.rel.type.RelDataType) RelRecordType(org.apache.calcite.rel.type.RelRecordType) QueryableTable(org.apache.calcite.schema.QueryableTable) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TranslatableTable(org.apache.calcite.schema.TranslatableTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ScannableTable(org.apache.calcite.schema.ScannableTable)

Example 7 with ScannableTable

use of org.apache.calcite.schema.ScannableTable in project calcite by apache.

the class PsTableFunction method eval.

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

        public Enumerable<Object[]> scan(DataContext root) {
            final RelDataType rowType = getRowType(root.getTypeFactory());
            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[]>() {

                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;
                    }
                }
            });
        }

        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();
        }

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

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

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

        public boolean rolledUpColumnValidInsideAgg(String column, SqlCall call, SqlNode parent, 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) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) ScannableTable(org.apache.calcite.schema.ScannableTable) SqlNode(org.apache.calcite.sql.SqlNode)

Example 8 with ScannableTable

use of org.apache.calcite.schema.ScannableTable in project calcite by apache.

the class SqlLatticeStatisticProvider method cardinality.

private double cardinality(Lattice lattice, Lattice.Column column) {
    final String sql = lattice.countSql(ImmutableBitSet.of(column.ordinal));
    final Table table = new MaterializationService.DefaultTableFactory().createTable(lattice.rootSchema, sql, ImmutableList.<String>of());
    final Object[] values = Iterables.getOnlyElement(((ScannableTable) table).scan(null));
    return ((Number) values[0]).doubleValue();
}
Also used : ScannableTable(org.apache.calcite.schema.ScannableTable) Table(org.apache.calcite.schema.Table)

Aggregations

ScannableTable (org.apache.calcite.schema.ScannableTable)8 RelDataType (org.apache.calcite.rel.type.RelDataType)5 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)5 DataContext (org.apache.calcite.DataContext)4 CalciteConnectionConfig (org.apache.calcite.config.CalciteConnectionConfig)4 Schema (org.apache.calcite.schema.Schema)4 SqlCall (org.apache.calcite.sql.SqlCall)4 SqlNode (org.apache.calcite.sql.SqlNode)4 FilterableTable (org.apache.calcite.schema.FilterableTable)3 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)3 QueryableTable (org.apache.calcite.schema.QueryableTable)3 AbstractEnumerable (org.apache.calcite.linq4j.AbstractEnumerable)2 Enumerator (org.apache.calcite.linq4j.Enumerator)2 RelOptTable (org.apache.calcite.plan.RelOptTable)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Type (java.lang.reflect.Type)1 BigDecimal (java.math.BigDecimal)1