@Override protected Void visitRenameColumn(RenameColumn node, Integer context) { builder.append("ALTER TABLE ") .append(node.getTable()) .append(" RENAME COLUMN ") .append(node.getSource()) .append(" TO ") .append(node.getTarget()); return null; }
@Override public CompletableFuture<?> execute(RenameColumn statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine) { Session session = stateMachine.getSession(); QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTable()); Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName); String source = statement.getSource().toLowerCase(ENGLISH); String target = statement.getTarget().toLowerCase(ENGLISH); if (!tableHandle.isPresent()) { throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName); } accessControl.checkCanRenameColumn(session.getRequiredTransactionId(), session.getIdentity(), tableName); Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle.get()); if (!columnHandles.containsKey(source)) { throw new SemanticException(MISSING_COLUMN, statement, "Column '%s' does not exist", source); } if (columnHandles.containsKey(target)) { throw new SemanticException(COLUMN_ALREADY_EXISTS, statement, "Column '%s' already exists", target); } metadata.renameColumn(session, tableHandle.get(), columnHandles.get(source), target); return completedFuture(null); }
@Override public Node visitRenameColumn(SqlBaseParser.RenameColumnContext context) { return new RenameColumn(getLocation(context), getQualifiedName(context.tableName), context.from.getText(), context.to.getText()); }
@Test public void testRenameColumn() throws Exception { assertStatement("ALTER TABLE foo.t RENAME COLUMN a TO b", new RenameColumn(QualifiedName.of("foo", "t"), "a", "b")); }