Maxwell-Guo commented on code in PR #3718:
URL: https://github.com/apache/cassandra/pull/3718#discussion_r1871467389


##########
src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.statements.schema;
+
+import org.apache.cassandra.audit.AuditLogContext;
+import org.apache.cassandra.audit.AuditLogEntryType;
+import org.apache.cassandra.auth.Permission;
+import org.apache.cassandra.cql3.CQLStatement;
+import org.apache.cassandra.cql3.QualifiedName;
+import org.apache.cassandra.db.guardrails.Guardrails;
+import org.apache.cassandra.exceptions.AlreadyExistsException;
+import org.apache.cassandra.schema.Indexes;
+import org.apache.cassandra.schema.KeyspaceMetadata;
+import org.apache.cassandra.schema.Keyspaces;
+import org.apache.cassandra.schema.TableId;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.schema.Triggers;
+import org.apache.cassandra.schema.UserFunctions;
+import org.apache.cassandra.service.ClientState;
+import org.apache.cassandra.service.reads.repair.ReadRepairStrategy;
+import org.apache.cassandra.tcm.ClusterMetadata;
+import org.apache.cassandra.transport.Event.SchemaChange;
+
+public final class CopyTableStatement extends AlterSchemaStatement
+{
+    private final String sourceKeyspace;
+    private final String sourceTableName;
+    private final String targetKeyspace;
+    private final String targetTableName;
+
+    public CopyTableStatement(String sourceKeyspace,
+                              String targetKeyspace,
+                              String sourceTableName,
+                              String targetTableName)
+    {
+        super(targetKeyspace);
+        this.sourceKeyspace = sourceKeyspace;
+        this.targetKeyspace = targetKeyspace;
+        this.sourceTableName = sourceTableName;
+        this.targetTableName = targetTableName;
+    }
+
+    @Override
+    SchemaChange schemaChangeEvent(Keyspaces.KeyspacesDiff diff)
+    {
+        return new SchemaChange(SchemaChange.Change.CREATED, 
SchemaChange.Target.TABLE, targetKeyspace, targetTableName);
+    }
+
+    @Override
+    public void authorize(ClientState client)
+    {
+        client.ensureTablePermission(sourceKeyspace, sourceTableName, 
Permission.SELECT);
+        client.ensureAllTablesPermission(targetKeyspace, Permission.CREATE);
+    }
+
+    @Override
+    public AuditLogContext getAuditLogContext()
+    {
+        return new AuditLogContext(AuditLogEntryType.CREATE_TABLE_LIKE, 
targetKeyspace, targetTableName);
+    }
+
+    public String toString()
+    {
+        return String.format("CREATE TABLE %s.%s LIKE %s.%s", targetKeyspace, 
targetTableName, sourceKeyspace, sourceTableName);
+    }
+
+    @Override
+    public void validate(ClientState state)

Review Comment:
   yes , we can remove it.



##########
src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.statements.schema;
+
+import org.apache.cassandra.audit.AuditLogContext;
+import org.apache.cassandra.audit.AuditLogEntryType;
+import org.apache.cassandra.auth.Permission;
+import org.apache.cassandra.cql3.CQLStatement;
+import org.apache.cassandra.cql3.QualifiedName;
+import org.apache.cassandra.db.guardrails.Guardrails;
+import org.apache.cassandra.exceptions.AlreadyExistsException;
+import org.apache.cassandra.schema.Indexes;
+import org.apache.cassandra.schema.KeyspaceMetadata;
+import org.apache.cassandra.schema.Keyspaces;
+import org.apache.cassandra.schema.TableId;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.schema.Triggers;
+import org.apache.cassandra.schema.UserFunctions;
+import org.apache.cassandra.service.ClientState;
+import org.apache.cassandra.service.reads.repair.ReadRepairStrategy;
+import org.apache.cassandra.tcm.ClusterMetadata;
+import org.apache.cassandra.transport.Event.SchemaChange;
+
+public final class CopyTableStatement extends AlterSchemaStatement
+{
+    private final String sourceKeyspace;
+    private final String sourceTableName;
+    private final String targetKeyspace;
+    private final String targetTableName;
+
+    public CopyTableStatement(String sourceKeyspace,
+                              String targetKeyspace,
+                              String sourceTableName,
+                              String targetTableName)
+    {
+        super(targetKeyspace);
+        this.sourceKeyspace = sourceKeyspace;
+        this.targetKeyspace = targetKeyspace;
+        this.sourceTableName = sourceTableName;
+        this.targetTableName = targetTableName;
+    }
+
+    @Override
+    SchemaChange schemaChangeEvent(Keyspaces.KeyspacesDiff diff)
+    {
+        return new SchemaChange(SchemaChange.Change.CREATED, 
SchemaChange.Target.TABLE, targetKeyspace, targetTableName);
+    }
+
+    @Override
+    public void authorize(ClientState client)
+    {
+        client.ensureTablePermission(sourceKeyspace, sourceTableName, 
Permission.SELECT);
+        client.ensureAllTablesPermission(targetKeyspace, Permission.CREATE);
+    }
+
+    @Override
+    public AuditLogContext getAuditLogContext()
+    {
+        return new AuditLogContext(AuditLogEntryType.CREATE_TABLE_LIKE, 
targetKeyspace, targetTableName);
+    }
+
+    public String toString()
+    {
+        return String.format("CREATE TABLE %s.%s LIKE %s.%s", targetKeyspace, 
targetTableName, sourceKeyspace, sourceTableName);
+    }
+
+    @Override
+    public void validate(ClientState state)
+    {
+        super.validate(state);
+    }
+
+    @Override
+    public Keyspaces apply(ClusterMetadata metadata)
+    {
+        Keyspaces schema = metadata.schema.getKeyspaces();
+        KeyspaceMetadata sourceKeyspaceMeta = 
schema.getNullable(sourceKeyspace);
+        TableMetadata sourceTableMeta = 
sourceKeyspaceMeta.getTableNullable(sourceTableName);
+
+        if (null == sourceKeyspaceMeta)
+            throw ire("Source Keyspace '%s' doesn't exist", sourceKeyspace);
+
+        if (null == sourceTableMeta)
+        {

Review Comment:
   ok



##########
src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.statements.schema;
+
+import org.apache.cassandra.audit.AuditLogContext;
+import org.apache.cassandra.audit.AuditLogEntryType;
+import org.apache.cassandra.auth.Permission;
+import org.apache.cassandra.cql3.CQLStatement;
+import org.apache.cassandra.cql3.QualifiedName;
+import org.apache.cassandra.db.guardrails.Guardrails;
+import org.apache.cassandra.exceptions.AlreadyExistsException;
+import org.apache.cassandra.schema.Indexes;
+import org.apache.cassandra.schema.KeyspaceMetadata;
+import org.apache.cassandra.schema.Keyspaces;
+import org.apache.cassandra.schema.TableId;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.schema.Triggers;
+import org.apache.cassandra.schema.UserFunctions;
+import org.apache.cassandra.service.ClientState;
+import org.apache.cassandra.service.reads.repair.ReadRepairStrategy;
+import org.apache.cassandra.tcm.ClusterMetadata;
+import org.apache.cassandra.transport.Event.SchemaChange;
+
+public final class CopyTableStatement extends AlterSchemaStatement
+{
+    private final String sourceKeyspace;
+    private final String sourceTableName;
+    private final String targetKeyspace;
+    private final String targetTableName;
+
+    public CopyTableStatement(String sourceKeyspace,
+                              String targetKeyspace,
+                              String sourceTableName,
+                              String targetTableName)
+    {
+        super(targetKeyspace);
+        this.sourceKeyspace = sourceKeyspace;
+        this.targetKeyspace = targetKeyspace;
+        this.sourceTableName = sourceTableName;
+        this.targetTableName = targetTableName;
+    }
+
+    @Override
+    SchemaChange schemaChangeEvent(Keyspaces.KeyspacesDiff diff)
+    {
+        return new SchemaChange(SchemaChange.Change.CREATED, 
SchemaChange.Target.TABLE, targetKeyspace, targetTableName);
+    }
+
+    @Override
+    public void authorize(ClientState client)
+    {
+        client.ensureTablePermission(sourceKeyspace, sourceTableName, 
Permission.SELECT);
+        client.ensureAllTablesPermission(targetKeyspace, Permission.CREATE);
+    }
+
+    @Override
+    public AuditLogContext getAuditLogContext()
+    {
+        return new AuditLogContext(AuditLogEntryType.CREATE_TABLE_LIKE, 
targetKeyspace, targetTableName);
+    }
+
+    public String toString()
+    {
+        return String.format("CREATE TABLE %s.%s LIKE %s.%s", targetKeyspace, 
targetTableName, sourceKeyspace, sourceTableName);
+    }
+
+    @Override
+    public void validate(ClientState state)
+    {
+        super.validate(state);
+    }
+
+    @Override
+    public Keyspaces apply(ClusterMetadata metadata)
+    {
+        Keyspaces schema = metadata.schema.getKeyspaces();
+        KeyspaceMetadata sourceKeyspaceMeta = 
schema.getNullable(sourceKeyspace);
+        TableMetadata sourceTableMeta = 
sourceKeyspaceMeta.getTableNullable(sourceTableName);
+
+        if (null == sourceKeyspaceMeta)
+            throw ire("Source Keyspace '%s' doesn't exist", sourceKeyspace);
+
+        if (null == sourceTableMeta)
+        {
+            throw ire("Souce Table '%s'.'%s' doesn't exist", sourceKeyspace, 
sourceTableName);
+        }
+
+        if (sourceTableMeta.isIndex())
+            throw ire("Cannot use CTREATE TABLE LIKE on a index table 
'%s'.'%s'.", sourceKeyspace, sourceTableName);
+
+        if (sourceTableMeta.isView())
+            throw ire("Cannot use CTREATE TABLE LIKE on a materialized view 
'%s'.'%s'.", sourceKeyspace, sourceTableName);
+
+        KeyspaceMetadata targetKeyspaceMeta = 
schema.getNullable(targetKeyspace);
+        if (null == targetKeyspaceMeta)
+            throw ire("Target Keyspace '%s' doesn't exist", targetKeyspace);
+
+        if (targetKeyspaceMeta.hasTable(targetTableName))
+        {
+            throw new AlreadyExistsException(targetKeyspace, targetTableName);
+        }
+
+        // todo support udt for differenet ks latter
+        if (!sourceKeyspace.equalsIgnoreCase(targetKeyspace) && 
!sourceKeyspaceMeta.types.isEmpty())
+        {

Review Comment:
   ok



##########
src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.statements.schema;
+
+import org.apache.cassandra.audit.AuditLogContext;
+import org.apache.cassandra.audit.AuditLogEntryType;
+import org.apache.cassandra.auth.Permission;
+import org.apache.cassandra.cql3.CQLStatement;
+import org.apache.cassandra.cql3.QualifiedName;
+import org.apache.cassandra.db.guardrails.Guardrails;
+import org.apache.cassandra.exceptions.AlreadyExistsException;
+import org.apache.cassandra.schema.Indexes;
+import org.apache.cassandra.schema.KeyspaceMetadata;
+import org.apache.cassandra.schema.Keyspaces;
+import org.apache.cassandra.schema.TableId;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.schema.Triggers;
+import org.apache.cassandra.schema.UserFunctions;
+import org.apache.cassandra.service.ClientState;
+import org.apache.cassandra.service.reads.repair.ReadRepairStrategy;
+import org.apache.cassandra.tcm.ClusterMetadata;
+import org.apache.cassandra.transport.Event.SchemaChange;
+
+public final class CopyTableStatement extends AlterSchemaStatement
+{
+    private final String sourceKeyspace;
+    private final String sourceTableName;
+    private final String targetKeyspace;
+    private final String targetTableName;
+
+    public CopyTableStatement(String sourceKeyspace,
+                              String targetKeyspace,
+                              String sourceTableName,
+                              String targetTableName)
+    {
+        super(targetKeyspace);
+        this.sourceKeyspace = sourceKeyspace;
+        this.targetKeyspace = targetKeyspace;
+        this.sourceTableName = sourceTableName;
+        this.targetTableName = targetTableName;
+    }
+
+    @Override
+    SchemaChange schemaChangeEvent(Keyspaces.KeyspacesDiff diff)
+    {
+        return new SchemaChange(SchemaChange.Change.CREATED, 
SchemaChange.Target.TABLE, targetKeyspace, targetTableName);
+    }
+
+    @Override
+    public void authorize(ClientState client)
+    {
+        client.ensureTablePermission(sourceKeyspace, sourceTableName, 
Permission.SELECT);
+        client.ensureAllTablesPermission(targetKeyspace, Permission.CREATE);
+    }
+
+    @Override
+    public AuditLogContext getAuditLogContext()
+    {
+        return new AuditLogContext(AuditLogEntryType.CREATE_TABLE_LIKE, 
targetKeyspace, targetTableName);
+    }
+
+    public String toString()
+    {
+        return String.format("CREATE TABLE %s.%s LIKE %s.%s", targetKeyspace, 
targetTableName, sourceKeyspace, sourceTableName);
+    }
+
+    @Override
+    public void validate(ClientState state)
+    {
+        super.validate(state);
+    }
+
+    @Override
+    public Keyspaces apply(ClusterMetadata metadata)
+    {
+        Keyspaces schema = metadata.schema.getKeyspaces();
+        KeyspaceMetadata sourceKeyspaceMeta = 
schema.getNullable(sourceKeyspace);
+        TableMetadata sourceTableMeta = 
sourceKeyspaceMeta.getTableNullable(sourceTableName);
+
+        if (null == sourceKeyspaceMeta)
+            throw ire("Source Keyspace '%s' doesn't exist", sourceKeyspace);
+
+        if (null == sourceTableMeta)
+        {
+            throw ire("Souce Table '%s'.'%s' doesn't exist", sourceKeyspace, 
sourceTableName);
+        }
+
+        if (sourceTableMeta.isIndex())
+            throw ire("Cannot use CTREATE TABLE LIKE on a index table 
'%s'.'%s'.", sourceKeyspace, sourceTableName);
+
+        if (sourceTableMeta.isView())
+            throw ire("Cannot use CTREATE TABLE LIKE on a materialized view 
'%s'.'%s'.", sourceKeyspace, sourceTableName);
+
+        KeyspaceMetadata targetKeyspaceMeta = 
schema.getNullable(targetKeyspace);
+        if (null == targetKeyspaceMeta)
+            throw ire("Target Keyspace '%s' doesn't exist", targetKeyspace);
+
+        if (targetKeyspaceMeta.hasTable(targetTableName))
+        {
+            throw new AlreadyExistsException(targetKeyspace, targetTableName);
+        }
+
+        // todo support udt for differenet ks latter
+        if (!sourceKeyspace.equalsIgnoreCase(targetKeyspace) && 
!sourceKeyspaceMeta.types.isEmpty())
+        {
+            throw ire("Cannot use CTREATE TABLE LIKE across different keyspace 
when source table have UDTs.");
+        }
+
+        String sourceCQLString = sourceTableMeta.toCqlString(false, false, 
true, false);
+        // add all user functions to be able to give a good error message to 
the user if the alter references
+        // a function from another keyspace
+        UserFunctions.Builder ufBuilder = UserFunctions.builder().add();
+        for (KeyspaceMetadata ksm : schema)
+            ufBuilder.add(ksm.userFunctions);
+
+        //todo support table params' setting in the future
+        TableMetadata.Builder targetBuilder = 
CreateTableStatement.parse(sourceCQLString,
+                                                                         
targetKeyspace,
+                                                                         
targetTableName,
+                                                                         
sourceKeyspaceMeta.types,
+                                                                         
ufBuilder.build())
+                                                                  
.indexes(Indexes.none())
+                                                                  
.triggers(Triggers.none());
+
+        TableMetadata table = targetBuilder.id(TableId.get(metadata)).build();

Review Comment:
   yes, I notice this. I kept it the first time, but I found that this CQL is 
actually the create cql of the like table, that is like:
    `create table like tb (pk int primay key, cn) with xxxx`  
   .But in fact, the CQL statement of the copytable statement should be
   `create table like tb like source tb`.
   
   So I removed expandedCql beforce pull 



##########
src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3.statements.schema;
+
+import org.apache.cassandra.audit.AuditLogContext;
+import org.apache.cassandra.audit.AuditLogEntryType;
+import org.apache.cassandra.auth.Permission;
+import org.apache.cassandra.cql3.CQLStatement;
+import org.apache.cassandra.cql3.QualifiedName;
+import org.apache.cassandra.db.guardrails.Guardrails;
+import org.apache.cassandra.exceptions.AlreadyExistsException;
+import org.apache.cassandra.schema.Indexes;
+import org.apache.cassandra.schema.KeyspaceMetadata;
+import org.apache.cassandra.schema.Keyspaces;
+import org.apache.cassandra.schema.TableId;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.schema.Triggers;
+import org.apache.cassandra.schema.UserFunctions;
+import org.apache.cassandra.service.ClientState;
+import org.apache.cassandra.service.reads.repair.ReadRepairStrategy;
+import org.apache.cassandra.tcm.ClusterMetadata;
+import org.apache.cassandra.transport.Event.SchemaChange;
+
+public final class CopyTableStatement extends AlterSchemaStatement
+{
+    private final String sourceKeyspace;
+    private final String sourceTableName;
+    private final String targetKeyspace;
+    private final String targetTableName;
+
+    public CopyTableStatement(String sourceKeyspace,
+                              String targetKeyspace,
+                              String sourceTableName,
+                              String targetTableName)
+    {
+        super(targetKeyspace);
+        this.sourceKeyspace = sourceKeyspace;
+        this.targetKeyspace = targetKeyspace;
+        this.sourceTableName = sourceTableName;
+        this.targetTableName = targetTableName;
+    }
+
+    @Override
+    SchemaChange schemaChangeEvent(Keyspaces.KeyspacesDiff diff)
+    {
+        return new SchemaChange(SchemaChange.Change.CREATED, 
SchemaChange.Target.TABLE, targetKeyspace, targetTableName);
+    }
+
+    @Override
+    public void authorize(ClientState client)
+    {
+        client.ensureTablePermission(sourceKeyspace, sourceTableName, 
Permission.SELECT);
+        client.ensureAllTablesPermission(targetKeyspace, Permission.CREATE);
+    }
+
+    @Override
+    public AuditLogContext getAuditLogContext()
+    {
+        return new AuditLogContext(AuditLogEntryType.CREATE_TABLE_LIKE, 
targetKeyspace, targetTableName);
+    }
+
+    public String toString()
+    {
+        return String.format("CREATE TABLE %s.%s LIKE %s.%s", targetKeyspace, 
targetTableName, sourceKeyspace, sourceTableName);
+    }
+
+    @Override
+    public void validate(ClientState state)
+    {
+        super.validate(state);
+    }
+
+    @Override
+    public Keyspaces apply(ClusterMetadata metadata)
+    {
+        Keyspaces schema = metadata.schema.getKeyspaces();
+        KeyspaceMetadata sourceKeyspaceMeta = 
schema.getNullable(sourceKeyspace);
+        TableMetadata sourceTableMeta = 
sourceKeyspaceMeta.getTableNullable(sourceTableName);
+
+        if (null == sourceKeyspaceMeta)
+            throw ire("Source Keyspace '%s' doesn't exist", sourceKeyspace);
+
+        if (null == sourceTableMeta)
+        {
+            throw ire("Souce Table '%s'.'%s' doesn't exist", sourceKeyspace, 
sourceTableName);
+        }
+
+        if (sourceTableMeta.isIndex())
+            throw ire("Cannot use CTREATE TABLE LIKE on a index table 
'%s'.'%s'.", sourceKeyspace, sourceTableName);
+
+        if (sourceTableMeta.isView())
+            throw ire("Cannot use CTREATE TABLE LIKE on a materialized view 
'%s'.'%s'.", sourceKeyspace, sourceTableName);
+
+        KeyspaceMetadata targetKeyspaceMeta = 
schema.getNullable(targetKeyspace);
+        if (null == targetKeyspaceMeta)
+            throw ire("Target Keyspace '%s' doesn't exist", targetKeyspace);
+
+        if (targetKeyspaceMeta.hasTable(targetTableName))
+        {
+            throw new AlreadyExistsException(targetKeyspace, targetTableName);
+        }
+
+        // todo support udt for differenet ks latter
+        if (!sourceKeyspace.equalsIgnoreCase(targetKeyspace) && 
!sourceKeyspaceMeta.types.isEmpty())
+        {
+            throw ire("Cannot use CTREATE TABLE LIKE across different keyspace 
when source table have UDTs.");
+        }
+
+        String sourceCQLString = sourceTableMeta.toCqlString(false, false, 
true, false);
+        // add all user functions to be able to give a good error message to 
the user if the alter references
+        // a function from another keyspace
+        UserFunctions.Builder ufBuilder = UserFunctions.builder().add();
+        for (KeyspaceMetadata ksm : schema)
+            ufBuilder.add(ksm.userFunctions);
+
+        //todo support table params' setting in the future
+        TableMetadata.Builder targetBuilder = 
CreateTableStatement.parse(sourceCQLString,
+                                                                         
targetKeyspace,
+                                                                         
targetTableName,
+                                                                         
sourceKeyspaceMeta.types,
+                                                                         
ufBuilder.build())
+                                                                  
.indexes(Indexes.none())
+                                                                  
.triggers(Triggers.none());
+
+        TableMetadata table = targetBuilder.id(TableId.get(metadata)).build();

Review Comment:
   > you are not doing it like it is in CreateTableStatement
   > 
   > ```
   >         // We do not want to set table ID here just yet, since we are 
using CQL for serialising a fully expanded CREATE TABLE statement.
   >         this.expandedCql = builder.build().toCqlString(false, 
attrs.hasProperty(TableAttributes.ID), ifNotExists);
   > 
   >         if (!attrs.hasProperty(TableAttributes.ID))
   >             builder.id(TableId.get(metadata));
   >         TableMetadata table = builder.build();
   >         table.validate();
   > ```
   > 
   > you dont deal with `expandedCql`
   
   for copytablestatement ,we are not going to have  CQL like :
   `CREATE TABLE %%s (a int, b int, c int, PRIMARY KEY(a, b)) WITH ID = %s` 
   
   at this time (we will support table params' cloning in the future), so I 
remove 
   `if (!attrs.hasProperty(TableAttributes.ID))`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to