mike-tr-adamson commented on code in PR #2916:
URL: https://github.com/apache/cassandra/pull/2916#discussion_r1399118948


##########
src/java/org/apache/cassandra/index/sai/utils/IndexTermType.java:
##########
@@ -0,0 +1,858 @@
+/*
+ * 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.index.sai.utils;
+
+import java.math.BigInteger;
+import java.net.InetAddress;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableSet;
+
+import com.googlecode.concurrenttrees.radix.ConcurrentRadixTree;
+import org.apache.cassandra.cql3.CQL3Type;
+import org.apache.cassandra.cql3.Operator;
+import org.apache.cassandra.cql3.statements.schema.IndexTarget;
+import org.apache.cassandra.db.DecoratedKey;
+import org.apache.cassandra.db.filter.RowFilter;
+import org.apache.cassandra.db.marshal.AbstractType;
+import org.apache.cassandra.db.marshal.AsciiType;
+import org.apache.cassandra.db.marshal.BooleanType;
+import org.apache.cassandra.db.marshal.ByteBufferAccessor;
+import org.apache.cassandra.db.marshal.CollectionType;
+import org.apache.cassandra.db.marshal.CompositeType;
+import org.apache.cassandra.db.marshal.DecimalType;
+import org.apache.cassandra.db.marshal.InetAddressType;
+import org.apache.cassandra.db.marshal.IntegerType;
+import org.apache.cassandra.db.marshal.LongType;
+import org.apache.cassandra.db.marshal.StringType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.db.marshal.UUIDType;
+import org.apache.cassandra.db.marshal.VectorType;
+import org.apache.cassandra.db.rows.Cell;
+import org.apache.cassandra.db.rows.ComplexColumnData;
+import org.apache.cassandra.db.rows.Row;
+import org.apache.cassandra.index.sai.plan.Expression;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.serializers.MarshalException;
+import org.apache.cassandra.utils.ByteBufferUtil;
+import org.apache.cassandra.utils.FastByteOperations;
+import org.apache.cassandra.utils.bytecomparable.ByteComparable;
+import org.apache.cassandra.utils.bytecomparable.ByteSource;
+import org.apache.cassandra.utils.bytecomparable.ByteSourceInverse;
+
+/**
+ * This class is a representation of an {@link AbstractType} as an indexable 
type. It is responsible for determining the
+ * capabilities of the type and provides helper methods for handling term 
values associated with the type.
+ */
+public class IndexTermType
+{
+    private static final Set<AbstractType<?>> EQ_ONLY_TYPES = 
ImmutableSet.of(UTF8Type.instance,
+                                                                              
AsciiType.instance,
+                                                                              
BooleanType.instance,
+                                                                              
UUIDType.instance);
+
+    private static final byte[] IPV4_PREFIX = new byte[] { 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, -1, -1 };
+
+    /**
+     * DecimalType / BigDecimal values are indexed by truncating their 
asComparableBytes representation to this size,
+     * padding on the right with zero-value-bytes until this size is reached 
(if necessary).  This causes
+     * false-positives that must be filtered in a separate step after hitting 
the index and reading the associated
+     * (full) values.
+     */
+    private static final int DECIMAL_APPROXIMATION_BYTES = 24;
+    private static final int BIG_INTEGER_APPROXIMATION_BYTES = 20;
+    private static final int INET_ADDRESS_SIZE = 16;
+    private static final int DEFAULT_FIXED_LENGTH = 16;
+
+    private static final int IS_LITERAL               = 1;
+    private static final int IS_STRING                = 1 << 1;
+    private static final int IS_VECTOR                = 1 << 2;
+    private static final int IS_REVERSED              = 1 << 3;
+    private static final int IS_FROZEN                = 1 << 4;
+    private static final int IS_NON_FROZEN_COLLECTION = 1 << 5;
+    private static final int IS_COMPOSITE             = 1 << 6;
+    private static final int IS_INET_ADDRESS          = 1 << 7;
+    private static final int IS_BIG_INTEGER           = 1 << 8;
+    private static final int IS_BIG_DECIMAL           = 1 << 9;
+    private static final int IS_LONG                  = 1 << 10;
+    private static final int IS_COMPOSITE_PARTITION   = 1 << 11;
+
+    private final ColumnMetadata columnMetadata;
+    private final IndexTarget.Type indexTargetType;
+    private final AbstractType<?> indexType;
+    private final List<IndexTermType> subTypes;
+    private final AbstractType<?> vectorElementType;
+    private final int vectorDimension;
+    private final int capabilityBitmap;
+
+    /**
+     * Create an {@link IndexTermType} from a {@link ColumnMetadata} and 
{@link IndexTarget.Type}.
+     *
+     * @param columnMetadata the {@link ColumnMetadata} for the column being 
indexed
+     * @param partitionColumns the partition columns for the table this column 
belongs to. This is used for identifying
+     *                         if the {@code columnMetadata} is a partition 
column and if it belongs to a composite
+     *                         partition
+     * @param indexTargetType the {@link IndexTarget.Type} for the index
+     *
+     * @return the {@link IndexTermType}
+     */
+    public static IndexTermType create(ColumnMetadata columnMetadata, 
List<ColumnMetadata> partitionColumns, IndexTarget.Type indexTargetType)
+    {
+        return new IndexTermType(columnMetadata, partitionColumns, 
indexTargetType);
+    }
+
+    private IndexTermType(ColumnMetadata columnMetadata, List<ColumnMetadata> 
partitionColumns, IndexTarget.Type indexTargetType)
+    {
+        this.columnMetadata = columnMetadata;
+        this.indexTargetType = indexTargetType;
+        this.capabilityBitmap = calculateIdentityBitmap(columnMetadata, 
partitionColumns, indexTargetType);
+        this.indexType = calculateIndexType(columnMetadata.type, 
capabilityBitmap, indexTargetType);
+        if (indexType.subTypes().isEmpty())
+        {
+            this.subTypes = Collections.emptyList();
+        }
+        else
+        {
+            List<IndexTermType> subTypes = new 
ArrayList<>(indexType.subTypes().size());
+            for (AbstractType<?> subType : indexType.subTypes())
+                subTypes.add(new 
IndexTermType(columnMetadata.withNewType(subType), partitionColumns, 
indexTargetType));
+            this.subTypes = Collections.unmodifiableList(subTypes);
+        }
+        if (isVector())
+        {
+            VectorType<?> vectorType = (VectorType<?>) indexType;
+            vectorElementType = vectorType.elementType;
+            vectorDimension = vectorType.dimension;
+        }
+        else
+        {
+            vectorElementType = null;
+            vectorDimension = -1;
+        }
+    }
+
+    /**
+     * Returns {@code true} if the index type is a literal type and will use a 
literal index. This applies to
+     * string types, frozen types, composite types and boolean type.
+     */
+    public boolean isLiteral()
+    {
+        return hasProperty(IS_LITERAL);
+    }
+
+    /**
+     * Returns {@code true} if the index type is a string type. This is used 
to determine if the type supports
+     * analysis.
+     */
+    public boolean isString()
+    {
+        return hasProperty(IS_STRING);
+    }
+
+    /**
+     * Returns {@code true} if the index type is a vector type. Note: being a 
vector type does not mean that the type
+     * is valid for indexing in that we don't check the element type and 
dimension constraints here.
+     */
+    public boolean isVector()
+    {
+        return hasProperty(IS_VECTOR);
+    }
+
+    /**
+     * Returns {@code true} if the index type is reversed. This is only the 
case (currently) for clustering keys with
+     * descending ordering.
+     */
+    public boolean isReversed()
+    {
+        return hasProperty(IS_REVERSED);
+    }
+
+    /**
+     * Returns {@code true} if the index type is frozen, e.g. the type is 
wrapped with {@code frozen<type>}.
+     */
+    public boolean isFrozen()
+    {
+        return hasProperty(IS_FROZEN);
+    }
+
+    /**
+     * Returns {@code true} if the index type is a non-frozen collection
+     */
+    public boolean isNonFrozenCollection()
+    {
+        return hasProperty(IS_NON_FROZEN_COLLECTION);
+    }
+
+    /**
+     * Returns {@code true} if the index type is a frozen collection. This is 
the inverse of a non-frozen collection
+     * but this method is here for clarity.
+     */
+    public boolean isFrozenCollection()
+    {
+        return !hasProperty(IS_NON_FROZEN_COLLECTION);

Review Comment:
   Correct, I've fixed on 
[62970c5](https://github.com/apache/cassandra/pull/2916/commits/62970c5d28560d20cef503bf05299a5a9e6a6c1d).
 I was trying to optimise the method and completely forgot about tuples and 
UDTs.



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