use of org.apache.calcite.sql.ddl.SqlKeyConstraint 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);
}
use of org.apache.calcite.sql.ddl.SqlKeyConstraint in project ignite-3 by apache.
the class DdlSqlToCommandConverter method convertCreateTable.
/**
* Converts a given CreateTable AST to a CreateTable command.
*
* @param createTblNode Root node of the given AST.
* @param ctx Planning context.
*/
private CreateTableCommand convertCreateTable(IgniteSqlCreateTable createTblNode, PlanningContext ctx) {
CreateTableCommand createTblCmd = new CreateTableCommand();
createTblCmd.schemaName(deriveSchemaName(createTblNode.name(), ctx));
createTblCmd.tableName(deriveObjectName(createTblNode.name(), ctx, "tableName"));
createTblCmd.ifTableExists(createTblNode.ifNotExists());
if (createTblNode.createOptionList() != null) {
for (SqlNode optNode : createTblNode.createOptionList().getList()) {
IgniteSqlCreateTableOption opt = (IgniteSqlCreateTableOption) optNode;
tblOptionProcessors.getOrDefault(opt.key(), UNSUPPORTED_OPTION_PROCESSOR).process(opt, ctx, createTblCmd);
}
}
List<SqlKeyConstraint> pkConstraints = createTblNode.columnList().getList().stream().filter(SqlKeyConstraint.class::isInstance).map(SqlKeyConstraint.class::cast).collect(Collectors.toList());
if (nullOrEmpty(pkConstraints)) {
throw new IgniteException("Table without PRIMARY KEY is not supported");
} else if (pkConstraints.size() > 1) {
throw new IgniteException("Unexpected amount of primary key constraints [" + "expected at most one, but was " + pkConstraints.size() + "; " + "querySql=\"" + ctx.query() + "\"]");
}
Set<String> dedupSetPk = new HashSet<>();
List<String> pkCols = pkConstraints.stream().map(pk -> pk.getOperandList().get(1)).map(SqlNodeList.class::cast).flatMap(l -> l.getList().stream()).map(SqlIdentifier.class::cast).map(SqlIdentifier::getSimple).filter(dedupSetPk::add).collect(Collectors.toList());
createTblCmd.primaryKeyColumns(pkCols);
List<SqlColumnDeclaration> colDeclarations = createTblNode.columnList().getList().stream().filter(SqlColumnDeclaration.class::isInstance).map(SqlColumnDeclaration.class::cast).collect(Collectors.toList());
IgnitePlanner planner = ctx.planner();
List<ColumnDefinition> cols = new ArrayList<>(colDeclarations.size());
for (SqlColumnDeclaration col : colDeclarations) {
if (!col.name.isSimple()) {
throw new IgniteException("Unexpected value of columnName [" + "expected a simple identifier, but was " + col.name + "; " + "querySql=\"" + ctx.query() + "\"]");
}
String name = col.name.getSimple();
if (col.dataType.getNullable() != null && col.dataType.getNullable() && dedupSetPk.contains(name)) {
throw new IgniteException("Primary key cannot contain nullable column [col=" + name + "]");
}
RelDataType relType = planner.convert(col.dataType, !dedupSetPk.contains(name));
dedupSetPk.remove(name);
Object dflt = null;
if (col.expression != null) {
dflt = ((SqlLiteral) col.expression).getValue();
}
cols.add(new ColumnDefinition(name, relType, dflt));
}
if (!dedupSetPk.isEmpty()) {
throw new IgniteException("Primary key constrain contains undefined columns: [cols=" + dedupSetPk + "]");
}
createTblCmd.columns(cols);
return createTblCmd;
}
Aggregations