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 2021/05/05 05:44:02 UTC

[GitHub] [phoenix] kadirozde commented on a change in pull request #1217: PHOENIX-6454: Add feature to SchemaTool to get the DDL in specificati…

kadirozde commented on a change in pull request #1217:
URL: https://github.com/apache/phoenix/pull/1217#discussion_r626264495



##########
File path: phoenix-tools/src/main/java/org/apache/phoenix/schema/SchemaSynthesisProcessor.java
##########
@@ -0,0 +1,261 @@
+/*
+ * 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.schema;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+import org.apache.hadoop.hbase.util.Pair;
+import org.apache.phoenix.parse.AddColumnStatement;
+import org.apache.phoenix.parse.BindableStatement;
+import org.apache.phoenix.parse.ColumnDef;
+import org.apache.phoenix.parse.ColumnName;
+import org.apache.phoenix.parse.CreateIndexStatement;
+import org.apache.phoenix.parse.CreateTableStatement;
+import org.apache.phoenix.parse.DropColumnStatement;
+import org.apache.phoenix.parse.SQLParser;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class SchemaSynthesisProcessor extends SchemaProcessor {
+    public static final String
+            ENTITY_NAME_IN_BASE_AND_ALTER_DDL_DON_T_MATCH =
+            "Entity name in base and alter DDL don't match";
+    private final String baseDDLFile;
+    private final String alterDDLFile;
+
+    public SchemaSynthesisProcessor(String baseDDLFile, String alterDDLFile) {
+        this.baseDDLFile = baseDDLFile;
+        this.alterDDLFile = alterDDLFile;
+    }
+
+    @Override
+    public String process() throws Exception {
+        String baseDDL = getQueriesFromFile(baseDDLFile).get(0);
+        List <String> alterDDL = getQueriesFromFile(alterDDLFile);
+        for(String s : alterDDL) {
+            baseDDL = synthesize(baseDDL, s);
+        }
+        return baseDDL;
+    }
+
+    private String synthesize(String baseDDL, String alterDDL) throws Exception {
+        BindableStatement createStatement = new SQLParser(baseDDL).parseStatement();
+        BindableStatement alterStatement = new SQLParser(alterDDL).parseStatement();
+        if (createStatement instanceof CreateTableStatement) {
+            CreateTableStatement newCreateStmt = null;
+            CreateTableStatement createStmt = (CreateTableStatement) createStatement;
+            if (alterStatement instanceof AddColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((AddColumnStatement) alterStatement, createStmt);
+            } else if(alterStatement instanceof DropColumnStatement) {
+                newCreateStmt =
+                        getCreateTableStatement((DropColumnStatement) alterStatement, createStmt);
+            }
+            return getCreateTableSQL(newCreateStmt);
+        } else if (createStatement instanceof CreateIndexStatement) {
+            CreateIndexStatement newCreateIndexStmt =
+                    getCreateIndexStatement((CreateIndexStatement) createStatement, alterStatement);
+            return getCreateIndexSQL(newCreateIndexStmt);
+        }
+        return "";

Review comment:
       @swaroopak, This is a great start. As @gjacoby126 pointed out, there are more cases to cover. I think we need a design doc on this to explain how the design covers all the cases and explain how this tool will be used. I also think we can add add a future to Phoenix to self verify/test DDL statements. The place to add such a feature would be MetaDataClient. After every DDL, we can verify if DDL is correctly implemented by Phoenix.




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