You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@phoenix.apache.org by GitBox <gi...@apache.org> on 2020/11/08 21:43:48 UTC

[GitHub] [phoenix] yanxinyi commented on a change in pull request #957: PHOENIX-6203 : Add new CQS method to return Table instance only if table exists

yanxinyi commented on a change in pull request #957:
URL: https://github.com/apache/phoenix/pull/957#discussion_r519481009



##########
File path: phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java
##########
@@ -1440,5 +1441,69 @@ public void testAddNonPKColumnWhenlastPKIsVARBINARYOrARRAY() throws Exception {
             stmt.execute(alterDdl2);
         }
     }
-}
- 
+
+    @Test
+    public void testTableExists() throws Exception {
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            ConnectionQueryServices cqs =
+                conn.unwrap(PhoenixConnection.class).getQueryServices();
+            String tableName = "randomTable";
+            // table never existed, still cqs.getTable() does not throw TNFE
+            Table randomTable = cqs.getTable(Bytes.toBytes(tableName));
+            assertNotNull(randomTable);
+            assertEquals(randomTable.getName(), TableName.valueOf(tableName));
+            try {
+                // this is correct check for existence of table
+                cqs.getTableIfExists(Bytes.toBytes(tableName));
+                fail("Should have thrown TableNotFoundException");
+            } catch (TableNotFoundException e) {
+                assertEquals(e.getTableName(), tableName);
+            }
+
+            String tableName1 = SchemaUtil.getTableName(schemaName, dataTableName);
+            String ddl = "CREATE TABLE " + tableName1
+                + " (col1 INTEGER PRIMARY KEY, col2 INTEGER)";
+            conn.createStatement().execute(ddl);
+            String tableName2 = SchemaUtil.getTableName(generateUniqueName(),
+                generateUniqueName());
+            ddl = "CREATE TABLE " + tableName2
+                + " (col1 INTEGER PRIMARY KEY, col2 INTEGER)";
+            conn.createStatement().execute(ddl);
+
+            // table does exist and cqs.getTable() does not throw TNFE
+            Table table1 = cqs.getTable(Bytes.toBytes(tableName1));
+            assertNotNull(table1);
+            try {
+                cqs.getTableIfExists(Bytes.toBytes(tableName1));
+            } catch (TableNotFoundException e) {
+                fail("Should not throw TableNotFoundException");
+            }
+
+            disableAndDropNonSystemTables();
+            // tables have been dropped, still cqs.getTable()
+            // does not throw TNFE for tableName1 and tableName2
+            Table t1 = cqs.getTable(Bytes.toBytes(tableName1));
+            assertEquals(t1.getName().getNameAsString(), tableName1);
+            Table t2 = cqs.getTable(Bytes.toBytes(tableName2));
+            assertEquals(t2.getName().getNameAsString(), tableName2);
+
+            // this is correct check for existence of table
+            try {
+                cqs.getTableIfExists(Bytes.toBytes(tableName1));
+                fail("Should have thrown TableNotFoundException");
+            } catch (TableNotFoundException e) {
+                // exact match is possible because schema name is empty
+                assertEquals(e.getTableName(), tableName1);

Review comment:
       nit: the first argument should be the expected value.




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

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