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


##########
src/java/org/apache/cassandra/cql3/constraints/RegexpConstraint.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.cassandra.cql3.ColumnIdentifier;
+import org.apache.cassandra.cql3.Operator;
+import org.apache.cassandra.cql3.functions.types.ParseUtils;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.AsciiType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.schema.ColumnMetadata;
+
+import static java.lang.String.format;
+import static org.apache.cassandra.cql3.Operator.EQ;
+import static org.apache.cassandra.cql3.Operator.NEQ;
+
+public class RegexpConstraint extends ConstraintFunction
+{
+    public static final String FUNCTION_NAME = "REGEXP";
+    private static final List<AbstractType<?>> SUPPORTED_TYPES = 
List.of(UTF8Type.instance, AsciiType.instance);
+    private static final List<Operator> ALLOWED_FUNCTION_OPERATORS = 
List.of(EQ, NEQ);
+
+    private Pattern pattern;
+
+    public RegexpConstraint(ColumnIdentifier columnName)
+    {
+        super(columnName, FUNCTION_NAME);
+    }
+
+    @Override
+    protected void internalEvaluate(AbstractType<?> valueType, Operator 
relationType, String regexp, ByteBuffer columnValue)
+    {
+        assert pattern != null;
+        Matcher matcher = pattern.matcher(valueType.getString(columnValue));
+
+        switch (relationType)
+        {
+            case EQ:
+                if (!matcher.matches())
+                    throw new ConstraintViolationException(format("Value does 
not match regular expression %s", regexp));
+                break;
+            case NEQ:
+                if (matcher.matches())
+                    throw new ConstraintViolationException(format("Value does 
match regular expression %s", regexp));
+                break;
+            default:
+                throw new IllegalStateException("Unsupported operator: " + 
relationType);
+        }
+    }
+
+    @Override
+    public List<AbstractType<?>> getSupportedTypes()
+    {
+        return SUPPORTED_TYPES;
+    }
+
+    @Override
+    public List<Operator> getSupportedOperators()
+    {
+        return ALLOWED_FUNCTION_OPERATORS;
+    }
+
+    @Override
+    public void validate(ColumnMetadata columnMetadata, String regexp) throws 
InvalidConstraintDefinitionException
+    {
+        try
+        {
+            pattern = Pattern.compile(ParseUtils.unquote(regexp));

Review Comment:
   Not an issue, but maybe just worth adding a note on why we are doing a bit 
more than just "validating" (we are also assigning the pattern).



##########
pylib/cqlshlib/test/test_cqlsh_completion.py:
##########
@@ -1109,7 +1109,7 @@ def test_complete_in_alter_table(self):
                             other_choices_ok=True)
         self.trycompletions('ALTER TABLE new_table ADD col int C', 
immediate='HECK ')
         self.trycompletions('ALTER TABLE new_table ADD col int CHECK ',
-                            choices=['<identifier', '<quotedName>', 'JSON', 
'LENGTH', 'NOT_NULL', 'OCTET_LENGTH'],
+                            choices=['<identifier', '<quotedName>', 'JSON', 
'LENGTH', 'NOT_NULL', 'OCTET_LENGTH', 'REGEXP'],

Review Comment:
   Note to myself: Make sure this is rebased for the typo fix



##########
pylib/cqlshlib/cql3handling.py:
##########
@@ -334,6 +334,7 @@ def dequote_value(cqlword):
 
 <constraintComparableFunction> ::= "LENGTH"
                                  | "OCTET_LENGTH"
+                                 | "REGEXP"

Review Comment:
   Thanks for adding this as well



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to