Skip to content
Open
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
25 changes: 17 additions & 8 deletions table/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,30 @@ func (scan *Scan) Projection() (*iceberg.Schema, error) {
}

func (scan *Scan) buildPartitionProjection(specID int) (iceberg.BooleanExpression, error) {
project := newInclusiveProjection(scan.metadata.CurrentSchema(),
scan.metadata.PartitionSpecs()[specID], true)
spec, err := getPartitionSpecByID(scan.metadata.PartitionSpecs(), specID)
if err != nil {
return nil, err
}
project := newInclusiveProjection(scan.metadata.CurrentSchema(), *spec, true)

return project(scan.rowFilter)
}

func (scan *Scan) buildManifestEvaluator(specID int) (func(iceberg.ManifestFile) (bool, error), error) {
spec := scan.metadata.PartitionSpecs()[specID]
spec, err := getPartitionSpecByID(scan.metadata.PartitionSpecs(), specID)
if err != nil {
return nil, err
}

return newManifestEvaluator(spec, scan.metadata.CurrentSchema(),
return newManifestEvaluator(*spec, scan.metadata.CurrentSchema(),
scan.partitionFilters.Get(specID), scan.caseSensitive)
}

func (scan *Scan) buildPartitionEvaluator(specID int) func(iceberg.DataFile) (bool, error) {
spec := scan.metadata.PartitionSpecs()[specID]
func (scan *Scan) buildPartitionEvaluator(specID int) (func(iceberg.DataFile) (bool, error), error) {
spec, err := getPartitionSpecByID(scan.metadata.PartitionSpecs(), specID)
if err != nil {
return nil, err
}
partType := spec.PartitionType(scan.metadata.CurrentSchema())
partSchema := iceberg.NewSchema(0, partType.FieldList...)
partExpr := scan.partitionFilters.Get(specID)
Expand All @@ -268,7 +277,7 @@ func (scan *Scan) buildPartitionEvaluator(specID int) func(iceberg.DataFile) (bo
}

return fn(getPartitionRecord(d, partType))
}
}, nil
}

func (scan *Scan) checkSequenceNumber(minSeqNum int64, manifest iceberg.ManifestFile) bool {
Expand Down Expand Up @@ -370,7 +379,7 @@ func (scan *Scan) collectManifestEntries(
g, _ := errgroup.WithContext(ctx)
g.SetLimit(concurrencyLimit)

partitionEvaluators := newKeyDefaultMap(scan.buildPartitionEvaluator)
partitionEvaluators := newKeyDefaultMapWrapErr(scan.buildPartitionEvaluator)

for _, mf := range manifestList {
if !scan.checkSequenceNumber(minSeqNum, mf) {
Expand Down
93 changes: 93 additions & 0 deletions table/scanner_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

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

func newDataManifest(minSeqNum int64) iceberg.ManifestFile {
Expand Down Expand Up @@ -135,3 +136,95 @@ func TestKeyDefaultMapRaceCondition(t *testing.T) {
assert.Equal(t, int64(1), callCount,
"factory should be called exactly once per key, but was called %d times", callCount)
}

func TestBuildPartitionProjectionWithInvalidSpecID(t *testing.T) {
schema := iceberg.NewSchema(
1,
iceberg.NestedField{
ID: 1, Name: "id",
Type: iceberg.PrimitiveTypes.Int64, Required: true,
},
)

metadata, err := NewMetadata(
schema,
iceberg.UnpartitionedSpec,
UnsortedSortOrder,
"s3://test-bucket/test_table",
iceberg.Properties{},
)
require.NoError(t, err)

scan := &Scan{
metadata: metadata,
rowFilter: iceberg.AlwaysTrue{},
caseSensitive: true,
}

expr, err := scan.buildPartitionProjection(999)
require.Error(t, err)
assert.Nil(t, expr)
assert.ErrorContains(t, err, "partition spec with id 999 not found")
}

func TestBuildManifestEvaluatorWithInvalidSpecID(t *testing.T) {
schema := iceberg.NewSchema(
1,
iceberg.NestedField{
ID: 1, Name: "id",
Type: iceberg.PrimitiveTypes.Int64, Required: true,
},
)

metadata, err := NewMetadata(
schema,
iceberg.UnpartitionedSpec,
UnsortedSortOrder,
"s3://test-bucket/test_table",
iceberg.Properties{},
)
require.NoError(t, err)

scan := &Scan{
metadata: metadata,
rowFilter: iceberg.AlwaysTrue{},
caseSensitive: true,
}

scan.partitionFilters = newKeyDefaultMapWrapErr(scan.buildPartitionProjection)

evaluator, err := scan.buildManifestEvaluator(999)
require.Error(t, err)
assert.Nil(t, evaluator)
assert.ErrorContains(t, err, "partition spec with id 999 not")
}

func TestBuildPartitionEvaluatorWithInvalidSpecID(t *testing.T) {
schema := iceberg.NewSchema(
1,
iceberg.NestedField{
ID: 1, Name: "id",
Type: iceberg.PrimitiveTypes.Int64, Required: true,
},
)

metadata, err := NewMetadata(
schema,
iceberg.UnpartitionedSpec,
UnsortedSortOrder,
"s3://test-bucket/test_table",
iceberg.Properties{},
)
require.NoError(t, err)

scan := &Scan{
metadata: metadata,
rowFilter: iceberg.AlwaysTrue{},
caseSensitive: true,
}

evaluator, err := scan.buildPartitionEvaluator(999)
require.Error(t, err)
assert.Nil(t, evaluator)
assert.ErrorContains(t, err, "partition spec with id 999 not found")
}
36 changes: 36 additions & 0 deletions table/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 (
"fmt"
"slices"

"github.com/apache/iceberg-go"
)

func getPartitionSpecByID(specs []iceberg.PartitionSpec, specID int) (*iceberg.PartitionSpec, error) {
i := slices.IndexFunc(specs, func(spec iceberg.PartitionSpec) bool {
return spec.ID() == specID
})
if i < 0 {
return nil, fmt.Errorf("partition spec with id %d not found", specID)
}

return &specs[i], nil
}
Loading