bereng commented on code in PR #3095: URL: https://github.com/apache/cassandra/pull/3095#discussion_r1551060613
########## src/java/org/apache/cassandra/cql3/ColumnsExpression.java: ########## @@ -0,0 +1,558 @@ +/* + * 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.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; + +import org.apache.cassandra.cql3.functions.Function; +import org.apache.cassandra.cql3.terms.Term; +import org.apache.cassandra.db.marshal.AbstractType; +import org.apache.cassandra.db.marshal.CollectionType; +import org.apache.cassandra.db.marshal.ListType; +import org.apache.cassandra.db.marshal.MapType; +import org.apache.cassandra.db.marshal.TupleType; +import org.apache.cassandra.exceptions.InvalidRequestException; +import org.apache.cassandra.schema.ColumnMetadata; +import org.apache.cassandra.schema.TableMetadata; +import org.apache.cassandra.utils.ByteBufferUtil; + +import static org.apache.cassandra.cql3.statements.RequestValidations.checkContainsNoDuplicates; +import static org.apache.cassandra.cql3.statements.RequestValidations.checkContainsOnly; +import static org.apache.cassandra.cql3.statements.RequestValidations.checkFalse; +import static org.apache.cassandra.cql3.statements.RequestValidations.checkTrue; +import static org.apache.cassandra.cql3.statements.RequestValidations.invalidRequest; + +/** + * An expression including one or several columns. + */ +public final class ColumnsExpression +{ + /** + * Represent the expression kind + */ + public enum Kind + { + /** + * Single column expression (e.g. {@code columnA}) + */ + SINGLE_COLUMN + { + @Override + void validateColumns(TableMetadata table, List<ColumnMetadata> columns) + { + } + + @Override + AbstractType<?> type(TableMetadata table, List<ColumnMetadata> columns) + { + return columns.get(0).type.unwrap(); + } + + @Override + String toCQLString(Stream<String> columns, String mapKey) + { + return columns.findFirst().orElseThrow(); + } + }, + /** + * Multi-column expression (e.g. {@code (columnA, columnB)}) + */ + MULTI_COLUMN + { + @Override + protected void validateColumns(TableMetadata table, List<ColumnMetadata> columns) + { + int previousPosition = -1; + for (int i = 0, m = columns.size(); i < m; i++) + { + ColumnMetadata column = columns.get(i); + checkTrue(column.isClusteringColumn(), "Multi-column relations can only be applied to clustering columns but was applied to: %s", column.name); + checkFalse(columns.lastIndexOf(column) != i, "Column \"%s\" appeared twice in a relation: %s", column.name, this); + + // check that no clustering columns were skipped + checkFalse(previousPosition != -1 && column.position() != previousPosition + 1, + "Clustering columns must appear in the PRIMARY KEY order in multi-column relations: %s", toCQLString(columns, null)); + + previousPosition = column.position(); + } + } + + @Override + AbstractType<?> type(TableMetadata table, List<ColumnMetadata> columns) + { + return new TupleType(columns.stream() + .map(c -> c.type.unwrap()) + .collect(Collectors.toList())); + } + + @Override + String toCQLString(Stream<String> columns, String mapKey) + { + return columns.collect(Collectors.joining(", ", "(", ")")); + } + }, + /** + * Token expression (e.g. {@code token(columnA, columnB)}) + */ + TOKEN + { + @Override + protected void validateColumns(TableMetadata table, List<ColumnMetadata> columns) + { + if (columns.equals(table.partitionKeyColumns())) + return; + + // If the columns do not match the partition key columns, let's try to narrow down the problem + checkTrue(new HashSet<>(columns).containsAll(table.partitionKeyColumns()), + "The token() function must be applied to all partition key components or none of them"); + + checkContainsNoDuplicates(columns, "The token() function contains duplicate partition key components"); + + checkContainsOnly(columns, table.partitionKeyColumns(), "The token() function must contains only partition key components"); + + throw invalidRequest("The token function arguments must be in the partition key order: %s", + Joiner.on(", ").join(ColumnMetadata.toIdentifiers(table.partitionKeyColumns()))); + } + + @Override + AbstractType<?> type(TableMetadata table, List<ColumnMetadata> columns) + { + return table.partitioner.getTokenValidator(); + } + + @Override + String toCQLString(Stream<String> columns, String mapKey) + { + return columns.collect(Collectors.joining(", ", "token(", ")")); + } + }, + /** + * Map element expression (e.g. {@code columnA[?]}) + */ + MAP_ELEMENT + { + @Override + void validateColumns(TableMetadata table, List<ColumnMetadata> columns) + { + ColumnMetadata column = columns.get(0); + checkFalse(column.type instanceof ListType, "Indexes on list entries (%s[index] = value) are not supported.", column.name); + checkTrue(column.type instanceof MapType, "Column %s cannot be used as a map", column.name); + checkTrue(column.type.isMultiCell(), "Map-entry predicates on frozen map column %s are not supported", column.name); + } + + @Override + AbstractType<?> type(TableMetadata table, List<ColumnMetadata> columns) + { + return ((MapType<?, ?>) columns.get(0).type).getValuesType(); + } + + @Override + String toCQLString(Stream<String> columns, String mapKey) + { + return new StringBuilder().append(columns.findFirst().orElseThrow()) + .append('[') + .append(mapKey) + .append(']') + .toString(); + } + }; + + /** + * Validates that the specified columns are valid for this kind of expression. + * @param table the table metadata + * @param columns the expression column + */ + abstract void validateColumns(TableMetadata table, List<ColumnMetadata> columns); + + /** + * Returns the expression type. + * @param table the table metadata + * @param columns the expression columns + * @return the expression type + */ + abstract AbstractType<?> type(TableMetadata table, List<ColumnMetadata> columns); + + /** + * Returns CQL representation of the expression. + * @param columns the expression's columns + * @param mapKey the key used to access the map element + * @return the CQL representation of the expression. + */ + abstract String toCQLString(Stream<String> columns, String mapKey); + + String toCQLString(List<ColumnMetadata> columns, Term mapKey) + { + String k = null; + if (this == Kind.MAP_ELEMENT) + { + CQL3Type type = ((MapType<?, ?>) columns.get(0).type).getKeysType().asCQL3Type(); + // If a Term is not terminal it can be a row marker or a function. + // We ignore the fact that it could be a function for now. + k = mapKey.isTerminal() ? type.toCQLLiteral(((Term.Terminal) mapKey).get()) : "?"; + } + + return toCQLString(columns.stream().map(c -> c.name.toCQLString()), k); + } + + String toCQLString(List<ColumnIdentifier> identifiers, Term.Raw rawMapKey) + { + String mapKey = rawMapKey == null ? null : rawMapKey.getText(); + return toCQLString(identifiers.stream().map(ColumnIdentifier::toCQLString), mapKey); + } + } + + /** + * The kind of columns expression. + */ + private final Kind kind; + + /** + * The type of this expression + */ + private final AbstractType<?> type; + + /** + * The expression columns + */ + private final List<ColumnMetadata> columns; + + /** + * The key used to access the map element if this expression is for a map element, + * {@code null} otherwise. + */ + private final Term mapKey; + + + private ColumnsExpression(Kind kind, AbstractType<?> type, List<ColumnMetadata> columns, Term mapKey) + { + this.kind = kind; + this.type = type; + this.columns = columns; + this.mapKey = mapKey; + } + + /** + * Creates an expression for a single column (e.g. {@code columnA}). + * @param column the column + * @return an expression for a single column. + */ + public static ColumnsExpression singleColumn(ColumnMetadata column) + { + return new ColumnsExpression(Kind.SINGLE_COLUMN, column.type.unwrap(), ImmutableList.of(column), null); + } + + /** + * Creates an expression for multi-columns (e.g. {@code (columnA, columnB)}). + * @param columns the columns + * @return an expression for multi-columns. + */ + public static ColumnsExpression multiColumns(List<ColumnMetadata> columns) + { + AbstractType<?> type = new TupleType(columns.stream() + .map(c -> c.type.unwrap()) + .collect(Collectors.toList())); + return new ColumnsExpression(Kind.MULTI_COLUMN, type, ImmutableList.copyOf(columns), null); + } + + /** + * Returns the first column metadata. + * @return the first column metadata. + */ + public ColumnMetadata firstColumn() + { + return columns().get(0); + } + + /** + * Returns the last column metadata. + * @return the last column metadata. + */ + public ColumnMetadata lastColumn() + { + List<ColumnMetadata> columns = columns(); + return columns.get(columns.size() - 1); + } + + /** + * Returns the columns metadata in position order. + * @return the columns metadata in position order. + */ + public List<ColumnMetadata> columns() + { + return columns; + } + + /** + * Returns the column kind (partition key, clustering, static or regular). + * @return the column kind. + */ + public ColumnMetadata.Kind columnsKind() + { + // All columns must have the same type. + return firstColumn().kind; + } + + /** + * Returns the expression kind. + * @return the expression kind. + */ + public Kind kind() + { + return kind; + } + + /** + * Returns the expression type + * @return the expression type + */ + public AbstractType<?> type() + { + return type; + } + + public ByteBuffer mapKey(QueryOptions options) + { + ByteBuffer key = mapKey.bindAndGet(options); + if (key == null) + throw invalidRequest("Invalid null map key for column %s", firstColumn().name.toCQLString()); + if (key == ByteBufferUtil.UNSET_BYTE_BUFFER) + throw invalidRequest("Invalid unset map key for column %s", firstColumn().name.toCQLString()); + return key; + } + + /** + * Collects the column specifications for the bind variables in the map key. + * This is obviously a no-op if the expression is not a {@code MAP_ELEMENT} expression. + * + * @param boundNames the variables specification where to collect the + * bind variables of the map key in. + */ + public void collectMarkerSpecification(VariableSpecifications boundNames) + { + if (mapKey != null) + mapKey.collectMarkerSpecification(boundNames); + } + + /** + * Checks if this instance is a column level expression (single or multi-column expression). + * @return {@code true} if this instance is a column level expression, {@code false} otherwise. + */ + public boolean isColumnLevelExpression() + { + return kind == Kind.SINGLE_COLUMN || kind == Kind.MULTI_COLUMN; + } + + /** + * Adds all functions (native and user-defined) used by any component of the restriction + * to the specified list. + * @param functions the list to add to + */ + public void addFunctionsTo(List<Function> functions) + { + if (mapKey != null) + mapKey.addFunctionsTo(functions); + } + + /** + * Returns CQL representation of this expression. + * @return the CQL representation of this expression. + */ + public String toCQLString() + { + return kind.toCQLString(columns, mapKey); + } + + @Override + public String toString() + { + String prefix = kind == Kind.SINGLE_COLUMN ? "column " + : kind == Kind.MULTI_COLUMN ? "tuple " : ""; + + return prefix + toCQLString(); + } + + /** + * Returns the column specification corresponding to this expression. + * @param table the table metadata + * @return the column specification corresponding to this expression. + */ + public ColumnSpecification columnSpecification(TableMetadata table) + { + return new ColumnSpecification(table.keyspace, table.name, new ColumnIdentifier(toCQLString(), true), type) ; + } + + public static final class Raw Review Comment: I would add a javadoc here explaining what `Raw` means and what it is intended for -- 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]

