smiklosovic commented on code in PR #3903: URL: https://github.com/apache/cassandra/pull/3903#discussion_r1958638812
########## 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: @bbotella that is covered already, this is a case of duplicity. -- 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