yifan-c commented on code in PR #3562:
URL: https://github.com/apache/cassandra/pull/3562#discussion_r1849224015


##########
src/java/org/apache/cassandra/cql3/CqlConstraint.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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;
+
+
+import java.io.IOException;
+import java.util.HashSet;

Review Comment:
   unused import



##########
src/java/org/apache/cassandra/cql3/CqlConstraint.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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;
+
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import com.google.common.base.Objects;
+
+import org.apache.cassandra.cql3.terms.Term;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.io.IVersionedAsymmetricSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.schema.TableMetadata;
+
+public class CqlConstraint
+{
+    public ColumnIdentifier constraintName;

Review Comment:
   The variable is not referenced anywhere. Can we remove it if it is not 
strictly needed? 



##########
src/java/org/apache/cassandra/cql3/CqlConstraint.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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;
+
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import com.google.common.base.Objects;
+
+import org.apache.cassandra.cql3.terms.Term;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.io.IVersionedAsymmetricSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.schema.TableMetadata;
+
+public class CqlConstraint
+{
+    public ColumnIdentifier constraintName;
+    public final ColumnIdentifier columnName;
+    public final ConstraintCondition constraintCondition;
+
+    public static Serializer serializer = new Serializer();
+
+    public final static class Raw
+    {
+        public final ColumnIdentifier constraintName;
+        public final ConstraintCondition constraintCondition;
+        public ColumnIdentifier columnName;
+
+        public Raw(ConstraintCondition constraintCondition)
+        {
+            this.constraintName = null;
+            this.constraintCondition = constraintCondition;
+        }
+
+        public CqlConstraint prepare(ColumnIdentifier columnName)
+        {
+            return new CqlConstraint(constraintName, columnName, 
constraintCondition);
+        }
+
+        public CqlConstraint prepareWithName(ColumnIdentifier constraintName)
+        {
+            return new CqlConstraint(constraintName, null, 
constraintCondition);
+        }
+    }
+
+    public CqlConstraint(ColumnIdentifier constraintName, ColumnIdentifier 
columnName, ConstraintCondition constraintCondition)
+    {
+        if (constraintName == null)
+        {
+            final String randomConstraintName = 
UUID.randomUUID().toString().replace("-", "");
+            this.constraintName = new ColumnIdentifier(randomConstraintName, 
false);
+        }
+        else
+        {
+            this.constraintName = constraintName;
+        }
+        this.columnName = columnName;
+        this.constraintCondition = constraintCondition;
+    }
+
+    public void appendCqlTo(CqlBuilder builder)
+    {
+        builder.append(toString());
+    }
+
+    public void checkConstraint(Map<String, Term.Raw> columnValues, 
ColumnMetadata columnMetadata, TableMetadata tableMetadata)
+    {
+        constraintCondition.evaluate(columnValues, columnMetadata, 
tableMetadata);
+    }
+
+    public void validateConstraint(Map<String, ColumnMetadata> columnMetadata, 
TableMetadata tableMetadata)
+    {
+        constraintCondition.validate(columnMetadata, tableMetadata);
+    }
+
+    @Override
+    public String toString()
+    {
+        return constraintCondition.toString();

Review Comment:
   why `toString` only considers `constraintCondition`? There are other fields 
like `constraintName` and `columnName`. 



##########
doc/modules/cassandra/pages/developing/cql/constraints.adoc:
##########
@@ -0,0 +1,53 @@
+= Constraints
+
+Constraints provide a way of specifying and enforcing conditions at a
+column level in a table schema definition and enforcing them at write time.
+
+== CREATE CONSTRAINT
+
+Constraints can be created within the column definition, or as part
+of the table properties.
+
+[source,bnf]
+----
+CREATE TABLE keyspace.table (
+       name text,
+       i int CHECK (condition)
+       ...,
+
+);
+----
+
+
+== ALTER CONSTRAINT
+
+[source,bnf]
+----
+ALTER TABLE keyspace.table ALTER [columnName] CHECK (condition)

Review Comment:
   ```suggestion
   ALTER TABLE [IF EXISTS] <table> ALTER [IF EXISTS] <column> CHECK <condition>;
   ```
   



##########
doc/modules/cassandra/pages/developing/cql/constraints.adoc:
##########
@@ -0,0 +1,53 @@
+= Constraints
+
+Constraints provide a way of specifying and enforcing conditions at a
+column level in a table schema definition and enforcing them at write time.
+
+== CREATE CONSTRAINT
+
+Constraints can be created within the column definition, or as part
+of the table properties.
+
+[source,bnf]
+----
+CREATE TABLE keyspace.table (
+       name text,
+       i int CHECK (condition)
+       ...,
+
+);
+----
+
+
+== ALTER CONSTRAINT
+
+[source,bnf]
+----
+ALTER TABLE keyspace.table ALTER [columnName] CHECK (condition)
+----
+
+== DROP CONSTRAINT
+
+[source,bnf]
+----
+ALTER TABLE keyspace.table ALTER DROP [columnName] CHECK

Review Comment:
   ```suggestion
   ALTER TABLE [IF EXISTS] <table> ALTER [IF EXISTS] <column> DROP CHECK;
   ```



##########
src/java/org/apache/cassandra/cql3/CqlConstraint.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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;
+
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import com.google.common.base.Objects;
+
+import org.apache.cassandra.cql3.terms.Term;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.io.IVersionedAsymmetricSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.schema.TableMetadata;
+
+public class CqlConstraint
+{
+    public ColumnIdentifier constraintName;
+    public final ColumnIdentifier columnName;
+    public final ConstraintCondition constraintCondition;
+
+    public static Serializer serializer = new Serializer();
+
+    public final static class Raw
+    {
+        public final ColumnIdentifier constraintName;
+        public final ConstraintCondition constraintCondition;
+        public ColumnIdentifier columnName;
+
+        public Raw(ConstraintCondition constraintCondition)
+        {
+            this.constraintName = null;
+            this.constraintCondition = constraintCondition;
+        }
+
+        public CqlConstraint prepare(ColumnIdentifier columnName)
+        {
+            return new CqlConstraint(constraintName, columnName, 
constraintCondition);
+        }
+
+        public CqlConstraint prepareWithName(ColumnIdentifier constraintName)
+        {
+            return new CqlConstraint(constraintName, null, 
constraintCondition);
+        }
+    }
+
+    public CqlConstraint(ColumnIdentifier constraintName, ColumnIdentifier 
columnName, ConstraintCondition constraintCondition)
+    {
+        if (constraintName == null)
+        {
+            final String randomConstraintName = 
UUID.randomUUID().toString().replace("-", "");

Review Comment:
   - no `final`
   - consider prefixing the `columnName`, e.g. foo_uuid



##########
src/java/org/apache/cassandra/cql3/CqlConstraint.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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;
+
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import com.google.common.base.Objects;
+
+import org.apache.cassandra.cql3.terms.Term;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.io.IVersionedAsymmetricSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.schema.TableMetadata;
+
+public class CqlConstraint
+{
+    public ColumnIdentifier constraintName;
+    public final ColumnIdentifier columnName;
+    public final ConstraintCondition constraintCondition;
+
+    public static Serializer serializer = new Serializer();
+
+    public final static class Raw
+    {
+        public final ColumnIdentifier constraintName;
+        public final ConstraintCondition constraintCondition;
+        public ColumnIdentifier columnName;
+
+        public Raw(ConstraintCondition constraintCondition)
+        {
+            this.constraintName = null;
+            this.constraintCondition = constraintCondition;
+        }
+
+        public CqlConstraint prepare(ColumnIdentifier columnName)
+        {
+            return new CqlConstraint(constraintName, columnName, 
constraintCondition);
+        }
+
+        public CqlConstraint prepareWithName(ColumnIdentifier constraintName)
+        {
+            return new CqlConstraint(constraintName, null, 
constraintCondition);
+        }
+    }
+
+    public CqlConstraint(ColumnIdentifier constraintName, ColumnIdentifier 
columnName, ConstraintCondition constraintCondition)
+    {
+        if (constraintName == null)
+        {
+            final String randomConstraintName = 
UUID.randomUUID().toString().replace("-", "");
+            this.constraintName = new ColumnIdentifier(randomConstraintName, 
false);
+        }
+        else
+        {
+            this.constraintName = constraintName;
+        }
+        this.columnName = columnName;
+        this.constraintCondition = constraintCondition;
+    }
+
+    public void appendCqlTo(CqlBuilder builder)
+    {
+        builder.append(toString());
+    }
+
+    public void checkConstraint(Map<String, Term.Raw> columnValues, 
ColumnMetadata columnMetadata, TableMetadata tableMetadata)
+    {
+        constraintCondition.evaluate(columnValues, columnMetadata, 
tableMetadata);
+    }
+
+    public void validateConstraint(Map<String, ColumnMetadata> columnMetadata, 
TableMetadata tableMetadata)
+    {
+        constraintCondition.validate(columnMetadata, tableMetadata);
+    }
+
+    @Override
+    public String toString()
+    {
+        return constraintCondition.toString();
+    }
+
+    public String toCqlString()
+    {
+        return toString();
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return Objects.hashCode(constraintName);

Review Comment:
   `hasCode` and `equals` implementations are not using the same set of 
variables. 



##########
src/java/org/apache/cassandra/cql3/CqlConstraint.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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;
+
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import com.google.common.base.Objects;
+
+import org.apache.cassandra.cql3.terms.Term;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.io.IVersionedAsymmetricSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.schema.TableMetadata;
+
+public class CqlConstraint
+{
+    public ColumnIdentifier constraintName;
+    public final ColumnIdentifier columnName;
+    public final ConstraintCondition constraintCondition;
+
+    public static Serializer serializer = new Serializer();
+
+    public final static class Raw
+    {
+        public final ColumnIdentifier constraintName;
+        public final ConstraintCondition constraintCondition;
+        public ColumnIdentifier columnName;
+
+        public Raw(ConstraintCondition constraintCondition)
+        {
+            this.constraintName = null;
+            this.constraintCondition = constraintCondition;
+        }
+
+        public CqlConstraint prepare(ColumnIdentifier columnName)
+        {
+            return new CqlConstraint(constraintName, columnName, 
constraintCondition);
+        }
+
+        public CqlConstraint prepareWithName(ColumnIdentifier constraintName)
+        {
+            return new CqlConstraint(constraintName, null, 
constraintCondition);
+        }

Review Comment:
   unused



##########
src/java/org/apache/cassandra/cql3/CqlConstraint.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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;
+
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import com.google.common.base.Objects;
+
+import org.apache.cassandra.cql3.terms.Term;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.io.IVersionedAsymmetricSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.schema.TableMetadata;
+
+public class CqlConstraint
+{
+    public ColumnIdentifier constraintName;
+    public final ColumnIdentifier columnName;

Review Comment:
   According to the code, `columnName` is also removable. 



##########
src/antlr/Parser.g:
##########
@@ -779,12 +779,33 @@ tableDefinition[CreateTableStatement.Raw stmt]
     ;
 
 tableColumns[CreateTableStatement.Raw stmt]
-    @init { boolean isStatic = false; }
-    : k=ident v=comparatorType (K_STATIC { isStatic = true; })? 
(mask=columnMask)? { $stmt.addColumn(k, v, isStatic, mask); }
+    @init {
+        boolean isStatic = false;
+        List<CqlConstraint.Raw> columnConstraints = new ArrayList<>();
+    }
+    : k=ident v=comparatorType (K_STATIC { isStatic = true; })? 
(mask=columnMask)? (K_CHECK kconst=cqlConstraintExp[stmt] { 
columnConstraints.add(kconst); } (K_AND kconst=cqlConstraintExp[stmt] { 
columnConstraints.add(kconst); })* )? { $stmt.addColumn(k, v, isStatic, mask, 
columnConstraints); }
         (K_PRIMARY K_KEY { $stmt.setPartitionKeyColumn(k); })?
     | K_PRIMARY K_KEY '(' tablePartitionKey[stmt] (',' c=ident { 
$stmt.markClusteringColumn(c); } )* ')'
     ;

Review Comment:
   How about extracting the columnConstraints definition out? 
   
   ```suggestion
   tableColumns[CreateTableStatement.Raw stmt]
       @init { boolean isStatic = false; }
       : k=ident v=comparatorType (K_STATIC { isStatic = true; })?
           (mask=columnMask)? (constraints=columnConstraints)? { 
$stmt.addColumn(k, v, isStatic, mask, constraints); }
           (K_PRIMARY K_KEY { $stmt.setPartitionKeyColumn(k); })?
       | K_PRIMARY K_KEY '(' tablePartitionKey[stmt] (',' c=ident { 
$stmt.markClusteringColumn(c); } )* ')'
       ;
   
   columnConstraints returns [List<CqlConstraint.Raw>]
       @init { List<CqlConstraint.Raw> columnConstraints = new ArrayList<>(); }
       : K_CHECK kconst=cqlConstraintExp[stmt] { columnConstraints.add(kconst); 
} (K_AND kconst=cqlConstraintExp[stmt] { columnConstraints.add(kconst); })* 
       ;
   ```



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