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


##########
table/variant_residual.go:
##########
@@ -0,0 +1,193 @@
+// 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"
+       "strconv"
+
+       "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, error) {
+       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.NewSchema(fileSchema.ID, fields...), nil
+}
+
+// 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())
+       varIdx := fieldIndexByID(rec.Schema(), col.Term.Ref().Field().ID)
+       varr, _ := columnAt(rec, varIdx).(*extensions.VariantArray)
+
+       for i := 0; i < n; i++ {
+               if varr == nil || varr.IsNull(i) {
+                       bldr.AppendNull()
+
+                       continue
+               }
+
+               v, verr := varr.Value(i)
+               if verr != nil {
+                       bldr.AppendNull()

Review Comment:
   Added a `slog.Warn` for the undecodable-value case so it's not silent. Kept 
it non-fatal (didn't want one bad row to kill the whole scan) - happy to make 
it a hard error if you'd prefer.



##########
variant_path.go:
##########
@@ -0,0 +1,132 @@
+// 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 iceberg
+
+import (
+       "fmt"
+       "strings"
+)
+
+// NormalizeVariantPath renders member names as the spec's RFC-9535 normalized 
JSON path.
+func NormalizeVariantPath(fields []string) string {
+       if len(fields) == 0 {
+               return "$"
+       }
+
+       var b strings.Builder
+       b.WriteByte('$')
+       for _, f := range fields {
+               b.WriteString("['")
+               b.WriteString(rfc9535Escape(f))
+               b.WriteString("']")
+       }
+
+       return b.String()
+}
+
+func rfc9535Escape(name string) string {
+       if strings.IndexFunc(name, func(r rune) bool {
+               return r < 0x20 || r == '\'' || r == '\\'
+       }) < 0 {
+               return name
+       }
+
+       var b strings.Builder
+       b.Grow(len(name) + 4)
+       for _, r := range name {
+               switch r {
+               case '\b':
+                       b.WriteString(`\b`)
+               case '\t':
+                       b.WriteString(`\t`)
+               case '\f':
+                       b.WriteString(`\f`)
+               case '\n':
+                       b.WriteString(`\n`)
+               case '\r':
+                       b.WriteString(`\r`)
+               case '\'':
+                       b.WriteString(`\'`)
+               case '\\':
+                       b.WriteString(`\\`)
+               default:
+                       if r < 0x20 {
+                               fmt.Fprintf(&b, `\u%04x`, r)
+                       } else {
+                               b.WriteRune(r)
+                       }
+               }
+       }
+
+       return b.String()
+}
+
+// parseVariantPath parses a dot-shorthand variant path ($.a.b) into its 
member names.
+func parseVariantPath(path string) ([]string, error) {

Review Comment:
   Fixed - `parseVariantPath` now takes both dot and bracket forms, so `Path()` 
round-trips. Went a bit further and taught it to decode `\uXXXX` surrogate 
pairs (astral chars) instead of mangling them to U+FFFD; lone/unpaired 
surrogates error now. I had a PR for the java one too but it needed some 
fallback mechanism that was not trivial - will do that later.



##########
table/evaluators.go:
##########
@@ -868,25 +880,44 @@ func (m *inclusiveMetricsEval) VisitNotNan(t 
iceberg.BoundTerm) bool {
        return rowsMightMatch
 }
 
-func (m *inclusiveMetricsEval) VisitLess(t iceberg.BoundTerm, lit 
iceberg.Literal) bool {
-       field := t.Ref().Field()
-       fieldID := field.ID
+// boundFor decodes the file bound for term t from raw: a scalar for a 
reference, or the

Review Comment:
   Intentional - a nil bound just means "nothing to prune on", so keeping the 
file is the right best-effort behavior. Same posture as the extract decode path 
above.



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