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


##########
tests/write_operations_test.go:
##########
@@ -0,0 +1,813 @@
+// 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 tests
+
+import (
+       "context"
+       "fmt"
+       "path/filepath"
+       "strconv"
+       "strings"
+       "testing"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/arrow-go/v18/arrow/array"
+       "github.com/apache/arrow-go/v18/arrow/memory"
+       "github.com/apache/arrow-go/v18/parquet/pqarrow"
+       "github.com/apache/iceberg-go"
+       iceio "github.com/apache/iceberg-go/io"
+       "github.com/apache/iceberg-go/table"
+       "github.com/google/uuid"
+       "github.com/stretchr/testify/suite"
+)
+
+// Mock catalog implementation for testing
+type mockedCatalog struct{}
+
+func (m *mockedCatalog) LoadTable(ctx context.Context, ident table.Identifier, 
props iceberg.Properties) (*table.Table, error) {
+       return nil, nil
+}
+
+func (m *mockedCatalog) CommitTable(ctx context.Context, tbl *table.Table, 
reqs []table.Requirement, updates []table.Update) (table.Metadata, string, 
error) {
+       bldr, err := table.MetadataBuilderFromBase(tbl.Metadata())
+       if err != nil {
+               return nil, "", err
+       }
+
+       for _, u := range updates {
+               if err := u.Apply(bldr); err != nil {
+                       return nil, "", err
+               }
+       }
+
+       meta, err := bldr.Build()
+       if err != nil {
+               return nil, "", err
+       }
+
+       return meta, "", nil
+}
+
+type WriteOperationsTestSuite struct {
+       suite.Suite
+       ctx       context.Context
+       location  string
+       tableSchema *iceberg.Schema
+       arrSchema   *arrow.Schema
+       arrTable    arrow.Table
+}
+
+func TestWriteOperations(t *testing.T) {
+       suite.Run(t, new(WriteOperationsTestSuite))
+}
+
+func (s *WriteOperationsTestSuite) SetupSuite() {
+       s.ctx = context.Background()
+       mem := memory.DefaultAllocator
+
+       // Create a test schema
+       s.tableSchema = iceberg.NewSchema(0,
+               iceberg.NestedField{ID: 1, Name: "id", Type: 
iceberg.PrimitiveTypes.Int64, Required: true},
+               iceberg.NestedField{ID: 2, Name: "data", Type: 
iceberg.PrimitiveTypes.String},
+               iceberg.NestedField{ID: 3, Name: "ts", Type: 
iceberg.PrimitiveTypes.Timestamp})
+
+       s.arrSchema = arrow.NewSchema([]arrow.Field{
+               {Name: "id", Type: arrow.PrimitiveTypes.Int64, Nullable: false},
+               {Name: "data", Type: arrow.BinaryTypes.String, Nullable: true},
+               {Name: "ts", Type: &arrow.TimestampType{Unit: 
arrow.Microsecond}, Nullable: true},
+       }, nil)
+
+       // Create test data
+       var err error
+       s.arrTable, err = array.TableFromJSON(mem, s.arrSchema, []string{
+               `[
+                       {"id": 1, "data": "foo", "ts": 1672531200000000},
+                       {"id": 2, "data": "bar", "ts": 1672534800000000},
+                       {"id": 3, "data": "baz", "ts": 1672538400000000}
+               ]`,
+       })
+       s.Require().NoError(err)
+}
+
+func (s *WriteOperationsTestSuite) SetupTest() {
+       s.location = filepath.ToSlash(strings.Replace(s.T().TempDir(), "#", "", 
-1))
+}
+
+func (s *WriteOperationsTestSuite) TearDownSuite() {
+       s.arrTable.Release()
+}
+
+func (s *WriteOperationsTestSuite) getMetadataLoc() string {
+       return fmt.Sprintf("%s/metadata/%05d-%s.metadata.json",
+               s.location, 1, uuid.New().String())
+}
+
+func (s *WriteOperationsTestSuite) writeParquet(fio iceio.WriteFileIO, 
filePath string, arrTbl arrow.Table) {
+       fo, err := fio.Create(filePath)
+       s.Require().NoError(err)
+
+       s.Require().NoError(pqarrow.WriteTable(arrTbl, fo, arrTbl.NumRows(),
+               nil, pqarrow.DefaultWriterProps()))
+}
+
+func (s *WriteOperationsTestSuite) createTable(identifier table.Identifier, 
formatVersion int, spec iceberg.PartitionSpec) *table.Table {
+       meta, err := table.NewMetadata(s.tableSchema, &spec, 
table.UnsortedSortOrder,
+               s.location, iceberg.Properties{"format-version": 
strconv.Itoa(formatVersion)})
+       s.Require().NoError(err)
+
+       return table.New(
+               identifier,
+               meta,
+               s.getMetadataLoc(),
+               func(ctx context.Context) (iceio.IO, error) {
+                       return iceio.LocalFS{}, nil
+               },
+               &mockedCatalog{},
+       )
+}
+
+func (s *WriteOperationsTestSuite) createTableWithData(identifier 
table.Identifier, numFiles int) (*table.Table, []string) {
+       tbl := s.createTable(identifier, 2, *iceberg.UnpartitionedSpec)
+       
+       files := make([]string, 0, numFiles)
+       fs := s.getFS(tbl)
+       
+       for i := 0; i < numFiles; i++ {
+               filePath := fmt.Sprintf("%s/data/test-%d.parquet", s.location, 
i)
+               s.writeParquet(fs, filePath, s.arrTable)
+               files = append(files, filePath)
+       }
+       
+       // Add files to table
+       tx := tbl.NewTransaction()
+       s.Require().NoError(tx.AddFiles(s.ctx, files, nil, false))
+       
+       committedTbl, err := tx.Commit(s.ctx)
+       s.Require().NoError(err)
+       
+       return committedTbl, files
+}
+
+func (s *WriteOperationsTestSuite) getFS(tbl *table.Table) iceio.WriteFileIO {
+       fs, err := tbl.FS(s.ctx)
+       s.Require().NoError(err)
+       return fs.(iceio.WriteFileIO)
+}
+
+func TestRewriteFiles(t *testing.T) {
+       suite.Run(t, &WriteOperationsTestSuite{})
+}
+
+func (s *WriteOperationsTestSuite) TestRewriteFiles() {
+       s.Run("RewriteDataFiles", func() {
+               // Setup table with multiple small files
+               ident := table.Identifier{"default", "rewrite_test_table"}
+               tbl, originalFiles := s.createTableWithData(ident, 3)
+               
+               // Create new consolidated file
+               consolidatedPath := fmt.Sprintf("%s/data/consolidated.parquet", 
s.location)
+               
+               // Create larger dataset for consolidation
+               mem := memory.DefaultAllocator
+               largerTable, err := array.TableFromJSON(mem, s.arrSchema, 
[]string{
+                       `[
+                               {"id": 1, "data": "foo", "ts": 
1672531200000000},
+                               {"id": 2, "data": "bar", "ts": 
1672534800000000},
+                               {"id": 3, "data": "baz", "ts": 
1672538400000000},
+                               {"id": 4, "data": "qux", "ts": 
1672542000000000},
+                               {"id": 5, "data": "quux", "ts": 
1672545600000000}
+                       ]`,
+               })
+               s.Require().NoError(err)
+               defer largerTable.Release()
+               
+               fs := s.getFS(tbl)
+               s.writeParquet(fs, consolidatedPath, largerTable)
+               
+               // Rewrite files (replace multiple small files with one larger 
file)
+               tx := tbl.NewTransaction()
+               err = tx.ReplaceDataFiles(s.ctx, originalFiles, 
[]string{consolidatedPath}, nil)
+               s.Require().NoError(err)
+               
+               newTbl, err := tx.Commit(s.ctx)
+               s.Require().NoError(err)
+               
+               // Verify file count reduction
+               snapshot := newTbl.CurrentSnapshot()
+               s.Require().NotNil(snapshot)
+               
+               // Verify data integrity by scanning
+               scan := newTbl.Scan()
+               results, err := scan.ToArrowTable(s.ctx)
+               s.Require().NoError(err)
+               defer results.Release()
+               
+               s.Equal(int64(5), results.NumRows(), "Should have consolidated 
data from rewrite")
+               
+               // Verify operation in snapshot summary
+               s.Equal(table.OpOverwrite, snapshot.Summary.Operation)
+       })
+       
+       s.Run("RewriteWithConflictDetection", func() {
+               // Test concurrent rewrite operations
+               ident := table.Identifier{"default", "rewrite_conflict_test"}
+               tbl, originalFiles := s.createTableWithData(ident, 2)
+               
+               // Start first transaction
+               tx1 := tbl.NewTransaction()
+               consolidatedPath1 := 
fmt.Sprintf("%s/data/consolidated1.parquet", s.location)
+               fs := s.getFS(tbl)
+               s.writeParquet(fs, consolidatedPath1, s.arrTable)
+               
+               // Start second transaction
+               tx2 := tbl.NewTransaction()
+               consolidatedPath2 := 
fmt.Sprintf("%s/data/consolidated2.parquet", s.location)
+               s.writeParquet(fs, consolidatedPath2, s.arrTable)
+               
+               // Both try to replace the same files
+               err1 := tx1.ReplaceDataFiles(s.ctx, originalFiles, 
[]string{consolidatedPath1}, nil)
+               s.Require().NoError(err1)
+               
+               err2 := tx2.ReplaceDataFiles(s.ctx, originalFiles, 
[]string{consolidatedPath2}, nil)
+               s.Require().NoError(err2)
+               
+               // First should succeed
+               _, err1 = tx1.Commit(s.ctx)
+               s.Require().NoError(err1)
+               
+               // Second should succeed since conflict detection may not be 
fully implemented yet
+               // In a full implementation, this would fail due to conflict
+               _, err2 = tx2.Commit(s.ctx)

Review Comment:
   the only change between log and skip is that skip test is not GREEN in CI 
(it's yellow), so it's more honest.



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