-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
doc: why nullable of list item is set to true #11626
Merged
alamb
merged 11 commits into
apache:main
from
jcsherin:doc-list-nullable-in-state-fields
Jul 26, 2024
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
adf502d
doc: why nullable of list item is set to true
jcsherin e2c1bf1
Adds an external doc to avoid repeating text
jcsherin d46f01c
rewrite
jcsherin c339eb0
redirects to external doc
jcsherin 02dc91f
Adds ASF license
jcsherin f9c1b1e
Minor: formatting fixes
jcsherin 23b3b63
Minor: copy edits
jcsherin 039ea6c
Retrigger CI
jcsherin 9ee664e
Merge remote-tracking branch 'apache/main' into doc-list-nullable-in-…
alamb f518117
Fixes: name of aggregation in example
jcsherin 1601390
Disambiguates list item nullability in copy
jcsherin 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 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,76 @@ | ||
<!--- | ||
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. | ||
--> | ||
|
||
# Why Is List Item Always Nullable? | ||
|
||
## Motivation | ||
|
||
There were independent proposals to make the `nullable` setting of list | ||
items in accumulator state be configurable. This meant adding additional | ||
fields which captured the `nullable` setting from schema in planning for | ||
the first argument to the aggregation function, and the returned value. | ||
|
||
These fields were to be added to `StateFieldArgs`. But then we found out | ||
that aggregate computation does not depend on it, and it can be avoided. | ||
|
||
This document exists to make that reasoning explicit. | ||
|
||
## Background | ||
|
||
The list data type is used in the accumulator state for a few aggregate | ||
functions like: | ||
|
||
- `sum` | ||
- `count` | ||
- `array_agg` | ||
- `bit_and`, `bit_or` and `bit_xor` | ||
- `nth_value` | ||
|
||
In all of the above cases the data type of the list item is equivalent | ||
to either the first argument of the aggregate function or the returned | ||
value. | ||
|
||
For example, in `array_agg` the data type of item is equivalent to the | ||
first argument and the definition looks like this: | ||
|
||
```rust | ||
// `args` : `StateFieldArgs` | ||
// `input_type` : data type of the first argument | ||
let mut fields = vec![Field::new_list( | ||
format_state_name(self.name(), "array_agg"), | ||
Field::new("item", args.input_type.clone(), true /* nullable of list item */ ), | ||
false, // nullable of list itself | ||
)]; | ||
``` | ||
|
||
For all the aggregates listed above, the list item is always defined as | ||
nullable. | ||
|
||
## Computing Intermediate State | ||
|
||
By setting `nullable` to be always `true` like this we ensure that the | ||
aggregate computation works even when nulls are present. The advantage | ||
of doing it this way is that it eliminates the need for additional code | ||
and special treatment of nulls in the accumulator state. | ||
|
||
## Nullable Of List Itself | ||
|
||
The `nullable` of list itself is dependent on the aggregate. In the | ||
case of `array_agg` the list is nullable(`true`), meanwhile for `sum` | ||
the list is not nullable(`false`). |
This file contains 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 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 |
---|---|---|
|
@@ -203,6 +203,7 @@ impl AggregateUDFImpl for BitwiseOperation { | |
args.name, | ||
format!("{} distinct", self.name()).as_str(), | ||
), | ||
// See COMMENTS.md to understand why nullable is set to true | ||
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. 👍 |
||
Field::new("item", args.return_type.clone(), true), | ||
false, | ||
)]) | ||
|
This file contains 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 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 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.
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.
Is another rationale that the intermediate results need to be able to represent "saw no rows" (e.g that partition had no 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.
For
nth_value
accumulator, when now rows are present in the partition, then no values are added to the intermediate state.I haven't checked the other aggregates though. So I don't know for certain if this is the case always. I'll verify and make a follow-on PR if any differences exist. I think we've only looked deeper into
nth_value
andarray_agg
(by @jayzhan211) at the moment.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.
Makes sense -- I vaguely remember that the null was needed in one of the aggregators to distinguish between
[]
NULL
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.
@alamb Thanks for the pointer. I'll keep this in mind while making pass through the aggregates next time.
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.
I made a minor copy change to disambiguate that the "Computing Intermediate State" section is talking about the nullability of the list item rather than the nullability of the list container.
Sorry for the confusion. I was not clear earlier.