stoty commented on code in PR #1409:
URL: https://github.com/apache/phoenix/pull/1409#discussion_r2689710851


##########
phoenix-core/src/it/java/org/apache/phoenix/end2end/TruncateTableIT.java:
##########
@@ -0,0 +1,533 @@
+/*
+ * 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.phoenix.end2end;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Properties;
+import org.apache.phoenix.exception.SQLExceptionCode;
+import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.query.QueryServices;
+import org.apache.phoenix.schema.TableNotFoundException;
+import org.apache.phoenix.util.PhoenixRuntime;
+import org.apache.phoenix.util.SchemaUtil;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category(ParallelStatsDisabledTest.class)
+public class TruncateTableIT extends ParallelStatsDisabledIT {
+
+  @Test
+  public void testTruncateTable() throws SQLException {
+    Properties props = new Properties();
+    String tableName = generateUniqueName();
+
+    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+      String createTableDDL = "CREATE TABLE " + tableName + " (pk char(2) not 
null primary key)";
+      conn.createStatement().execute(createTableDDL);
+
+      try {
+        conn.setAutoCommit(true);
+        try (PreparedStatement stmt =
+          conn.prepareStatement("UPSERT INTO " + tableName + " VALUES('a')")) {
+          stmt.execute();
+        }
+
+        try (
+          ResultSet rs = conn.createStatement().executeQuery("SELECT COUNT(*) 
FROM " + tableName)) {
+          assertTrue(rs.next());
+          assertEquals(1, rs.getInt(1));
+        }
+
+        conn.createStatement().execute("TRUNCATE TABLE " + tableName);
+
+        try (
+          ResultSet rs = conn.createStatement().executeQuery("SELECT COUNT(*) 
FROM " + tableName)) {
+          assertTrue(rs.next());
+          assertEquals(0, rs.getInt(1));
+        }
+      } finally {
+        conn.createStatement().execute("DROP TABLE IF EXISTS " + tableName);
+      }
+    } catch (SQLException e) {
+      fail(e.getMessage());
+    }
+  }
+
+  @Test
+  public void testTruncateTableNotExist() throws Exception {
+    Properties props = new Properties();
+    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+      String tableName = "nonExistentTable";
+      try {
+        conn.createStatement().execute("TRUNCATE TABLE " + tableName);
+        fail("Should have thrown TableNotFoundException");
+      } catch (TableNotFoundException e) {
+        // Expected
+      }
+    }
+  }
+
+  @Test
+  public void testTruncateTableNonExistentSchema() throws SQLException {
+    Properties props = new Properties();
+    String schemaName = "nonExistentSchema";
+    String tableName = generateUniqueName();
+
+    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+      conn.createStatement().execute("CREATE TABLE " + tableName + " (C1 
INTEGER PRIMARY KEY)");
+      try {
+        conn.setAutoCommit(true);
+
+        try (PreparedStatement stmt =
+          conn.prepareStatement("UPSERT INTO " + tableName + " VALUES(1)")) {
+          stmt.execute();
+        }
+
+        try (
+          ResultSet rs = conn.createStatement().executeQuery("SELECT COUNT(*) 
FROM " + tableName)) {
+          assertTrue(rs.next());
+          assertEquals(1, rs.getInt(1));
+        }
+
+        try {
+          conn.createStatement().execute("TRUNCATE TABLE " + schemaName + "." 
+ tableName);
+          fail("Should have failed due to non-existent schema/table");
+        } catch (SQLException e) {
+          // Expected
+        }
+      } finally {
+        conn.createStatement().execute("DROP TABLE IF EXISTS " + tableName);
+      }
+    }
+  }
+
+  @Test
+  public void testTruncateTableWithImplicitSchema() throws SQLException {
+    Properties props = new Properties();
+    String schemaName = generateUniqueName();
+    String tableName = generateUniqueName();
+    String fullTableName = schemaName + "." + tableName;
+    String createTableWithSchema =
+      "CREATE TABLE " + fullTableName + " (C1 char(2) NOT NULL PRIMARY KEY)";
+
+    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+      conn.createStatement().execute(createTableWithSchema);
+      try {
+        conn.setAutoCommit(true);
+
+        try (PreparedStatement stmt =
+          conn.prepareStatement("UPSERT INTO " + fullTableName + " 
values('a')")) {
+          stmt.execute();
+        }
+
+        try (ResultSet rs =
+          conn.createStatement().executeQuery("SELECT COUNT(*) FROM " + 
fullTableName)) {
+          assertTrue(rs.next());
+          assertEquals(1, rs.getInt(1));
+        }
+
+        try (PreparedStatement stmt =
+          conn.prepareStatement("UPSERT INTO " + fullTableName + " 
values('b')")) {
+          stmt.execute();
+        }
+
+        try (ResultSet rs =
+          conn.createStatement().executeQuery("SELECT COUNT(*) FROM " + 
fullTableName)) {
+          assertTrue(rs.next());
+          assertEquals(2, rs.getInt(1));
+        }
+
+        conn.createStatement().execute("TRUNCATE TABLE " + fullTableName);
+
+        try (ResultSet rs =
+          conn.createStatement().executeQuery("SELECT COUNT(*) FROM " + 
fullTableName)) {
+          assertTrue(rs.next());
+          assertEquals(0, rs.getInt(1));
+        }
+      } finally {
+        conn.createStatement().execute("DROP TABLE IF EXISTS " + 
fullTableName);
+      }
+    } catch (SQLException e) {
+      fail(e.getMessage());
+    }
+  }
+
+  @Test
+  public void testTruncateTableWithExplicitSchema() throws SQLException {
+    Properties props = new Properties();
+    props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, 
Boolean.toString(true));

Review Comment:
   You should either
   1. split this into two test files, one with namespace mapping enabled andone 
with disabled, and classify those are @NeedOwnCluster tests.
   2. Or you should set phoenix.schema.mapSystemTablesToNamespace to false to 
avoid migrating the system tables on establishing the connection.
   
   



-- 
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]

Reply via email to