-
Notifications
You must be signed in to change notification settings - Fork 2.8k
API: required nested fields within optional structs can produce null #13804
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
Open
dejangvozdenac
wants to merge
12
commits into
apache:main
Choose a base branch
from
dejangvozdenac:fix-field-optional
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−17
Open
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f056595
add ancestor check to field optional is null check
dejangvozdenac a05ba32
fix tests and simplify
dejangvozdenac 115704b
add test
dejangvozdenac e77a816
add spark tests and address review feedback
dejangvozdenac b6c0cb6
style
dejangvozdenac 707382f
fix tests
dejangvozdenac 7223d0a
fix tests
dejangvozdenac 03fda5f
fix typo
dejangvozdenac f6563c7
move the spark test to TestSelect class
dejangvozdenac fbd7921
sql formatting
dejangvozdenac 740e5f1
Update api/src/main/java/org/apache/iceberg/Accessor.java
dejangvozdenac 535c8a1
remove api breakage and add improve tests
dejangvozdenac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
api/src/test/java/org/apache/iceberg/expressions/TestBoundReference.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* 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.expressions; | ||
|
||
import static org.apache.iceberg.types.Types.NestedField.optional; | ||
import static org.apache.iceberg.types.Types.NestedField.required; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import org.apache.iceberg.Accessor; | ||
import org.apache.iceberg.Schema; | ||
import org.apache.iceberg.StructLike; | ||
import org.apache.iceberg.types.Types; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
public class TestBoundReference { | ||
dejangvozdenac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Build a schema with a single nested struct with optionalList.size() levels with the following | ||
// structure: | ||
// s1: struct(s2: struct(s3: struct(..., sn: struct(leaf: int)))) | ||
// where each s{i} is an optional struct if optionalList.get(i) is true and a required struct if | ||
// false | ||
private static Schema buildSchemaFromOptionalList(List<Boolean> optionalList, String leafName) { | ||
if (optionalList == null || optionalList.isEmpty()) { | ||
dejangvozdenac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new IllegalArgumentException("optionalList must not be empty"); | ||
} | ||
|
||
Types.NestedField leaf = | ||
optionalList.get(optionalList.size() - 1) | ||
? optional(optionalList.size(), leafName, Types.IntegerType.get()) | ||
: required(optionalList.size(), leafName, Types.IntegerType.get()); | ||
|
||
Types.StructType current = Types.StructType.of(leaf); | ||
|
||
for (int i = optionalList.size() - 2; i >= 0; i--) { | ||
int id = i + 1; | ||
String name = "s" + (i + 1); | ||
current = | ||
Types.StructType.of( | ||
optionalList.get(i) ? optional(id, name, current) : required(id, name, current)); | ||
} | ||
|
||
return new Schema(current.fields()); | ||
} | ||
|
||
private static Stream<Arguments> producesNullCases() { | ||
return Stream.of( | ||
// basic fields, no struct levels | ||
Arguments.of(Arrays.asList(false), false), | ||
dejangvozdenac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Arguments.of(Arrays.asList(true), true), | ||
// one level | ||
Arguments.of(Arrays.asList(false, false), false), | ||
Arguments.of(Arrays.asList(false, true), true), | ||
Arguments.of(Arrays.asList(true, false), true), | ||
// two levels | ||
Arguments.of(Arrays.asList(false, false, false), false), | ||
Arguments.of(Arrays.asList(false, false, true), true), | ||
Arguments.of(Arrays.asList(true, false, false), true), | ||
Arguments.of(Arrays.asList(false, true, false), true), | ||
// three levels | ||
Arguments.of(Arrays.asList(false, false, false, false), false), | ||
Arguments.of(Arrays.asList(false, false, false, true), true), | ||
Arguments.of(Arrays.asList(true, false, false, false), true), | ||
Arguments.of(Arrays.asList(false, true, false, false), true), | ||
// four levels | ||
Arguments.of(Arrays.asList(false, false, false, false, false), false), | ||
Arguments.of(Arrays.asList(false, false, false, false, true), true), | ||
Arguments.of(Arrays.asList(true, false, false, false, false), true), | ||
Arguments.of(Arrays.asList(false, true, true, true, false), true)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("producesNullCases") | ||
public void testProducesNull(List<Boolean> optionalList, boolean expectedProducesNull) { | ||
String leafName = "leaf"; | ||
Schema schema = buildSchemaFromOptionalList(optionalList, leafName); | ||
int leafId = optionalList.size(); | ||
Types.NestedField leafField = schema.findField(leafId); | ||
Accessor<StructLike> accessor = schema.accessorForField(leafId); | ||
|
||
BoundReference<Integer> ref = new BoundReference<>(leafField, accessor, leafName); | ||
assertThat(ref.producesNull()).isEqualTo(expectedProducesNull); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.