@Override protected Void visitAddColumn(AddColumn node, Integer indent) { builder.append("ALTER TABLE ") .append(node.getName()) .append(" ADD COLUMN ") .append(node.getColumn().getName()) .append(" ") .append(node.getColumn().getType()); return null; }
@Override public CompletableFuture<?> execute(AddColumn statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine) { Session session = stateMachine.getSession(); QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getName()); Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName); if (!tableHandle.isPresent()) { throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName); } accessControl.checkCanAddColumns(session.getRequiredTransactionId(), session.getIdentity(), tableName); Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle.get()); TableElement element = statement.getColumn(); Type type = metadata.getType(parseTypeSignature(element.getType())); if ((type == null) || type.equals(UNKNOWN)) { throw new SemanticException(TYPE_MISMATCH, element, "Unknown type for column '%s' ", element.getName()); } if (columnHandles.containsKey(element.getName())) { throw new SemanticException(COLUMN_ALREADY_EXISTS, statement, "Column '%s' already exists", element.getName()); } metadata.addColumn(session, tableHandle.get(), new ColumnMetadata(element.getName(), type, false)); return completedFuture(null); }
@Override public Node visitAddColumn(SqlBaseParser.AddColumnContext context) { return new AddColumn(getLocation(context), getQualifiedName(context.qualifiedName()), (TableElement) visit(context.tableElement())); }
@Test public void testAddColumn() throws Exception { assertStatement("ALTER TABLE foo.t ADD COLUMN c bigint", new AddColumn(QualifiedName.of("foo", "t"), new TableElement("c", "bigint"))); }