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


##########
src/java/org/apache/cassandra/cql3/constraints/ColumnConstraints.java:
##########
@@ -101,9 +119,87 @@ public boolean hasRelevantConstraints()
         return false;
     }
 
-    public void checkInvalidConstraintsCombinations(ColumnIdentifier 
columnName)
+    public void checkInvalidConstraintsCombinations(ColumnIdentifier 
columnName, AbstractType<?> type)
     {
-        // TODO check duplicities etc CASSANDRA-20330
+        Set<String> constraintNames = new TreeSet<>();
+        Set<String> duplicateConstraints = new TreeSet<>();
+        List<ColumnConstraint<?>> scalarConstraints = new LinkedList<>();
+        for (ColumnConstraint<?> columnConstraint : constraints)
+        {
+            String constraintName = columnConstraint.name();
+            if (!constraintNames.add(constraintName))
+            {
+                if (columnConstraint.getConstraintType() == 
ConstraintType.SCALAR)
+                {
+                    Operator operator = ((ScalarColumnConstraint) 
columnConstraint).relationType();
+                    if (operator != NEQ)
+                        duplicateConstraints.add(constraintName);
+                }
+                else
+                {
+                    duplicateConstraints.add(constraintName);
+                }
+            }
+
+            if (columnConstraint.getConstraintType() == ConstraintType.SCALAR)
+                scalarConstraints.add(columnConstraint);
+        }
+
+        if (!duplicateConstraints.isEmpty())
+            throw new InvalidConstraintDefinitionException(format("There are 
duplicate constraint definitions on column '%s': %s",
+                                                                  columnName,
+                                                                  
duplicateConstraints));
+        if (scalarConstraints.size() > 2)

Review Comment:
   Not so sure about this particular one. Would something like:
   
   `CHECK val > -5 AND val !=0 AND val < 5`? Or `CHECK val != -5 AND val !=0 
AND val != 5`? I think there may be cases for more than 2 scalar constraints



##########
src/java/org/apache/cassandra/cql3/constraints/ColumnConstraints.java:
##########
@@ -101,9 +119,87 @@ public boolean hasRelevantConstraints()
         return false;
     }
 
-    public void checkInvalidConstraintsCombinations(ColumnIdentifier 
columnName)
+    public void checkInvalidConstraintsCombinations(ColumnIdentifier 
columnName, AbstractType<?> type)
     {
-        // TODO check duplicities etc CASSANDRA-20330
+        Set<String> constraintNames = new TreeSet<>();
+        Set<String> duplicateConstraints = new TreeSet<>();
+        List<ColumnConstraint<?>> scalarConstraints = new LinkedList<>();
+        for (ColumnConstraint<?> columnConstraint : constraints)
+        {
+            String constraintName = columnConstraint.name();
+            if (!constraintNames.add(constraintName))
+            {
+                if (columnConstraint.getConstraintType() == 
ConstraintType.SCALAR)
+                {
+                    Operator operator = ((ScalarColumnConstraint) 
columnConstraint).relationType();
+                    if (operator != NEQ)
+                        duplicateConstraints.add(constraintName);
+                }
+                else
+                {
+                    duplicateConstraints.add(constraintName);
+                }
+            }
+
+            if (columnConstraint.getConstraintType() == ConstraintType.SCALAR)
+                scalarConstraints.add(columnConstraint);
+        }
+
+        if (!duplicateConstraints.isEmpty())
+            throw new InvalidConstraintDefinitionException(format("There are 
duplicate constraint definitions on column '%s': %s",
+                                                                  columnName,
+                                                                  
duplicateConstraints));
+        if (scalarConstraints.size() > 2)
+            throw new InvalidConstraintDefinitionException(format("There can 
not be more than 2 scalar constraints on a column '%s' but you have specified 
%s",
+                                                                  columnName,
+                                                                  
scalarConstraints.size()));
+
+        for (ColumnConstraint<?> scalar : scalarConstraints)
+        {
+            Operator operator = ((ScalarColumnConstraint) 
scalar).relationType();
+
+            if (!ScalarColumnConstraint.SUPPORTED_OPERATORS.contains(operator))

Review Comment:
   I think this check should be done in the `validate` method of the 
ScalarColumnConstraint



##########
test/unit/org/apache/cassandra/contraints/ScalarSatisfiabilityTest.java:
##########
@@ -0,0 +1,159 @@
+/*
+ * 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.contraints;
+
+import org.junit.Test;
+
+import org.apache.cassandra.cql3.ColumnIdentifier;
+import org.apache.cassandra.cql3.Operator;
+import org.apache.cassandra.cql3.constraints.ColumnConstraints;
+import org.apache.cassandra.cql3.constraints.ScalarColumnConstraint;
+import org.apache.cassandra.cql3.constraints.ScalarColumnConstraint.Raw;
+import org.apache.cassandra.db.marshal.IntegerType;
+import org.assertj.core.api.ThrowableAssert;
+
+import static java.util.List.of;
+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.ScalarColumnConstraint.SUPPORTED_OPERATORS;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class ScalarSatisfiabilityTest
+{
+    private static final ColumnIdentifier columnIdentifier = new 
ColumnIdentifier("a_column", false);
+
+    @Test
+    public void testSatisfiability() throws Throwable
+    {
+        for (Operator op1 : SUPPORTED_OPERATORS)
+        {
+            for (Operator op2 : SUPPORTED_OPERATORS)
+            {
+                if (op1 == op2)
+                {
+                    if (op1 == NEQ)
+                    {
+                        // a_column != 0 and a_column != 10 -> valid
+                        check(op1, 0, op2, 10, null);
+                        // does not make sense to check twice
+                        // check a_column != 0 and a_column != 0
+                        check(op1, 0, op2, 0, String.format("There are 
duplicate constraint definitions on column '%s'.", columnIdentifier));
+                    }
+                    else
+                        check(op1, 0, op2, 10, "There are duplicate constraint 
definitions on column 'a_column': [a_column " + op1 + ']');
+                }
+                else if ((op1 == GT && op2 == GTE) ||
+                         (op1 == GTE && op2 == GT) ||
+                         (op1 == LT && op2 == LTE) ||
+                         (op1 == LTE && op2 == LT) ||
+                         (op1 == EQ || op2 == EQ))
+                {
+                    check(op1, 0, op2, 10, "Scalar constraints combination is 
not supported: a_column " + op1 + " 0, a_column " + op2 + " 10");
+                }
+                else if ((op1 == LTE && op2 == GT) ||
+                         (op1 == LT && op2 == GT) ||
+                         (op1 == LTE && op2 == GTE) ||
+                         (op1 == LT && op2 == GTE))
+                {
+                    check(op1, 0, op2, 10, "Scalar constraints are not 
satisfiable: a_column " + op1 + " 0, a_column " + op2 + " 10");
+                }
+                else if (!(op1 == NEQ || op2 == NEQ))
+                {
+                    check(op1, 0, op2, 10, null);
+                }
+                else
+                {
+                    // this is valid
+                    // a_column < 0, a_column != 10
+                }
+            }
+        }
+    }
+
+    @Test
+    public void testNumberOfScalarConstraints()
+    {
+        // one
+        new ColumnConstraints(of(get(LT, 
5))).checkInvalidConstraintsCombinations(columnIdentifier, 
IntegerType.instance);
+
+        // two
+        new ColumnConstraints(of(get(LT, 5), get(GT, 
0))).checkInvalidConstraintsCombinations(columnIdentifier, 
IntegerType.instance);
+
+        // three - invalid
+
+        assertThatThrownBy(() -> new ColumnConstraints(of(get(LT, 5), get(GT, 
0), get(GTE, 0)))
+                                 
.checkInvalidConstraintsCombinations(columnIdentifier, IntegerType.instance))
+        .hasMessage("There can not be more than 2 scalar constraints (not 
including non-equal relations) on a column 'a_column' but you have specified 
3");
+
+        // valid
+        new ColumnConstraints(of(get(LT, 5), get(GT, 0), get(NEQ, 3)))

Review Comment:
   Add a 
   ```
   new ColumnConstraints(of(get(EQ, 5), get(EQ, 0)))
           .checkInvalidConstraintsCombinations(columnIdentifier, 
IntegerType.instance);
   ```
   
   case



##########
src/java/org/apache/cassandra/cql3/constraints/ColumnConstraints.java:
##########
@@ -101,9 +119,110 @@ public boolean hasRelevantConstraints()
         return false;
     }
 
-    public void checkInvalidConstraintsCombinations(ColumnIdentifier 
columnName)
+    public void checkInvalidConstraintsCombinations(ColumnIdentifier 
columnName, AbstractType<?> type)
     {
-        // TODO check duplicities etc CASSANDRA-20330
+        Set<String> constraintNames = new TreeSet<>();
+        Set<String> duplicateConstraints = new TreeSet<>();
+        List<ColumnConstraint<?>> scalarConstraints = new LinkedList<>();
+        List<ScalarColumnConstraint> notEquals = new LinkedList<>();
+        for (ColumnConstraint<?> columnConstraint : constraints)
+        {
+            String constraintName = columnConstraint.name();
+            if (!constraintNames.add(constraintName))
+            {
+                if (columnConstraint.getConstraintType() == 
ConstraintType.SCALAR)
+                {
+                    ScalarColumnConstraint scalarColumnConstraint = 
(ScalarColumnConstraint) columnConstraint;
+                    Operator operator = scalarColumnConstraint.relationType();
+                    if (operator != NEQ)
+                    {
+                        duplicateConstraints.add(constraintName);
+                    }
+                }
+                else
+                {
+                    duplicateConstraints.add(constraintName);
+                }
+            }
+
+            if (columnConstraint.getConstraintType() == ConstraintType.SCALAR)
+            {
+                ScalarColumnConstraint scalarColumnConstraint = 
(ScalarColumnConstraint) columnConstraint;
+                scalarConstraints.add(scalarColumnConstraint);
+                if (scalarColumnConstraint.relationType() == NEQ)
+                    notEquals.add(scalarColumnConstraint);
+            }
+        }
+
+        if (!duplicateConstraints.isEmpty())
+            throw new InvalidConstraintDefinitionException(format("There are 
duplicate constraint definitions on column '%s': %s",
+                                                                  columnName,
+                                                                  
duplicateConstraints));
+        if ((scalarConstraints.size() - notEquals.size() > 2))

Review Comment:
   Nice. That last commit fixes the comment I was writing here :-)



##########
test/unit/org/apache/cassandra/contraints/ScalarSatisfiabilityTest.java:
##########
@@ -0,0 +1,159 @@
+/*
+ * 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.contraints;
+
+import org.junit.Test;
+
+import org.apache.cassandra.cql3.ColumnIdentifier;
+import org.apache.cassandra.cql3.Operator;
+import org.apache.cassandra.cql3.constraints.ColumnConstraints;
+import org.apache.cassandra.cql3.constraints.ScalarColumnConstraint;
+import org.apache.cassandra.cql3.constraints.ScalarColumnConstraint.Raw;
+import org.apache.cassandra.db.marshal.IntegerType;
+import org.assertj.core.api.ThrowableAssert;
+
+import static java.util.List.of;
+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.ScalarColumnConstraint.SUPPORTED_OPERATORS;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class ScalarSatisfiabilityTest

Review Comment:
   We also need tests for LENGHT and NOT NULL constraints.



##########
src/java/org/apache/cassandra/cql3/constraints/ColumnConstraints.java:
##########
@@ -101,9 +119,110 @@ public boolean hasRelevantConstraints()
         return false;
     }
 
-    public void checkInvalidConstraintsCombinations(ColumnIdentifier 
columnName)
+    public void checkInvalidConstraintsCombinations(ColumnIdentifier 
columnName, AbstractType<?> type)
     {
-        // TODO check duplicities etc CASSANDRA-20330
+        Set<String> constraintNames = new TreeSet<>();
+        Set<String> duplicateConstraints = new TreeSet<>();
+        List<ColumnConstraint<?>> scalarConstraints = new LinkedList<>();
+        List<ScalarColumnConstraint> notEquals = new LinkedList<>();
+        for (ColumnConstraint<?> columnConstraint : constraints)
+        {
+            String constraintName = columnConstraint.name();
+            if (!constraintNames.add(constraintName))
+            {
+                if (columnConstraint.getConstraintType() == 
ConstraintType.SCALAR)
+                {
+                    ScalarColumnConstraint scalarColumnConstraint = 
(ScalarColumnConstraint) columnConstraint;
+                    Operator operator = scalarColumnConstraint.relationType();
+                    if (operator != NEQ)

Review Comment:
   I think this check shouldn't only be added for scalar constraints. On 
checking duplicates by name, for function constraints, the relation is ignored, 
and I think something like this should also be valid:
   `CHECK LENGTH(val) != 0 AND LENGTH(val) != 10`
   
   Take this test for example:
   ```
   @Test
       public void testLength() throws Throwable
       {
           FunctionColumnConstraint l1 = new FunctionColumnConstraint.Raw(new 
ColumnIdentifier("LENGTH", false),
                                                                          new 
ColumnIdentifier("val", true),
                                                                          NEQ,
                                                                          
"10").prepare();
   
           FunctionColumnConstraint l2 = new FunctionColumnConstraint.Raw(new 
ColumnIdentifier("LENGTH", false),
                                                                          new 
ColumnIdentifier("val", true),
                                                                          NEQ,
                                                                          
"0").prepare();
   
   
           ColumnConstraints constraints = new ColumnConstraints(List.of(l1, 
l2));
           constraints.checkInvalidConstraintsCombinations(new 
ColumnIdentifier("val", true), UTF8Type.instance);
       }
   ```



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