Maxwell-Guo commented on code in PR #2655: URL: https://github.com/apache/cassandra/pull/2655#discussion_r1311714955
########## src/java/org/apache/cassandra/cql3/InMarker.java: ########## @@ -0,0 +1,178 @@ +/* + * 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.List; + +import org.apache.cassandra.cql3.functions.Function; +import org.apache.cassandra.db.marshal.AbstractType; +import org.apache.cassandra.db.marshal.ByteBufferAccessor; +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.SetType; +import org.apache.cassandra.db.marshal.UserType; +import org.apache.cassandra.db.marshal.TupleType; +import org.apache.cassandra.exceptions.InvalidRequestException; +import org.apache.cassandra.utils.ByteBufferUtil; + +public class InMarker extends Terms.NonTerminals +{ + private final int bindIndex; + private final ColumnSpecification receiver; + + protected InMarker(int bindIndex, ColumnSpecification receiver) + { + this.bindIndex = bindIndex; + this.receiver = receiver; + } + + @Override + public void addFunctionsTo(List<Function> functions) {} + + @Override + public void collectMarkerSpecification(VariableSpecifications boundNames) + { + boundNames.add(bindIndex, receiver); + } + + @Override + public Terminals bind(QueryOptions options) + { + ByteBuffer values = options.getValues().get(bindIndex); + if (values == null) + return null; + + if (values == ByteBufferUtil.UNSET_BYTE_BUFFER) + return UNSET_TERMINALS; + + ListType<?> type = (ListType) receiver.type; + return toTerminals(values, type, terminalConverter(type.getElementsType())); + } + + private <T> Terminals toTerminals(ByteBuffer value, + ListType<T> type, + java.util.function.Function<ByteBuffer, Term.Terminal> terminalConverter) + { + List<T> l = type.getSerializer().deserialize(value, ByteBufferAccessor.instance); Review Comment: What about change the variable name to lists which looks more like plural instand of l ? especially when I look at line 77 , a little awkward ########## src/java/org/apache/cassandra/cql3/InMarker.java: ########## @@ -0,0 +1,178 @@ +/* + * 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.List; + +import org.apache.cassandra.cql3.functions.Function; +import org.apache.cassandra.db.marshal.AbstractType; +import org.apache.cassandra.db.marshal.ByteBufferAccessor; +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.SetType; +import org.apache.cassandra.db.marshal.UserType; +import org.apache.cassandra.db.marshal.TupleType; +import org.apache.cassandra.exceptions.InvalidRequestException; +import org.apache.cassandra.utils.ByteBufferUtil; + +public class InMarker extends Terms.NonTerminals +{ + private final int bindIndex; + private final ColumnSpecification receiver; + + protected InMarker(int bindIndex, ColumnSpecification receiver) + { + this.bindIndex = bindIndex; + this.receiver = receiver; + } + + @Override + public void addFunctionsTo(List<Function> functions) {} + + @Override + public void collectMarkerSpecification(VariableSpecifications boundNames) + { + boundNames.add(bindIndex, receiver); + } + + @Override + public Terminals bind(QueryOptions options) + { + ByteBuffer values = options.getValues().get(bindIndex); + if (values == null) + return null; + + if (values == ByteBufferUtil.UNSET_BYTE_BUFFER) + return UNSET_TERMINALS; + + ListType<?> type = (ListType) receiver.type; + return toTerminals(values, type, terminalConverter(type.getElementsType())); + } + + private <T> Terminals toTerminals(ByteBuffer value, + ListType<T> type, + java.util.function.Function<ByteBuffer, Term.Terminal> terminalConverter) + { + List<T> l = type.getSerializer().deserialize(value, ByteBufferAccessor.instance); + List<Term.Terminal> terminals = new ArrayList<>(l.size()); + for (T element : l) + { + terminals.add(element == null ? null : terminalConverter.apply(type.getElementsType().decompose(element))); + } + return Terminals.of(terminals); + } + + public static java.util.function.Function<ByteBuffer, Term.Terminal> terminalConverter(AbstractType<?> type) + { + if (type.isCollection()) + { + switch (((CollectionType<?>) type).kind) + { + case LIST: + return e -> Lists.Value.fromSerialized(e, (ListType<?>) type); + case SET: + return e -> Sets.Value.fromSerialized(e, (SetType<?>) type); + case MAP: + return e -> Maps.Value.fromSerialized(e, (MapType<?, ?>) type); + } + throw new AssertionError(); Review Comment: when will this AssertionError be throw ? ########## src/java/org/apache/cassandra/cql3/Marker.java: ########## @@ -0,0 +1,147 @@ +/* + * 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.List; + +import org.apache.cassandra.cql3.functions.Function; +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.SetType; +import org.apache.cassandra.db.marshal.TupleType; +import org.apache.cassandra.db.marshal.UserType; +import org.apache.cassandra.exceptions.InvalidRequestException; +import org.apache.cassandra.serializers.MarshalException; +import org.apache.cassandra.utils.ByteBufferUtil; + +import static org.apache.cassandra.cql3.statements.RequestValidations.checkFalse; + +/** + * A single bind marker. + */ +public final class Marker extends Term.NonTerminal +{ + private final int bindIndex; + private final ColumnSpecification receiver; + + private Marker(int bindIndex, ColumnSpecification receiver) + { + this.bindIndex = bindIndex; + this.receiver = receiver; + } + + public void collectMarkerSpecification(VariableSpecifications boundNames) + { + boundNames.add(bindIndex, receiver); + } + + public boolean containsBindMarker() + { + return true; + } + + public void addFunctionsTo(List<Function> functions) + { + } + + public Term.Terminal bind(QueryOptions options) throws InvalidRequestException + { + try + { + ByteBuffer bytes = options.getValues().get(bindIndex); + if (bytes == null) + return null; + if (bytes == ByteBufferUtil.UNSET_BYTE_BUFFER) + { + checkFalse(receiver.type.isTuple(), "Invalid unset value for tuple %s", receiver.name); + return Constants.UNSET_VALUE; + } + + return toTerminal(receiver.type, bytes); + } + catch (MarshalException e) + { + throw new InvalidRequestException(e.getMessage(), e); + } + } + + private static Terminal toTerminal(AbstractType<?> type, ByteBuffer bytes) + { + if (type.isCollection()) + { + switch (((CollectionType) type).kind) + { + case LIST: + return Lists.Value.fromSerialized(bytes, (ListType<?>) type); + case SET: + return Sets.Value.fromSerialized(bytes, (SetType<?>) type); + case MAP: + return Maps.Value.fromSerialized(bytes, (MapType<?, ?>) type); + default: + throw new AssertionError(); Review Comment: when will this AssertionError be throw ? ########## src/java/org/apache/cassandra/cql3/InMarker.java: ########## @@ -0,0 +1,178 @@ +/* + * 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.List; + +import org.apache.cassandra.cql3.functions.Function; +import org.apache.cassandra.db.marshal.AbstractType; +import org.apache.cassandra.db.marshal.ByteBufferAccessor; +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.SetType; +import org.apache.cassandra.db.marshal.UserType; +import org.apache.cassandra.db.marshal.TupleType; +import org.apache.cassandra.exceptions.InvalidRequestException; +import org.apache.cassandra.utils.ByteBufferUtil; + +public class InMarker extends Terms.NonTerminals +{ + private final int bindIndex; + private final ColumnSpecification receiver; + + protected InMarker(int bindIndex, ColumnSpecification receiver) + { + this.bindIndex = bindIndex; + this.receiver = receiver; + } + + @Override + public void addFunctionsTo(List<Function> functions) {} + + @Override + public void collectMarkerSpecification(VariableSpecifications boundNames) + { + boundNames.add(bindIndex, receiver); + } + + @Override + public Terminals bind(QueryOptions options) + { + ByteBuffer values = options.getValues().get(bindIndex); + if (values == null) + return null; + + if (values == ByteBufferUtil.UNSET_BYTE_BUFFER) + return UNSET_TERMINALS; + + ListType<?> type = (ListType) receiver.type; + return toTerminals(values, type, terminalConverter(type.getElementsType())); + } + + private <T> Terminals toTerminals(ByteBuffer value, + ListType<T> type, + java.util.function.Function<ByteBuffer, Term.Terminal> terminalConverter) + { + List<T> l = type.getSerializer().deserialize(value, ByteBufferAccessor.instance); + List<Term.Terminal> terminals = new ArrayList<>(l.size()); + for (T element : l) + { + terminals.add(element == null ? null : terminalConverter.apply(type.getElementsType().decompose(element))); + } + return Terminals.of(terminals); + } + + public static java.util.function.Function<ByteBuffer, Term.Terminal> terminalConverter(AbstractType<?> type) + { + if (type.isCollection()) + { + switch (((CollectionType<?>) type).kind) + { + case LIST: + return e -> Lists.Value.fromSerialized(e, (ListType<?>) type); + case SET: + return e -> Sets.Value.fromSerialized(e, (SetType<?>) type); + case MAP: + return e -> Maps.Value.fromSerialized(e, (MapType<?, ?>) type); + } + throw new AssertionError(); + } + + if (type.isUDT()) + return e -> UserTypes.Value.fromSerialized(e, (UserType) type); + + if (type.isTuple()) + return e -> Tuples.Value.fromSerialized(e, (TupleType) type); + + return Constants.Value::new; Review Comment: Does vector type return this too ? -- 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]

