smiklosovic commented on code in PR #3903: URL: https://github.com/apache/cassandra/pull/3903#discussion_r1963065773
########## 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: @bbotella that will be all possible. This check is for _one particular constraint function_. So it will be treated for e.g. scalar function, then it will be treated for lenght specifically ... for any function, separately. json function will have it own "satisfiability checker" and for that particular function, it is unary function, and it will be always satisfied. We do not need to look into any other function to decide if it is satisfiable or not. -- 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