use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class SqlCreateMaterializedView method execute.
public void execute(CalcitePrepare.Context context) {
final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
if (pair.left.plus().getTable(pair.right) != null) {
// Materialized view exists.
if (!ifNotExists) {
// They did not specify IF NOT EXISTS, so give error.
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.tableExists(pair.right));
}
return;
}
final SqlNode q = SqlDdlNodes.renameColumns(columnList, query);
final String sql = q.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
final List<String> schemaPath = pair.left.path(null);
final ViewTableMacro viewTableMacro = ViewTable.viewMacro(pair.left.plus(), sql, schemaPath, context.getObjectPath(), false);
final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
final RelDataType rowType = x.getRowType(context.getTypeFactory());
// Table does not exist. Create it.
final MaterializedViewTable table = new MaterializedViewTable(pair.right, RelDataTypeImpl.proto(rowType));
pair.left.add(pair.right, table);
SqlDdlNodes.populate(name, query, context);
table.key = MaterializationService.instance().defineMaterialization(pair.left, null, sql, schemaPath, pair.right, true, true);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class SqlCreateTable method execute.
public void execute(CalcitePrepare.Context context) {
final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
final JavaTypeFactory typeFactory = new JavaTypeFactoryImpl();
final RelDataType queryRowType;
if (query != null) {
// A bit of a hack: pretend it's a view, to get its row type
final String sql = query.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
final ViewTableMacro viewTableMacro = ViewTable.viewMacro(pair.left.plus(), sql, pair.left.path(null), context.getObjectPath(), false);
final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
queryRowType = x.getRowType(typeFactory);
if (columnList != null && queryRowType.getFieldCount() != columnList.size()) {
throw SqlUtil.newContextException(columnList.getParserPosition(), RESOURCE.columnCountMismatch());
}
} else {
queryRowType = null;
}
final List<SqlNode> columnList;
if (this.columnList != null) {
columnList = this.columnList.getList();
} else {
if (queryRowType == null) {
// a list of column names and types, "CREATE TABLE t (INT c)".
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.createTableRequiresColumnList());
}
columnList = new ArrayList<>();
for (String name : queryRowType.getFieldNames()) {
columnList.add(new SqlIdentifier(name, SqlParserPos.ZERO));
}
}
final ImmutableList.Builder<ColumnDef> b = ImmutableList.builder();
final RelDataTypeFactory.Builder builder = typeFactory.builder();
final RelDataTypeFactory.Builder storedBuilder = typeFactory.builder();
for (Ord<SqlNode> c : Ord.zip(columnList)) {
if (c.e instanceof SqlColumnDeclaration) {
final SqlColumnDeclaration d = (SqlColumnDeclaration) c.e;
final RelDataType type = d.dataType.deriveType(typeFactory, true);
builder.add(d.name.getSimple(), type);
if (d.strategy != ColumnStrategy.VIRTUAL) {
storedBuilder.add(d.name.getSimple(), type);
}
b.add(ColumnDef.of(d.expression, type, d.strategy));
} else if (c.e instanceof SqlIdentifier) {
final SqlIdentifier id = (SqlIdentifier) c.e;
if (queryRowType == null) {
throw SqlUtil.newContextException(id.getParserPosition(), RESOURCE.createTableRequiresColumnTypes(id.getSimple()));
}
final RelDataTypeField f = queryRowType.getFieldList().get(c.i);
final ColumnStrategy strategy = f.getType().isNullable() ? ColumnStrategy.NULLABLE : ColumnStrategy.NOT_NULLABLE;
b.add(ColumnDef.of(c.e, f.getType(), strategy));
builder.add(id.getSimple(), f.getType());
storedBuilder.add(id.getSimple(), f.getType());
} else {
throw new AssertionError(c.e.getClass());
}
}
final RelDataType rowType = builder.build();
final RelDataType storedRowType = storedBuilder.build();
final List<ColumnDef> columns = b.build();
final InitializerExpressionFactory ief = new NullInitializerExpressionFactory() {
@Override
public ColumnStrategy generationStrategy(RelOptTable table, int iColumn) {
return columns.get(iColumn).strategy;
}
@Override
public RexNode newColumnDefaultValue(RelOptTable table, int iColumn, InitializerContext context) {
final ColumnDef c = columns.get(iColumn);
if (c.expr != null) {
return context.convertExpression(c.expr);
}
return super.newColumnDefaultValue(table, iColumn, context);
}
};
if (pair.left.plus().getTable(pair.right) != null) {
// Table exists.
if (!ifNotExists) {
// They did not specify IF NOT EXISTS, so give error.
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.tableExists(pair.right));
}
return;
}
// Table does not exist. Create it.
pair.left.add(pair.right, new MutableArrayTable(pair.right, RelDataTypeImpl.proto(storedRowType), RelDataTypeImpl.proto(rowType), ief));
if (query != null) {
SqlDdlNodes.populate(name, query, context);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.
the class SqlDdlNodes method populate.
/**
* Populates the table called {@code name} by executing {@code query}.
*/
protected static void populate(SqlIdentifier name, SqlNode query, CalcitePrepare.Context context) {
// Generate, prepare and execute an "INSERT INTO table query" statement.
// (It's a bit inefficient that we convert from SqlNode to SQL and back
// again.)
final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(context.getRootSchema().plus()).build();
final Planner planner = Frameworks.getPlanner(config);
try {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
final SqlPrettyWriter w = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, false, pw);
pw.print("INSERT INTO ");
name.unparse(w, 0, 0);
pw.print(" ");
query.unparse(w, 0, 0);
pw.flush();
final String sql = sw.toString();
final SqlNode query1 = planner.parse(sql);
final SqlNode query2 = planner.validate(query1);
final RelRoot r = planner.rel(query2);
final PreparedStatement prepare = context.getRelRunner().prepare(r.rel);
int rowCount = prepare.executeUpdate();
Util.discard(rowCount);
prepare.close();
} catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
throw new RuntimeException(e);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode 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;
}
};
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode 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;
}
};
}
Aggregations