You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "mediprtl (via GitHub)" <gi...@apache.org> on 2026/05/18 10:34:08 UTC

[PR] fix(c/driver/postgresql): use array_view->offset when writing list rows [arrow-adbc]

mediprtl opened a new pull request, #4320:
URL: https://github.com/apache/arrow-adbc/pull/4320

   ## Summary
   
   `PostgresCopyListFieldWriter::Write` computed each row's child range from the *logical* row index without adding `array_view_->offset`. When the parent `List` / `LargeList` / `FixedSizeList` array has `offset > 0` (a sliced parent), the variable-length branch read the wrong slot of the offsets buffer (`ArrowArrayViewListChildOffset` does not honor `offset`, unlike `ArrowArrayViewGetIntUnsafe`), and the fixed-size branch multiplied the wrong base index by the element size. The child ranges still indexed into the still-full child values buffer, so list elements ended up attached to the wrong rows.
   
   Practical impact: silent, per-row drift of list-column values when an Arrow table is sliced into multiple batches and ingested via `adbc_ingest` with `mode="create"` then `mode="append"`. The first chunk (`offset=0`) is always correct; every subsequent chunk's list column is shifted by the chunk's `parent.offset`. Scalar columns are unaffected because their writers route through `ArrowArrayViewGetInt*`, which honors `offset`.
   
   Fixes #4319.
   
   ## Fix
   
   ```cpp
   const int64_t logical = array_view_->offset + index;
   if constexpr (IsFixedSize) {
     start = logical * array_view_->layout.child_size_elements;
     end   = start + array_view_->layout.child_size_elements;
   } else {
     start = ArrowArrayViewListChildOffset(array_view_, logical);
     end   = ArrowArrayViewListChildOffset(array_view_, logical + 1);
   }
   ```
   
   ## Test plan
   
   - [x] Added `PostgresCopyWriteListSlicedMatchesDirect` (parameterized over `LIST` and `LARGE_LIST`) and `PostgresCopyWriteFixedSizeListSlicedMatchesDirect`. Each writes rows 3..5 of a 6-row source via `offset`/`length` and asserts the output body equals what the writer produces for those same rows passed as a fresh 3-row array.
   - [x] Verified the new tests **fail on `main`** (sliced output is the wrong size — extra bytes from longer rows the writer mis-attributes) and **pass with this change**.
   - [x] Full `adbc-driver-postgresql-copy-test` suite (73 tests) passes with the change — no regressions in existing list / fixed-size-list / multi-batch coverage.
   - [x] End-to-end reproduction against the `postgres-test` service from `compose.yaml`, using `adbc-driver-postgresql 1.11.0` with the patched `.so` swapped in: a multi-chunk PyArrow `Table.slice` → `adbc_ingest` flow that drifted before now round-trips correctly for `list<string>`, `large_list<string>`, and `fixed_size_list<string, 2>`.
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(c/driver/postgresql): use array_view->offset when writing list rows [arrow-adbc]

Posted by "lidavidm (via GitHub)" <gi...@apache.org>.
lidavidm merged PR #4320:
URL: https://github.com/apache/arrow-adbc/pull/4320


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(c/driver/postgresql): use array_view->offset when writing list rows [arrow-adbc]

Posted by "mediprtl (via GitHub)" <gi...@apache.org>.
mediprtl commented on PR #4320:
URL: https://github.com/apache/arrow-adbc/pull/4320#issuecomment-4476889921

   Added a bind-path regression test as a follow-up commit (b5a8d88). Context (cross-posted from apache#4319): `PostgresCopyListFieldWriter` is reused by `BindStream` via the same `MakeCopyFieldWriter` factory (`c/driver/postgresql/bind_stream.h:166`), so the offset bug also drifts list-typed parameters on `INSERT` / `UPDATE` / `DELETE` / upsert when the bound Arrow array is sliced.
   
   The new `PostgresStatementTest.BindUpsertWithSlicedListParameter` exercises the bind path end-to-end: binds rows 3..5 of a 6-row Arrow batch (with `parent.offset=3` on the struct, id and tags children) as the `text[]` parameter of an `INSERT ... ON CONFLICT DO UPDATE` upsert, then asserts `array_length(tags, 1)` per id matches the slice (1/2/1), not the underlying-rows-0..2 lengths (2/1/2). Fails on the unpatched driver at the first `len` assertion (id=3 reports length 2 — drift to row 0); passes with the writer.h fix from this branch.
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(c/driver/postgresql): use array_view->offset when writing list rows [arrow-adbc]

Posted by "lidavidm (via GitHub)" <gi...@apache.org>.
lidavidm commented on code in PR #4320:
URL: https://github.com/apache/arrow-adbc/pull/4320#discussion_r3331037220


##########
c/driver/postgresql/postgresql_test.cc:
##########
@@ -1892,6 +1892,103 @@ TEST_F(PostgresStatementTest, EmptyStringAndBinaryParameter) {
   ASSERT_THAT(AdbcStatementRelease(&statement, &error), IsOkStatus(&error));
 }
 
+// Regression test for https://github.com/apache/arrow-adbc/issues/4319.
+// The parameterized prepared-statement path (BindAndExecuteCurrentRow)
+// constructs its field writers via the same MakeCopyFieldWriter factory
+// as the COPY ingest path, so the offset bug in PostgresCopyListFieldWriter
+// drifts list-typed bound parameters the same way. This exercises the bind
+// path end-to-end with an INSERT … ON CONFLICT DO UPDATE upsert whose list
+// parameter comes from a sliced Arrow array (parent.offset > 0).
+TEST_F(PostgresStatementTest, BindUpsertWithSlicedListParameter) {
+  ASSERT_THAT(quirks()->DropTable(&connection, "adbc_test", &error), IsOkStatus(&error));
+  ASSERT_THAT(AdbcStatementNew(&connection, &statement, &error), IsOkStatus(&error));
+
+  ASSERT_THAT(
+      AdbcStatementSetSqlQuery(
+          &statement, "CREATE TABLE adbc_test (id INT PRIMARY KEY, tags TEXT[] NOT NULL)",
+          &error),
+      IsOkStatus(&error));
+  {
+    adbc_validation::StreamReader reader;
+    ASSERT_THAT(
+        AdbcStatementExecuteQuery(&statement, &reader.stream.value, nullptr, &error),
+        IsOkStatus(&error));
+  }
+
+  // 6-row struct(int32, list<string>) where odd rows have 1-element tags
+  // and even rows have 2-element tags. The size difference makes drift
+  // structurally observable, not just value-different.
+  adbc_validation::Handle<struct ArrowSchema> bind_schema;
+  adbc_validation::Handle<struct ArrowArray> bind;
+  struct ArrowError na_error;
+  ASSERT_EQ(adbc_validation::MakeSchema(
+                &bind_schema.value,
+                {{"id", NANOARROW_TYPE_INT32},
+                 adbc_validation::SchemaField::Nested(
+                     "tags", NANOARROW_TYPE_LIST, {{"item", NANOARROW_TYPE_STRING}})}),
+            ADBC_STATUS_OK);
+
+  ASSERT_EQ(
+      (adbc_validation::MakeBatch<int32_t, std::vector<std::string>>(
+          &bind_schema.value, &bind.value, &na_error, {0, 1, 2, 3, 4, 5},
+          {std::vector<std::string>{"r0a", "r0b"}, std::vector<std::string>{"r1x"},
+           std::vector<std::string>{"r2a", "r2b"}, std::vector<std::string>{"r3x"},
+           std::vector<std::string>{"r4a", "r4b"}, std::vector<std::string>{"r5x"}})),
+      ADBC_STATUS_OK);
+
+  // Hide rows 0..2 by setting offset/length on the struct root and on
+  // each child column (nanoarrow does not propagate the struct's offset).

Review Comment:
   ```suggestion
     // Hide rows 0..2 by setting offset/length on the struct root and on
     // each child column
   ```



##########
c/driver/postgresql/copy/postgres_copy_writer_test.cc:
##########
@@ -1587,6 +1587,121 @@ TEST_F(PostgresCopyTest, PostgresCopyWriteFixedSizeListInteger) {
   }
 }
 
+// Regression test for https://github.com/apache/arrow-adbc/issues/4319.
+// When the source array has offset > 0 (a sliced parent), the list writer
+// must read child offsets at (array_view->offset + index), not at index.
+// Writing rows 3..5 of a 6-row source via offset/length must produce the
+// same body as writing those rows as a fresh 3-row array.
+TEST_P(PostgresCopyListTest, PostgresCopyWriteListSlicedMatchesDirect) {
+  adbc_validation::Handle<struct ArrowSchema> schema;
+  adbc_validation::Handle<struct ArrowArray> source;
+  adbc_validation::Handle<struct ArrowArray> tail;
+  struct ArrowError na_error;
+
+  ASSERT_EQ(adbc_validation::MakeSchema(
+                &schema.value, {adbc_validation::SchemaField::Nested(
+                                   "col", GetParam(), {{"item", NANOARROW_TYPE_INT32}})}),
+            ADBC_STATUS_OK);
+
+  ASSERT_EQ(
+      adbc_validation::MakeBatch<std::vector<int32_t>>(
+          &schema.value, &source.value, &na_error,
+          {std::vector<int32_t>{1, 2}, std::vector<int32_t>{3, 4, 5}, std::nullopt,
+           std::vector<int32_t>{6}, std::vector<int32_t>{7, 8}, std::vector<int32_t>{9}}),
+      ADBC_STATUS_OK);
+
+  ASSERT_EQ(
+      adbc_validation::MakeBatch<std::vector<int32_t>>(
+          &schema.value, &tail.value, &na_error,
+          {std::vector<int32_t>{6}, std::vector<int32_t>{7, 8}, std::vector<int32_t>{9}}),
+      ADBC_STATUS_OK);
+
+  PostgresCopyStreamWriteTester ref_tester;
+  ASSERT_EQ(ref_tester.Init(&schema.value, &tail.value, *type_resolver_), NANOARROW_OK);
+  ASSERT_EQ(ref_tester.WriteAll(nullptr), ENODATA);
+  const struct ArrowBuffer ref_buf = ref_tester.WriteBuffer();
+
+  // Slice: hide the first 3 rows by setting offset/length on the struct
+  // root and on the list-typed column. nanoarrow does not propagate the
+  // struct's offset down to children, so both levels need updating.

Review Comment:
   ```suggestion
     // Slice: hide the first 3 rows by setting offset/length on the struct
     // root and on the list-typed column.
   ```
   
   (offsets aren't meant to be propagated; an offset on the child would be combined with the offset on the struct)



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(c/driver/postgresql): use array_view->offset when writing list rows [arrow-adbc]

Posted by "mediprtl (via GitHub)" <gi...@apache.org>.
mediprtl commented on PR #4320:
URL: https://github.com/apache/arrow-adbc/pull/4320#issuecomment-4552762367

   @lidavidm can you rerun the pipeline please? Do you want me to the forked repo?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(c/driver/postgresql): use array_view->offset when writing list rows [arrow-adbc]

Posted by "mediprtl (via GitHub)" <gi...@apache.org>.
mediprtl commented on PR #4320:
URL: https://github.com/apache/arrow-adbc/pull/4320#issuecomment-4553369961

   Friendly bump — CI is fully green on a mirror of this branch in my fork (same SHA `fa5cac6`): mediprtl/arrow-adbc#1. Happy to action any feedback; just waiting on workflow approval here. Thanks!


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org