blerer commented on code in PR #3095:
URL: https://github.com/apache/cassandra/pull/3095#discussion_r1547784799


##########
src/java/org/apache/cassandra/cql3/restrictions/SimpleRestriction.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.restrictions;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import com.google.common.collect.RangeSet;
+
+import org.apache.cassandra.cql3.ColumnsExpression;
+import org.apache.cassandra.cql3.Operator;
+import org.apache.cassandra.cql3.QueryOptions;
+import org.apache.cassandra.cql3.functions.Function;
+import org.apache.cassandra.cql3.terms.Term;
+import org.apache.cassandra.cql3.terms.Terms;
+import org.apache.cassandra.db.filter.RowFilter;
+import org.apache.cassandra.db.marshal.ListType;
+import org.apache.cassandra.index.Index;
+import org.apache.cassandra.index.IndexRegistry;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.utils.ByteBufferUtil;
+
+import static 
org.apache.cassandra.cql3.statements.RequestValidations.checkFalse;
+import static 
org.apache.cassandra.cql3.statements.RequestValidations.invalidRequest;
+
+/**
+ * A simple predicate on a columns expression (e.g. columnA = X).
+ */
+public final class SimpleRestriction implements SingleRestriction
+{
+    /**
+     * The columns expression to which the restriction applies.
+     */
+    private final ColumnsExpression columnsExpression;
+
+    /**
+     * The operator
+     */
+    private final Operator operator;
+
+    /**
+     * The values
+     */
+    private final Terms values;
+
+    public SimpleRestriction(ColumnsExpression columnsExpression, Operator 
operator, Terms values)
+    {
+        this.columnsExpression = columnsExpression;
+        this.operator = operator;
+        this.values = values;
+    }
+
+    @Override
+    public boolean isOnToken()
+    {
+        return columnsExpression.kind() == ColumnsExpression.Kind.TOKEN;
+    }
+
+    @Override
+    public ColumnMetadata firstColumn()
+    {
+        return columnsExpression.firstColumn();
+    }
+
+    @Override
+    public ColumnMetadata lastColumn()
+    {
+        return columnsExpression.lastColumn();
+    }
+
+    @Override
+    public List<ColumnMetadata> columns()
+    {
+        return columnsExpression.columns();
+    }
+
+    @Override
+    public boolean isMultiColumn()
+    {
+        return columnsExpression.kind() == ColumnsExpression.Kind.MULTI_COLUMN;
+    }
+
+    @Override
+    public boolean isColumnLevel()
+    {
+        return columnsExpression.isColumnLevelExpression();
+    }
+
+    public Operator operator()
+    {
+        return operator;
+    }
+
+    @Override
+    public boolean isANN()
+    {
+        return operator == Operator.ANN;
+    }
+
+    @Override
+    public boolean isEQ()
+    {
+        return operator == Operator.EQ;
+    }
+
+    @Override
+    public boolean isSlice()
+    {
+        return operator.isSlice();
+    }
+
+    @Override
+    public boolean isIN()
+    {
+        return operator == Operator.IN;
+    }
+
+    /**
+     * Checks if this restriction operator is a CONTAINS, CONTAINS_KEY or is 
an equality on a map element.
+     * @return {@code true} if the restriction operator is one of the contains 
operations, {@code false} otherwise.
+     */
+    public boolean isContains()
+    {
+        return operator == Operator.CONTAINS
+               || operator == Operator.CONTAINS_KEY
+               || columnsExpression.kind() == 
ColumnsExpression.Kind.MAP_ELEMENT;
+    }
+
+    @Override
+    public boolean needsFilteringOrIndexing()
+    {
+        // The need for filtering or indexing is a combination of columns 
expression type and operator
+        // Therefore, we have to take both into account.
+        return columnsExpression.kind() == ColumnsExpression.Kind.MAP_ELEMENT
+               || 
operator.requiresFilteringOrIndexingFor(columnsExpression.columnsKind());
+    }
+
+    @Override
+    public void addFunctionsTo(List<Function> functions)
+    {
+        columnsExpression.addFunctionsTo(functions);
+        values.addFunctionsTo(functions);
+    }
+
+    @Override
+    public boolean needsFiltering(Index.Group indexGroup)
+    {
+        for (ColumnMetadata column : columns())
+        {
+            if (!isSupportedBy(indexGroup.getIndexes(), column))
+                return true;
+        }
+        return false;
+    }
+
+    private boolean isSupportedBy(Iterable<Index> indexes, ColumnMetadata 
column)
+    {
+        if (isOnToken())
+            return false;
+
+        for (Index index : indexes)
+        {
+            if (index.supportsExpression(column, operator))
+                return true;
+        }
+        return false;
+    }
+
+    @Override
+    public Index findSupportingIndex(Iterable<Index> indexes)
+    {
+        if (isOnToken())
+            return null;
+
+        for (Index index : indexes)
+            if (isSupportedBy(index))
+                return index;
+        return null;
+    }
+
+    @Override
+    public boolean isSupportedBy(Index index)
+    {
+        if (isOnToken())
+            return false;
+
+        for (ColumnMetadata column : columns())
+        {
+            if (index.supportsExpression(column, operator))
+                return true;
+        }
+        return false;
+    }
+
+    @Override
+    public List<ClusteringElements> values(QueryOptions options)
+    {
+        assert operator == Operator.EQ || operator == Operator.IN || operator 
== Operator.ANN;
+        return bindAndGetClusteringElements(options);
+    }
+
+    @Override
+    public RangeSet<ClusteringElements> restrict(RangeSet<ClusteringElements> 
rangeSet, QueryOptions options)
+    {
+        assert operator.isSlice() || operator == Operator.EQ;
+        return operator.restrict(rangeSet, 
bindAndGetClusteringElements(options));
+    }
+
+    private List<ClusteringElements> bindAndGetClusteringElements(QueryOptions 
options)
+    {
+        switch (columnsExpression.kind())
+        {
+            case SINGLE_COLUMN:
+            case TOKEN:
+                return bindAndGet(options).stream()

Review Comment:
   You will get that call between zero and 2 times at most for a given query. 
Considering that token and multi columns are rarely used. Is it really the hot 
path?



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