-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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
ESQL: COALESCE function #98542
Merged
Merged
ESQL: COALESCE function #98542
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6e9e5f8
ESQL: COALESCE function
nik9000 429f01d
Explain where the nulls come from
nik9000 3311a5b
Spotless
nik9000 cacf69a
Test lazy
nik9000 eb19d9a
Explain why lazy
nik9000 f457f3d
Merge branch 'main' into coalesce
nik9000 c8ecc97
Fix build
nik9000 888e3d1
Merge branch 'main' into coalesce
nik9000 1307dab
Add more tests and fixup nullable
nik9000 d879684
Foldable
nik9000 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
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[[esql-coalesce]] | ||
=== `COALESCE` | ||
|
||
Returns the first non-null value. | ||
|
||
[source.merge.styled,esql] | ||
---- | ||
include::{esql-specs}/null.csv-spec[tag=coalesce] | ||
---- | ||
[%header.monospaced.styled,format=dsv,separator=|] | ||
|=== | ||
include::{esql-specs}/null.csv-spec[tag=coalesce-result] | ||
|=== |
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
137 changes: 137 additions & 0 deletions
137
x-pack/plugin/esql/qa/testFixtures/src/main/resources/null.csv-spec
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,137 @@ | ||
isNull | ||
from employees | ||
| where gender is null | ||
| sort first_name | ||
| keep first_name, gender | ||
| limit 3; | ||
|
||
first_name:keyword|gender:keyword | ||
Berni |null | ||
Cristinel |null | ||
Duangkaew |null | ||
; | ||
|
||
notIsNull | ||
from employees | ||
| where gender is not null | ||
| sort first_name | ||
| keep first_name, gender | ||
| limit 3; | ||
|
||
first_name:keyword|gender:keyword | ||
Alejandro |F | ||
Amabile |M | ||
Anneke |F | ||
; | ||
|
||
isNullForDocs | ||
// tag::is-null[] | ||
FROM employees | ||
| WHERE birth_date IS NULL | ||
| KEEP first_name, last_name | ||
| SORT first_name | ||
| LIMIT 3 | ||
// end::is-null[] | ||
; | ||
|
||
// tag::is-null-result[] | ||
first_name:keyword|last_name:keyword | ||
Basil |Tramer | ||
Florian |Syrotiuk | ||
Lucien |Rosenbaum | ||
// end::is-null-result[] | ||
; | ||
|
||
isNotNullForDocs | ||
// tag::is-not-null[] | ||
FROM employees | ||
| WHERE is_rehired IS NOT NULL | ||
| STATS count(emp_no) | ||
// end::is-not-null[] | ||
; | ||
|
||
// tag::is-not-null-result[] | ||
count(emp_no):long | ||
84 | ||
// end::is-not-null-result[] | ||
; | ||
|
||
coalesceSimple | ||
// tag::coalesce[] | ||
ROW a=null, b="b" | ||
| EVAL COALESCE(a, b) | ||
// end::coalesce[] | ||
; | ||
|
||
// tag::coalesce-result[] | ||
a:null | b:keyword | COALESCE(a,b):keyword | ||
null | b | b | ||
// end::coalesce-result[] | ||
; | ||
|
||
coalesce | ||
FROM employees | ||
| EVAL first_name = COALESCE(first_name, "X") | ||
| SORT first_name DESC, emp_no ASC | ||
| KEEP emp_no, first_name | ||
| limit 10; | ||
|
||
emp_no:integer | first_name:keyword | ||
10047 | Zvonko | ||
10081 | Zhongwei | ||
10026 | Yongqiao | ||
10043 | Yishay | ||
10050 | Yinghua | ||
10087 | Xinglin | ||
10030 | X | ||
10031 | X | ||
10032 | X | ||
10033 | X | ||
; | ||
|
||
coalesceBackwards | ||
FROM employees | ||
| EVAL first_name = COALESCE("X", first_name) | ||
| SORT first_name DESC, emp_no ASC | ||
| KEEP emp_no, first_name | ||
| limit 10; | ||
|
||
emp_no:integer | first_name:keyword | ||
10001 | X | ||
10002 | X | ||
10003 | X | ||
10004 | X | ||
10005 | X | ||
10006 | X | ||
10007 | X | ||
10008 | X | ||
10009 | X | ||
10010 | X | ||
; | ||
|
||
coalesceEndsInNull | ||
# ending in null is sill because it'll noop but it shouldn't break things. | ||
FROM employees | ||
| EVAL first_name = COALESCE(first_name, last_name, null) | ||
| SORT first_name DESC, emp_no ASC | ||
| KEEP emp_no, first_name | ||
| limit 3; | ||
|
||
emp_no:integer | first_name:keyword | ||
10047 | Zvonko | ||
10081 | Zhongwei | ||
10026 | Yongqiao | ||
; | ||
|
||
coalesceFolding | ||
FROM employees | ||
| EVAL foo=COALESCE(true, false, null) | ||
| SORT emp_no ASC | ||
| KEEP emp_no, first_name, foo | ||
| limit 3; | ||
|
||
emp_no:integer | first_name:keyword | foo:boolean | ||
10001 | Georgi | true | ||
10002 | Bezalel | true | ||
10003 | Parto | true | ||
; |
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
Oops, something went wrong.
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.
Even though Coalesce can have an arbitrary number of parameters, the tests here always use two.
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.
y=COALESCE(true, false, null)
doesn't seem to work. The value fory
isnull
. Only if I the last value is non-null it works.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.
Huh. I thought I had tests for all that in the unit tests. I'll add more.
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.
Interesting! the break here was with an optimizer rule. I'll push a fix.