Search in sources :

Example 6 with DataContext

use of org.apache.beam.vendor.calcite.v1_28_0.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 7 with DataContext

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

the class CalciteMetaImpl method createResultSet.

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.<AvaticaParameter>of(), internalParameters, null, columns, cursorFactory, null, ImmutableList.<RelCollation>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 8 with DataContext

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

the class FilesTableFunction method eval.

/**
 * Evaluates the function.
 *
 * @param path Directory in which to start the search. Typically '.'
 * @return Table that can be inspected, planned, and evaluated
 */
public static ScannableTable eval(final String path) {
    return new ScannableTable() {

        public RelDataType getRowType(RelDataTypeFactory typeFactory) {
            return typeFactory.builder().add("access_time", // %A@ sec since epoch
            SqlTypeName.TIMESTAMP).add("block_count", // %b in 512B blocks
            SqlTypeName.INTEGER).add("change_time", // %C@ sec since epoch
            SqlTypeName.TIMESTAMP).add("depth", // %d depth in directory tree
            SqlTypeName.INTEGER).add("device", // %D device number
            SqlTypeName.INTEGER).add("file_name", // %f file name, sans dirs
            SqlTypeName.VARCHAR).add("fstype", // %F file system type
            SqlTypeName.VARCHAR).add("gname", // %g group name
            SqlTypeName.VARCHAR).add("gid", // %G numeric group id
            SqlTypeName.INTEGER).add("dir_name", // %h leading dirs
            SqlTypeName.VARCHAR).add("inode", // %i inode number
            SqlTypeName.BIGINT).add("link", // %l object of sym link
            SqlTypeName.VARCHAR).add("perm", SqlTypeName.CHAR, // %#m permission octal
            4).add("hard", // %n number of hard links
            SqlTypeName.INTEGER).add("path", // %P file's name
            SqlTypeName.VARCHAR).add("size", // %s file's size in bytes
            SqlTypeName.BIGINT).add("mod_time", // %T@ seconds since epoch
            SqlTypeName.TIMESTAMP).add("user", // %u user name
            SqlTypeName.VARCHAR).add("uid", // %U numeric user id
            SqlTypeName.INTEGER).add("type", SqlTypeName.CHAR, // %Y file type
            1).build();
        // Fields in Linux find that are currently ignored:
        // %y file type (not following sym links)
        // %k block count in 1KB blocks
        // %p file name (including argument)
        }

        private Enumerable<String> sourceLinux() {
            final String[] args = { "find", path, "-printf", "" + // access_time
            "%A@\\0" + // block_count
            "%b\\0" + // change_time
            "%C@\\0" + // depth
            "%d\\0" + // device
            "%D\\0" + // file_name
            "%f\\0" + // fstype
            "%F\\0" + // gname
            "%g\\0" + // gid
            "%G\\0" + // dir_name
            "%h\\0" + // inode
            "%i\\0" + // link
            "%l\\0" + // perm
            "%#m\\0" + // hard
            "%n\\0" + // path
            "%P\\0" + // size
            "%s\\0" + // mod_time
            "%T@\\0" + // user
            "%u\\0" + // uid
            "%U\\0" + // type
            "%Y\\0" };
            return Processes.processLines('\0', args);
        }

        private Enumerable<String> sourceMacOs() {
            if (path.contains("'")) {
                // no injection monkey business
                throw new IllegalArgumentException();
            }
            final String[] args = { "/bin/sh", "-c", "find '" + path + "' | xargs stat -f " + // access_time
            "%a%n" + // block_count
            "%b%n" + // change_time
            "%c%n" + // depth: not supported by macOS stat
            "0%n" + // device: we only use the high part of "H,L" device
            "%Hd%n" + // filename: not supported by macOS stat
            "filename%n" + // fstype: not supported by macOS stat
            "fstype%n" + // gname
            "%Sg%n" + // gid
            "%g%n" + // dir_name: not supported by macOS stat
            "dir_name%n" + // inode
            "%i%n" + // link
            "%Y%n" + // perm
            "%Lp%n" + // hard
            "%l%n" + // path
            "%SN%n" + // size
            "%z%n" + // mod_time
            "%m%n" + // user
            "%Su%n" + // uid
            "%u%n" + // type
            "%LT%n" };
            return Processes.processLines('\n', args);
        }

        public Enumerable<Object[]> scan(DataContext root) {
            final RelDataType rowType = getRowType(root.getTypeFactory());
            final List<String> fieldNames = ImmutableList.copyOf(rowType.getFieldNames());
            final String osName = System.getProperty("os.name");
            final String osVersion = System.getProperty("os.version");
            Util.discard(osVersion);
            final Enumerable<String> enumerable;
            switch(osName) {
                case // tested on version 10.12.5
                "Mac OS X":
                    enumerable = sourceMacOs();
                    break;
                default:
                    enumerable = sourceLinux();
            }
            return new AbstractEnumerable<Object[]>() {

                public Enumerator<Object[]> enumerator() {
                    final Enumerator<String> e = enumerable.enumerator();
                    return new Enumerator<Object[]>() {

                        Object[] current;

                        public Object[] current() {
                            return current;
                        }

                        public boolean moveNext() {
                            current = new Object[fieldNames.size()];
                            for (int i = 0; i < current.length; i++) {
                                if (!e.moveNext()) {
                                    return false;
                                }
                                final String v = e.current();
                                try {
                                    current[i] = field(fieldNames.get(i), v);
                                } catch (RuntimeException e) {
                                    throw new RuntimeException("while parsing value [" + v + "] of field [" + fieldNames.get(i) + "] in line [" + Arrays.toString(current) + "]", e);
                                }
                            }
                            switch(osName) {
                                case "Mac OS X":
                                    // Strip leading "./"
                                    String path = (String) current[14];
                                    if (path.equals(".")) {
                                        current[14] = path = "";
                                        // depth
                                        current[3] = 0;
                                    } else if (path.startsWith("./")) {
                                        current[14] = path = path.substring(2);
                                        // depth
                                        current[3] = count(path, '/') + 1;
                                    } else {
                                        // depth
                                        current[3] = count(path, '/');
                                    }
                                    final int slash = path.lastIndexOf('/');
                                    if (slash >= 0) {
                                        // filename
                                        current[5] = path.substring(slash + 1);
                                        // dir_name
                                        current[9] = path.substring(0, slash);
                                    } else {
                                        // filename
                                        current[5] = path;
                                        // dir_name
                                        current[9] = "";
                                    }
                                    // Make type values more like those on Linux
                                    final String type = (String) current[19];
                                    current[19] = type.equals("/") ? "d" : type.equals("") || type.equals("*") ? "f" : type.equals("@") ? "l" : type;
                            }
                            return true;
                        }

                        private int count(String s, char c) {
                            int n = 0;
                            for (int i = 0, len = s.length(); i < len; i++) {
                                if (s.charAt(i) == c) {
                                    ++n;
                                }
                            }
                            return n;
                        }

                        public void reset() {
                            throw new UnsupportedOperationException();
                        }

                        public void close() {
                            e.close();
                        }

                        private Object field(String field, String value) {
                            switch(field) {
                                case "block_count":
                                case "depth":
                                case "device":
                                case "gid":
                                case "uid":
                                case "hard":
                                    return Integer.valueOf(value);
                                case "inode":
                                case "size":
                                    return Long.valueOf(value);
                                case "access_time":
                                case "change_time":
                                case "mod_time":
                                    return new BigDecimal(value).multiply(THOUSAND).longValue();
                                default:
                                    return value;
                            }
                        }
                    };
                }
            };
        }

        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 : 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) BigDecimal(java.math.BigDecimal) DataContext(org.apache.calcite.DataContext) Enumerator(org.apache.calcite.linq4j.Enumerator) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) ScannableTable(org.apache.calcite.schema.ScannableTable) SqlNode(org.apache.calcite.sql.SqlNode)

Example 9 with DataContext

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

the class StdinTableFunction method eval.

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

        public Enumerable<Object[]> scan(DataContext root) {
            final InputStream is = DataContext.Variable.STDIN.get(root);
            return new AbstractEnumerable<Object[]>() {

                final InputStreamReader in = new InputStreamReader(is, StandardCharsets.UTF_8);

                final BufferedReader br = new BufferedReader(in);

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

                        String line;

                        int i;

                        public Object[] current() {
                            if (line == null) {
                                throw new NoSuchElementException();
                            }
                            return new Object[] { i, line };
                        }

                        public boolean moveNext() {
                            try {
                                line = br.readLine();
                                ++i;
                                return line != null;
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                        }

                        public void reset() {
                            throw new UnsupportedOperationException();
                        }

                        public void close() {
                            try {
                                br.close();
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    };
                }
            };
        }

        public RelDataType getRowType(RelDataTypeFactory typeFactory) {
            return typeFactory.builder().add("ordinal", SqlTypeName.INTEGER).add("line", 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 : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SqlCall(org.apache.calcite.sql.SqlCall) CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) Schema(org.apache.calcite.schema.Schema) IOException(java.io.IOException) DataContext(org.apache.calcite.DataContext) Enumerator(org.apache.calcite.linq4j.Enumerator) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) BufferedReader(java.io.BufferedReader) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) ScannableTable(org.apache.calcite.schema.ScannableTable) NoSuchElementException(java.util.NoSuchElementException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 10 with DataContext

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

the class VmstatTableFunction 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);
            // Could do this here too..
            switch(osName) {
                case // tested on version 10.11.6
                "Mac OS X":
                    args = new String[] { "/bin/sh", "-c", "vm_stat | tail -n +2 | awk '{print $NF}' | sed 's/\\.//' | tr '\\n' ' '" };
                    break;
                default:
                    args = new String[] { "/bin/sh", "-c", "vmstat -n | tail -n +3" };
            }
            return Processes.processLines(args).select(new Function1<String, Object[]>() {

                public Object[] apply(String line) {
                    final String[] fields = line.trim().split("\\s+");
                    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) {
                            e.printStackTrace(System.out);
                            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) {
                    if (value.isEmpty()) {
                        return 0;
                    }
                    if (value.endsWith(".")) {
                        return Long.parseLong(value.substring(0, value.length()));
                    }
                    return Long.parseLong(value);
                }
            });
        }

        public RelDataType getRowType(RelDataTypeFactory typeFactory) {
            final String osName = System.getProperty("os.name");
            final RelDataTypeFactory.Builder builder = typeFactory.builder();
            switch(osName) {
                case "Mac OS X":
                    return builder.add("pages_free", SqlTypeName.BIGINT).add("pages_active", SqlTypeName.BIGINT).add("pages_inactive", SqlTypeName.BIGINT).add("pages_speculative", SqlTypeName.BIGINT).add("pages_throttled", SqlTypeName.BIGINT).add("pages_wired_down", SqlTypeName.BIGINT).add("pages_purgeable", SqlTypeName.BIGINT).add("translation_faults", SqlTypeName.BIGINT).add("pages_copy_on_write", SqlTypeName.BIGINT).add("pages_zero_filed", SqlTypeName.BIGINT).add("pages_reactivated", SqlTypeName.BIGINT).add("pages_purged", SqlTypeName.BIGINT).add("pages_file_backed", SqlTypeName.BIGINT).add("pages_anonymous", SqlTypeName.BIGINT).add("pages_stored_compressor", SqlTypeName.BIGINT).add("pages_occupied_compressor", SqlTypeName.BIGINT).add("decompressions", SqlTypeName.BIGINT).add("compressions", SqlTypeName.BIGINT).add("pageins", SqlTypeName.BIGINT).add("pageouts", SqlTypeName.BIGINT).add("swapins", SqlTypeName.BIGINT).add("swapouts", SqlTypeName.BIGINT).build();
                default:
                    return builder.add("proc_r", SqlTypeName.BIGINT).add("proc_b", SqlTypeName.BIGINT).add("mem_swpd", SqlTypeName.BIGINT).add("mem_free", SqlTypeName.BIGINT).add("mem_buff", SqlTypeName.BIGINT).add("mem_cache", SqlTypeName.BIGINT).add("swap_si", SqlTypeName.BIGINT).add("swap_so", SqlTypeName.BIGINT).add("io_bi", SqlTypeName.BIGINT).add("io_bo", SqlTypeName.BIGINT).add("system_in", SqlTypeName.BIGINT).add("system_cs", SqlTypeName.BIGINT).add("cpu_us", SqlTypeName.BIGINT).add("cpu_sy", SqlTypeName.BIGINT).add("cpu_id", SqlTypeName.BIGINT).add("cpu_wa", SqlTypeName.BIGINT).add("cpu_st", SqlTypeName.BIGINT).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 : 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)

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