rambleraptor commented on code in PR #2073: URL: https://github.com/apache/iceberg-go/pull/2073#discussion_r4148911337
########## 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 Review Comment: Changed the comment! -- 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]
