You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "smiklosovic (via GitHub)" <gi...@apache.org> on 2025/03/13 15:27:46 UTC

[PR] CASSANDRA-20275 constraint for regular expression [cassandra]

smiklosovic opened a new pull request, #3979:
URL: https://github.com/apache/cassandra/pull/3979

   Thanks for sending a pull request! Here are some tips if you're new here:
    
    * Ensure you have added or run the [appropriate tests](https://cassandra.apache.org/_/development/testing.html) for your PR.
    * Be sure to keep the PR description updated to reflect all changes.
    * Write your PR title to summarize what this PR proposes.
    * If possible, provide a concise example to reproduce the issue for a faster review.
    * Read our [contributor guidelines](https://cassandra.apache.org/_/development/index.html)
    * If you're making a documentation change, see our [guide to documentation contribution](https://cassandra.apache.org/_/development/documentation.html)
    
   Commit messages should follow the following format:
   
   ```
   <One sentence description, usually Jira title or CHANGES.txt summary>
   
   <Optional lengthier description (context on patch)>
   
   patch by <Authors>; reviewed by <Reviewers> for CASSANDRA-#####
   
   Co-authored-by: Name1 <email1>
   Co-authored-by: Name2 <email2>
   
   ```
   
   The [Cassandra Jira](https://issues.apache.org/jira/projects/CASSANDRA/issues/)
   
   


-- 
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-unsubscribe@cassandra.apache.org

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


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


Re: [PR] CASSANDRA-20275 constraint for regular expression [cassandra]

Posted by "bbotella (via GitHub)" <gi...@apache.org>.
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: pr-unsubscribe@cassandra.apache.org

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


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


Re: [PR] CASSANDRA-20275 constraint for regular expression [cassandra]

Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
smiklosovic closed pull request #3979: CASSANDRA-20275 constraint for regular expression
URL: https://github.com/apache/cassandra/pull/3979


-- 
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-unsubscribe@cassandra.apache.org

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


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