nssalian commented on code in PR #1607:
URL: https://github.com/apache/iceberg-go/pull/1607#discussion_r3937012732


##########
table/variant_residual.go:
##########
@@ -0,0 +1,316 @@
+// 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"
+       "log/slog"
+       "strconv"
+       "strings"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/arrow-go/v18/arrow/array"
+       "github.com/apache/arrow-go/v18/arrow/compute"
+       "github.com/apache/arrow-go/v18/arrow/extensions"
+       "github.com/apache/arrow-go/v18/arrow/memory"
+       "github.com/apache/iceberg-go"
+       "github.com/google/uuid"
+)
+
+// augmentSchemaWithExtracts returns fileSchema plus one primitive column per 
variant extract term.
+func augmentSchemaWithExtracts(fileSchema *iceberg.Schema, cols 
[]iceberg.VariantExtractColumn) *iceberg.Schema {
+       fields := fileSchema.Fields()
+       for _, c := range cols {
+               fields = append(fields, iceberg.NestedField{
+                       ID:   c.FieldID,
+                       Name: c.Name,
+                       Type: c.Term.Type().(iceberg.PrimitiveType),
+               })
+       }
+
+       return iceberg.NewSchemaWithIdentifiers(fileSchema.ID, 
fileSchema.IdentifierFieldIDs, fields...)
+}
+
+// buildExtractColumn materializes one variant extract term into a typed Arrow 
array over rec.
+func buildExtractColumn(col iceberg.VariantExtractColumn, rec 
arrow.RecordBatch, mem memory.Allocator) (arrow.Array, arrow.Field, error) {
+       typ := col.Term.Type().(iceberg.PrimitiveType)
+       dt, err := TypeToArrowType(typ, false, false)
+       if err != nil {
+               return nil, arrow.Field{}, err
+       }
+
+       bldr := array.NewBuilder(mem, dt)
+       defer bldr.Release()
+
+       n := int(rec.NumRows())
+       varName := col.Term.Ref().Field().Name
+       arr := resolveVariantSource(rec, col.Term.Ref().Field().ID, 
col.SourcePath)
+       if arr == nil {
+               return nil, arrow.Field{}, fmt.Errorf("%w: variant extract 
column %q not found in file", iceberg.ErrInvalidArgument, varName)
+       }
+       varr, ok := arr.(*extensions.VariantArray)
+       if !ok {
+               return nil, arrow.Field{}, fmt.Errorf("%w: variant extract 
column %q is not a VariantArray (got %T)", iceberg.ErrInvalidArgument, varName, 
arr)
+       }
+
+       for i := range n {
+               if varr.IsNull(i) {
+                       bldr.AppendNull()
+
+                       continue
+               }
+
+               v, verr := varr.Value(i)
+               if verr != nil {
+                       slog.Warn("variant extract: skipping undecodable 
variant value", "column", varName, "row", i, "err", verr)
+                       bldr.AppendNull()
+
+                       continue
+               }
+
+               lit, ok := col.Term.ExtractValue(v)
+               if !ok {
+                       bldr.AppendNull()
+
+                       continue
+               }
+
+               if aerr := appendExtractLiteral(bldr, lit); aerr != nil {
+                       return nil, arrow.Field{}, aerr
+               }
+       }
+
+       field := arrow.Field{
+               Name:     col.Name,
+               Type:     dt,
+               Nullable: true,
+               Metadata: arrow.NewMetadata([]string{ArrowParquetFieldIDKey}, 
[]string{strconv.Itoa(col.FieldID)}),
+       }
+
+       return bldr.NewArray(), field, nil
+}
+
+// resolveVariantSource locates the extract's source array by field id, 
descending
+// nested structs; it falls back to the file-schema path when field ids are 
absent.
+func resolveVariantSource(rec arrow.RecordBatch, fieldID int, sourcePath 
string) arrow.Array {
+       for i, f := range rec.Schema().Fields() {
+               if a := descendByFieldID(f, rec.Column(i), fieldID); a != nil {
+                       return a
+               }
+       }
+       if sourcePath == "" {
+               return nil
+       }

Review Comment:
   Fixed this. This now carries structured path segments from the schema 
(columnPathSegments walks the nested fields) instead of re-splitting the 
flattened name, so a column literally named a.b stays one segment. Added 
TestBuildExtractColumnDottedName.



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