in baremaps-calcite/src/main/java/org/apache/baremaps/calcite/BaremapsDdlExecutor.java [446:559]
public void execute(SqlCreateTable create,
CalcitePrepare.Context context) {
final SchemaInfo schemaInfo =
schema(context, true, create.name);
requireNonNull(schemaInfo.schema()); // TODO: should not assume parent schema exists
final JavaTypeFactory typeFactory = context.getTypeFactory();
final RelDataType queryRowType;
// Process WITH options if present
Map<String, String> withOptions = processWithOptions(create.withOptions);
if (create.query != null) {
// A bit of a hack: pretend it's a view, to get its row type
final String sql =
create.query.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
final ViewTableMacro viewTableMacro =
ViewTable.viewMacro(schemaInfo.schema().plus(), sql, schemaInfo.schema().path(null),
context.getObjectPath(), false);
final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
queryRowType = x.getRowType(typeFactory);
if (create.columnList != null
&& queryRowType.getFieldCount() != create.columnList.size()) {
throw SqlUtil.newContextException(
create.columnList.getParserPosition(),
RESOURCE.columnCountMismatch());
}
} else {
queryRowType = null;
}
final List<SqlNode> columnList;
if (create.columnList != null) {
columnList = create.columnList;
} else {
if (queryRowType == null) {
// "CREATE TABLE t" is invalid; because there is no "AS query" we need
// a list of column names and types, "CREATE TABLE t (INT c)".
throw SqlUtil.newContextException(create.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();
// REVIEW 2019-08-19 Danny Chan: Should we implement the
// #validate(SqlValidator) to get the SqlValidator instance?
final SqlValidator validator = validator(context, true);
for (Ord<SqlNode> c : Ord.zip(columnList)) {
if (c.e instanceof SqlColumnDeclaration) {
final SqlColumnDeclaration d = (SqlColumnDeclaration) c.e;
final RelDataType type = d.dataType.deriveType(validator, 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();
if (schemaInfo.schema().plus().getTable(schemaInfo.name()) != null) {
// Table exists.
if (create.ifNotExists) {
return;
}
if (!create.getReplace()) {
// They did not specify IF NOT EXISTS, so give error.
throw SqlUtil.newContextException(create.name.getParserPosition(),
RESOURCE.tableExists(schemaInfo.name()));
}
}
// Create the operand map for the table
Map<String, Object> operand = new HashMap<>();
operand.put("format", withOptions.computeIfAbsent("format", k -> "data"));
operand.put("file", withOptions.computeIfAbsent("file", k -> {
try {
Path file = Files.createTempDirectory("baremaps_");
return file.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}));
// Create the table using BaremapsTableFactory
BaremapsTableFactory tableFactory = new BaremapsTableFactory();
Table table =
tableFactory.create(schemaInfo.schema().plus(), schemaInfo.name(), operand, rowType);
// Add the table to the schema
schemaInfo.schema().add(schemaInfo.name(), table);
if (create.query != null) {
populate(create.name, create.query, context);
}
}