Repository navigation
API: Prevent intermediate overflow in integer truncation #18112
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * 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 org.apache.iceberg; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.Map; | ||
| import org.apache.iceberg.catalog.Namespace; | ||
| import org.apache.iceberg.catalog.TableIdentifier; | ||
| import org.apache.iceberg.expressions.Expressions; | ||
| import org.apache.iceberg.inmemory.InMemoryCatalog; | ||
| import org.apache.iceberg.io.CloseableIterable; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class TestTruncatePartitionScanning { | ||
| @Test | ||
| void largeWidthRetainsMatchingPartition() throws Exception { | ||
| Schema schema = new Schema(Types.NestedField.required(1, "id", Types.IntegerType.get())); | ||
| PartitionSpec spec = PartitionSpec.builderFor(schema).truncate("id", Integer.MAX_VALUE).build(); | ||
| PartitionKey key = new PartitionKey(spec, schema); | ||
| key.partition(TestHelpers.Row.of(Integer.MAX_VALUE - 1)); | ||
|
|
||
| try (InMemoryCatalog catalog = new InMemoryCatalog()) { | ||
| catalog.initialize("test", Map.of()); | ||
| catalog.createNamespace(Namespace.of("db")); | ||
| Table table = catalog.createTable(TableIdentifier.of("db", "table"), schema, spec); | ||
| DataFile file = | ||
| DataFiles.builder(spec) | ||
| .withPath("/data/file.parquet") | ||
| .withPartition(key) | ||
| .withFileSizeInBytes(100) | ||
| .withRecordCount(1) | ||
| .build(); | ||
| table.newAppend().appendFile(file).commit(); | ||
|
|
||
| try (CloseableIterable<FileScanTask> tasks = | ||
| table.newScan().filter(Expressions.greaterThanOrEqual("id", 0)).planFiles()) { | ||
| assertThat(tasks) | ||
| .extracting(task -> task.file().location()) | ||
| .containsExactly(file.location()); | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,6 +124,20 @@ public void removeTables() { | |
| sql("DROP TABLE IF EXISTS %s", tableName); | ||
| } | ||
|
|
||
| @TestTemplate | ||
| void scanWithLargeIntegerTruncateWidth() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add this one for the other Spark versions as well?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the regression test for Spark 3.5, 4.0, and 4.2. All nine added cases passed across Parquet, Avro, etc. Updated validation notes too. |
||
| sql( | ||
| "CREATE TABLE %s (id INT) USING iceberg PARTITIONED BY (truncate(%s, id)) " | ||
| + "TBLPROPERTIES('%s' = '%s')", | ||
| tableName, Integer.MAX_VALUE, TableProperties.DEFAULT_FILE_FORMAT, format); | ||
| int value = Integer.MAX_VALUE - 1; | ||
| sql("INSERT INTO %s VALUES (%s)", tableName, value); | ||
|
|
||
| assertThat(sql("SELECT id FROM %s WHERE id >= 0", tableName)) | ||
| .singleElement() | ||
| .satisfies(row -> assertThat(row[0]).isEqualTo(value)); | ||
| } | ||
|
|
||
| @TestTemplate | ||
| public void testEstimatedRowCount() throws NoSuchTableException { | ||
| sql( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iceberg/format/spec.md
Line 656 in e5f8ab1
This fixes the implementation, but the spec above still doesn’t say the arithmetic must use unbounded integers. Could we clarify the spec as
r = floorMod(v, W)andv - rcomputed with unbounded arithmetic? Otherwise other implementations can still produce different partition values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another issue is that
truncateInt(10, Integer.MIN_VALUE)still returnsInteger.MAX_VALUE - 1after this PR: the final mathematical result is outside int, so wrapping breaks ordering and can cause incorrect partition pruning as reported in #9767 and #13105.Do we need a spec change for clarification?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to updating the spec accordingly.