use of org.apache.calcite.sql2rel.InitializerExpressionFactory 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.sql2rel.InitializerExpressionFactory in project calcite by apache.
the class RelOptTableImpl method columnStrategies.
/**
* Helper for {@link #getColumnStrategies()}.
*/
public static List<ColumnStrategy> columnStrategies(final RelOptTable table) {
final int fieldCount = table.getRowType().getFieldCount();
final InitializerExpressionFactory ief = Util.first(table.unwrap(InitializerExpressionFactory.class), NullInitializerExpressionFactory.INSTANCE);
return new AbstractList<ColumnStrategy>() {
public int size() {
return fieldCount;
}
public ColumnStrategy get(int index) {
return ief.generationStrategy(table, index);
}
};
}
use of org.apache.calcite.sql2rel.InitializerExpressionFactory in project calcite by apache.
the class MockCatalogReader method init.
/**
* Initializes this catalog reader.
*/
public MockCatalogReader init() {
final Fixture f = new Fixture();
addressType = f.addressType;
// Register "SALES" schema.
MockSchema salesSchema = new MockSchema("SALES");
registerSchema(salesSchema);
// Register "EMP" table with customer InitializerExpressionFactory
// to check whether newDefaultValue method called or not.
final InitializerExpressionFactory countingInitializerExpressionFactory = new CountingFactory(ImmutableList.of("DEPTNO"));
// Register "EMP" table.
final MockTable empTable = MockTable.create(this, salesSchema, "EMP", false, 14, null, countingInitializerExpressionFactory);
empTable.addColumn("EMPNO", f.intType, true);
empTable.addColumn("ENAME", f.varchar20Type);
empTable.addColumn("JOB", f.varchar10Type);
empTable.addColumn("MGR", f.intTypeNull);
empTable.addColumn("HIREDATE", f.timestampType);
empTable.addColumn("SAL", f.intType);
empTable.addColumn("COMM", f.intType);
empTable.addColumn("DEPTNO", f.intType);
empTable.addColumn("SLACKER", f.booleanType);
registerTable(empTable);
// Register "EMPNULLABLES" table with nullable columns.
final MockTable empNullablesTable = MockTable.create(this, salesSchema, "EMPNULLABLES", false, 14);
empNullablesTable.addColumn("EMPNO", f.intType, true);
empNullablesTable.addColumn("ENAME", f.varchar20Type);
empNullablesTable.addColumn("JOB", f.varchar10TypeNull);
empNullablesTable.addColumn("MGR", f.intTypeNull);
empNullablesTable.addColumn("HIREDATE", f.timestampTypeNull);
empNullablesTable.addColumn("SAL", f.intTypeNull);
empNullablesTable.addColumn("COMM", f.intTypeNull);
empNullablesTable.addColumn("DEPTNO", f.intTypeNull);
empNullablesTable.addColumn("SLACKER", f.booleanTypeNull);
registerTable(empNullablesTable);
// Register "EMPDEFAULTS" table with default values for some columns.
final MockTable empDefaultsTable = MockTable.create(this, salesSchema, "EMPDEFAULTS", false, 14, null, new EmpInitializerExpressionFactory());
empDefaultsTable.addColumn("EMPNO", f.intType, true);
empDefaultsTable.addColumn("ENAME", f.varchar20Type);
empDefaultsTable.addColumn("JOB", f.varchar10TypeNull);
empDefaultsTable.addColumn("MGR", f.intTypeNull);
empDefaultsTable.addColumn("HIREDATE", f.timestampTypeNull);
empDefaultsTable.addColumn("SAL", f.intTypeNull);
empDefaultsTable.addColumn("COMM", f.intTypeNull);
empDefaultsTable.addColumn("DEPTNO", f.intTypeNull);
empDefaultsTable.addColumn("SLACKER", f.booleanTypeNull);
registerTable(empDefaultsTable);
// Register "EMP_B" table. As "EMP", birth with a "BIRTHDATE" column.
final MockTable empBTable = MockTable.create(this, salesSchema, "EMP_B", false, 14);
empBTable.addColumn("EMPNO", f.intType, true);
empBTable.addColumn("ENAME", f.varchar20Type);
empBTable.addColumn("JOB", f.varchar10Type);
empBTable.addColumn("MGR", f.intTypeNull);
empBTable.addColumn("HIREDATE", f.timestampType);
empBTable.addColumn("SAL", f.intType);
empBTable.addColumn("COMM", f.intType);
empBTable.addColumn("DEPTNO", f.intType);
empBTable.addColumn("SLACKER", f.booleanType);
empBTable.addColumn("BIRTHDATE", f.dateType);
registerTable(empBTable);
// Register "DEPT" table.
MockTable deptTable = MockTable.create(this, salesSchema, "DEPT", false, 4);
deptTable.addColumn("DEPTNO", f.intType, true);
deptTable.addColumn("NAME", f.varchar10Type);
registerTable(deptTable);
// Register "DEPT_NESTED" table.
MockTable deptNestedTable = MockTable.create(this, salesSchema, "DEPT_NESTED", false, 4);
deptNestedTable.addColumn("DEPTNO", f.intType, true);
deptNestedTable.addColumn("NAME", f.varchar10Type);
deptNestedTable.addColumn("SKILL", f.skillRecordType);
deptNestedTable.addColumn("EMPLOYEES", f.empListType);
registerTable(deptNestedTable);
// Register "BONUS" table.
MockTable bonusTable = MockTable.create(this, salesSchema, "BONUS", false, 0);
bonusTable.addColumn("ENAME", f.varchar20Type);
bonusTable.addColumn("JOB", f.varchar10Type);
bonusTable.addColumn("SAL", f.intType);
bonusTable.addColumn("COMM", f.intType);
registerTable(bonusTable);
// Register "SALGRADE" table.
MockTable salgradeTable = MockTable.create(this, salesSchema, "SALGRADE", false, 5);
salgradeTable.addColumn("GRADE", f.intType, true);
salgradeTable.addColumn("LOSAL", f.intType);
salgradeTable.addColumn("HISAL", f.intType);
registerTable(salgradeTable);
// Register "EMP_ADDRESS" table
MockTable contactAddressTable = MockTable.create(this, salesSchema, "EMP_ADDRESS", false, 26);
contactAddressTable.addColumn("EMPNO", f.intType, true);
contactAddressTable.addColumn("HOME_ADDRESS", addressType);
contactAddressTable.addColumn("MAILING_ADDRESS", addressType);
registerTable(contactAddressTable);
// Register "DYNAMIC" schema.
MockSchema dynamicSchema = new MockSchema("DYNAMIC");
registerSchema(dynamicSchema);
MockTable nationTable = new MockDynamicTable(this, dynamicSchema.getCatalogName(), dynamicSchema.getName(), "NATION", false, 100);
registerTable(nationTable);
MockTable customerTable = new MockDynamicTable(this, dynamicSchema.getCatalogName(), dynamicSchema.getName(), "CUSTOMER", false, 100);
registerTable(customerTable);
// Register "CUSTOMER" schema.
MockSchema customerSchema = new MockSchema("CUSTOMER");
registerSchema(customerSchema);
// Register "CONTACT" table.
MockTable contactTable = MockTable.create(this, customerSchema, "CONTACT", false, 1000);
contactTable.addColumn("CONTACTNO", f.intType);
contactTable.addColumn("FNAME", f.varchar10Type);
contactTable.addColumn("LNAME", f.varchar10Type);
contactTable.addColumn("EMAIL", f.varchar20Type);
contactTable.addColumn("COORD", f.rectilinearCoordType);
registerTable(contactTable);
// Register "CONTACT_PEEK" table. The
MockTable contactPeekTable = MockTable.create(this, customerSchema, "CONTACT_PEEK", false, 1000);
contactPeekTable.addColumn("CONTACTNO", f.intType);
contactPeekTable.addColumn("FNAME", f.varchar10Type);
contactPeekTable.addColumn("LNAME", f.varchar10Type);
contactPeekTable.addColumn("EMAIL", f.varchar20Type);
contactPeekTable.addColumn("COORD", f.rectilinearPeekCoordType);
contactPeekTable.addColumn("COORD_NE", f.rectilinearPeekNoExpandCoordType);
registerTable(contactPeekTable);
// Register "ACCOUNT" table.
MockTable accountTable = MockTable.create(this, customerSchema, "ACCOUNT", false, 457);
accountTable.addColumn("ACCTNO", f.intType);
accountTable.addColumn("TYPE", f.varchar20Type);
accountTable.addColumn("BALANCE", f.intType);
registerTable(accountTable);
// Register "ORDERS" stream.
MockTable ordersStream = MockTable.create(this, salesSchema, "ORDERS", true, Double.POSITIVE_INFINITY);
ordersStream.addColumn("ROWTIME", f.timestampType);
ordersStream.addMonotonic("ROWTIME");
ordersStream.addColumn("PRODUCTID", f.intType);
ordersStream.addColumn("ORDERID", f.intType);
registerTable(ordersStream);
// Register "SHIPMENTS" stream.
// "ROWTIME" is not column 0, just to mix things up.
MockTable shipmentsStream = MockTable.create(this, salesSchema, "SHIPMENTS", true, Double.POSITIVE_INFINITY);
shipmentsStream.addColumn("ORDERID", f.intType);
shipmentsStream.addColumn("ROWTIME", f.timestampType);
shipmentsStream.addMonotonic("ROWTIME");
registerTable(shipmentsStream);
// Register "PRODUCTS" table.
MockTable productsTable = MockTable.create(this, salesSchema, "PRODUCTS", false, 200D);
productsTable.addColumn("PRODUCTID", f.intType);
productsTable.addColumn("NAME", f.varchar20Type);
productsTable.addColumn("SUPPLIERID", f.intType);
registerTable(productsTable);
// Register "SUPPLIERS" table.
MockTable suppliersTable = MockTable.create(this, salesSchema, "SUPPLIERS", false, 10D);
suppliersTable.addColumn("SUPPLIERID", f.intType);
suppliersTable.addColumn("NAME", f.varchar20Type);
suppliersTable.addColumn("CITY", f.intType);
registerTable(suppliersTable);
// Register "EMP_20" and "EMPNULLABLES_20 views.
// Same columns as "EMP" amd "EMPNULLABLES",
// but "DEPTNO" not visible and set to 20 by default
// and "SAL" is visible but must be greater than 1000,
// which is the equivalent of:
// SELECT EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, SLACKER
// FROM EMP
// WHERE DEPTNO = 20 AND SAL > 1000
final ImmutableIntList m0 = ImmutableIntList.of(0, 1, 2, 3, 4, 5, 6, 8);
MockTable emp20View = new MockViewTable(this, salesSchema.getCatalogName(), salesSchema.name, "EMP_20", false, 600, empTable, m0, null, NullInitializerExpressionFactory.INSTANCE) {
public RexNode getConstraint(RexBuilder rexBuilder, RelDataType tableRowType) {
final RelDataTypeField deptnoField = tableRowType.getFieldList().get(7);
final RelDataTypeField salField = tableRowType.getFieldList().get(5);
final List<RexNode> nodes = Arrays.asList(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(deptnoField.getType(), deptnoField.getIndex()), rexBuilder.makeExactLiteral(BigDecimal.valueOf(20L), deptnoField.getType())), rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, rexBuilder.makeInputRef(salField.getType(), salField.getIndex()), rexBuilder.makeExactLiteral(BigDecimal.valueOf(1000L), salField.getType())));
return RexUtil.composeConjunction(rexBuilder, nodes, false);
}
};
salesSchema.addTable(Util.last(emp20View.getQualifiedName()));
emp20View.addColumn("EMPNO", f.intType);
emp20View.addColumn("ENAME", f.varchar20Type);
emp20View.addColumn("JOB", f.varchar10Type);
emp20View.addColumn("MGR", f.intTypeNull);
emp20View.addColumn("HIREDATE", f.timestampType);
emp20View.addColumn("SAL", f.intType);
emp20View.addColumn("COMM", f.intType);
emp20View.addColumn("SLACKER", f.booleanType);
registerTable(emp20View);
MockTable empNullables20View = new MockViewTable(this, salesSchema.getCatalogName(), salesSchema.name, "EMPNULLABLES_20", false, 600, empNullablesTable, m0, null, NullInitializerExpressionFactory.INSTANCE) {
public RexNode getConstraint(RexBuilder rexBuilder, RelDataType tableRowType) {
final RelDataTypeField deptnoField = tableRowType.getFieldList().get(7);
final RelDataTypeField salField = tableRowType.getFieldList().get(5);
final List<RexNode> nodes = Arrays.asList(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(deptnoField.getType(), deptnoField.getIndex()), rexBuilder.makeExactLiteral(BigDecimal.valueOf(20L), deptnoField.getType())), rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, rexBuilder.makeInputRef(salField.getType(), salField.getIndex()), rexBuilder.makeExactLiteral(BigDecimal.valueOf(1000L), salField.getType())));
return RexUtil.composeConjunction(rexBuilder, nodes, false);
}
};
salesSchema.addTable(Util.last(empNullables20View.getQualifiedName()));
empNullables20View.addColumn("EMPNO", f.intType);
empNullables20View.addColumn("ENAME", f.varchar20Type);
empNullables20View.addColumn("JOB", f.varchar10TypeNull);
empNullables20View.addColumn("MGR", f.intTypeNull);
empNullables20View.addColumn("HIREDATE", f.timestampTypeNull);
empNullables20View.addColumn("SAL", f.intTypeNull);
empNullables20View.addColumn("COMM", f.intTypeNull);
empNullables20View.addColumn("SLACKER", f.booleanTypeNull);
registerTable(empNullables20View);
MockSchema structTypeSchema = new MockSchema("STRUCT");
registerSchema(structTypeSchema);
final List<CompoundNameColumn> columns = Arrays.asList(new CompoundNameColumn("", "K0", f.varchar20Type), new CompoundNameColumn("", "C1", f.varchar20Type), new CompoundNameColumn("F1", "A0", f.intType), new CompoundNameColumn("F2", "A0", f.booleanType), new CompoundNameColumn("F0", "C0", f.intType), new CompoundNameColumn("F1", "C0", f.intTypeNull), new CompoundNameColumn("F0", "C1", f.intType), new CompoundNameColumn("F1", "C2", f.intType), new CompoundNameColumn("F2", "C3", f.intType));
final CompoundNameColumnResolver structTypeTableResolver = new CompoundNameColumnResolver(columns, "F0");
final MockTable structTypeTable = MockTable.create(this, structTypeSchema, "T", false, 100, structTypeTableResolver);
for (CompoundNameColumn column : columns) {
structTypeTable.addColumn(column.getName(), column.type);
}
registerTable(structTypeTable);
final List<CompoundNameColumn> columnsNullable = Arrays.asList(new CompoundNameColumn("", "K0", f.varchar20TypeNull), new CompoundNameColumn("", "C1", f.varchar20TypeNull), new CompoundNameColumn("F1", "A0", f.intTypeNull), new CompoundNameColumn("F2", "A0", f.booleanTypeNull), new CompoundNameColumn("F0", "C0", f.intTypeNull), new CompoundNameColumn("F1", "C0", f.intTypeNull), new CompoundNameColumn("F0", "C1", f.intTypeNull), new CompoundNameColumn("F1", "C2", f.intType), new CompoundNameColumn("F2", "C3", f.intTypeNull));
final MockTable structNullableTypeTable = MockTable.create(this, structTypeSchema, "T_NULLABLES", false, 100, structTypeTableResolver);
for (CompoundNameColumn column : columnsNullable) {
structNullableTypeTable.addColumn(column.getName(), column.type);
}
registerTable(structNullableTypeTable);
// Register "STRUCT.T_10" view.
// Same columns as "STRUCT.T",
// but "F0.C0" is set to 10 by default,
// which is the equivalent of:
// SELECT *
// FROM T
// WHERE F0.C0 = 10
// This table uses MockViewTable which does not populate the constrained columns with default
// values on INSERT.
final ImmutableIntList m1 = ImmutableIntList.of(0, 1, 2, 3, 4, 5, 6, 7, 8);
MockTable struct10View = new MockViewTable(this, structTypeSchema.getCatalogName(), structTypeSchema.name, "T_10", false, 20, structTypeTable, m1, structTypeTableResolver, NullInitializerExpressionFactory.INSTANCE) {
@Override
public RexNode getConstraint(RexBuilder rexBuilder, RelDataType tableRowType) {
final RelDataTypeField c0Field = tableRowType.getFieldList().get(4);
return rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(c0Field.getType(), c0Field.getIndex()), rexBuilder.makeExactLiteral(BigDecimal.valueOf(10L), c0Field.getType()));
}
};
structTypeSchema.addTable(Util.last(struct10View.getQualifiedName()));
for (CompoundNameColumn column : columns) {
struct10View.addColumn(column.getName(), column.type);
}
registerTable(struct10View);
registerTablesWithRollUp(salesSchema, f);
return this;
}
Aggregations