Search in sources :

Example 1 with ContextSqlValidator

use of org.apache.calcite.jdbc.ContextSqlValidator in project calcite by apache.

the class ExtensionDdlExecutor method execute.

/**
 * Executes a {@code CREATE TABLE} command. Called via reflection.
 */
public void execute(SqlCreateTable create, CalcitePrepare.Context context) {
    final CalciteSchema schema = Schemas.subSchema(context.getRootSchema(), context.getDefaultSchemaPath());
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    final RelDataType queryRowType;
    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(schema.plus(), sql, 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 RelDataTypeFactory.Builder builder = typeFactory.builder();
    if (create.columnList != null) {
        final SqlValidator validator = new ContextSqlValidator(context, false);
        create.forEachNameType((name, typeSpec) -> builder.add(name.getSimple(), typeSpec.deriveType(validator, true)));
    } else {
        if (queryRowType == null) {
            // a list of column names and types, "CREATE TABLE t (INT c)".
            throw SqlUtil.newContextException(create.name.getParserPosition(), RESOURCE.createTableRequiresColumnList());
        }
        builder.addAll(queryRowType.getFieldList());
    }
    final RelDataType rowType = builder.build();
    schema.add(create.name.getSimple(), new MutableArrayTable(create.name.getSimple(), RelDataTypeImpl.proto(rowType)));
    if (create.query != null) {
        populate(create.name, create.query, context);
    }
}
Also used : CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) ViewTableMacro(org.apache.calcite.schema.impl.ViewTableMacro) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) ContextSqlValidator(org.apache.calcite.jdbc.ContextSqlValidator) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) TranslatableTable(org.apache.calcite.schema.TranslatableTable) ContextSqlValidator(org.apache.calcite.jdbc.ContextSqlValidator)

Example 2 with ContextSqlValidator

use of org.apache.calcite.jdbc.ContextSqlValidator in project dingo by dingodb.

the class DingoDdlExecutor method execute.

@SuppressWarnings({ "unused", "MethodMayBeStatic" })
public void execute(SqlCreateTable create, CalcitePrepare.Context context) {
    log.info("DDL execute: {}", create);
    final String tableName = getTableName(create.name, context);
    TableDefinition td = new TableDefinition(tableName);
    List<String> keyList = null;
    SqlNodeList columnList = create.columnList;
    if (columnList == null) {
        throw SqlUtil.newContextException(create.name.getParserPosition(), RESOURCE.createTableRequiresColumnList());
    }
    for (SqlNode sqlNode : create.columnList) {
        if (sqlNode instanceof SqlKeyConstraint) {
            SqlKeyConstraint constraint = (SqlKeyConstraint) sqlNode;
            if (constraint.getOperator().getKind() == SqlKind.PRIMARY_KEY) {
                // The 0th element is the name of the constraint
                keyList = ((SqlNodeList) constraint.getOperandList().get(1)).getList().stream().map(t -> ((SqlIdentifier) Objects.requireNonNull(t)).getSimple()).collect(Collectors.toList());
                break;
            }
        }
    }
    SqlValidator validator = new ContextSqlValidator(context, true);
    for (SqlNode sqlNode : create.columnList) {
        if (sqlNode.getKind() == SqlKind.COLUMN_DECL) {
            SqlColumnDeclaration scd = (SqlColumnDeclaration) sqlNode;
            ColumnDefinition cd = fromSqlColumnDeclaration(scd, validator, keyList);
            td.addColumn(cd);
        }
    }
    if (td.getColumns().stream().noneMatch(ColumnDefinition::isPrimary)) {
        throw new RuntimeException("Not have primary key!");
    }
    final MutableSchema schema = getSchema(context);
    if (schema.getTable(tableName) != null) {
        if (!create.ifNotExists) {
            // They did not specify IF NOT EXISTS, so give error.
            throw SqlUtil.newContextException(create.name.getParserPosition(), RESOURCE.tableExists(tableName));
        }
    }
    schema.createTable(tableName, td);
}
Also used : SqlValidator(org.apache.calcite.sql.validate.SqlValidator) ContextSqlValidator(org.apache.calcite.jdbc.ContextSqlValidator) TableDefinition(io.dingodb.common.table.TableDefinition) SqlNodeList(org.apache.calcite.sql.SqlNodeList) ContextSqlValidator(org.apache.calcite.jdbc.ContextSqlValidator) SqlColumnDeclaration(org.apache.calcite.sql.ddl.SqlColumnDeclaration) SqlKeyConstraint(org.apache.calcite.sql.ddl.SqlKeyConstraint) SqlNode(org.apache.calcite.sql.SqlNode) ColumnDefinition(io.dingodb.common.table.ColumnDefinition)

Aggregations

ContextSqlValidator (org.apache.calcite.jdbc.ContextSqlValidator)2 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)2 ColumnDefinition (io.dingodb.common.table.ColumnDefinition)1 TableDefinition (io.dingodb.common.table.TableDefinition)1 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)1 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 TranslatableTable (org.apache.calcite.schema.TranslatableTable)1 ViewTableMacro (org.apache.calcite.schema.impl.ViewTableMacro)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlNodeList (org.apache.calcite.sql.SqlNodeList)1 SqlColumnDeclaration (org.apache.calcite.sql.ddl.SqlColumnDeclaration)1 SqlKeyConstraint (org.apache.calcite.sql.ddl.SqlKeyConstraint)1