You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tw...@apache.org on 2019/06/07 09:29:36 UTC

[flink] 03/03: [hotfix][table-common] Introduce an across-catalog object identifier

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

twalthr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit ef150eeb654bcb6df27f44402b9c9c8a24471217
Author: Timo Walther <tw...@apache.org>
AuthorDate: Wed Jun 5 12:08:22 2019 +0200

    [hotfix][table-common] Introduce an across-catalog object identifier
---
 .../flink/table/catalog/ObjectIdentifier.java      | 106 +++++++++++++++++++++
 .../flink/table/types/logical/DistinctType.java    |  15 +--
 .../flink/table/types/logical/LogicalType.java     |  12 ---
 .../apache/flink/table/types/logical/RowType.java  |   3 +
 .../flink/table/types/logical/StructuredType.java  |  15 +--
 .../flink/table/types/logical/UserDefinedType.java |  98 +++----------------
 .../apache/flink/table/utils/EncodingUtils.java    |  12 +++
 .../apache/flink/table/types/LogicalTypesTest.java |   8 +-
 8 files changed, 153 insertions(+), 116 deletions(-)

diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/catalog/ObjectIdentifier.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/catalog/ObjectIdentifier.java
new file mode 100644
index 0000000..b791a70
--- /dev/null
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/catalog/ObjectIdentifier.java
@@ -0,0 +1,106 @@
+/*
+ * 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;
+
+import org.apache.flink.util.Preconditions;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+import static org.apache.flink.table.utils.EncodingUtils.escapeIdentifier;
+
+/**
+ * Identifies an object in a catalog. It allows to identify objects such as tables, views, function,
+ * or types in a catalog. An identifier must be fully qualified. It is the responsibility of the
+ * catalog manager to resolve an identifier to an object.
+ *
+ * <p>While {@link ObjectPath} is used within the same catalog, instances of this class can be used
+ * across catalogs.
+ *
+ * <p>Two objects are considered equal if they share the same type identifier in a stable session context.
+ */
+public final class ObjectIdentifier implements Serializable {
+
+	private String catalogName;
+
+	private String databaseName;
+
+	private String objectName;
+
+	public static ObjectIdentifier of(String catalogName, String databaseName, String objectName) {
+		return new ObjectIdentifier(catalogName, databaseName, objectName);
+	}
+
+	private ObjectIdentifier(
+			String catalogName,
+			String databaseName,
+			String objectName) {
+		this.catalogName = Preconditions.checkNotNull(catalogName, "Catalog name must not be null.");
+		this.databaseName = Preconditions.checkNotNull(databaseName, "Database name must not be null.");
+		this.objectName = Preconditions.checkNotNull(objectName, "Object name must not be null.");
+	}
+
+	public String getCatalogName() {
+		return catalogName;
+	}
+
+	public String getDatabaseName() {
+		return databaseName;
+	}
+
+	public String getObjectName() {
+		return objectName;
+	}
+
+	/**
+	 * Returns a string that fully serializes this instance. The serialized string can be used for
+	 * transmitting or persisting an object identifier.
+	 */
+	public String asSerializableString() {
+		return String.format(
+			"%s.%s.%s",
+			escapeIdentifier(catalogName),
+			escapeIdentifier(databaseName),
+			escapeIdentifier(objectName));
+	}
+
+	@Override
+	public boolean equals(Object o) {
+		if (this == o) {
+			return true;
+		}
+		if (o == null || getClass() != o.getClass()) {
+			return false;
+		}
+		ObjectIdentifier that = (ObjectIdentifier) o;
+		return catalogName.equals(that.catalogName) &&
+			databaseName.equals(that.databaseName) &&
+			objectName.equals(that.objectName);
+	}
+
+	@Override
+	public int hashCode() {
+		return Objects.hash(catalogName, databaseName, objectName);
+	}
+
+	@Override
+	public String toString() {
+		return asSerializableString();
+	}
+}
diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/DistinctType.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/DistinctType.java
index 80aa433..35e2b78 100644
--- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/DistinctType.java
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/DistinctType.java
@@ -19,6 +19,7 @@
 package org.apache.flink.table.types.logical;
 
 import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.catalog.ObjectIdentifier;
 import org.apache.flink.util.Preconditions;
 
 import javax.annotation.Nullable;
@@ -51,14 +52,14 @@ public final class DistinctType extends UserDefinedType {
 	 */
 	public static final class Builder {
 
-		private final TypeIdentifier typeIdentifier;
+		private final ObjectIdentifier objectIdentifier;
 
 		private final LogicalType sourceType;
 
 		private @Nullable String description;
 
-		public Builder(TypeIdentifier typeIdentifier, LogicalType sourceType) {
-			this.typeIdentifier = Preconditions.checkNotNull(typeIdentifier, "Type identifier must not be null.");
+		public Builder(ObjectIdentifier objectIdentifier, LogicalType sourceType) {
+			this.objectIdentifier = Preconditions.checkNotNull(objectIdentifier, "Object identifier must not be null.");
 			this.sourceType = Preconditions.checkNotNull(sourceType, "Source type must not be null.");
 
 			Preconditions.checkArgument(
@@ -72,20 +73,20 @@ public final class DistinctType extends UserDefinedType {
 		}
 
 		public DistinctType build() {
-			return new DistinctType(typeIdentifier, sourceType, description);
+			return new DistinctType(objectIdentifier, sourceType, description);
 		}
 	}
 
 	private final LogicalType sourceType;
 
 	private DistinctType(
-			TypeIdentifier typeIdentifier,
+			ObjectIdentifier objectIdentifier,
 			LogicalType sourceType,
 			@Nullable String description) {
 		super(
 			sourceType.isNullable(),
 			LogicalTypeRoot.DISTINCT_TYPE,
-			typeIdentifier,
+			objectIdentifier,
 			true,
 			description);
 		this.sourceType = Preconditions.checkNotNull(sourceType, "Source type must not be null.");
@@ -98,7 +99,7 @@ public final class DistinctType extends UserDefinedType {
 	@Override
 	public LogicalType copy(boolean isNullable) {
 		return new DistinctType(
-			getTypeIdentifier(),
+			getObjectIdentifier(),
 			sourceType.copy(isNullable),
 			getDescription().orElse(null));
 	}
diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/LogicalType.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/LogicalType.java
index 6ea827c..6ac0df0 100644
--- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/LogicalType.java
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/LogicalType.java
@@ -197,16 +197,4 @@ public abstract class LogicalType implements Serializable {
 	protected static Set<String> conversionSet(String... elements) {
 		return new HashSet<>(Arrays.asList(elements));
 	}
-
-	protected static String escapeBackticks(String s) {
-		return s.replace("`", "``");
-	}
-
-	protected static String escapeSingleQuotes(String s) {
-		return s.replace("'", "''");
-	}
-
-	protected static String escapeIdentifier(String s) {
-		return "`" + escapeBackticks(s) + "`";
-	}
 }
diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/RowType.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/RowType.java
index 3f6147e..0d5511f 100644
--- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/RowType.java
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/RowType.java
@@ -35,6 +35,9 @@ import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
 
+import static org.apache.flink.table.utils.EncodingUtils.escapeIdentifier;
+import static org.apache.flink.table.utils.EncodingUtils.escapeSingleQuotes;
+
 /**
  * Logical type of a sequence of fields. A field consists of a field name, field type, and an optional
  * description. The most specific type of a row of a table is a row type. In this case, each column
diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/StructuredType.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/StructuredType.java
index 44bd0ad..ba3f32e 100644
--- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/StructuredType.java
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/StructuredType.java
@@ -19,6 +19,7 @@
 package org.apache.flink.table.types.logical;
 
 import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.catalog.ObjectIdentifier;
 import org.apache.flink.types.Row;
 import org.apache.flink.util.Preconditions;
 
@@ -133,7 +134,7 @@ public final class StructuredType extends UserDefinedType {
 	 */
 	public static class Builder {
 
-		private final TypeIdentifier typeIdentifier;
+		private final ObjectIdentifier objectIdentifier;
 
 		private final List<StructuredAttribute> attributes;
 
@@ -151,8 +152,8 @@ public final class StructuredType extends UserDefinedType {
 
 		private @Nullable Class<?> implementationClass;
 
-		public Builder(TypeIdentifier typeIdentifier, List<StructuredAttribute> attributes) {
-			this.typeIdentifier = Preconditions.checkNotNull(typeIdentifier, "Type identifier must not be null.");
+		public Builder(ObjectIdentifier objectIdentifier, List<StructuredAttribute> attributes) {
+			this.objectIdentifier = Preconditions.checkNotNull(objectIdentifier, "Object identifier must not be null.");
 			this.attributes = Collections.unmodifiableList(
 				new ArrayList<>(
 					Preconditions.checkNotNull(attributes, "Attributes must not be null.")));
@@ -200,7 +201,7 @@ public final class StructuredType extends UserDefinedType {
 		public StructuredType build() {
 			return new StructuredType(
 				isNullable,
-				typeIdentifier,
+				objectIdentifier,
 				attributes,
 				isFinal,
 				isInstantiable,
@@ -223,7 +224,7 @@ public final class StructuredType extends UserDefinedType {
 
 	private StructuredType(
 			boolean isNullable,
-			TypeIdentifier typeIdentifier,
+			ObjectIdentifier objectIdentifier,
 			List<StructuredAttribute> attributes,
 			boolean isFinal,
 			boolean isInstantiable,
@@ -234,7 +235,7 @@ public final class StructuredType extends UserDefinedType {
 		super(
 			isNullable,
 			LogicalTypeRoot.STRUCTURED_TYPE,
-			typeIdentifier,
+			objectIdentifier,
 			isFinal,
 			description);
 
@@ -269,7 +270,7 @@ public final class StructuredType extends UserDefinedType {
 	public LogicalType copy(boolean isNullable) {
 		return new StructuredType(
 			isNullable,
-			getTypeIdentifier(),
+			getObjectIdentifier(),
 			attributes.stream().map(StructuredAttribute::copy).collect(Collectors.toList()),
 			isFinal(),
 			isInstantiable,
diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/UserDefinedType.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/UserDefinedType.java
index 4509ab9..fde7eaa 100644
--- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/UserDefinedType.java
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/logical/UserDefinedType.java
@@ -19,11 +19,11 @@
 package org.apache.flink.table.types.logical;
 
 import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.catalog.ObjectIdentifier;
 import org.apache.flink.util.Preconditions;
 
 import javax.annotation.Nullable;
 
-import java.io.Serializable;
 import java.util.Objects;
 import java.util.Optional;
 
@@ -32,7 +32,7 @@ import java.util.Optional;
  * type is either a distinct type or a structured type.
  *
  * <p>A {@link UserDefinedType} instance is the result of a catalog lookup or an explicit definition.
- * Therefore, the serialized string representation is a unique {@link TypeIdentifier}.
+ * Therefore, the serialized string representation is a unique {@link ObjectIdentifier}.
  *
  * <p>NOTE: Compared to the SQL standard, this class and subclasses are incomplete. We might add new
  * features such as method declarations in the future.
@@ -43,83 +43,7 @@ import java.util.Optional;
 @PublicEvolving
 public abstract class UserDefinedType extends LogicalType {
 
-	/**
-	 * Fully qualifies a user-defined type. Two user-defined types are considered equal if they
-	 * share the same type identifier.
-	 */
-	public static final class TypeIdentifier implements Serializable {
-
-		private @Nullable String catalogName;
-
-		private @Nullable String databaseName;
-
-		private String typeName;
-
-		public TypeIdentifier(
-				@Nullable String catalogName,
-				@Nullable String databaseName,
-				String typeName) {
-			this.catalogName = catalogName;
-			this.databaseName = databaseName;
-			this.typeName = Preconditions.checkNotNull(typeName, "Type name must not be null.");
-		}
-
-		public TypeIdentifier(@Nullable String databaseName, String typeName) {
-			this(null, databaseName, typeName);
-		}
-
-		public TypeIdentifier(String typeName) {
-			this(null, null, typeName);
-		}
-
-		public Optional<String> getCatalogName() {
-			return Optional.ofNullable(catalogName);
-		}
-
-		public Optional<String> getDatabaseName() {
-			return Optional.ofNullable(databaseName);
-		}
-
-		public String getTypeName() {
-			return typeName;
-		}
-
-		@Override
-		public String toString() {
-			final StringBuilder sb = new StringBuilder();
-			if (catalogName != null) {
-				sb.append(escapeIdentifier(catalogName));
-				sb.append('.');
-			}
-			if (databaseName != null) {
-				sb.append(escapeIdentifier(databaseName));
-				sb.append('.');
-			}
-			sb.append(escapeIdentifier(typeName));
-			return sb.toString();
-		}
-
-		@Override
-		public boolean equals(Object o) {
-			if (this == o) {
-				return true;
-			}
-			if (o == null || getClass() != o.getClass()) {
-				return false;
-			}
-			TypeIdentifier that = (TypeIdentifier) o;
-			return Objects.equals(catalogName, that.catalogName) &&
-				Objects.equals(databaseName, that.databaseName) &&
-				typeName.equals(that.typeName);
-		}
-
-		@Override
-		public int hashCode() {
-			return Objects.hash(catalogName, databaseName, typeName);
-		}
-	}
-
-	private final TypeIdentifier typeIdentifier;
+	private final ObjectIdentifier objectIdentifier;
 
 	private final boolean isFinal;
 
@@ -128,17 +52,19 @@ public abstract class UserDefinedType extends LogicalType {
 	UserDefinedType(
 			boolean isNullable,
 			LogicalTypeRoot typeRoot,
-			TypeIdentifier typeIdentifier,
+			ObjectIdentifier objectIdentifier,
 			boolean isFinal,
 			@Nullable String description) {
 		super(isNullable, typeRoot);
-		this.typeIdentifier = Preconditions.checkNotNull(typeIdentifier, "Type identifier must not be null.");
+		this.objectIdentifier = Preconditions.checkNotNull(
+			objectIdentifier,
+			"Object identifier must not be null.");
 		this.isFinal = isFinal;
 		this.description = description;
 	}
 
-	public TypeIdentifier getTypeIdentifier() {
-		return typeIdentifier;
+	public ObjectIdentifier getObjectIdentifier() {
+		return objectIdentifier;
 	}
 
 	public boolean isFinal() {
@@ -151,7 +77,7 @@ public abstract class UserDefinedType extends LogicalType {
 
 	@Override
 	public String asSerializableString() {
-		return withNullability(typeIdentifier.toString());
+		return withNullability(objectIdentifier.asSerializableString());
 	}
 
 	@Override
@@ -167,12 +93,12 @@ public abstract class UserDefinedType extends LogicalType {
 		}
 		UserDefinedType that = (UserDefinedType) o;
 		return isFinal == that.isFinal &&
-			typeIdentifier.equals(that.typeIdentifier) &&
+			objectIdentifier.equals(that.objectIdentifier) &&
 			Objects.equals(description, that.description);
 	}
 
 	@Override
 	public int hashCode() {
-		return Objects.hash(super.hashCode(), typeIdentifier, isFinal, description);
+		return Objects.hash(super.hashCode(), objectIdentifier, isFinal, description);
 	}
 }
diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/utils/EncodingUtils.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/utils/EncodingUtils.java
index 7b03cd7..95d10e3 100644
--- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/utils/EncodingUtils.java
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/utils/EncodingUtils.java
@@ -51,6 +51,18 @@ public abstract class EncodingUtils {
 		// do not instantiate
 	}
 
+	public static String escapeBackticks(String s) {
+		return s.replace("`", "``");
+	}
+
+	public static String escapeSingleQuotes(String s) {
+		return s.replace("'", "''");
+	}
+
+	public static String escapeIdentifier(String s) {
+		return "`" + escapeBackticks(s) + "`";
+	}
+
 	public static String encodeObjectToString(Serializable obj) {
 		try {
 			final byte[] bytes = InstantiationUtil.serializeObject(obj);
diff --git a/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/LogicalTypesTest.java b/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/LogicalTypesTest.java
index a3d4edd..bdc9d90 100644
--- a/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/LogicalTypesTest.java
+++ b/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/LogicalTypesTest.java
@@ -24,6 +24,7 @@ import org.apache.flink.api.java.tuple.Tuple;
 import org.apache.flink.api.java.tuple.Tuple2;
 import org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer;
 import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.catalog.ObjectIdentifier;
 import org.apache.flink.table.expressions.TimeIntervalUnit;
 import org.apache.flink.table.expressions.TimePointUnit;
 import org.apache.flink.table.types.logical.AnyType;
@@ -53,7 +54,6 @@ import org.apache.flink.table.types.logical.TimestampKind;
 import org.apache.flink.table.types.logical.TimestampType;
 import org.apache.flink.table.types.logical.TinyIntType;
 import org.apache.flink.table.types.logical.TypeInformationAnyType;
-import org.apache.flink.table.types.logical.UserDefinedType;
 import org.apache.flink.table.types.logical.VarBinaryType;
 import org.apache.flink.table.types.logical.VarCharType;
 import org.apache.flink.table.types.logical.YearMonthIntervalType;
@@ -695,7 +695,7 @@ public class LogicalTypesTest {
 
 	private DistinctType createDistinctType(String typeName) {
 		return new DistinctType.Builder(
-				new UserDefinedType.TypeIdentifier("cat", "db", typeName),
+				ObjectIdentifier.of("cat", "db", typeName),
 				new DecimalType(10, 2))
 			.setDescription("Money type desc.")
 			.build();
@@ -707,7 +707,7 @@ public class LogicalTypesTest {
 
 	private StructuredType createHumanType(boolean useDifferentImplementation) {
 		return new StructuredType.Builder(
-				new UserDefinedType.TypeIdentifier("cat", "db", "Human"),
+				ObjectIdentifier.of("cat", "db", "Human"),
 				Collections.singletonList(
 					new StructuredType.StructuredAttribute("name", UDT_NAME_TYPE, "Description.")))
 			.setDescription("Human type desc.")
@@ -719,7 +719,7 @@ public class LogicalTypesTest {
 
 	private StructuredType createUserType(boolean isFinal) {
 		return new StructuredType.Builder(
-				new UserDefinedType.TypeIdentifier("cat", "db", "User"),
+				ObjectIdentifier.of("cat", "db", "User"),
 				Collections.singletonList(
 					new StructuredType.StructuredAttribute("setting", UDT_SETTING_TYPE)))
 			.setDescription("User type desc.")