Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

materialize-iceberg: use a different mechanism for computing the highest field ID from a schema #2535

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion materialize-iceberg/type_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func computeSchemaForUpdatedTable(current *iceberg.Schema, update boilerplate.Ma
nextFields = append(nextFields, f)
}

appendProjectionsAsFields(&nextFields, update.NewProjections, current.HighestFieldID())
appendProjectionsAsFields(&nextFields, update.NewProjections, getHighestFieldID(current))

return iceberg.NewSchemaWithIdentifiers(current.ID+1, current.IdentifierFieldIDs, nextFields...)
}
Expand Down Expand Up @@ -164,3 +164,25 @@ func appendProjectionsAsFields(dst *[]iceberg.NestedField, ps []boilerplate.Mapp

return ids, id
}

// TODO(whb): The version of go-iceberg we are using has a broken
// (*Schema).HighestFieldID method which does not work with most logical types.
// It looks like that is probably fixed in a more recent version, but we need to
// be on Go 1.23 to use that.
func getHighestFieldID(sch *iceberg.Schema) int {
var out int

for _, f := range sch.Fields() {
out = max(out, f.ID)
if l, ok := f.Type.(*iceberg.ListType); ok {
out = max(out, l.ElementID)
}
// Ignoring the possibility of Map and Struct types, which also have
// elements with their own field IDs since we don't create columns with
// those types. We _also_ don't create columns with list types, but it
// is more conceivable that we could add that in the short term. This
// should all be removed when we upgrade Go & iceberg-go anyway.
}

return out
}
26 changes: 26 additions & 0 deletions materialize-iceberg/type_mapping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package connector

import (
"testing"

"github.com/apache/iceberg-go"
"github.com/stretchr/testify/require"
)

func TestGetHighestFieldID(t *testing.T) {
originalFields := []iceberg.NestedField{
{ID: 1, Name: "firstKey", Required: true, Type: iceberg.Int64Type{}},
{ID: 2, Name: "secondKey", Required: true, Type: iceberg.StringType{}},
{ID: 3, Name: "reqVal", Required: true, Type: iceberg.StringType{}},
{ID: 4, Name: "optVal", Required: false, Type: iceberg.BooleanType{}},
{ID: 5, Name: "thirdVal", Required: true, Type: iceberg.DecimalTypeOf(38, 0)},
{ID: 6, Name: "fourthVal", Required: true, Type: &iceberg.ListType{
ElementID: 7,
Element: iceberg.StringType{},
}},
}

originalSchema := iceberg.NewSchemaWithIdentifiers(1, []int{1, 2}, originalFields...)
require.NotEqual(t, originalSchema.HighestFieldID(), getHighestFieldID(originalSchema))
require.Equal(t, 7, getHighestFieldID(originalSchema))
}
Loading