smiklosovic commented on code in PR #3718: URL: https://github.com/apache/cassandra/pull/3718#discussion_r1888193591
########## test/unit/org/apache/cassandra/schema/createlike/CreateLikeTest.java: ########## @@ -0,0 +1,545 @@ +/* + * 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.schema.createlike; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import org.apache.cassandra.cql3.CQLTester; +import org.apache.cassandra.cql3.Duration; +import org.apache.cassandra.cql3.validation.operations.CreateTest; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.db.compaction.LeveledCompactionStrategy; +import org.apache.cassandra.exceptions.AlreadyExistsException; +import org.apache.cassandra.exceptions.InvalidRequestException; +import org.apache.cassandra.schema.CachingParams; +import org.apache.cassandra.schema.CompactionParams; +import org.apache.cassandra.schema.CompressionParams; +import org.apache.cassandra.schema.Indexes; +import org.apache.cassandra.schema.TableMetadata; +import org.apache.cassandra.schema.TableParams; +import org.apache.cassandra.service.reads.SpeculativeRetryPolicy; +import org.apache.cassandra.service.reads.repair.ReadRepairStrategy; +import org.apache.cassandra.utils.TimeUUID; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +@RunWith(Parameterized.class) +public class CreateLikeTest extends CQLTester +{ + @Parameterized.Parameter + public boolean differentKs; + + //todo support other parameter like indexs/trigger/all options copy + @Parameterized.Parameters(name = "{index}: differentKs={0}") + public static Collection<Object[]> data() + { + List<Object[]> result = new ArrayList<>(); + result.add(new Object[]{false}); + result.add(new Object[]{true}); + return result; + } + + private UUID uuid1 = UUID.fromString("62c3e96f-55cd-493b-8c8e-5a18883a1698"); + private UUID uuid2 = UUID.fromString("52c3e96f-55cd-493b-8c8e-5a18883a1698"); + private TimeUUID timeUuid1 = TimeUUID.fromString("00346642-2d2f-11ed-a261-0242ac120002"); + private TimeUUID timeUuid2 = TimeUUID.fromString("10346642-2d2f-11ed-a261-0242ac120002"); + private Duration duration1 = Duration.newInstance(1, 2, 3); + private Duration duration2 = Duration.newInstance(1, 2, 4); + private Date date1 = new Date(); + private Double d1 = Double.valueOf("1.1"); + private Double d2 = Double.valueOf("2.2"); + private Float f1 = Float.valueOf("3.33"); + private Float f2 = Float.valueOf("4.44"); + private BigDecimal decimal1 = BigDecimal.valueOf(1.1); + private BigDecimal decimal2 = BigDecimal.valueOf(2.2); + private Vector<Integer> vector1 = vector(1, 2); + private Vector<Integer> vector2 = vector(3, 4); + private String keyspace1 = "keyspace1"; + private String keyspace2 = "keyspace2"; + private String sourceKs; + private String targetKs; + + @Before + public void before() + { + createKeyspace("CREATE KEYSPACE " + keyspace1 + " WITH replication={ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }"); + createKeyspace("CREATE KEYSPACE " + keyspace2 + " WITH replication={ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }"); + sourceKs = keyspace1; + targetKs = differentKs ? keyspace2 : keyspace1; + } + + @Test + public void testTableShemaCopy() + { + String sourceTb = createTable(sourceKs, "CREATE TABLE %s (a int PRIMARY KEY, b duration, c text);"); + String targetTb = createTableLike("CREATE TABLE %s LIKE %s", sourceTb, sourceKs, targetKs); + assertTableMetaEquals(sourceKs, targetKs, sourceTb, targetTb); + execute("INSERT INTO " + sourceKs + "." + sourceTb + " (a, b, c) VALUES (?, ?, ?)", 1, duration1, "1"); + execute("INSERT INTO " + targetKs + "." + targetTb + " (a, b, c) VALUES (?, ?, ?)", 2, duration2, "2"); + assertRows(execute("SELECT * FROM " + sourceKs + "." + sourceTb), + row(1, duration1, "1")); + assertRows(execute("SELECT * FROM " + targetKs + "." + targetTb), + row(2, duration2, "2")); + + sourceTb = createTable(sourceKs, "CREATE TABLE %s (a int PRIMARY KEY);"); + targetTb = createTableLike("CREATE TABLE %s LIKE %s", sourceTb, sourceKs, targetKs); + assertTableMetaEquals(sourceKs, targetKs, sourceTb, targetTb); + execute("INSERT INTO " + sourceKs + "." + sourceTb + " (a) VALUES (1)"); + execute("INSERT INTO " + targetKs + "." + targetTb + " (a) VALUES (2)"); + assertRows(execute("SELECT * FROM " + sourceKs + "." + sourceTb), + row(1)); + assertRows(execute("SELECT * FROM " + targetKs + "." + targetTb), + row(2)); + + sourceTb = createTable(sourceKs, "CREATE TABLE %s (a frozen<map<text, text>> PRIMARY KEY);"); + targetTb = createTableLike("CREATE TABLE %s LIKE %s", sourceTb, sourceKs, targetKs); + assertTableMetaEquals(sourceKs, targetKs, sourceTb, targetTb); + execute("INSERT INTO " + sourceKs + "." + sourceTb + " (a) VALUES (?)", map("k", "v")); + execute("INSERT INTO " + targetKs + "." + targetTb + " (a) VALUES (?)", map("nk", "nv")); + assertRows(execute("SELECT * FROM " + sourceKs + "." + sourceTb), + row(map("k", "v"))); + assertRows(execute("SELECT * FROM " + targetKs + "." + targetTb), + row(map("nk", "nv"))); + + sourceTb = createTable(sourceKs, "CREATE TABLE %s (a int PRIMARY KEY, b set<frozen<list<text>>>, c map<text, int>, d smallint, e duration, f tinyint);"); + targetTb = createTableLike("CREATE TABLE %s LIKE %s", sourceTb, sourceKs, targetKs); + assertTableMetaEquals(sourceKs, targetKs, sourceTb, targetTb); + execute("INSERT INTO " + sourceKs + "." + sourceTb + " (a, b, c, d, e, f) VALUES (?, ?, ?, ?, ?, ?)", + 1, set(list("1", "2"), list("3", "4")), map("k", 1), (short)2, duration1, (byte)4); + execute("INSERT INTO " + targetKs + "." + targetTb + " (a, b, c, d, e, f) VALUES (?, ?, ?, ?, ?, ?)", + 2, set(list("5", "6"), list("7", "8")), map("nk", 2), (short)3, duration2, (byte)5); + assertRows(execute("SELECT * FROM " + sourceKs + "." + sourceTb), + row(1, set(list("1", "2"), list("3", "4")), map("k", 1), (short)2, duration1, (byte)4)); + assertRows(execute("SELECT * FROM " + targetKs + "." + targetTb), + row(2, set(list("5", "6"), list("7", "8")), map("nk", 2), (short)3, duration2, (byte)5)); + + sourceTb = createTable(sourceKs, "CREATE TABLE %s (a int , b double, c tinyint, d float, e list<text>, f map<text, int>, g duration, PRIMARY KEY((a, b, c), d));"); + targetTb = createTableLike("CREATE TABLE %s LIKE %s", sourceTb, sourceKs, targetKs); + assertTableMetaEquals(sourceKs, targetKs, sourceTb, targetTb); + execute("INSERT INTO " + sourceKs + "." + sourceTb + " (a, b, c, d, e, f, g) VALUES (?, ?, ?, ?, ?, ?, ?) ", + 1, d1, (byte)4, f1, list("a", "b"), map("k", 1), duration1); + execute("INSERT INTO " + targetKs + "." + targetTb + " (a, b, c, d, e, f, g) VALUES (?, ?, ?, ?, ?, ?, ?) ", + 2, d2, (byte)5, f2, list("c", "d"), map("nk", 2), duration2); + assertRows(execute("SELECT * FROM " + sourceKs + "." + sourceTb), + row(1, d1, (byte)4, f1, list("a", "b"), map("k", 1), duration1)); + assertRows(execute("SELECT * FROM " + targetKs + "." + targetTb), + row(2, d2, (byte)5, f2, list("c", "d"), map("nk", 2), duration2)); + + sourceTb = createTable(sourceKs, "CREATE TABLE %s (a int , " + + "b text, " + + "c bigint, " + + "d decimal, " + + "e set<text>, " + + "f uuid, " + + "g vector<int, 2>, " + + "h list<float>, " + + "i timeuuid, " + + "j map<text, frozen<set<int>>>, " + + "PRIMARY KEY((a, b), c, d)) " + + "WITH CLUSTERING ORDER BY (c DESC, d ASC);"); + targetTb = createTableLike("CREATE TABLE %s LIKE %s", sourceTb, sourceKs, targetKs); + assertTableMetaEquals(sourceKs, targetKs, sourceTb, targetTb); + execute("INSERT INTO " + sourceKs + "." + sourceTb + " (a, b, c, d, e, f, g, h, i, j) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + 1, "b", 100L, decimal1, set("1", "2"), uuid1, vector1, list(1.1F, 2.2F), timeUuid1, map("k", set(1, 2))); + execute("INSERT INTO " + targetKs + "." + targetTb + " (a, b, c, d, e, f, g, h, i, j) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + 2, "nb", 200L, decimal2, set("3", "4"), uuid2, vector2, list(3.3F, 4.4F), timeUuid2, map("nk", set(3, 4))); + assertRows(execute("SELECT * FROM " + sourceKs + "." + sourceTb), + row(1, "b", 100L, decimal1, set("1", "2"), uuid1, vector1, list(1.1F, 2.2F), timeUuid1, map("k", set(1, 2)))); + assertRows(execute("SELECT * FROM " + targetKs + "." + targetTb), + row(2, "nb", 200L, decimal2, set("3", "4"), uuid2, vector2, list(3.3F, 4.4F), timeUuid2, map("nk", set(3, 4)))); + } + + @Test + public void testIfNotExists() throws Throwable + { + String sourceTb = createTable(sourceKs, "CREATE TABLE %s (a int, b text, c duration, d float, PRIMARY KEY(a, b));"); + String targetTb = createTableLike("CREATE TABLE IF NOT EXISTS %s LIKE %s", sourceTb, sourceKs, targetKs); + assertTableMetaEquals(sourceKs, targetKs, sourceTb, targetTb); + + createTableLike("CREATE TABLE IF NOT EXISTS %s LIKE %s", sourceTb, sourceKs, targetTb, targetKs); + assertInvalidThrowMessage("Cannot add already existing table \"" + targetTb + "\" to keyspace \"" + targetKs + "\"", AlreadyExistsException.class, Review Comment: @Maxwell-Guo ```` // we create the source table first CREATE TABLE ks.tb ... // this will not fail CREATE TABLE ks.tb_copy LIKE ks.tb; // this will fail because we just created ks.tb_copy on the line above CREATE TABLE ks.tb_copy LIKE ks.tb; // this will not fail, because ks.tb_copy already exists, // so "IF NOT EXISTS" means that we are not going to create it if it exists, so no error CREATE TABLE IF NOT EXISTS ks.tb_copy LIKE ks.tb; ```` -- 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