hcrosse commented on code in PR #2831:
URL: https://github.com/apache/iceberg-go/pull/2831#discussion_r4813353277


##########
table/arrow_utils.go:
##########
@@ -1596,13 +1597,446 @@ func convertPartitionConstants(requested 
*iceberg.Schema, constants map[int]any)
 }
 
 func arrowSchemaEqual(left, right *arrow.Schema) bool {
+       if left == right {
+               return true
+       }
        if left == nil || right == nil {
-               return left == right
+               return false
+       }
+
+       // Schema.Equal ignores top-level metadata, nested list/map child 
names, and
+       // extension storage metadata; full projection normalizes all of them.
+       if !left.Equal(right) || !left.Metadata().Equal(right.Metadata()) {
+               return false
+       }
+       for i := range left.NumFields() {
+               if !arrowNestedFieldNamesEqual(left.Field(i).Type, 
right.Field(i).Type) {
+                       return false
+               }
+       }
+
+       return true
+}
+
+type projectionTier uint8
+
+const (
+       projectionFull projectionTier = iota
+       projectionIdentity
+       projectionRelabel
+)
+
+// relabelNode describes a zero-copy structural relabel for an arrow.ArrayData.
+// A nil node means the input ArrayData already matches the target type exactly
+// (including nested field names, nullability, and metadata) and is retained 
as-is.
+type relabelNode struct {
+       targetType arrow.DataType
+       children   []*relabelNode
+}
+
+// recordProjector caches schema classification for repeated projection of 
record
+// batches against a fixed requested/file schema pair on a single goroutine.
+type recordProjector struct {
+       requested         *iceberg.Schema
+       fileSchema        *iceberg.Schema
+       opts              SchemaOptions
+       targetArrowSchema *arrow.Schema
+
+       targetChecked   bool
+       targetValid     bool
+       lastInputSchema *arrow.Schema
+       tier            projectionTier
+       relabelNodes    []*relabelNode
+}
+
+func newRecordProjector(requested, fileSchema *iceberg.Schema, opts 
SchemaOptions, targetArrowSchema *arrow.Schema) *recordProjector {
+       return &recordProjector{
+               requested:         requested,
+               fileSchema:        fileSchema,
+               opts:              opts,
+               targetArrowSchema: targetArrowSchema,
+       }
+}
+
+func (p *recordProjector) hasValidTargetSchema() bool {
+       if p.targetChecked {
+               return p.targetValid
+       }
+       p.targetChecked = true
+       // Full projection strips top-level metadata via 
RecordFromStructArray(..., nil).
+       if p.targetArrowSchema == nil || p.requested == nil || 
p.targetArrowSchema.HasMetadata() {
+               return false
+       }
+       expected, err := SchemaToArrowSchemaWithOptions(p.requested, 
ArrowSchemaOptions{
+               IncludeFieldIDs:    p.opts.IncludeFieldIDs,
+               UseLargeTypes:      p.opts.UseLargeTypes,
+               TableProperties:    p.opts.TableProperties,
+               dictionaryFieldIDs: p.opts.dictionaryFieldIDs,
+       })
+       if err != nil {
+               return false
+       }
+       p.targetValid = arrowSchemaEqual(p.targetArrowSchema, expected)
+
+       return p.targetValid
+}
+
+func (p *recordProjector) project(ctx context.Context, batch 
arrow.RecordBatch) (arrow.RecordBatch, error) {
+       inSchema := batch.Schema()
+       if inSchema != p.lastInputSchema && !arrowSchemaEqual(inSchema, 
p.lastInputSchema) {
+               p.classify(inSchema)
+       }

Review Comment:
   Good catch. I updated `project` to refresh `lastInputSchema` on structurally 
equal schema cache hits and added a regression test covering pointer reuse 
without rebuilding `relabelNodes`.



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