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/11/26 14:49:49 UTC

[flink] 02/06: [FLINK-12996][table-common] Require equals/hashCode for type inference classes

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 edf440a91143091af50285fdee8c3b3d49d13070
Author: Timo Walther <tw...@apache.org>
AuthorDate: Mon Nov 25 10:17:32 2019 +0100

    [FLINK-12996][table-common] Require equals/hashCode for type inference classes
---
 .../flink/table/types/inference/ArgumentCount.java |  2 +
 .../types/inference/ConstantArgumentCount.java     | 98 ++++++++++++++++++++++
 .../table/types/inference/InputTypeValidator.java  |  5 +-
 .../flink/table/types/inference/TypeStrategy.java  |  2 +
 .../inference/validators/PassingTypeValidator.java | 28 +++----
 5 files changed, 116 insertions(+), 19 deletions(-)

diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/ArgumentCount.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/ArgumentCount.java
index ffa65fa..b1e9cae 100644
--- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/ArgumentCount.java
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/ArgumentCount.java
@@ -24,6 +24,8 @@ import java.util.Optional;
 
 /**
  * Defines the count of accepted arguments (including open intervals) that a function can take.
+ *
+ * <p>Note: Implementations should implement {@link Object#hashCode()} and {@link Object#equals(Object)}.
  */
 @PublicEvolving
 public interface ArgumentCount {
diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/ConstantArgumentCount.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/ConstantArgumentCount.java
new file mode 100644
index 0000000..6cfa439
--- /dev/null
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/ConstantArgumentCount.java
@@ -0,0 +1,98 @@
+/*
+ * 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.types.inference;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.util.Preconditions;
+
+import java.util.Objects;
+import java.util.Optional;
+
+/**
+ * Helper class for {@link ArgumentCount} with constant boundaries.
+ */
+@Internal
+public final class ConstantArgumentCount implements ArgumentCount {
+
+	private static final int OPEN_INTERVAL = -1;
+
+	private final int minCount;
+
+	private final int maxCount;
+
+	private ConstantArgumentCount(int minCount, int maxCount) {
+		this.minCount = minCount;
+		this.maxCount = maxCount;
+	}
+
+	public static ArgumentCount of(int count) {
+		Preconditions.checkArgument(count >= 0);
+		return new ConstantArgumentCount(count, count);
+	}
+
+	public static ArgumentCount between(int minCount, int maxCount) {
+		Preconditions.checkArgument(minCount <= maxCount);
+		Preconditions.checkArgument(minCount >= 0);
+		return new ConstantArgumentCount(minCount, maxCount);
+	}
+
+	public static ArgumentCount from(int minCount) {
+		Preconditions.checkArgument(minCount >= 0);
+		return new ConstantArgumentCount(minCount, OPEN_INTERVAL);
+	}
+
+	public static ArgumentCount any() {
+		return new ConstantArgumentCount(0, OPEN_INTERVAL);
+	}
+
+	@Override
+	public boolean isValidCount(int count) {
+		return count >= minCount && (maxCount == OPEN_INTERVAL || count <= maxCount);
+	}
+
+	@Override
+	public Optional<Integer> getMinCount() {
+		return Optional.of(minCount);
+	}
+
+	@Override
+	public Optional<Integer> getMaxCount() {
+		if (maxCount == OPEN_INTERVAL) {
+			return Optional.empty();
+		}
+		return Optional.of(maxCount);
+	}
+
+	@Override
+	public boolean equals(Object o) {
+		if (this == o) {
+			return true;
+		}
+		if (o == null || getClass() != o.getClass()) {
+			return false;
+		}
+		ConstantArgumentCount that = (ConstantArgumentCount) o;
+		return minCount == that.minCount && maxCount == that.maxCount;
+	}
+
+	@Override
+	public int hashCode() {
+		return Objects.hash(minCount, maxCount);
+	}
+}
diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/InputTypeValidator.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/InputTypeValidator.java
index b3bf89f..52687fe 100644
--- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/InputTypeValidator.java
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/InputTypeValidator.java
@@ -27,6 +27,8 @@ import java.util.List;
 /**
  * Validator for checking the input data types of a function call.
  *
+ * <p>Note: Implementations should implement {@link Object#hashCode()} and {@link Object#equals(Object)}.
+ *
  * @see InputTypeValidators
  */
 @PublicEvolving
@@ -44,7 +46,8 @@ public interface InputTypeValidator {
 	 * @param callContext provides details about the function call
 	 * @param throwOnFailure whether this function is allowed to throw an {@link ValidationException}
 	 *                       with a meaningful exception in case the validation is not successful or
-	 *                       if this function should simple return {@code false}.
+	 *                       if this function should simply return {@code false}.
+	 * @see CallContext#newValidationError(String, Object...)
 	 */
 	boolean validate(CallContext callContext, boolean throwOnFailure);
 
diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/TypeStrategy.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/TypeStrategy.java
index 84b7397..764994f 100644
--- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/TypeStrategy.java
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/TypeStrategy.java
@@ -27,6 +27,8 @@ import java.util.Optional;
  * Strategy for inferring the data type of a function call. The inferred type might describe the
  * final result or an intermediate result (accumulation type) of a function.
  *
+ * <p>Note: Implementations should implement {@link Object#hashCode()} and {@link Object#equals(Object)}.
+ *
  * @see TypeStrategies
  */
 @PublicEvolving
diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/validators/PassingTypeValidator.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/validators/PassingTypeValidator.java
index 0cd8cd4..a14f451 100644
--- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/validators/PassingTypeValidator.java
+++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/validators/PassingTypeValidator.java
@@ -22,21 +22,21 @@ import org.apache.flink.annotation.Internal;
 import org.apache.flink.table.functions.FunctionDefinition;
 import org.apache.flink.table.types.inference.ArgumentCount;
 import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
 import org.apache.flink.table.types.inference.InputTypeValidator;
 import org.apache.flink.table.types.inference.Signature;
 import org.apache.flink.table.types.inference.Signature.Argument;
 
 import java.util.Collections;
 import java.util.List;
-import java.util.Optional;
 
 /**
  * Validator that does not perform any validation and always passes.
  */
 @Internal
-public class PassingTypeValidator implements InputTypeValidator {
+public final class PassingTypeValidator implements InputTypeValidator {
 
-	private static final PassingArgumentCount PASSING_ARGUMENT_COUNT = new PassingArgumentCount();
+	private static final ArgumentCount PASSING_ARGUMENT_COUNT = ConstantArgumentCount.any();
 
 	@Override
 	public ArgumentCount getArgumentCount() {
@@ -53,21 +53,13 @@ public class PassingTypeValidator implements InputTypeValidator {
 		return Collections.singletonList(Signature.of(Argument.of("*")));
 	}
 
-	private static class PassingArgumentCount implements ArgumentCount {
-
-		@Override
-		public boolean isValidCount(int count) {
-			return true;
-		}
-
-		@Override
-		public Optional<Integer> getMinCount() {
-			return Optional.empty();
-		}
+	@Override
+	public boolean equals(Object o) {
+		return this == o || o instanceof PassingTypeValidator;
+	}
 
-		@Override
-		public Optional<Integer> getMaxCount() {
-			return Optional.empty();
-		}
+	@Override
+	public int hashCode() {
+		return PassingTypeValidator.class.hashCode();
 	}
 }