You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "smiklosovic (via GitHub)" <gi...@apache.org> on 2024/12/03 18:20:58 UTC
Re: [PR] CASSANDRA-19964 second approach - CREATE TABLE LIKE for copying Basic Table definition using create table parser [cassandra]
smiklosovic commented on code in PR #3721:
URL: https://github.com/apache/cassandra/pull/3721#discussion_r1868202091
##########
src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java:
##########
@@ -104,34 +113,77 @@ public Keyspaces apply(ClusterMetadata metadata)
{
Keyspaces schema = metadata.schema.getKeyspaces();
KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);
+
+ KeyspaceMetadata targetKeyspace = keyspace;
if (null == keyspace)
throw ire("Keyspace '%s' doesn't exist", keyspaceName);
- if (keyspace.hasTable(tableName))
+ TableMetadata table;
+ if (isCreateLike)
{
- if (ifNotExists)
- return schema;
+ // when using create table like ,that means the source table has already been created
+ // so there is no need to do some validation for the first time creating a new table
+ if (!keyspace.hasTable(tableName))
+ {
+ throw ire("Souce Table '%s'.'%s' doesn't exist", keyspaceName, tableName);
+ }
+
+ TableMetadata sourceTableMeta = keyspace.getTableNullable(tableName);
+
+ if (sourceTableMeta.isIndex())
+ throw ire("Cannot use CTREAT TABLE LIKE on a index table '%s'.'%s'.", keyspaceName, tableName);
+
+ if (sourceTableMeta.isView())
+ throw ire("Cannot use CTREAT TABLE LIKE on a materialized view '%s'.'%s'.", keyspaceName, tableName);
+
+ KeyspaceMetadata likeKeyspaceMeta = schema.getNullable(likeKeyspaceName);
+ if (null == likeKeyspaceMeta)
+ throw ire("Target Keyspace '%s' doesn't exist", likeKeyspaceName);
+
+ targetKeyspace = likeKeyspaceMeta;
+ if (likeKeyspaceMeta.hasTable(likeTableName))
+ {
+ throw new AlreadyExistsException(likeKeyspaceName, likeTableName);
+ }
- throw new AlreadyExistsException(keyspaceName, tableName);
+ // todo support udt for differenet ks latter
+ if (!keyspaceName.equalsIgnoreCase(likeKeyspaceName) &&
+ !keyspace.types.isEmpty())
+ {
+ throw ire("Cannot use CTREAT TABLE LIKE across different keyspace when source table have UDT.");
+ }
+
+ table = buildTargetTableMeta(metadata, sourceTableMeta);
}
+ else
+ {
+ assert likeKeyspaceName == null && likeTableName == null ;
+ if (keyspace.hasTable(tableName))
+ {
+ if (ifNotExists)
+ return schema;
- // 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);
+ throw new AlreadyExistsException(keyspaceName, tableName);
+ }
- TableMetadata.Builder builder = builder(keyspace.types, ufBuilder.build()).epoch(metadata.nextEpoch());
+ // 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);
- // 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);
+ TableMetadata.Builder builder = builder(keyspace.types, ufBuilder.build()).epoch(metadata.nextEpoch());
- if (!attrs.hasProperty(TableAttributes.ID))
- builder.id(TableId.get(metadata));
- TableMetadata table = builder.build();
- table.validate();
+ // 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 (keyspace.replicationStrategy.hasTransientReplicas()
+ if (!attrs.hasProperty(TableAttributes.ID))
+ builder.id(TableId.get(metadata));
Review Comment:
I am not sure we need this, id should be automatically set when `build` is called.
##########
src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java:
##########
@@ -104,34 +113,77 @@ public Keyspaces apply(ClusterMetadata metadata)
{
Keyspaces schema = metadata.schema.getKeyspaces();
KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);
+
+ KeyspaceMetadata targetKeyspace = keyspace;
if (null == keyspace)
throw ire("Keyspace '%s' doesn't exist", keyspaceName);
- if (keyspace.hasTable(tableName))
+ TableMetadata table;
+ if (isCreateLike)
{
- if (ifNotExists)
- return schema;
+ // when using create table like ,that means the source table has already been created
+ // so there is no need to do some validation for the first time creating a new table
+ if (!keyspace.hasTable(tableName))
+ {
+ throw ire("Souce Table '%s'.'%s' doesn't exist", keyspaceName, tableName);
+ }
+
+ TableMetadata sourceTableMeta = keyspace.getTableNullable(tableName);
+
+ if (sourceTableMeta.isIndex())
+ throw ire("Cannot use CTREAT TABLE LIKE on a index table '%s'.'%s'.", keyspaceName, tableName);
+
+ if (sourceTableMeta.isView())
+ throw ire("Cannot use CTREAT TABLE LIKE on a materialized view '%s'.'%s'.", keyspaceName, tableName);
+
+ KeyspaceMetadata likeKeyspaceMeta = schema.getNullable(likeKeyspaceName);
+ if (null == likeKeyspaceMeta)
+ throw ire("Target Keyspace '%s' doesn't exist", likeKeyspaceName);
+
+ targetKeyspace = likeKeyspaceMeta;
+ if (likeKeyspaceMeta.hasTable(likeTableName))
+ {
+ throw new AlreadyExistsException(likeKeyspaceName, likeTableName);
+ }
- throw new AlreadyExistsException(keyspaceName, tableName);
+ // todo support udt for differenet ks latter
+ if (!keyspaceName.equalsIgnoreCase(likeKeyspaceName) &&
+ !keyspace.types.isEmpty())
+ {
+ throw ire("Cannot use CTREAT TABLE LIKE across different keyspace when source table have UDT.");
+ }
+
+ table = buildTargetTableMeta(metadata, sourceTableMeta);
}
+ else
+ {
+ assert likeKeyspaceName == null && likeTableName == null ;
+ if (keyspace.hasTable(tableName))
+ {
+ if (ifNotExists)
+ return schema;
- // 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);
+ throw new AlreadyExistsException(keyspaceName, tableName);
+ }
- TableMetadata.Builder builder = builder(keyspace.types, ufBuilder.build()).epoch(metadata.nextEpoch());
+ // 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);
- // 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);
+ TableMetadata.Builder builder = builder(keyspace.types, ufBuilder.build()).epoch(metadata.nextEpoch());
- if (!attrs.hasProperty(TableAttributes.ID))
- builder.id(TableId.get(metadata));
- TableMetadata table = builder.build();
- table.validate();
+ // 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 (keyspace.replicationStrategy.hasTransientReplicas()
+ if (!attrs.hasProperty(TableAttributes.ID))
+ builder.id(TableId.get(metadata));
Review Comment:
I am not sure we need this, id should be automatically set when `build` is called.
--
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-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org