Search in sources :

Example 26 with SqlCall

use of org.apache.calcite.sql.SqlCall in project calcite by apache.

the class SqlDdlNodes method renameColumns.

/**
 * Wraps a query to rename its columns. Used by CREATE VIEW and CREATE
 * MATERIALIZED VIEW.
 */
static SqlNode renameColumns(SqlNodeList columnList, SqlNode query) {
    if (columnList == null) {
        return query;
    }
    final SqlParserPos p = query.getParserPosition();
    final SqlNodeList selectList = new SqlNodeList(ImmutableList.<SqlNode>of(SqlIdentifier.star(p)), p);
    final SqlCall from = SqlStdOperatorTable.AS.createCall(p, ImmutableList.<SqlNode>builder().add(query).add(new SqlIdentifier("_", p)).addAll(columnList).build());
    return new SqlSelect(p, null, selectList, from, null, null, null, null, null, null, null);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlCall(org.apache.calcite.sql.SqlCall) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 27 with SqlCall

use of org.apache.calcite.sql.SqlCall 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 28 with SqlCall

use of org.apache.calcite.sql.SqlCall 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 29 with SqlCall

use of org.apache.calcite.sql.SqlCall 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)

Example 30 with SqlCall

use of org.apache.calcite.sql.SqlCall in project calcite by apache.

the class AggChecker method visit.

public Void visit(SqlIdentifier id) {
    if (isGroupExpr(id) || id.isStar()) {
        // Star may validly occur in "SELECT COUNT(*) OVER w"
        return null;
    }
    // Is it a call to a parentheses-free function?
    SqlCall call = SqlUtil.makeCall(validator.getOperatorTable(), id);
    if (call != null) {
        return call.accept(this);
    }
    // Didn't find the identifier in the group-by list as is, now find
    // it fully-qualified.
    // TODO: It would be better if we always compared fully-qualified
    // to fully-qualified.
    final SqlQualified fqId = scopes.peek().fullyQualify(id);
    if (isGroupExpr(fqId.identifier)) {
        return null;
    }
    SqlNode originalExpr = validator.getOriginal(id);
    final String exprString = originalExpr.toString();
    throw validator.newValidationError(originalExpr, distinct ? RESOURCE.notSelectDistinctExpr(exprString) : RESOURCE.notGroupExpr(exprString));
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlCall (org.apache.calcite.sql.SqlCall)108 SqlNode (org.apache.calcite.sql.SqlNode)81 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)32 SqlNodeList (org.apache.calcite.sql.SqlNodeList)32 RelDataType (org.apache.calcite.rel.type.RelDataType)30 SqlOperator (org.apache.calcite.sql.SqlOperator)26 BitString (org.apache.calcite.util.BitString)19 ArrayList (java.util.ArrayList)18 SqlSelect (org.apache.calcite.sql.SqlSelect)17 RexNode (org.apache.calcite.rex.RexNode)13 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)12 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)11 SqlLiteral (org.apache.calcite.sql.SqlLiteral)11 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 SqlKind (org.apache.calcite.sql.SqlKind)9 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)9 List (java.util.List)8 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)8 Pair (org.apache.calcite.util.Pair)8 RelNode (org.apache.calcite.rel.RelNode)7