skoppu22 commented on code in PR #144: URL: https://github.com/apache/cassandra-analytics/pull/144#discussion_r3414067073
########## cassandra-five-zero-types/src/main/java/org/apache/cassandra/spark/data/complex/CqlVector.java: ########## @@ -0,0 +1,138 @@ +/* + * 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.spark.data.complex; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import org.apache.cassandra.bridge.CassandraVersion; +import org.apache.cassandra.cql3.functions.types.SettableByIndexData; +import org.apache.cassandra.db.marshal.AbstractType; +import org.apache.cassandra.db.marshal.VectorType; +import org.apache.cassandra.db.rows.BufferCell; +import org.apache.cassandra.db.rows.CellPath; +import org.apache.cassandra.schema.ColumnMetadata; +import org.apache.cassandra.serializers.TypeSerializer; +import org.apache.cassandra.spark.data.CqlField; +import org.apache.cassandra.spark.data.CqlType; +import org.apache.cassandra.utils.TimeUUID; + +import static org.apache.cassandra.spark.data.CqlField.NO_TTL; + +public class CqlVector extends CqlCollection implements CqlField.CqlVector +{ + private final int dimensions; + + public CqlVector(CqlField.CqlType type, int dimensions) + { + super(type); + this.dimensions = dimensions; + } + + @Override + public AbstractType<?> dataType(boolean isMultiCell) + { + return VectorType.getInstance(((CqlType) type()).dataType(), dimensions); + } + + @Override + public InternalType internalType() + { + return InternalType.Vector; + } + + @Override + @SuppressWarnings("unchecked") + public <T> TypeSerializer<T> serializer() + { + return (TypeSerializer<T>) dataType(false).getSerializer(); + } + + @Override + public String name() + { + return "vector"; + } + + @Override + public String cqlName() + { + return String.format("%s<%s, %d>", + internalType().name().toLowerCase(), + types.get(0).cqlName(), + dimensions); + } + + @Override + protected void setInnerValueInternal(SettableByIndexData<?> udtValue, int position, Object value) + { + udtValue.setVector(position, (List<?>) value); + } + + @Override + public Object randomValue(int minCollectionSize) + { + return IntStream.range(0, dimensions) + .mapToObj(element -> type().randomValue(minCollectionSize)) + .collect(Collectors.toList()); + } + + @Override + public org.apache.cassandra.cql3.functions.types.DataType driverDataType(boolean isFrozen) + { + return org.apache.cassandra.cql3.functions.types.DataType.vector(((CqlType) type()).driverDataType(isFrozen), dimensions); + } + + @Override + public Object convertForCqlWriter(Object value, CassandraVersion version, boolean isCollectionElement) + { + return ((List<?>) value).stream() + .map(element -> type().convertForCqlWriter(element, version, true)) + .collect(Collectors.toList()); + } + + @Override + public void addCell(final org.apache.cassandra.db.rows.Row.Builder rowBuilder, + ColumnMetadata cd, + long timestamp, + int ttl, + long now, + Object value) + { + for (Object o : (List<?>) value) Review Comment: I am not very sure, instead looping and creating cell per element, i.,e multi cell, should we create a single cell for entire vector 'value' ? Does C* expect vector to be single cell or multi cell? -- 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]
