andygrove commented on code in PR #1419:
URL: https://github.com/apache/arrow-java/pull/1419#discussion_r4460643439


##########
c/src/main/java/org/apache/arrow/c/ArrayImporter.java:
##########
@@ -84,15 +100,78 @@ private void importChild(ArrayImporter parent, ArrowArray 
src) {
   }
 
   private void doImport(ArrowArray.Snapshot snapshot) {
-    // A non-zero offset is only meaningful for arrays that have buffers to 
offset into.
-    // Null arrays carry no buffers, so their offset is inert and safe to 
ignore.
-    checkState(
-        snapshot.offset == 0
-            || snapshot.length == 0
-            || vector.getField().getType().getTypeID() == 
ArrowType.ArrowTypeID.Null,
-        "ArrowArray struct has non-zero offset (%s), which is not supported",
-        snapshot.offset);
+    checkState(snapshot.offset >= 0, "ArrowArray struct has negative offset 
(%s)", snapshot.offset);

Review Comment:
   Added in 0dcebc16e. `testImportArrayWithNegativeOffsetFails` covers the 
negative offset, and the importer now rejects a negative length the same way 
(`testImportArrayWithNegativeLengthFails`).
   
   Offset plus length beyond an int is fine for most types: the importer skips 
the whole bytes of rows before the slice by advancing the buffer pointers, so 
the temporary vector holds fewer than 8 rows before the slice. 
`testImportSliceWithOffsetBeyondIntRange` imports 5 rows at offset 2^31 + 3, 
with the buffer pointers moved back by the rows that are skipped. Run-end 
encoded arrays are the exception, because their run ends count from the start 
of the array, so their rows cannot be skipped. Such an import used to fail with 
an `ArithmeticException` from `Math.addExact`, and with bounds checking off 
`checkedCastToInt` could truncate the offset. It now checks the row count first 
and fails with a message that gives the offset, the length and the rows needed 
(`testImportRunEndEncodedSliceBeyondIntRangeFails`).
   



##########
c/src/test/java/org/apache/arrow/c/DictionaryTest.java:
##########
@@ -100,37 +99,113 @@ void roundtrip(FieldVector vector, DictionaryProvider 
provider, Class<?> clazz)
 
   @Test
   public void testWithDictionary() throws Exception {
-    DictionaryProvider.MapDictionaryProvider provider =
-        new DictionaryProvider.MapDictionaryProvider();
-    // create dictionary and provider
+    try (DictionaryProvider.MapDictionaryProvider provider =
+            new DictionaryProvider.MapDictionaryProvider();
+        IntVector encodedVector = createEncodedVector(provider, "bb", "bb", 
"cc", "aa")) {
+      roundtrip(encodedVector, provider, IntVector.class);
+    }
+  }
+
+  @Test
+  public void testWithDictionaryAndNonZeroOffset() throws Exception {
+    try (DictionaryProvider.MapDictionaryProvider provider =
+            new DictionaryProvider.MapDictionaryProvider();
+        IntVector encodedVector = createEncodedVector(provider, "bb", "bb", 
"cc", "aa");
+        ArrowSchema consumerArrowSchema = ArrowSchema.allocateNew(allocator);
+        ArrowArray consumerArrowArray = ArrowArray.allocateNew(allocator)) {
+      Data.exportVector(
+          allocator, encodedVector, provider, consumerArrowArray, 
consumerArrowSchema);
+
+      // Slicing a dictionary encoded array slices the indices, not the 
dictionary
+      ArrowArray.Snapshot snapshot = consumerArrowArray.snapshot();
+      snapshot.offset = 1;
+      snapshot.length = 2;
+      consumerArrowArray.save(snapshot);
+
+      try (CDataDictionaryProvider cDictionaryProvider = new 
CDataDictionaryProvider();
+          FieldVector imported =
+              Data.importVector(
+                  allocator, consumerArrowArray, consumerArrowSchema, 
cDictionaryProvider)) {
+        IntVector indices = (IntVector) imported;
+        assertEquals(2, indices.getValueCount());
+        assertEquals(1, indices.get(0));
+        assertEquals(2, indices.get(1));
+
+        Dictionary importedDictionary = cDictionaryProvider.lookup(0L);
+        assertEquals(3, importedDictionary.getVector().getValueCount());
+        try (VarCharVector decoded =
+            (VarCharVector) DictionaryEncoder.decode(imported, 
importedDictionary)) {
+          assertEquals("bb", new String(decoded.get(0), 
StandardCharsets.UTF_8));
+          assertEquals("cc", new String(decoded.get(1), 
StandardCharsets.UTF_8));
+        }
+      }
+    }
+  }
+
+  @Test
+  public void testWithDictionaryAndOffsetsPastTheFirstByte() throws Exception {

Review Comment:
   Added in 0dcebc16e. `testWithDictionaryValuesWithNonZeroOffset` slices the 
values of the dictionary at offsets 1 and 9, so that the indices refer to the 
values from the offset, and its values include `é`, `日本語` and `😀`. 
`setTestStrings` and the view tests now have those characters too, in strings 
both within and beyond the 12 bytes that a view holds inline.
   



-- 
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]

Reply via email to