absurdfarce commented on code in PR #1952: URL: https://github.com/apache/cassandra-java-driver/pull/1952#discussion_r1926027504
########## core/src/main/java/com/datastax/oss/driver/internal/core/type/codec/VectorCodec.java: ########## @@ -156,4 +116,195 @@ public CqlVector<SubtypeT> parse(@Nullable String value) { ? null : CqlVector.from(value, this.subtypeCodec); } + + private static class FixedLength<SubtypeT> implements VectorCodecProxy<SubtypeT> { + private final VectorType cqlType; + private final TypeCodec<SubtypeT> subtypeCodec; + + private FixedLength(VectorType cqlType, TypeCodec<SubtypeT> subtypeCodec) { + this.cqlType = cqlType; + this.subtypeCodec = subtypeCodec; + } + + @Override + public ByteBuffer encode( + @Nullable CqlVector<SubtypeT> value, @NonNull ProtocolVersion protocolVersion) { + if (value == null || cqlType.getDimensions() <= 0) { + return null; + } + ByteBuffer[] valueBuffs = new ByteBuffer[cqlType.getDimensions()]; + Iterator<SubtypeT> values = value.iterator(); + int allValueBuffsSize = 0; + for (int i = 0; i < cqlType.getDimensions(); ++i) { + ByteBuffer valueBuff; + SubtypeT valueObj; + + try { + valueObj = values.next(); + } catch (NoSuchElementException nsee) { + throw new IllegalArgumentException( + String.format( + "Not enough elements; must provide elements for %d dimensions", + cqlType.getDimensions())); + } + + try { + valueBuff = this.subtypeCodec.encode(valueObj, protocolVersion); + } catch (ClassCastException e) { + throw new IllegalArgumentException("Invalid type for element: " + valueObj.getClass()); + } + if (valueBuff == null) { + throw new NullPointerException("Vector elements cannot encode to CQL NULL"); + } + allValueBuffsSize += valueBuff.limit(); + valueBuff.rewind(); + valueBuffs[i] = valueBuff; + } + // if too many elements, throw + if (values.hasNext()) { + throw new IllegalArgumentException( + String.format( + "Too many elements; must provide elements for %d dimensions", + cqlType.getDimensions())); Review Comment: @SiyaoIsHiding hit me up with a link to a Slack conversation in which a decision was announced on this point. The decision sprang out of a conversation the poster had with @joao-r-reis about our desire to standardize behaviour on this point across the drivers and how the default behaviour should be to throw. The poster also pointed out that they'd already made changes to the Python driver to bring about this change in behaviour. That poster was me. 🤦 Move along, nothing to see here... -- 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: pr-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For additional commands, e-mail: pr-h...@cassandra.apache.org