dcapwell commented on code in PR #2195:
URL: https://github.com/apache/cassandra/pull/2195#discussion_r1131885188


##########
src/java/org/apache/cassandra/cql3/StatementSource.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.util.Objects;
+import java.util.regex.Pattern;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import com.google.common.base.Preconditions;
+
+import org.antlr.runtime.Token;
+import org.apache.cassandra.cql3.statements.QualifiedStatement;
+
+public class StatementSource
+{
+    public static final StatementSource INTERNAL = 
Bounds.INTERNAL.describeSource(null);
+
+    public final String text;
+    public final Bounds bounds;
+
+    public StatementSource(@Nullable String text, @Nonnull Bounds bounds)
+    {
+        Objects.requireNonNull(bounds);
+        this.text = text;
+        this.bounds = bounds;
+    }
+
+    public String describe(boolean mayIncludeSourceText)
+    {
+        if (this == INTERNAL)
+        {
+            return "<<<internal statement>>>";
+        }
+        else
+        {
+            if (bounds != Bounds.INTERNAL && !bounds.isEmpty() && text != null 
&& mayIncludeSourceText)
+                return String.format("at %d:%d - %s", bounds.startLine + 1, 
bounds.startChar + 1, text);
+            else if (bounds != Bounds.INTERNAL && !bounds.isEmpty())
+                return String.format("at %d:%d", bounds.startLine + 1, 
bounds.startChar + 1);
+            else if (text != null && !bounds.isEmpty() && mayIncludeSourceText)
+                return String.format("%s", text);
+            else
+                return "";
+        }
+    }
+
+    public static Bounds createBounds(Token startToken, Token endToken)
+    {
+        Objects.requireNonNull(startToken);
+        Objects.requireNonNull(endToken);
+
+        int startLine, startChar, endLine, endChar;
+
+        if (startToken.getType() == Token.EOF)
+        {
+            startLine = Character.MAX_VALUE;
+            startChar = Character.MAX_VALUE;
+        }
+        else
+        {
+            startLine = Math.min(Math.max(startToken.getLine(), 1) - 1, 
Character.MAX_VALUE);
+            startChar = Math.min(Math.max(startToken.getCharPositionInLine(), 
0), Character.MAX_VALUE);
+        }
+
+        if (endToken.getType() == Token.EOF)
+        {
+            endLine = Character.MAX_VALUE;
+            endChar = Character.MAX_VALUE;
+        }
+        else
+        {
+            endLine = Math.min(Math.max(endToken.getLine(), 1) - 1, 
Character.MAX_VALUE);
+            endChar = Math.min(Math.max(endToken.getCharPositionInLine(), 0), 
Character.MAX_VALUE);
+        }
+
+        return new Bounds(startLine, startChar, endLine, endChar);
+    }
+
+    public static class Bounds
+    {
+        public static final Bounds INTERNAL = new Bounds(0, 0, 
Character.MAX_VALUE, Character.MAX_VALUE);
+
+        private static final Pattern LINE_BREAK = Pattern.compile("\\R");
+        private static final String SEPARATOR = " ";
+
+        private final int startLine;
+        private final int startChar;
+        private final int endLine;
+        private final int endChar;
+
+        private Bounds(int startLine, int startChar, int endLine, int endChar)
+        {
+            Preconditions.checkArgument(startLine >= 0 && startLine <= 
Character.MAX_VALUE);
+            Preconditions.checkArgument(startChar >= 0 && startChar <= 
Character.MAX_VALUE);
+            Preconditions.checkArgument(endLine >= 0 && endLine <= 
Character.MAX_VALUE);
+            Preconditions.checkArgument(endChar >= 0 && endChar <= 
Character.MAX_VALUE);
+
+            this.startLine = startLine;
+            this.startChar = startChar;
+            this.endLine = endLine;
+            this.endChar = endChar;
+        }
+
+        private boolean isEmpty()
+        {
+            return startLine == Character.MAX_VALUE ||
+                   startLine > endLine ||

Review Comment:
   what is the case that this happens?  this feels like a bug, should we not 
fail?



##########
src/java/org/apache/cassandra/cql3/StatementSource.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.util.Objects;
+import java.util.regex.Pattern;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import com.google.common.base.Preconditions;
+
+import org.antlr.runtime.Token;
+import org.apache.cassandra.cql3.statements.QualifiedStatement;
+
+public class StatementSource
+{
+    public static final StatementSource INTERNAL = 
Bounds.INTERNAL.describeSource(null);
+
+    public final String text;
+    public final Bounds bounds;
+
+    public StatementSource(@Nullable String text, @Nonnull Bounds bounds)
+    {
+        Objects.requireNonNull(bounds);
+        this.text = text;
+        this.bounds = bounds;
+    }
+
+    public String describe(boolean mayIncludeSourceText)
+    {
+        if (this == INTERNAL)
+        {
+            return "<<<internal statement>>>";
+        }
+        else
+        {
+            if (bounds != Bounds.INTERNAL && !bounds.isEmpty() && text != null 
&& mayIncludeSourceText)
+                return String.format("at %d:%d - %s", bounds.startLine + 1, 
bounds.startChar + 1, text);
+            else if (bounds != Bounds.INTERNAL && !bounds.isEmpty())
+                return String.format("at %d:%d", bounds.startLine + 1, 
bounds.startChar + 1);
+            else if (text != null && !bounds.isEmpty() && mayIncludeSourceText)
+                return String.format("%s", text);
+            else
+                return "";
+        }
+    }
+
+    public static Bounds createBounds(Token startToken, Token endToken)
+    {
+        Objects.requireNonNull(startToken);
+        Objects.requireNonNull(endToken);
+
+        int startLine, startChar, endLine, endChar;
+
+        if (startToken.getType() == Token.EOF)
+        {
+            startLine = Character.MAX_VALUE;
+            startChar = Character.MAX_VALUE;
+        }
+        else
+        {
+            startLine = Math.min(Math.max(startToken.getLine(), 1) - 1, 
Character.MAX_VALUE);
+            startChar = Math.min(Math.max(startToken.getCharPositionInLine(), 
0), Character.MAX_VALUE);
+        }
+
+        if (endToken.getType() == Token.EOF)
+        {
+            endLine = Character.MAX_VALUE;
+            endChar = Character.MAX_VALUE;
+        }
+        else
+        {
+            endLine = Math.min(Math.max(endToken.getLine(), 1) - 1, 
Character.MAX_VALUE);
+            endChar = Math.min(Math.max(endToken.getCharPositionInLine(), 0), 
Character.MAX_VALUE);
+        }
+
+        return new Bounds(startLine, startChar, endLine, endChar);
+    }
+
+    public static class Bounds
+    {
+        public static final Bounds INTERNAL = new Bounds(0, 0, 
Character.MAX_VALUE, Character.MAX_VALUE);
+
+        private static final Pattern LINE_BREAK = Pattern.compile("\\R");
+        private static final String SEPARATOR = " ";
+
+        private final int startLine;
+        private final int startChar;
+        private final int endLine;
+        private final int endChar;
+
+        private Bounds(int startLine, int startChar, int endLine, int endChar)
+        {
+            Preconditions.checkArgument(startLine >= 0 && startLine <= 
Character.MAX_VALUE);
+            Preconditions.checkArgument(startChar >= 0 && startChar <= 
Character.MAX_VALUE);
+            Preconditions.checkArgument(endLine >= 0 && endLine <= 
Character.MAX_VALUE);
+            Preconditions.checkArgument(endChar >= 0 && endChar <= 
Character.MAX_VALUE);
+
+            this.startLine = startLine;
+            this.startChar = startChar;
+            this.endLine = endLine;
+            this.endChar = endChar;
+        }
+
+        private boolean isEmpty()
+        {
+            return startLine == Character.MAX_VALUE ||
+                   startLine > endLine ||
+                   startLine == endLine && (startChar == Character.MAX_VALUE 
|| startChar > endChar);

Review Comment:
   `startChar > endChar` feels like a bug



##########
src/java/org/apache/cassandra/cql3/StatementSource.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.util.Objects;
+import java.util.regex.Pattern;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import com.google.common.base.Preconditions;
+
+import org.antlr.runtime.Token;
+import org.apache.cassandra.cql3.statements.QualifiedStatement;
+
+public class StatementSource
+{
+    public static final StatementSource INTERNAL = 
Bounds.INTERNAL.describeSource(null);
+
+    public final String text;
+    public final Bounds bounds;
+
+    public StatementSource(@Nullable String text, @Nonnull Bounds bounds)
+    {
+        Objects.requireNonNull(bounds);
+        this.text = text;
+        this.bounds = bounds;
+    }
+
+    public String describe(boolean mayIncludeSourceText)
+    {
+        if (this == INTERNAL)
+        {
+            return "<<<internal statement>>>";
+        }
+        else
+        {
+            if (bounds != Bounds.INTERNAL && !bounds.isEmpty() && text != null 
&& mayIncludeSourceText)
+                return String.format("at %d:%d - %s", bounds.startLine + 1, 
bounds.startChar + 1, text);
+            else if (bounds != Bounds.INTERNAL && !bounds.isEmpty())
+                return String.format("at %d:%d", bounds.startLine + 1, 
bounds.startChar + 1);
+            else if (text != null && !bounds.isEmpty() && mayIncludeSourceText)
+                return String.format("%s", text);
+            else
+                return "";
+        }
+    }
+
+    public static Bounds createBounds(Token startToken, Token endToken)
+    {
+        Objects.requireNonNull(startToken);
+        Objects.requireNonNull(endToken);
+
+        int startLine, startChar, endLine, endChar;
+
+        if (startToken.getType() == Token.EOF)
+        {
+            startLine = Character.MAX_VALUE;

Review Comment:
   if start token is EOF do we not know that endToken is as well?



##########
src/java/org/apache/cassandra/cql3/statements/DeleteStatement.java:
##########
@@ -174,7 +186,8 @@ protected ModificationStatement prepareInternal(ClientState 
state,
                                                        operations,
                                                        restrictions,
                                                        conditions,
-                                                       attrs);
+                                                       attrs,
+                                                       
bounds.describeSource(enclosingStatement));

Review Comment:
   this feels backwards, we create the underline statements before we create 
the composite statement, so how do we pass it in?



##########
src/java/org/apache/cassandra/cql3/statements/TransactionStatement.java:
##########
@@ -223,12 +217,9 @@ TxnNamedRead createNamedRead(NamedSelect namedSelect, 
QueryOptions options, Clie
     List<TxnNamedRead> createNamedReads(NamedSelect namedSelect, QueryOptions 
options, ClientState state)
     {
         SelectStatement select = namedSelect.select;
-        ReadQuery readQuery = select.getQuery(options, 0);
-        checkTrue(readQuery instanceof  SinglePartitionReadQuery.Group, 
ILLEGAL_RANGE_QUERY_MESSAGE, select.asCQL(options, state));
-
         // We reject reads from both LET and SELECT that do not specify a 
single row.
         @SuppressWarnings("unchecked")
-        SinglePartitionReadQuery.Group<SinglePartitionReadCommand> selectQuery 
= (SinglePartitionReadQuery.Group<SinglePartitionReadCommand>) readQuery;
+        SinglePartitionReadQuery.Group<SinglePartitionReadCommand> selectQuery 
= (SinglePartitionReadQuery.Group<SinglePartitionReadCommand>) 
select.getQuery(options, 0);

Review Comment:
   why prefer class cast over `ILLEGAL_RANGE_QUERY_MESSAGE`?



##########
src/java/org/apache/cassandra/cql3/statements/TransactionStatement.java:
##########
@@ -207,12 +204,9 @@ public ResultSet.ResultMetadata getResultMetadata()
     TxnNamedRead createNamedRead(NamedSelect namedSelect, QueryOptions 
options, ClientState state)
     {
         SelectStatement select = namedSelect.select;
-        ReadQuery readQuery = select.getQuery(options, 0);
-        checkTrue(readQuery instanceof  SinglePartitionReadQuery.Group, 
ILLEGAL_RANGE_QUERY_MESSAGE, select.asCQL(options, state));
-
         // We reject reads from both LET and SELECT that do not specify a 
single row.
         @SuppressWarnings("unchecked")
-        SinglePartitionReadQuery.Group<SinglePartitionReadCommand> selectQuery 
= (SinglePartitionReadQuery.Group<SinglePartitionReadCommand>) readQuery;
+        SinglePartitionReadQuery.Group<SinglePartitionReadCommand> selectQuery 
= (SinglePartitionReadQuery.Group<SinglePartitionReadCommand>) 
select.getQuery(options, 0);

Review Comment:
   why prefer ClassCast rather than `ILLEGAL_RANGE_QUERY_MESSAGE`?



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