dawidwys commented on a change in pull request #10312: 
[FLINK-12996][table-common] Add type validators and strategies for FLIP-65
URL: https://github.com/apache/flink/pull/10312#discussion_r350171236
 
 

 ##########
 File path: 
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/validators/CompositeTypeValidator.java
 ##########
 @@ -0,0 +1,331 @@
+/*
+ * 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.validators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.functions.FunctionDefinition;
+import org.apache.flink.table.types.DataType;
+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.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.util.AbstractList;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * Validator that checks for set of {@link InputTypeValidator}s. It is able to 
combine function signature validators
+ * or single argument validators.
+ *
+ * <p>This validator offers different semantics depending on the passed {@link 
Composition}.
+ *
+ * <p>AND: Conjunction of multiple {@link InputTypeValidator}s into one like 
{@code f(NUMERIC) && f(LITERAL)}. Only
+ * the first validator is used for signature generation.
+ *
+ * <p>OR: Disjunction of multiple {@link InputTypeValidator}s into one like 
{@code f(NUMERIC) || f(STRING)}.
+ *
+ * <p>SEQUENCE: A sequence of {@link SingleInputTypeValidator}s for validating 
an entire function signature
+ * like {@code f(STRING, NUMERIC)}.
+ */
+@Internal
+public final class CompositeTypeValidator implements SingleInputTypeValidator {
+
+       private final Composition composition;
+
+       private final List<? extends InputTypeValidator> validators;
+
+       private final @Nullable List<String> argumentNames;
+
+       /**
+        * Kind of composition for combining {@link InputTypeValidator}s.
+        */
+       public enum Composition {
+               AND,
+               OR,
+               SEQUENCE
+       }
+
+       public CompositeTypeValidator(
+                       Composition composition,
+                       List<? extends InputTypeValidator> validators,
+                       @Nullable List<String> argumentNames) {
+               Preconditions.checkArgument(validators.size() > 0);
+               Preconditions.checkArgument(argumentNames == null || 
argumentNames.size() == validators.size());
+               this.composition = composition;
+               this.validators = validators;
+               this.argumentNames = argumentNames;
+       }
+
+       @Override
+       public boolean validateArgument(CallContext callContext, int 
argumentPos, int validatorPos, boolean throwOnFailure) {
+               final List<SingleInputTypeValidator> singleValidators = 
validators.stream()
+                       .map(v -> (SingleInputTypeValidator) v)
+                       .collect(Collectors.toList());
+
+               switch (composition) {
+                       case SEQUENCE:
+                               return singleValidators.get(validatorPos)
+                                       .validateArgument(callContext, 
argumentPos, 0, throwOnFailure);
+                       case AND:
+                               for (SingleInputTypeValidator validator : 
singleValidators) {
+                                       if 
(!validator.validateArgument(callContext, argumentPos, validatorPos, 
throwOnFailure)) {
+                                               return false;
+                                       }
+                               }
+                               return true;
+                       case OR:
+                               for (SingleInputTypeValidator validator : 
singleValidators) {
+                                       if 
(validator.validateArgument(callContext, argumentPos, validatorPos, false)) {
+                                               return true;
+                                       }
+                               }
+                               // generate a helpful exception if possible
+                               if (throwOnFailure) {
+                                       for (SingleInputTypeValidator validator 
: singleValidators) {
+                                               
validator.validateArgument(callContext, argumentPos, validatorPos, true);
+                                       }
+                               }
+                               return false;
+                       default:
+                               throw new IllegalStateException("Unsupported 
composition.");
+               }
+       }
+
+       @Override
+       public ArgumentCount getArgumentCount() {
+               switch (composition) {
+                       case SEQUENCE:
+                               return 
ConstantArgumentCount.of(validators.size());
+                       case AND:
+                       case OR:
+                       default:
+                               final List<ArgumentCount> counts = new 
AbstractList<ArgumentCount>() {
+                                       public ArgumentCount get(int index) {
+                                               return 
validators.get(index).getArgumentCount();
+                                       }
+
+                                       public int size() {
+                                               return validators.size();
+                                       }
+                               };
+                               final Integer min = commonMin(counts);
+                               final Integer max = commonMax(counts);
+                               final ArgumentCount compositeCount = new 
ArgumentCount() {
+                                       @Override
+                                       public boolean isValidCount(int count) {
+                                               switch (composition) {
+                                                       case AND:
+                                                               for 
(ArgumentCount c : counts) {
+                                                                       if 
(!c.isValidCount(count)) {
+                                                                               
return false;
+                                                                       }
+                                                               }
+                                                               return true;
+                                                       case OR:
+                                                       default:
+                                                               for 
(ArgumentCount c : counts) {
+                                                                       if 
(c.isValidCount(count)) {
+                                                                               
return true;
+                                                                       }
+                                                               }
+                                                               return false;
+                                               }
+                                       }
+
+                                       @Override
+                                       public Optional<Integer> getMinCount() {
+                                               return Optional.ofNullable(min);
+                                       }
+
+                                       @Override
+                                       public Optional<Integer> getMaxCount() {
+                                               return Optional.ofNullable(max);
+                                       }
+                               };
+
+                               // use constant count if applicable
 
 Review comment:
   Is this optimisation necessary? I mean I'm fine with having it, but maybe 
let's just add a comment that this is just an optimisation for range merging?

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


With regards,
Apache Git Services

Reply via email to