This is an automated email from the ASF dual-hosted git repository.

skadam pushed a commit to branch 4.x
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/4.x by this push:
     new 2a530da  PHOENIX-6148: [SchemaExtractionTool] DDL parsing exception in 
Phoenix in view name (Addendum)
2a530da is described below

commit 2a530da17693b6a06b7d21c2566439ffc440822f
Author: Swaroopa Kadam <s.ka...@apache.org>
AuthorDate: Fri Jan 8 13:10:53 2021 -0800

    PHOENIX-6148: [SchemaExtractionTool] DDL parsing exception in Phoenix in 
view name (Addendum)
---
 .../java/org/apache/phoenix/util/SchemaUtil.java   | 38 ++++++++++++++++++++--
 .../phoenix/schema/SchemaExtractionToolIT.java     | 30 ++++++++++++++---
 2 files changed, 61 insertions(+), 7 deletions(-)

diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java 
b/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
index c453dd1..6abc2a2 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
@@ -1232,11 +1232,45 @@ public class SchemaUtil {
         return columnParseNode.getName();
     }
 
+    /**
+     * This function is needed so that SchemaExtractionTool returns a valid 
DDL with correct
+     * table/schema name that can be parsed
+     *
+     * @param pSchemaName
+     * @param pTableName
+     * @return quoted string if schema or table name has non-alphabetic 
characters in it.
+     */
     public static String getPTableFullNameWithQuotes(String pSchemaName, 
String pTableName) {
         String pTableFullName = getQualifiedTableName(pSchemaName, pTableName);
-        if(!(Character.isAlphabetic(pTableName.charAt(0)))) {
-            pTableFullName = pSchemaName+".\""+pTableName+"\"";
+        boolean tableNameNeedsQuotes = isQuotesNeeded(pTableName);
+        boolean schemaNameNeedsQuotes = isQuotesNeeded(pSchemaName);
+
+        if(schemaNameNeedsQuotes) {
+            pSchemaName= "\""+pSchemaName+"\"";
         }
+        if(tableNameNeedsQuotes) {
+            pTableName = "\""+pTableName+"\"";
+        }
+        if(tableNameNeedsQuotes || schemaNameNeedsQuotes) {
+            pTableFullName = pSchemaName + "." + pTableName;
+        }
+
         return pTableFullName;
     }
+
+    private static boolean isQuotesNeeded(String name) {
+        // first char numeric or non-underscore
+        if(!Character.isAlphabetic(name.charAt(0)) && name.charAt(0)!='_') {
+            return true;
+        }
+        // for all other chars
+        // ex. name like z@@ will need quotes whereas t0001 will not need 
quotes
+        for (int i=1; i<name.toCharArray().length; i++) {
+            char charAtI = name.charAt(i);
+            if (!(Character.isAlphabetic(charAtI)) && 
!Character.isDigit(charAtI) && charAtI != '_') {
+                return true;
+            }
+        }
+        return false;
+    }
 }
diff --git 
a/phoenix-tools/src/it/java/org/apache/phoenix/schema/SchemaExtractionToolIT.java
 
b/phoenix-tools/src/it/java/org/apache/phoenix/schema/SchemaExtractionToolIT.java
index 8d0704b..4f9b11b 100644
--- 
a/phoenix-tools/src/it/java/org/apache/phoenix/schema/SchemaExtractionToolIT.java
+++ 
b/phoenix-tools/src/it/java/org/apache/phoenix/schema/SchemaExtractionToolIT.java
@@ -101,18 +101,38 @@ public class SchemaExtractionToolIT extends 
ParallelStatsEnabledIT {
                 + "v1 VARCHAR, v2 VARCHAR)"
                 + properties;
         String viewFullName = SchemaUtil.getQualifiedTableName(schemaName, 
viewName);
-        String viewFullName1 = SchemaUtil.getQualifiedTableName(schemaName, 
viewName+"1");
         String createView = "CREATE VIEW "+viewFullName + "(id1 BIGINT, id2 
BIGINT NOT NULL, "
                 + "id3 VARCHAR NOT NULL CONSTRAINT PKVIEW PRIMARY KEY (id2, 
id3 DESC)) "
                 + "AS SELECT * FROM "+pTableFullName;
-        String createView1 = "CREATE VIEW "+viewFullName1 + "(id1 BIGINT, id2 
BIGINT NOT NULL, "
+
+        List<String> queries = new ArrayList<String>(){};
+        queries.add(createTableStmt);
+        queries.add(createView);
+        String result = runSchemaExtractionTool(schemaName, viewName, null, 
queries);
+        Assert.assertEquals(createView.toUpperCase(), result.toUpperCase());
+
+    }
+
+    @Test
+    public void testCreateViewStatement_customName() throws Exception {
+        String tableName = generateUniqueName();
+        String schemaName = generateUniqueName();
+        String viewName = generateUniqueName()+"@@";
+        String properties = "TTL=2592000,IMMUTABLE_ROWS=true,DISABLE_WAL=true";
+
+        String pTableFullName = SchemaUtil.getQualifiedTableName(schemaName, 
tableName);
+        String createTableStmt = "CREATE TABLE "+pTableFullName + "(k BIGINT 
NOT NULL PRIMARY KEY, "
+                + "v1 VARCHAR, v2 VARCHAR)"
+                + properties;
+        String viewFullName = 
SchemaUtil.getPTableFullNameWithQuotes(schemaName, viewName);
+
+        String createView = "CREATE VIEW "+viewFullName + "(id1 BIGINT, id2 
BIGINT NOT NULL, "
                 + "id3 VARCHAR NOT NULL CONSTRAINT PKVIEW PRIMARY KEY (id2, 
id3 DESC)) "
                 + "AS SELECT * FROM "+pTableFullName;
 
         List<String> queries = new ArrayList<String>(){};
         queries.add(createTableStmt);
         queries.add(createView);
-        queries.add(createView1);
         String result = runSchemaExtractionTool(schemaName, viewName, null, 
queries);
         Assert.assertEquals(createView.toUpperCase(), result.toUpperCase());
 
@@ -153,7 +173,7 @@ public class SchemaExtractionToolIT extends 
ParallelStatsEnabledIT {
         String pTableFullName = SchemaUtil.getQualifiedTableName(schemaName, 
tableName);
         String createTableStmt = "CREATE TABLE "+pTableFullName + "(k BIGINT 
NOT NULL PRIMARY KEY, "
                 + "v1 VARCHAR, v2 VARCHAR)";
-        String viewFullName = SchemaUtil.getQualifiedTableName(schemaName, 
viewName);
+        String viewFullName = 
SchemaUtil.getPTableFullNameWithQuotes(schemaName, viewName);
         String createViewStmt = "CREATE VIEW "+viewFullName + "(id1 BIGINT, 
id2 BIGINT NOT NULL, "
                 + "id3 VARCHAR NOT NULL CONSTRAINT PKVIEW PRIMARY KEY (id2, 
id3 DESC)) "
                 + "AS SELECT * FROM "+pTableFullName;
@@ -321,7 +341,7 @@ public class SchemaExtractionToolIT extends 
ParallelStatsEnabledIT {
     private String runSchemaExtractionTool(String schemaName, String 
tableName, String tenantId, List<String> queries) throws Exception {
         Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
         String output;
-        if (tenantId == null){
+        if (tenantId == null) {
             try (Connection conn = DriverManager.getConnection(getUrl(), 
props)) {
                 executeCreateStatements(conn, queries);
                 String [] args = {"-tb", tableName, "-s", schemaName};

Reply via email to