JiajunBernoulli commented on code in PR #3442:
URL: https://github.com/apache/calcite/pull/3442#discussion_r1359275872
##########
server/src/test/java/org/apache/calcite/test/ServerTest.java:
##########
@@ -277,6 +279,156 @@ static Connection connect() throws SQLException {
}
}
+ @Test void testCreateTableLike() throws Exception {
+ try (Connection c = connect();
+ Statement s = c.createStatement()) {
+ s.execute("create table t (i int not null)");
+ s.execute("create table t2 like t");
+ int x = s.executeUpdate("insert into t2 values 1");
+ assertThat(x, is(1));
+ x = s.executeUpdate("insert into t2 values 3");
+ assertThat(x, is(1));
+ try (ResultSet r = s.executeQuery("select sum(i) from t2")) {
+ assertThat(r.next(), is(true));
+ assertThat(r.getInt(1), is(4));
+ assertThat(r.next(), is(false));
+ }
+ }
+ }
+
+ @Test void testCreateTableLikeWithStoredGeneratedColumn() throws Exception {
+ try (Connection c = connect();
+ Statement s = c.createStatement()) {
+ s.execute("create table t (\n"
+ + " h int not null,\n"
+ + " i int,\n"
+ + " j int as (i + 1) stored,\n"
+ + " k int default -1)\n");
+ s.execute("create table t2 like t including defaults including
generated");
+
+ int x = s.executeUpdate("insert into t2 (h, i) values (3, 4)");
+ assertThat(x, is(1));
+
+ final String sql1 = "explain plan for\n"
+ + "insert into t2 (h, i) values (3, 4)";
+ try (ResultSet r = s.executeQuery(sql1)) {
+ assertThat(r.next(), is(true));
+ final String plan = ""
+ + "EnumerableTableModify(table=[[T2]], operation=[INSERT],
flattened=[false])\n"
+ + " EnumerableCalc(expr#0..1=[{inputs}], expr#2=[1],
expr#3=[+($t1, $t2)], expr#4=[-1], proj#0..1=[{exprs}], J=[$t3], K=[$t4])\n"
+ + " EnumerableValues(tuples=[[{ 3, 4 }]])\n";
+ assertThat(r.getString(1), isLinux(plan));
+ assertThat(r.next(), is(false));
+ }
+
+ try (ResultSet r = s.executeQuery("select * from t2")) {
+ assertThat(r.next(), is(true));
+ assertThat(r.getInt("H"), is(3));
+ assertThat(r.wasNull(), is(false));
+ assertThat(r.getInt("I"), is(4));
+ assertThat(r.getInt("J"), is(5)); // j = i + 1
+ assertThat(r.getInt("K"), is(-1)); // k = -1 (default)
+ assertThat(r.next(), is(false));
+ }
+
+ try {
+ x = s.executeUpdate("insert into t2 values (3, 4, 5, 6)");
+ fail("expected error, got " + x);
+ } catch (SQLException e) {
+ assertThat(e.getMessage(),
+ containsString("Cannot INSERT into generated column 'J'"));
+ }
+ }
+ }
+
+ @Test void testCreateTableLikeWithVirtualGeneratedColumn() throws Exception {
+ try (Connection c = connect();
+ Statement s = c.createStatement()) {
+ s.execute("create table t (\n"
+ + " h int not null,\n"
+ + " i int,\n"
+ + " j int as (i + 1) virtual)\n");
+ s.execute("create table t2 like t including defaults including
generated");
+
+ int x = s.executeUpdate("insert into t2 (h, i) values (3, 4)");
+ assertThat(x, is(1));
+
+ final String sql1 = "explain plan for\n"
+ + "insert into t (h, i) values (3, 4)";
+ try (ResultSet r = s.executeQuery(sql1)) {
+ final String sql2 = "explain plan for\n"
+ + "insert into t2 (h, i) values (3, 4)";
+ assertThat(r.next(), is(true));
+ final String plan = r.getString(1);
+ assertThat(r.next(), is(false));
+
+ ResultSet r2 = s.executeQuery(sql2);
+ assertThat(r2.next(), is(true));
+ assertEquals(plan, r2.getString(1).replace("T2", "T"));
+ assertThat(r2.next(), is(false));
+ }
+
+ try (ResultSet r = s.executeQuery("select * from t2")) {
+ assertThat(r.next(), is(true));
+ assertThat(r.getInt("H"), is(3));
+ assertThat(r.wasNull(), is(false));
+ assertThat(r.getInt("I"), is(4));
+ assertThat(r.getInt("J"), is(5)); // j = i + 1
+ assertThat(r.next(), is(false));
+ }
+
+ try {
+ x = s.executeUpdate("insert into t2 values (3, 4, 5)");
+ fail("expected error, got " + x);
+ } catch (SQLException e) {
Review Comment:
assertThrows
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]