bbotella commented on code in PR #3903:
URL: https://github.com/apache/cassandra/pull/3903#discussion_r1962788865


##########
src/java/org/apache/cassandra/cql3/constraints/AbstractFunctionSatisfiabilityChecker.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * 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.cassandra.cql3.constraints;
+
+import java.nio.ByteBuffer;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.apache.cassandra.cql3.Operator;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.utils.Pair;
+
+import static java.lang.String.format;
+import static org.apache.cassandra.cql3.Operator.EQ;
+import static org.apache.cassandra.cql3.Operator.GT;
+import static org.apache.cassandra.cql3.Operator.GTE;
+import static org.apache.cassandra.cql3.Operator.LT;
+import static org.apache.cassandra.cql3.Operator.LTE;
+import static org.apache.cassandra.cql3.Operator.NEQ;
+import static 
org.apache.cassandra.cql3.constraints.ColumnConstraint.ConstraintType.FUNCTION;
+
+public abstract class AbstractFunctionSatisfiabilityChecker<CONSTRAINT_TYPE 
extends AbstractFunctionConstraint<CONSTRAINT_TYPE>>
+{
+    /**
+     * Performs check if constraints are satisfiable or not.
+     *
+     * @param functionName   name of function
+     * @param constraints    list of constraints to set
+     * @param columnMetadata metadata of a column.
+     */
+    public void check(String functionName, List<ColumnConstraint<?>> 
constraints, ColumnMetadata columnMetadata)
+    {
+        Pair<List<CONSTRAINT_TYPE>, List<CONSTRAINT_TYPE>> filteredConstraints 
= categorizeConstraints(constraints, functionName);
+
+        if (filteredConstraints.left.isEmpty())
+            return;
+
+        checkNumberOfConstraints(columnMetadata, filteredConstraints);
+        checkSupportedOperators(filteredConstraints.left, functionName);
+        ensureSatisfiability(columnMetadata, functionName, 
filteredConstraints.left);
+    }
+
+    /**
+     * Categorizes given constraints into two lists. The first list, the left 
one in Pair, contains all
+     * constraints of implementation-specific {@link 
org.apache.cassandra.cql3.constraints.ColumnConstraint.ConstraintType}.
+     * The second list, the right one in Pair, contains all constraints of 
such constraint type which do have "not equal" operator.
+     *
+     * @param constraints  constraints to categorize
+     * @param functionName name of function
+     * @return pair of categorized constraints
+     */
+    abstract Pair<List<CONSTRAINT_TYPE>, List<CONSTRAINT_TYPE>> 
categorizeConstraints(List<ColumnConstraint<?>> constraints, String 
functionName);
+
+    private void checkSupportedOperators(List<CONSTRAINT_TYPE> allConstraints, 
String functionName)
+    {
+        for (CONSTRAINT_TYPE constraint : allConstraints)
+        {
+            if 
(!constraint.getSupportedOperators().contains(constraint.relationType()))
+                throw new InvalidConstraintDefinitionException(format("%s 
constraint of relation '%s' is not supported.", functionName, 
constraint.relationType()));
+        }
+    }
+
+    /**
+     * Checks if there are no duplicate constraints having same operator.
+     *
+     * @param columnMetadata      medata of a column
+     * @param filteredConstraints pair of all constraints and all constraints 
having not-equal operator
+     */
+    private void checkNumberOfConstraints(ColumnMetadata columnMetadata, 
Pair<List<CONSTRAINT_TYPE>, List<CONSTRAINT_TYPE>> filteredConstraints)
+    {
+        List<? extends AbstractFunctionConstraint<CONSTRAINT_TYPE>> 
allConstraints = filteredConstraints.left;
+        List<? extends AbstractFunctionConstraint<CONSTRAINT_TYPE>> 
notEqualConstraints = filteredConstraints.right;
+
+        if ((allConstraints.size() - notEqualConstraints.size() > 2))
+        {
+            throw new InvalidConstraintDefinitionException(format("There can 
not be more than 2 constraints (not including non-equal relations) on a column 
'%s' but you have specified %s",

Review Comment:
   This has me worried after the counter example we discussed offline:
   ```
   alter ks.tb alter val check json(val) and length(val) < 1000 and 
length(val)>100
   ```
   
   Should be a valid constraint.



##########
src/java/org/apache/cassandra/cql3/constraints/ColumnConstraint.java:
##########
@@ -46,32 +53,57 @@ public enum ConstraintType
         // The order of that enum matters!!
         // We are serializing its enum position instead of its name.
         // Changing this enum would affect how that int is interpreted when 
deserializing.
-        COMPOSED(ColumnConstraints.serializer),
-        FUNCTION(FunctionColumnConstraint.serializer),
-        SCALAR(ScalarColumnConstraint.serializer),
-        UNARY_FUNCTION(UnaryFunctionColumnConstraint.serializer);
+        COMPOSED(ColumnConstraints.serializer, new DuplicatesChecker()),
+        FUNCTION(FunctionColumnConstraint.serializer, 
FunctionColumnConstraint.Functions.values()),
+        SCALAR(ScalarColumnConstraint.serializer, new 
ScalarColumnConstraintSatisfiabilityChecker()),
+        UNARY_FUNCTION(UnaryFunctionColumnConstraint.serializer, 
UnaryFunctionColumnConstraint.Functions.values());
 
         private final MetadataSerializer<?> serializer;
+        private final SatisfiabilityChecker[] satisfiabilityCheckers;
+
+        ConstraintType(MetadataSerializer<?> serializer, SatisfiabilityChecker 
satisfiabilityChecker)
+        {
+            this(serializer, new SatisfiabilityChecker[]{ 
satisfiabilityChecker });
+        }
 
-        ConstraintType(MetadataSerializer<?> serializer)
+        ConstraintType(MetadataSerializer<?> serializer, 
SatisfiabilityChecker[] satisfiabilityCheckers)
         {
             this.serializer = serializer;
+            this.satisfiabilityCheckers = satisfiabilityCheckers;
         }
 
         public static MetadataSerializer<?> getSerializer(int i)
         {
             return ConstraintType.values()[i].serializer;
         }
+
+        private static SatisfiabilityChecker[] getSatisfiabilityCheckers()
+        {
+            List<SatisfiabilityChecker> result = new ArrayList<>();
+            for (ConstraintType constraintType : ConstraintType.values())
+                
result.addAll(Arrays.asList(constraintType.satisfiabilityCheckers));
+
+            return result.toArray(new SatisfiabilityChecker[0]);
+        }
+    }
+
+    public abstract String name();
+
+    public String fullName()

Review Comment:
   We probably don't need this method



##########
src/java/org/apache/cassandra/cql3/constraints/ColumnConstraint.java:
##########
@@ -46,32 +53,57 @@ public enum ConstraintType
         // The order of that enum matters!!
         // We are serializing its enum position instead of its name.
         // Changing this enum would affect how that int is interpreted when 
deserializing.
-        COMPOSED(ColumnConstraints.serializer),
-        FUNCTION(FunctionColumnConstraint.serializer),
-        SCALAR(ScalarColumnConstraint.serializer),
-        UNARY_FUNCTION(UnaryFunctionColumnConstraint.serializer);
+        COMPOSED(ColumnConstraints.serializer, new DuplicatesChecker()),
+        FUNCTION(FunctionColumnConstraint.serializer, 
FunctionColumnConstraint.Functions.values()),
+        SCALAR(ScalarColumnConstraint.serializer, new 
ScalarColumnConstraintSatisfiabilityChecker()),
+        UNARY_FUNCTION(UnaryFunctionColumnConstraint.serializer, 
UnaryFunctionColumnConstraint.Functions.values());
 
         private final MetadataSerializer<?> serializer;
+        private final SatisfiabilityChecker[] satisfiabilityCheckers;
+
+        ConstraintType(MetadataSerializer<?> serializer, SatisfiabilityChecker 
satisfiabilityChecker)
+        {
+            this(serializer, new SatisfiabilityChecker[]{ 
satisfiabilityChecker });
+        }
 
-        ConstraintType(MetadataSerializer<?> serializer)
+        ConstraintType(MetadataSerializer<?> serializer, 
SatisfiabilityChecker[] satisfiabilityCheckers)
         {
             this.serializer = serializer;
+            this.satisfiabilityCheckers = satisfiabilityCheckers;
         }
 
         public static MetadataSerializer<?> getSerializer(int i)
         {
             return ConstraintType.values()[i].serializer;
         }
+
+        private static SatisfiabilityChecker[] getSatisfiabilityCheckers()
+        {
+            List<SatisfiabilityChecker> result = new ArrayList<>();
+            for (ConstraintType constraintType : ConstraintType.values())
+                
result.addAll(Arrays.asList(constraintType.satisfiabilityCheckers));
+
+            return result.toArray(new SatisfiabilityChecker[0]);
+        }
+    }
+
+    public abstract String name();
+
+    public String fullName()
+    {
+        return name();
     }
 
     public abstract MetadataSerializer<T> serializer();
 
     public abstract void appendCqlTo(CqlBuilder builder);
 
+    public abstract boolean enablesDuplicateDefinitions(String name);
+
     /**
      * Method that evaluates the condition. It can either succeed or throw a 
{@link ConstraintViolationException}.
      *
-     * @param valueType value type of the column value under test
+     * @param valueType   value type of the column value under test

Review Comment:
   Remove extra space



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

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to