You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by bl...@apache.org on 2019/12/17 23:32:35 UTC

[flink] 01/02: [FLINK-15234][hive] hive table created from flink catalog table cannot have null properties in parameters

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

bli pushed a commit to branch release-1.9
in repository https://gitbox.apache.org/repos/asf/flink.git

commit cca2387a48e42950f0e53f8d9f002a4e0acf0c94
Author: bowen <bo...@US-164288MP.local>
AuthorDate: Tue Dec 17 15:10:46 2019 -0800

    [FLINK-15234][hive] hive table created from flink catalog table cannot have null properties in parameters
    
    backport from 1.10
---
 .../flink/table/catalog/hive/HiveCatalog.java      |  7 +-
 .../flink/table/catalog/hive/HiveCatalogTest.java  | 80 ++++++++++++++++++++++
 .../flink/table/catalog/AbstractCatalogTable.java  |  6 ++
 3 files changed, 91 insertions(+), 2 deletions(-)

diff --git a/flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/HiveCatalog.java b/flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/HiveCatalog.java
index dd50ce2..dcfb313 100644
--- a/flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/HiveCatalog.java
+++ b/flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/HiveCatalog.java
@@ -542,7 +542,8 @@ public class HiveCatalog extends AbstractCatalog {
 		}
 	}
 
-	private  static Table instantiateHiveTable(ObjectPath tablePath, CatalogBaseTable table) {
+	@VisibleForTesting
+	protected static Table instantiateHiveTable(ObjectPath tablePath, CatalogBaseTable table) {
 		// let Hive set default parameters for us, e.g. serialization.format
 		Table hiveTable = org.apache.hadoop.hive.ql.metadata.Table.getEmptyTable(tablePath.getDatabaseName(),
 			tablePath.getObjectName());
@@ -550,7 +551,9 @@ public class HiveCatalog extends AbstractCatalog {
 
 		Map<String, String> properties = new HashMap<>(table.getProperties());
 		// Table comment
-		properties.put(HiveCatalogConfig.COMMENT, table.getComment());
+		if (table.getComment() != null) {
+			properties.put(HiveCatalogConfig.COMMENT, table.getComment());
+		}
 
 		boolean isGeneric = Boolean.valueOf(properties.get(CatalogConfig.IS_GENERIC));
 
diff --git a/flink-connectors/flink-connector-hive/src/test/java/org/apache/flink/table/catalog/hive/HiveCatalogTest.java b/flink-connectors/flink-connector-hive/src/test/java/org/apache/flink/table/catalog/hive/HiveCatalogTest.java
new file mode 100644
index 0000000..23ccb45
--- /dev/null
+++ b/flink-connectors/flink-connector-hive/src/test/java/org/apache/flink/table/catalog/hive/HiveCatalogTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.catalog.hive;
+
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.catalog.CatalogTableImpl;
+import org.apache.flink.table.catalog.ObjectPath;
+import org.apache.flink.table.catalog.config.CatalogConfig;
+import org.apache.flink.table.descriptors.FileSystem;
+
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test for HiveCatalog.
+ */
+public class HiveCatalogTest {
+
+	TableSchema schema = TableSchema.builder()
+		.field("name", DataTypes.STRING())
+		.field("age", DataTypes.INT())
+		.build();
+
+	@Test
+	public void testCreateGenericTable() {
+		Table hiveTable = HiveCatalog.instantiateHiveTable(
+			new ObjectPath("test", "test"),
+			new CatalogTableImpl(
+				schema,
+				new FileSystem().path("/test_path").toProperties(),
+				null
+			));
+
+		Map<String, String> prop = hiveTable.getParameters();
+		assertEquals(prop.remove(CatalogConfig.IS_GENERIC), String.valueOf("true"));
+		assertTrue(prop.keySet().stream().allMatch(k -> k.startsWith(CatalogConfig.FLINK_PROPERTY_PREFIX)));
+	}
+
+	@Test
+	public void testCreateHiveTable() {
+		Map<String, String> map = new HashMap<>(new FileSystem().path("/test_path").toProperties());
+
+		map.put(CatalogConfig.IS_GENERIC, String.valueOf(false));
+
+		Table hiveTable = HiveCatalog.instantiateHiveTable(
+			new ObjectPath("test", "test"),
+			new CatalogTableImpl(
+				schema,
+				map,
+				null
+			));
+
+		Map<String, String> prop = hiveTable.getParameters();
+		assertEquals(prop.remove(CatalogConfig.IS_GENERIC), String.valueOf(false));
+		assertTrue(prop.keySet().stream().noneMatch(k -> k.startsWith(CatalogConfig.FLINK_PROPERTY_PREFIX)));
+	}
+}
diff --git a/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/catalog/AbstractCatalogTable.java b/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/catalog/AbstractCatalogTable.java
index d586b5d..4f0f440 100644
--- a/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/catalog/AbstractCatalogTable.java
+++ b/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/catalog/AbstractCatalogTable.java
@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
+import static org.apache.flink.util.Preconditions.checkArgument;
 import static org.apache.flink.util.Preconditions.checkNotNull;
 
 /**
@@ -54,6 +55,11 @@ public abstract class AbstractCatalogTable implements CatalogTable {
 		this.tableSchema = checkNotNull(tableSchema, "tableSchema cannot be null");
 		this.partitionKeys = checkNotNull(partitionKeys, "partitionKeys cannot be null");
 		this.properties = checkNotNull(properties, "properties cannot be null");
+
+		checkArgument(
+			properties.entrySet().stream().allMatch(e -> e.getKey() != null && e.getValue() != null),
+			"properties cannot have null keys or values");
+
 		this.comment = comment;
 	}