use of org.apache.calcite.schema.impl.ViewTableMacro 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.calcite.schema.impl.ViewTableMacro 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.calcite.schema.impl.ViewTableMacro in project calcite by apache.
the class CalciteAssert method addSchema.
public static SchemaPlus addSchema(SchemaPlus rootSchema, SchemaSpec schema) {
SchemaPlus foodmart;
SchemaPlus jdbcScott;
final ConnectionSpec cs;
final DataSource dataSource;
switch(schema) {
case REFLECTIVE_FOODMART:
return rootSchema.add("foodmart", new ReflectiveSchema(new JdbcTest.FoodmartSchema()));
case JDBC_SCOTT:
cs = DatabaseInstance.HSQLDB.scott;
dataSource = JdbcSchema.dataSource(cs.url, cs.driver, cs.username, cs.password);
return rootSchema.add("JDBC_SCOTT", JdbcSchema.create(rootSchema, "JDBC_SCOTT", dataSource, cs.catalog, cs.schema));
case JDBC_FOODMART:
cs = DB.foodmart;
dataSource = JdbcSchema.dataSource(cs.url, cs.driver, cs.username, cs.password);
return rootSchema.add("foodmart", JdbcSchema.create(rootSchema, "foodmart", dataSource, cs.catalog, cs.schema));
case JDBC_FOODMART_WITH_LATTICE:
foodmart = rootSchema.getSubSchema("foodmart");
if (foodmart == null) {
foodmart = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_FOODMART);
}
foodmart.add("lattice", Lattice.create(foodmart.unwrap(CalciteSchema.class), "select 1 from \"foodmart\".\"sales_fact_1997\" as s\n" + "join \"foodmart\".\"time_by_day\" as t using (\"time_id\")\n" + "join \"foodmart\".\"customer\" as c using (\"customer_id\")\n" + "join \"foodmart\".\"product\" as p using (\"product_id\")\n" + "join \"foodmart\".\"product_class\" as pc on p.\"product_class_id\" = pc.\"product_class_id\"", true));
return foodmart;
case SCOTT:
jdbcScott = rootSchema.getSubSchema("jdbc_scott");
if (jdbcScott == null) {
jdbcScott = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_SCOTT);
}
return rootSchema.add("scott", new CloneSchema(jdbcScott));
case CLONE_FOODMART:
foodmart = rootSchema.getSubSchema("foodmart");
if (foodmart == null) {
foodmart = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_FOODMART);
}
return rootSchema.add("foodmart2", new CloneSchema(foodmart));
case GEO:
ModelHandler.addFunctions(rootSchema, null, ImmutableList.<String>of(), GeoFunctions.class.getName(), "*", true);
final SchemaPlus s = rootSchema.add("GEO", new AbstractSchema());
ModelHandler.addFunctions(s, "countries", ImmutableList.<String>of(), CountriesTableFunction.class.getName(), null, false);
final String sql = "select * from table(\"countries\"(true))";
final ViewTableMacro viewMacro = ViewTable.viewMacro(rootSchema, sql, ImmutableList.of("GEO"), ImmutableList.<String>of(), false);
s.add("countries", viewMacro);
return s;
case HR:
return rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
case LINGUAL:
return rootSchema.add("SALES", new ReflectiveSchema(new JdbcTest.LingualSchema()));
case BLANK:
return rootSchema.add("BLANK", new AbstractSchema());
case ORINOCO:
final SchemaPlus orinoco = rootSchema.add("ORINOCO", new AbstractSchema());
orinoco.add("ORDERS", new StreamTest.OrdersHistoryTable(StreamTest.OrdersStreamTableFactory.getRowList()));
return orinoco;
case POST:
final SchemaPlus post = rootSchema.add("POST", new AbstractSchema());
post.add("EMP", ViewTable.viewMacro(post, "select * from (values\n" + " ('Jane', 10, 'F'),\n" + " ('Bob', 10, 'M'),\n" + " ('Eric', 20, 'M'),\n" + " ('Susan', 30, 'F'),\n" + " ('Alice', 30, 'F'),\n" + " ('Adam', 50, 'M'),\n" + " ('Eve', 50, 'F'),\n" + " ('Grace', 60, 'F'),\n" + " ('Wilma', cast(null as integer), 'F'))\n" + " as t(ename, deptno, gender)", ImmutableList.<String>of(), ImmutableList.of("POST", "EMP"), null));
post.add("DEPT", ViewTable.viewMacro(post, "select * from (values\n" + " (10, 'Sales'),\n" + " (20, 'Marketing'),\n" + " (30, 'Engineering'),\n" + " (40, 'Empty')) as t(deptno, dname)", ImmutableList.<String>of(), ImmutableList.of("POST", "DEPT"), null));
post.add("EMPS", ViewTable.viewMacro(post, "select * from (values\n" + " (100, 'Fred', 10, CAST(NULL AS CHAR(1)), CAST(NULL AS VARCHAR(20)), 40, 25, TRUE, FALSE, DATE '1996-08-03'),\n" + " (110, 'Eric', 20, 'M', 'San Francisco', 3, 80, UNKNOWN, FALSE, DATE '2001-01-01'),\n" + " (110, 'John', 40, 'M', 'Vancouver', 2, CAST(NULL AS INT), FALSE, TRUE, DATE '2002-05-03'),\n" + " (120, 'Wilma', 20, 'F', CAST(NULL AS VARCHAR(20)), 1, 5, UNKNOWN, TRUE, DATE '2005-09-07'),\n" + " (130, 'Alice', 40, 'F', 'Vancouver', 2, CAST(NULL AS INT), FALSE, TRUE, DATE '2007-01-01'))\n" + " as t(empno, name, deptno, gender, city, empid, age, slacker, manager, joinedat)", ImmutableList.<String>of(), ImmutableList.of("POST", "EMPS"), null));
return post;
default:
throw new AssertionError("unknown schema " + schema);
}
}
use of org.apache.calcite.schema.impl.ViewTableMacro in project calcite by apache.
the class SqlCreateView method execute.
public void execute(CalcitePrepare.Context context) {
final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
final SchemaPlus schemaPlus = pair.left.plus();
for (Function function : schemaPlus.getFunctions(pair.right)) {
if (function.getParameters().isEmpty()) {
if (!getReplace()) {
throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.viewExists(pair.right));
}
pair.left.removeFunction(pair.right);
}
}
final SqlNode q = SqlDdlNodes.renameColumns(columnList, query);
final String sql = q.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
final ViewTableMacro viewTableMacro = ViewTable.viewMacro(schemaPlus, sql, pair.left.path(null), context.getObjectPath(), false);
final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
Util.discard(x);
schemaPlus.add(pair.right, viewTableMacro);
}
Aggregations