laskoviymishka commented on code in PR #776:
URL: https://github.com/apache/iceberg-go/pull/776#discussion_r2923635024


##########
table/write_records.go:
##########
@@ -0,0 +1,99 @@
+// 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.
+
+package table
+
+import (
+       "context"
+       "fmt"
+       "iter"
+       "strconv"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/iceberg-go"
+       iceio "github.com/apache/iceberg-go/io"
+       "github.com/google/uuid"
+)
+
+// WriteRecordOption configures the behavior of WriteRecords.
+type WriteRecordOption func(*writeRecordConfig)
+
+type writeRecordConfig struct {
+       targetFileSize int64
+       writeUUID      *uuid.UUID
+}
+
+// WithTargetFileSize overrides the table's default target file size.
+func WithTargetFileSize(size int64) WriteRecordOption {
+       return func(c *writeRecordConfig) {
+               c.targetFileSize = size
+       }
+}
+
+// WithWriteUUID sets a specific UUID for file naming.
+func WithWriteUUID(id uuid.UUID) WriteRecordOption {
+       return func(c *writeRecordConfig) {
+               c.writeUUID = &id
+       }
+}
+
+// WriteRecords writes Arrow record batches to Parquet data files for the given
+// table, returning an iterator of the resulting DataFile objects.
+func WriteRecords(ctx context.Context, tbl *Table,
+       schema *arrow.Schema,

Review Comment:
   Is there any validation that the provided schema is compatible with the 
table's Iceberg schema before we start writing? 
   
   If not, a mismatch will produce an error somewhere inside recordsToDataFiles 
that could be hard to connect back to the root cause. Even a note in the doc 
comment explaining what "compatible" means here (field name matching? field 
IDs?) would help callers avoid surprises.



##########
table/write_records.go:
##########
@@ -0,0 +1,99 @@
+// 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.
+
+package table
+
+import (
+       "context"
+       "fmt"
+       "iter"
+       "strconv"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/iceberg-go"
+       iceio "github.com/apache/iceberg-go/io"
+       "github.com/google/uuid"
+)
+
+// WriteRecordOption configures the behavior of WriteRecords.
+type WriteRecordOption func(*writeRecordConfig)
+
+type writeRecordConfig struct {
+       targetFileSize int64
+       writeUUID      *uuid.UUID
+}
+
+// WithTargetFileSize overrides the table's default target file size.
+func WithTargetFileSize(size int64) WriteRecordOption {
+       return func(c *writeRecordConfig) {
+               c.targetFileSize = size
+       }
+}
+
+// WithWriteUUID sets a specific UUID for file naming.
+func WithWriteUUID(id uuid.UUID) WriteRecordOption {
+       return func(c *writeRecordConfig) {
+               c.writeUUID = &id
+       }
+}
+
+// WriteRecords writes Arrow record batches to Parquet data files for the given
+// table, returning an iterator of the resulting DataFile objects.
+func WriteRecords(ctx context.Context, tbl *Table,
+       schema *arrow.Schema,
+       records iter.Seq2[arrow.RecordBatch, error],
+       opts ...WriteRecordOption,
+) iter.Seq2[iceberg.DataFile, error] {
+       cfg := writeRecordConfig{}
+       for _, opt := range opts {
+               opt(&cfg)
+       }
+
+       fs, err := tbl.fsF(ctx)
+       if err != nil {
+               return singleErrorIter(err)
+       }
+
+       writeFS, ok := fs.(iceio.WriteFileIO)
+       if !ok {
+               return singleErrorIter(fmt.Errorf("%w: filesystem does not 
support writing", iceberg.ErrNotImplemented))
+       }
+
+       meta, _ := MetadataBuilderFromBase(tbl.metadata, tbl.metadataLocation)

Review Comment:
   This error discard makes me a bit nervous for a public API. If 
MetadataBuilderFromBase fails, we continue with a nil/broken MetadataBuilder 
and the failure will surface somewhere deep in recordsToDataFiles as a panic or 
a confusing error. 
   
   NewTransaction does the same today, but I think since we're exposing this 
publicly it's worth handling it properly here:
   
   ```
     meta, err := MetadataBuilderFromBase(tbl.metadata, tbl.metadataLocation)
     if err != nil {
         return singleErrorIter(fmt.Errorf("failed to build metadata: %w", err))
     }
   ```



##########
table/write_records.go:
##########
@@ -0,0 +1,99 @@
+// 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.
+
+package table
+
+import (
+       "context"
+       "fmt"
+       "iter"
+       "strconv"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/iceberg-go"
+       iceio "github.com/apache/iceberg-go/io"
+       "github.com/google/uuid"
+)
+
+// WriteRecordOption configures the behavior of WriteRecords.
+type WriteRecordOption func(*writeRecordConfig)
+
+type writeRecordConfig struct {
+       targetFileSize int64
+       writeUUID      *uuid.UUID
+}
+
+// WithTargetFileSize overrides the table's default target file size.
+func WithTargetFileSize(size int64) WriteRecordOption {
+       return func(c *writeRecordConfig) {
+               c.targetFileSize = size
+       }
+}
+
+// WithWriteUUID sets a specific UUID for file naming.
+func WithWriteUUID(id uuid.UUID) WriteRecordOption {
+       return func(c *writeRecordConfig) {
+               c.writeUUID = &id
+       }
+}
+
+// WriteRecords writes Arrow record batches to Parquet data files for the given
+// table, returning an iterator of the resulting DataFile objects.
+func WriteRecords(ctx context.Context, tbl *Table,
+       schema *arrow.Schema,
+       records iter.Seq2[arrow.RecordBatch, error],
+       opts ...WriteRecordOption,
+) iter.Seq2[iceberg.DataFile, error] {
+       cfg := writeRecordConfig{}
+       for _, opt := range opts {
+               opt(&cfg)
+       }
+
+       fs, err := tbl.fsF(ctx)
+       if err != nil {
+               return singleErrorIter(err)
+       }
+
+       writeFS, ok := fs.(iceio.WriteFileIO)
+       if !ok {
+               return singleErrorIter(fmt.Errorf("%w: filesystem does not 
support writing", iceberg.ErrNotImplemented))
+       }
+
+       meta, _ := MetadataBuilderFromBase(tbl.metadata, tbl.metadataLocation)
+
+       if cfg.targetFileSize > 0 {
+               if meta.props == nil {
+                       meta.props = make(iceberg.Properties)
+               }
+               meta.props[WriteTargetFileSizeBytesKey] = 
strconv.FormatInt(cfg.targetFileSize, 10)
+       }
+
+       args := recordWritingArgs{
+               sc:        schema,
+               itr:       records,
+               fs:        writeFS,
+               writeUUID: cfg.writeUUID,
+       }
+
+       return recordsToDataFiles(ctx, tbl.Location(), meta, args)
+}
+
+func singleErrorIter(err error) iter.Seq2[iceberg.DataFile, error] {
+       return func(yield func(iceberg.DataFile, error) bool) {
+               yield(nil, err)

Review Comment:
   nit: it's idiomatic for Go range functions to check the return value of 
yield even when there's only one element  `_ = yield(nil, err)` or `if 
!yield(nil, err) { return }`. 



##########
table/write_records.go:
##########
@@ -0,0 +1,99 @@
+// 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.
+
+package table
+
+import (
+       "context"
+       "fmt"
+       "iter"
+       "strconv"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/iceberg-go"
+       iceio "github.com/apache/iceberg-go/io"
+       "github.com/google/uuid"
+)
+
+// WriteRecordOption configures the behavior of WriteRecords.
+type WriteRecordOption func(*writeRecordConfig)
+
+type writeRecordConfig struct {
+       targetFileSize int64
+       writeUUID      *uuid.UUID
+}
+
+// WithTargetFileSize overrides the table's default target file size.
+func WithTargetFileSize(size int64) WriteRecordOption {
+       return func(c *writeRecordConfig) {
+               c.targetFileSize = size
+       }
+}
+
+// WithWriteUUID sets a specific UUID for file naming.
+func WithWriteUUID(id uuid.UUID) WriteRecordOption {
+       return func(c *writeRecordConfig) {
+               c.writeUUID = &id
+       }
+}
+
+// WriteRecords writes Arrow record batches to Parquet data files for the given
+// table, returning an iterator of the resulting DataFile objects.
+func WriteRecords(ctx context.Context, tbl *Table,
+       schema *arrow.Schema,
+       records iter.Seq2[arrow.RecordBatch, error],

Review Comment:
   Could you add a note about RecordBatch ownership? 
   
   Since Arrow uses reference counting, callers need to know whether it's safe 
to call Release() inside their yield callback or only after the yield returns. 



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to