This is an automated email from the ASF dual-hosted git repository.

amashenkov pushed a commit to branch ignite-18015
in repository https://gitbox.apache.org/repos/asf/ignite-3.git

commit 8ccbfcbe18d326f1af22b68fc4c00d105265c241
Author: amashenkov <[email protected]>
AuthorDate: Tue Nov 8 13:43:22 2022 +0300

    wip. fix varlen types validation.
---
 .../ignite/internal/schema/VarlenNativeType.java   |  2 +-
 .../internal/schema/row/ExpandableByteBuf.java     | 33 +++++++++++++++++--
 .../ignite/internal/schema/row/RowAssembler.java   | 37 +++++++++++++++++++---
 .../internal/table/MutableRowTupleAdapterTest.java | 13 ++++----
 4 files changed, 69 insertions(+), 16 deletions(-)

diff --git 
a/modules/schema/src/main/java/org/apache/ignite/internal/schema/VarlenNativeType.java
 
b/modules/schema/src/main/java/org/apache/ignite/internal/schema/VarlenNativeType.java
index 6920835280..bef2c2d185 100644
--- 
a/modules/schema/src/main/java/org/apache/ignite/internal/schema/VarlenNativeType.java
+++ 
b/modules/schema/src/main/java/org/apache/ignite/internal/schema/VarlenNativeType.java
@@ -40,7 +40,7 @@ public class VarlenNativeType extends NativeType {
 
     @Override
     public boolean mismatch(NativeType type) {
-        return super.mismatch(type) || len < ((VarlenNativeType) type).len;
+        return super.mismatch(type);
     }
 
     /**
diff --git 
a/modules/schema/src/main/java/org/apache/ignite/internal/schema/row/ExpandableByteBuf.java
 
b/modules/schema/src/main/java/org/apache/ignite/internal/schema/row/ExpandableByteBuf.java
index 9cdfc58298..4388a7d324 100644
--- 
a/modules/schema/src/main/java/org/apache/ignite/internal/schema/row/ExpandableByteBuf.java
+++ 
b/modules/schema/src/main/java/org/apache/ignite/internal/schema/row/ExpandableByteBuf.java
@@ -149,12 +149,25 @@ public class ExpandableByteBuf {
      * @param val Value.
      */
     public void putBytes(int off, byte[] val) {
-        ensureCapacity(off + val.length);
+        putBytes(off, val, val.length);
+    }
+
+    /**
+     * Writes {@code byte[]} value to the buffer.
+     *
+     * @param off Buffer offset.
+     * @param val Value.
+     * @param bytesToWrite Max amount of bytes to be written.
+     */
+    public void putBytes(int off, byte[] val, int bytesToWrite) {
+        int length = Math.min(val.length, bytesToWrite);
+
+        ensureCapacity(off + length);
 
         buf.position(off);
 
         try {
-            buf.put(val);
+            buf.put(val, 0, length);
         } finally {
             buf.position(0);
         }
@@ -170,6 +183,20 @@ public class ExpandableByteBuf {
      * @throws CharacterCodingException If encoding failed.
      */
     public int putString(int off, String val, CharsetEncoder encoder) throws 
CharacterCodingException {
+        return putString(off, val, val.length(), encoder);
+    }
+
+    /**
+     * Writes {@code String} value to the buffer.
+     *
+     * @param off     Buffer offset.
+     * @param val     Value.
+     * @param charsToWrite Characters to write.
+     * @param encoder Charset encoder.
+     * @return Bytes written.
+     * @throws CharacterCodingException If encoding failed.
+     */
+    public int putString(int off, String val, int charsToWrite, CharsetEncoder 
encoder) throws CharacterCodingException {
         ensureCapacity(off);
 
         if (val.isEmpty()) {
@@ -181,7 +208,7 @@ public class ExpandableByteBuf {
         encoder.reset();
 
         try {
-            CharBuffer valBuf = CharBuffer.wrap(val);
+            CharBuffer valBuf = CharBuffer.wrap(val, 0, Math.min(val.length(), 
charsToWrite));
 
             while (true) {
                 CoderResult cr = valBuf.hasRemaining() ? 
encoder.encode(valBuf, buf, true) : CoderResult.UNDERFLOW;
diff --git 
a/modules/schema/src/main/java/org/apache/ignite/internal/schema/row/RowAssembler.java
 
b/modules/schema/src/main/java/org/apache/ignite/internal/schema/row/RowAssembler.java
index 06a771672d..149a64a7f3 100644
--- 
a/modules/schema/src/main/java/org/apache/ignite/internal/schema/row/RowAssembler.java
+++ 
b/modules/schema/src/main/java/org/apache/ignite/internal/schema/row/RowAssembler.java
@@ -46,6 +46,7 @@ import org.apache.ignite.internal.schema.NumberNativeType;
 import org.apache.ignite.internal.schema.SchemaDescriptor;
 import org.apache.ignite.internal.schema.SchemaMismatchException;
 import org.apache.ignite.internal.schema.TemporalNativeType;
+import org.apache.ignite.internal.schema.VarlenNativeType;
 import org.apache.ignite.internal.util.HashUtils;
 
 /**
@@ -225,12 +226,12 @@ public class RowAssembler {
                 break;
             }
             case STRING: {
-                rowAsm.appendString((String) val);
+                rowAsm.appendString((String) val, ((VarlenNativeType) 
type).length());
 
                 break;
             }
             case BYTES: {
-                rowAsm.appendBytes((byte[]) val);
+                rowAsm.appendBytes((byte[]) val, ((VarlenNativeType) 
type).length());
 
                 break;
             }
@@ -546,10 +547,22 @@ public class RowAssembler {
      * @throws SchemaMismatchException If a value doesn't match the current 
column type.
      */
     public RowAssembler appendString(String val) throws 
SchemaMismatchException {
+        return appendString(val, val.length());
+    }
+
+    /**
+     * Appends String value for the current column to the chunk.
+     *
+     * @param val Column value.
+     * @param charsToWrite Max amount of chars to write.
+     * @return {@code this} for chaining.
+     * @throws SchemaMismatchException If a value doesn't match the current 
column type.
+     */
+    public RowAssembler appendString(String val, int charsToWrite) throws 
SchemaMismatchException {
         checkType(NativeTypeSpec.STRING);
 
         try {
-            final int written = buf.putString(curOff, val, encoder());
+            final int written = buf.putString(curOff, val, charsToWrite, 
encoder());
 
             writeVarlenOffset(curVartblEntry, curOff - dataOff);
 
@@ -571,15 +584,29 @@ public class RowAssembler {
      * @throws SchemaMismatchException If a value doesn't match the current 
column type.
      */
     public RowAssembler appendBytes(byte[] val) throws SchemaMismatchException 
{
+        return appendBytes(val, val.length);
+    }
+
+    /**
+     * Appends byte[] value for the current column to the chunk.
+     *
+     * @param val Column value.
+     * @param bytesToWrite Max amount of bytes to be written.
+     * @return {@code this} for chaining.
+     * @throws SchemaMismatchException If a value doesn't match the current 
column type.
+     */
+    public RowAssembler appendBytes(byte[] val, int bytesToWrite) throws 
SchemaMismatchException {
+        assert bytesToWrite <= val.length;
+
         checkType(NativeTypeSpec.BYTES);
 
-        buf.putBytes(curOff, val);
+        buf.putBytes(curOff, val, bytesToWrite);
 
         writeVarlenOffset(curVartblEntry, curOff - dataOff);
 
         curVartblEntry++;
 
-        shiftColumn(val.length);
+        shiftColumn(bytesToWrite);
 
         return this;
     }
diff --git 
a/modules/table/src/test/java/org/apache/ignite/internal/table/MutableRowTupleAdapterTest.java
 
b/modules/table/src/test/java/org/apache/ignite/internal/table/MutableRowTupleAdapterTest.java
index f423efb9e9..d2b52b1caf 100644
--- 
a/modules/table/src/test/java/org/apache/ignite/internal/table/MutableRowTupleAdapterTest.java
+++ 
b/modules/table/src/test/java/org/apache/ignite/internal/table/MutableRowTupleAdapterTest.java
@@ -621,24 +621,23 @@ public class MutableRowTupleAdapterTest {
         assertEquals(tuple, tuple1);
     }
 
-    //TODO: Fix varlen types truncation.
-    @Disabled("https://issues.apache.org/jira/browse/IGNITE-17729";)
     @Test
     void testVarlenValuesLengthNarrowing() throws Exception {
         SchemaDescriptor schemaDescriptor = new SchemaDescriptor(1,
                 new Column[]{new Column("key", NativeTypes.INT32, false)},
                 new Column[]{
-                        new Column("string", NativeTypes.stringOf(5), true),
                         new Column("bytes", NativeTypes.blobOf(5), true),
+                        new Column("string", NativeTypes.stringOf(4), true),
                 }
         );
 
         Tuple tuple = Tuple.create().set("key", 1)
-                .set("string", "abcefghi")
-                .set("bytes", new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
+                .set("bytes", new byte[]{1, 2, 3, 4, 5, 6, 7, 8})
+                        .set("string", "abcefghi");
+
         Tuple expected = Tuple.create().set("key", 1)
-                .set("string", "abcef")
-                .set("bytes", new byte[]{1, 2, 3, 4, 5});
+                .set("bytes", new byte[]{1, 2, 3, 4, 5})
+                .set("string", "abce");
 
         TupleMarshaller marshaller = new TupleMarshallerImpl(new 
DummySchemaManagerImpl(schemaDescriptor));
 

Reply via email to