bereng commented on code in PR #3095: URL: https://github.com/apache/cassandra/pull/3095#discussion_r1538723514
########## src/java/org/apache/cassandra/cql3/ColumnsExpression.java: ########## @@ -0,0 +1,574 @@ +/* + * 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; + } + + @Override + String toCQLString(Stream<String> columns, String mapKey) + { + return columns.findFirst().orElseThrow(); + } + + @Override + public String toString() + { + return "single column"; + } + }, + /** + * 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) + .collect(Collectors.toList())); + } + + @Override + String toCQLString(Stream<String> columns, String mapKey) + { + return columns.collect(Collectors.joining(", ", "(", ")")); + } + + @Override + public String toString() + { + return "multi-column"; + } + }, + /** + * 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(", ")")); + } + + @Override + public String toString() + { + return "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(); + } + + @Override + public String toString() + { + return "Map element"; + } + }; + + /** + * 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, 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) + .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(); Review Comment: Unnecessary? -- 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]

