JingsongLi commented on code in PR #420:
URL: https://github.com/apache/paimon-rust/pull/420#discussion_r3491241866


##########
bindings/python/src/write.rs:
##########
@@ -0,0 +1,184 @@
+// 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.
+
+use std::sync::Arc;
+
+use arrow::datatypes::{DataType as ArrowDataType, Schema as ArrowSchema};
+use arrow::pyarrow::FromPyArrow;
+use arrow::record_batch::RecordBatch;
+use paimon::table::{CommitMessage, Table, TableCommit, TableWrite};
+use paimon_datafusion::runtime::runtime;
+use pyo3::exceptions::{PyTypeError, PyValueError};
+use pyo3::prelude::*;
+
+use crate::error::to_py_err;
+
+/// Validate an incoming batch schema against the table's target Arrow schema,
+/// replicating pypaimon's `_validate_pyarrow_schema` semantics (minus the
+/// projection-write branch PR1 lacks): field count, order, and names must 
match;
+/// types must match field-by-field, tolerating binary-family interchange
+/// (Binary / LargeBinary / FixedSizeBinary). The nullable flag is 
intentionally
+/// NOT compared, since `build_target_arrow_schema` derives nullability from 
the
+/// Paimon field while pyarrow-constructed batches infer nullable=true. No 
cast.
+fn validate_batch_schema(input: &ArrowSchema, target: &ArrowSchema) -> 
PyResult<()> {
+    let mismatch = || {
+        PyValueError::new_err(format!(
+            "Input schema is not consistent with the table schema. \
+             input: {input:?}, table: {target:?}"
+        ))
+    };
+    if input.fields().len() != target.fields().len() {
+        return Err(mismatch());
+    }
+    for (i, t) in input.fields().iter().zip(target.fields().iter()) {
+        if i.name() != t.name() {
+            return Err(mismatch());
+        }
+        if i.data_type() != t.data_type()
+            && !(is_binary_family(i.data_type()) && 
is_binary_family(t.data_type()))

Review Comment:
   This accepts LargeBinary/FixedSizeBinary as equivalent to Binary, but the 
batch is then passed through unchanged. The lower write path still uses the 
original Arrow arrays/schema: for example `extract_datum_from_arrow` only 
downcasts binary Paimon fields to `arrow_array::BinaryArray`, and append writes 
would also see the incoming schema rather than the table schema. So a batch 
that this check accepts can later fail with a type-mismatch (or write files 
whose Arrow schema differs from the table schema). Please either reject 
non-exact binary-family types here or normalize/cast the batch to the target 
Arrow schema before calling `write_arrow_batch`.



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