@Override public CompletableFuture<?> execute(RenameTable statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine) { Session session = stateMachine.getSession(); QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getSource()); Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName); if (!tableHandle.isPresent()) { throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName); } QualifiedObjectName target = createQualifiedObjectName(session, statement, statement.getTarget()); if (!metadata.getCatalogNames().containsKey(target.getCatalogName())) { throw new SemanticException(MISSING_CATALOG, statement, "Target catalog '%s' does not exist", target.getCatalogName()); } if (metadata.getTableHandle(session, target).isPresent()) { throw new SemanticException(TABLE_ALREADY_EXISTS, statement, "Target table '%s' already exists", target); } if (!tableName.getCatalogName().equals(target.getCatalogName())) { throw new SemanticException(NOT_SUPPORTED, statement, "Table rename across catalogs is not supported"); } accessControl.checkCanRenameTable(session.getRequiredTransactionId(), session.getIdentity(), tableName, target); metadata.renameTable(session, tableHandle.get(), target); return completedFuture(null); }
@Override protected Void visitRenameTable(RenameTable node, Integer context) { builder.append("ALTER TABLE ") .append(node.getSource()) .append(" RENAME TO ") .append(node.getTarget()); return null; }
@Override public Node visitRenameTable(SqlBaseParser.RenameTableContext context) { return new RenameTable(getLocation(context), getQualifiedName(context.from), getQualifiedName(context.to)); }
@Test public void testRenameTable() throws Exception { assertStatement("ALTER TABLE a RENAME TO b", new RenameTable(QualifiedName.of("a"), QualifiedName.of("b"))); }
@Override protected CatalogSchemaContext visitRenameTable(RenameTable node, CatalogSchemaContext context) { references.add(qualifiedNameToTable(node.getSource(), context)); return context; }