lirui-apache commented on a change in pull request #15151:
URL: https://github.com/apache/flink/pull/15151#discussion_r594132528



##########
File path: 
flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/planner/delegation/hive/DDLOperationConverter.java
##########
@@ -0,0 +1,808 @@
+/*
+ * 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.flink.table.planner.delegation.hive;
+
+import org.apache.flink.connectors.hive.FlinkHiveException;
+import org.apache.flink.sql.parser.hive.ddl.HiveDDLUtils;
+import 
org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveDatabase.AlterHiveDatabaseOp;
+import org.apache.flink.table.api.TableColumn;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.api.WatermarkSpec;
+import org.apache.flink.table.api.constraints.UniqueConstraint;
+import org.apache.flink.table.catalog.Catalog;
+import org.apache.flink.table.catalog.CatalogBaseTable;
+import org.apache.flink.table.catalog.CatalogDatabase;
+import org.apache.flink.table.catalog.CatalogDatabaseImpl;
+import org.apache.flink.table.catalog.CatalogFunction;
+import org.apache.flink.table.catalog.CatalogFunctionImpl;
+import org.apache.flink.table.catalog.CatalogManager;
+import org.apache.flink.table.catalog.CatalogPartition;
+import org.apache.flink.table.catalog.CatalogPartitionImpl;
+import org.apache.flink.table.catalog.CatalogPartitionSpec;
+import org.apache.flink.table.catalog.CatalogPropertiesUtil;
+import org.apache.flink.table.catalog.CatalogTable;
+import org.apache.flink.table.catalog.CatalogTableImpl;
+import org.apache.flink.table.catalog.CatalogView;
+import org.apache.flink.table.catalog.CatalogViewImpl;
+import org.apache.flink.table.catalog.FunctionCatalog.InlineCatalogFunction;
+import org.apache.flink.table.catalog.FunctionLanguage;
+import org.apache.flink.table.catalog.ObjectIdentifier;
+import org.apache.flink.table.catalog.UnresolvedIdentifier;
+import org.apache.flink.table.catalog.exceptions.DatabaseNotExistException;
+import org.apache.flink.table.catalog.hive.HiveCatalog;
+import org.apache.flink.table.catalog.hive.client.HiveShim;
+import 
org.apache.flink.table.catalog.hive.factories.HiveFunctionDefinitionFactory;
+import org.apache.flink.table.catalog.hive.util.HiveTableUtil;
+import org.apache.flink.table.catalog.hive.util.HiveTypeUtil;
+import org.apache.flink.table.functions.FunctionDefinition;
+import org.apache.flink.table.operations.DescribeTableOperation;
+import org.apache.flink.table.operations.Operation;
+import org.apache.flink.table.operations.ShowDatabasesOperation;
+import org.apache.flink.table.operations.ShowFunctionsOperation;
+import org.apache.flink.table.operations.ShowPartitionsOperation;
+import org.apache.flink.table.operations.ShowTablesOperation;
+import org.apache.flink.table.operations.ShowViewsOperation;
+import org.apache.flink.table.operations.UseDatabaseOperation;
+import org.apache.flink.table.operations.ddl.AddPartitionsOperation;
+import org.apache.flink.table.operations.ddl.AlterDatabaseOperation;
+import org.apache.flink.table.operations.ddl.AlterPartitionPropertiesOperation;
+import org.apache.flink.table.operations.ddl.AlterTableOptionsOperation;
+import org.apache.flink.table.operations.ddl.AlterTableRenameOperation;
+import org.apache.flink.table.operations.ddl.AlterTableSchemaOperation;
+import org.apache.flink.table.operations.ddl.AlterViewAsOperation;
+import org.apache.flink.table.operations.ddl.AlterViewPropertiesOperation;
+import org.apache.flink.table.operations.ddl.AlterViewRenameOperation;
+import org.apache.flink.table.operations.ddl.CreateCatalogFunctionOperation;
+import org.apache.flink.table.operations.ddl.CreateDatabaseOperation;
+import org.apache.flink.table.operations.ddl.CreateTableOperation;
+import 
org.apache.flink.table.operations.ddl.CreateTempSystemInlineFunctionOperation;
+import org.apache.flink.table.operations.ddl.CreateViewOperation;
+import org.apache.flink.table.operations.ddl.DropCatalogFunctionOperation;
+import org.apache.flink.table.operations.ddl.DropDatabaseOperation;
+import org.apache.flink.table.operations.ddl.DropPartitionsOperation;
+import org.apache.flink.table.operations.ddl.DropTableOperation;
+import org.apache.flink.table.operations.ddl.DropTempSystemFunctionOperation;
+import org.apache.flink.table.operations.ddl.DropViewOperation;
+import 
org.apache.flink.table.planner.delegation.hive.HiveParserCreateTableDesc.NotNullConstraint;
+import 
org.apache.flink.table.planner.delegation.hive.HiveParserCreateTableDesc.PrimaryKey;
+import 
org.apache.flink.table.planner.delegation.hive.parse.HiveParserBaseSemanticAnalyzer;
+import 
org.apache.flink.table.planner.delegation.hive.parse.HiveParserStorageFormat;
+import org.apache.flink.table.planner.utils.OperationConverterUtils;
+
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.ql.plan.AddPartitionDesc;
+import org.apache.hadoop.hive.ql.plan.CreateDatabaseDesc;
+import org.apache.hadoop.hive.ql.plan.CreateFunctionDesc;
+import org.apache.hadoop.hive.ql.plan.DDLWork;
+import org.apache.hadoop.hive.ql.plan.DescTableDesc;
+import org.apache.hadoop.hive.ql.plan.FunctionWork;
+import org.apache.hadoop.hive.ql.plan.PrincipalDesc;
+import org.apache.hadoop.hive.ql.plan.ShowDatabasesDesc;
+import org.apache.hadoop.hive.ql.plan.ShowFunctionsDesc;
+import org.apache.hadoop.hive.ql.plan.ShowPartitionsDesc;
+import org.apache.hadoop.hive.ql.plan.SwitchDatabaseDesc;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.sql.parser.hive.ddl.HiveDDLUtils.COL_DELIMITER;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveDatabase.ALTER_DATABASE_OP;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveDatabaseOwner.DATABASE_OWNER_NAME;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveDatabaseOwner.DATABASE_OWNER_TYPE;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveTable.ALTER_COL_CASCADE;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveTable.ALTER_TABLE_OP;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveTable.AlterTableOp.ALTER_COLUMNS;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveTable.AlterTableOp.CHANGE_FILE_FORMAT;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveTable.AlterTableOp.CHANGE_LOCATION;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveTable.AlterTableOp.CHANGE_SERDE_PROPS;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveTable.AlterTableOp.CHANGE_TBL_PROPS;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveDatabase.DATABASE_LOCATION_URI;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.HiveTableRowFormat.COLLECTION_DELIM;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.HiveTableRowFormat.ESCAPE_CHAR;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.HiveTableRowFormat.FIELD_DELIM;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.HiveTableRowFormat.LINE_DELIM;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.HiveTableRowFormat.MAPKEY_DELIM;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.HiveTableRowFormat.SERDE_INFO_PROP_PREFIX;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.HiveTableRowFormat.SERDE_LIB_CLASS_NAME;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.HiveTableRowFormat.SERIALIZATION_NULL_FORMAT;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.HiveTableStoredAs.STORED_AS_FILE_FORMAT;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.HiveTableStoredAs.STORED_AS_INPUT_FORMAT;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.HiveTableStoredAs.STORED_AS_OUTPUT_FORMAT;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.NOT_NULL_COLS;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.NOT_NULL_CONSTRAINT_TRAITS;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.PK_CONSTRAINT_TRAIT;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.TABLE_IS_EXTERNAL;
+import static 
org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable.TABLE_LOCATION_URI;
+
+/** A converter to generate DDL operations. */
+public class DDLOperationConverter {
+
+    private final CatalogManager catalogManager;
+    private final HiveFunctionDefinitionFactory funcDefFactory;
+
+    public DDLOperationConverter(CatalogManager catalogManager, HiveShim 
hiveShim) {
+        this.catalogManager = catalogManager;
+        this.funcDefFactory = new HiveFunctionDefinitionFactory(hiveShim);
+    }
+
+    public Operation convert(Serializable work) {
+        if (work instanceof DDLWork) {
+            DDLWork ddlWork = (DDLWork) work;
+            if (ddlWork.getCreateDatabaseDesc() != null) {
+                return convertCreateDatabase(ddlWork.getCreateDatabaseDesc());
+            } else if (ddlWork.getShowDatabasesDesc() != null) {
+                return convertShowDatabases(ddlWork.getShowDatabasesDesc());
+            } else if (ddlWork.getSwitchDatabaseDesc() != null) {
+                return convertUseDatabase(ddlWork.getSwitchDatabaseDesc());
+            } else if (ddlWork.getAddPartitionDesc() != null) {
+                return convertAddPartitions(ddlWork.getAddPartitionDesc());
+            } else if (ddlWork.getShowPartsDesc() != null) {
+                return convertShowPartitions(ddlWork.getShowPartsDesc());
+            } else if (ddlWork.getShowFuncsDesc() != null) {
+                return convertShowFunctions(ddlWork.getShowFuncsDesc());
+            } else if (ddlWork.getDescTblDesc() != null) {
+                return convertDescTable(ddlWork.getDescTblDesc());
+            } else {
+                throw new FlinkHiveException("Unsupported DDLWork");
+            }
+        } else if (work instanceof HiveParserShowTablesDesc) {
+            if (((HiveParserShowTablesDesc) work).isExpectView()) {
+                return convertShowViews((HiveParserShowTablesDesc) work);
+            } else {
+                return convertShowTables((HiveParserShowTablesDesc) work);
+            }
+        } else if (work instanceof HiveParserAlterTableDesc) {
+            HiveParserAlterTableDesc alterTableDesc = 
(HiveParserAlterTableDesc) work;
+            if (alterTableDesc.expectView()) {
+                return convertAlterView(alterTableDesc);
+            } else {
+                return convertAlterTable(alterTableDesc);
+            }
+        } else if (work instanceof HiveParserCreateTableDesc) {
+            return convertCreateTable((HiveParserCreateTableDesc) work);
+        } else if (work instanceof HiveParserDropTableDesc) {
+            HiveParserDropTableDesc dropTableDesc = (HiveParserDropTableDesc) 
work;
+            if (dropTableDesc.isExpectView()) {
+                return convertDropView(dropTableDesc);
+            } else {
+                return convertDropTable(dropTableDesc);
+            }
+        } else if (work instanceof DropPartitionDesc) {
+            return convertDropPartitions((DropPartitionDesc) work);
+        } else if (work instanceof HiveParserCreateViewDesc) {
+            return convertCreateAlterView((HiveParserCreateViewDesc) work);
+        } else if (work instanceof HiveParserDropFunctionDesc) {
+            return convertDropFunction((HiveParserDropFunctionDesc) work);
+        } else if (work instanceof FunctionWork) {
+            FunctionWork functionWork = (FunctionWork) work;
+            if (functionWork.getCreateFunctionDesc() != null) {
+                return 
convertCreateFunction(functionWork.getCreateFunctionDesc());
+            }
+            throw new FlinkHiveException("Unsupported FunctionWork");
+        } else if (work instanceof HiveParserAlterDatabaseDesc) {
+            return convertAlterDatabase((HiveParserAlterDatabaseDesc) work);
+        } else if (work instanceof HiveParserDropDatabaseDesc) {
+            return convertDropDatabase((HiveParserDropDatabaseDesc) work);
+        } else {
+            throw new FlinkHiveException("Unsupported work class " + 
work.getClass().getName());
+        }
+    }
+
+    private Operation convertDescTable(DescTableDesc desc) {
+        ObjectIdentifier tableIdentifier = 
parseObjectIdentifier(desc.getTableName());
+        return new DescribeTableOperation(tableIdentifier, desc.isExt() || 
desc.isFormatted());
+    }
+
+    private Operation convertShowFunctions(ShowFunctionsDesc desc) {
+        return new ShowFunctionsOperation();
+    }
+
+    private Operation convertShowPartitions(ShowPartitionsDesc desc) {
+        ObjectIdentifier tableIdentifier = 
parseObjectIdentifier(desc.getTabName());
+        CatalogPartitionSpec spec = null;
+        if (desc.getPartSpec() != null && !desc.getPartSpec().isEmpty()) {
+            spec = new CatalogPartitionSpec(new HashMap<>(desc.getPartSpec()));
+        }
+        return new ShowPartitionsOperation(tableIdentifier, spec);
+    }
+
+    private Operation convertCreateFunction(CreateFunctionDesc desc) {
+        if (desc.isTemp()) {
+            // hive's temporary function is more like flink's temp system 
function, e.g. doesn't
+            // belong to a catalog/db
+            // the DDL analyzer makes sure temp function name is not a 
compound one
+            FunctionDefinition funcDefinition =
+                    funcDefFactory.createFunctionDefinition(
+                            desc.getFunctionName(),
+                            new CatalogFunctionImpl(desc.getClassName(), 
FunctionLanguage.JAVA));
+            return new CreateTempSystemInlineFunctionOperation(
+                    desc.getFunctionName(), false, new 
InlineCatalogFunction(funcDefinition));
+        } else {
+            ObjectIdentifier identifier = 
parseObjectIdentifier(desc.getFunctionName());
+            CatalogFunction catalogFunction =
+                    new CatalogFunctionImpl(desc.getClassName(), 
FunctionLanguage.JAVA);
+            return new CreateCatalogFunctionOperation(
+                    identifier, catalogFunction, false, desc.isTemp());
+        }
+    }
+
+    private Operation convertDropFunction(HiveParserDropFunctionDesc desc) {
+        if (desc.getDesc().isTemp()) {
+            return new DropTempSystemFunctionOperation(
+                    desc.getDesc().getFunctionName(), desc.ifExists());
+        } else {
+            ObjectIdentifier identifier = 
parseObjectIdentifier(desc.getDesc().getFunctionName());
+            return new DropCatalogFunctionOperation(
+                    identifier, desc.ifExists(), desc.getDesc().isTemp());
+        }
+    }
+
+    private Operation convertDropView(HiveParserDropTableDesc desc) {
+        ObjectIdentifier identifier = 
parseObjectIdentifier(desc.getCompoundName());
+        CatalogBaseTable baseTable = getCatalogBaseTable(identifier, true);
+        if (baseTable instanceof CatalogTable) {
+            throw new ValidationException("DROP VIEW for a table is not 
allowed");
+        }
+        return new DropViewOperation(identifier, desc.ifExists(), false);
+    }
+
+    private Operation convertDropTable(HiveParserDropTableDesc desc) {
+        ObjectIdentifier identifier = 
parseObjectIdentifier(desc.getCompoundName());
+        CatalogBaseTable baseTable = getCatalogBaseTable(identifier, true);
+        if (baseTable instanceof CatalogView) {
+            throw new ValidationException("DROP TABLE for a view is not 
allowed");
+        }
+        return new DropTableOperation(identifier, desc.ifExists(), false);
+    }
+
+    // handles both create view and alter view as
+    private Operation convertCreateAlterView(HiveParserCreateViewDesc desc) {
+        ObjectIdentifier viewIdentifier = 
parseObjectIdentifier(desc.getCompoundName());
+        TableSchema schema =
+                HiveTableUtil.createTableSchema(
+                        desc.getSchema(), Collections.emptyList(), 
Collections.emptySet(), null);
+        Map<String, String> props = new HashMap<>();
+        String comment;
+        if (desc.isAlterViewAs()) {
+            CatalogBaseTable baseTable = getCatalogBaseTable(viewIdentifier);
+            if (baseTable instanceof CatalogTable) {
+                throw new ValidationException("ALTER VIEW for a table is not 
allowed");
+            }
+            props.putAll(baseTable.getOptions());
+            comment = baseTable.getComment();
+        } else {
+            markNonGeneric(props);
+            comment = desc.getComment();
+            if (desc.getTblProps() != null) {
+                props.putAll(desc.getTblProps());
+            }
+        }
+        CatalogView catalogView =
+                new CatalogViewImpl(
+                        desc.getOriginalText(), desc.getExpandedText(), 
schema, props, comment);
+        if (desc.isAlterViewAs()) {
+            return new AlterViewAsOperation(viewIdentifier, catalogView);
+        } else {
+            return new CreateViewOperation(viewIdentifier, catalogView, 
desc.ifNotExists(), false);
+        }
+    }
+
+    private Operation convertDropPartitions(DropPartitionDesc desc) {
+        ObjectIdentifier tableIdentifier =
+                catalogManager.qualifyIdentifier(
+                        UnresolvedIdentifier.of(desc.getDbName(), 
desc.getTableName()));
+        CatalogBaseTable catalogBaseTable = 
getCatalogBaseTable(tableIdentifier);
+        if (catalogBaseTable instanceof CatalogView) {
+            throw new ValidationException("ALTER TABLE for a view is not 
allowed");

Review comment:
       Nice catch




----------------------------------------------------------------
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:
[email protected]


Reply via email to