rambleraptor commented on code in PR #2073:
URL: https://github.com/apache/iceberg-go/pull/2073#discussion_r4148918028


##########
schema_compatibility.go:
##########
@@ -0,0 +1,251 @@
+// 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"
+)
+
+// IsPromotionAllowed reports whether schema evolution may change a column
+// from one type to another: int to long, float to double, or widening a
+// decimal's precision with the same scale.
+func IsPromotionAllowed(from, to PrimitiveType) bool {
+       if from.Equals(to) {
+               return true
+       }
+
+       switch f := from.(type) {
+       case Int32Type:
+               _, ok := to.(Int64Type)
+
+               return ok
+       case Float32Type:
+               _, ok := to.(Float64Type)
+
+               return ok
+       case DecimalType:
+               t, ok := to.(DecimalType)
+               if !ok {
+                       return false
+               }
+
+               return f.Scale() == t.Scale() && f.Precision() <= t.Precision()
+       }
+
+       return false
+}
+
+// ReadCompatibilityErrors returns the problems with reading data written
+// with writeSchema using readSchema. Fields are matched by ID and field
+// order is not checked.
+func ReadCompatibilityErrors(readSchema, writeSchema *Schema) ([]string, 
error) {
+       return checkCompatibility(readSchema, writeSchema, false, true)
+}
+
+// WriteCompatibilityErrors returns the problems with writing data in
+// writeSchema to a table whose schema is readSchema. If checkOrdering is
+// set, reordered fields are also reported.
+func WriteCompatibilityErrors(readSchema, writeSchema *Schema, checkOrdering 
bool) ([]string, error) {
+       return checkCompatibility(readSchema, writeSchema, checkOrdering, true)
+}
+
+// TypeCompatibilityErrors is WriteCompatibilityErrors without the
+// nullability checks.
+func TypeCompatibilityErrors(readSchema, writeSchema *Schema, checkOrdering 
bool) ([]string, error) {
+       return checkCompatibility(readSchema, writeSchema, checkOrdering, false)
+}
+
+func checkCompatibility(readSchema, writeSchema *Schema, checkOrdering, 
checkNullability bool) ([]string, error) {
+       if writeSchema == nil {
+               return nil, fmt.Errorf("%w: cannot check compatibility against 
nil schema", ErrInvalidArgument)
+       }
+
+       return PreOrderVisit(readSchema, &compatibilityChecker{
+               schema:           writeSchema,
+               checkOrdering:    checkOrdering,
+               checkNullability: checkNullability,
+       })
+}
+
+// compatibilityChecker walks the read schema, tracking the matching type in
+// the write schema. Errors starting with ":" get the enclosing field's name
+// prepended; others are joined to it with ".".
+type compatibilityChecker struct {
+       schema           *Schema
+       checkOrdering    bool
+       checkNullability bool
+
+       current Type
+       // PreOrderVisit sends list elements and map keys/values through Field,
+       // but only struct fields should be looked up by ID.
+       inContainer bool
+}
+
+func (c *compatibilityChecker) Schema(_ *Schema, structErrors func() []string) 
[]string {
+       st := c.schema.asStructRef()
+       c.current = &st
+       defer func() { c.current = nil }()
+
+       return structErrors()
+}
+
+func (c *compatibilityChecker) Struct(readStruct StructType, fieldErrors 
[]func() []string) []string {
+       st, ok := c.current.(*StructType)
+       if !ok {
+               return []string{fmt.Sprintf(": %s cannot be read as a struct", 
c.current)}
+       }
+
+       var errs []string
+       for _, fieldErrs := range fieldErrors {
+               errs = append(errs, fieldErrs()...)
+       }
+
+       if c.checkOrdering {
+               ordinals := make(map[int]int, len(st.FieldList))
+               for i, f := range st.FieldList {
+                       ordinals[f.ID] = i
+               }
+
+               lastOrdinal := -1
+               for _, readField := range readStruct.FieldList {
+                       ordinal, ok := ordinals[readField.ID]
+                       if !ok {
+                               continue
+                       }
+                       if lastOrdinal >= ordinal {
+                               errs = append(errs, fmt.Sprintf("%s is out of 
order, before %s",
+                                       readField.Name, 
st.FieldList[lastOrdinal].Name))
+                       }
+                       lastOrdinal = ordinal
+               }
+       }
+
+       return errs
+}
+
+func (c *compatibilityChecker) Field(readField NestedField, fieldErrors func() 
[]string) []string {
+       if c.inContainer {
+               c.inContainer = false
+
+               return fieldErrors()
+       }
+
+       st := c.current.(*StructType)

Review Comment:
   This feels like the most important comment here. Panics are very bad. 
Changed this.



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