-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-31753][SQL][DOCS] Add missing keywords in the SQL docs #29056
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
Changes from 41 commits
bb1efa2
1459d5b
88c40fe
df22083
0436611
ed80c84
39ca87c
c3b3c89
c3546eb
77a339a
a6b4f74
664277e
fd677c9
93b1f63
a5b5474
f4556a4
6071006
596b842
dc24541
c6f541b
a80fa85
e4c2a17
24b64a8
3b15c75
0d9e72d
a119c85
61c57d1
b3a3248
2ee5419
95071f3
bbce7d0
15aae2b
0625ca8
03da596
5b562eb
1570d76
d72139e
21ebafc
e5a1cf1
b281494
bed618b
caa7130
2813735
ffa0603
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,107 @@ | ||
| --- | ||
| layout: global | ||
| title: CASE Clause | ||
| displayTitle: CASE Clause | ||
| license: | | ||
| 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. | ||
| --- | ||
|
|
||
| ### Description | ||
|
|
||
| `CASE` clause uses a rule to return specific result based on the specified condition, similar to if/else statements in other programming languages. | ||
|
GuoPhilipse marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### Syntax | ||
|
|
||
| ```sql | ||
| CASE [ expression ] { WHEN boolean_expression THEN then_expression } [ ... ] | ||
| [ ELSE else_expression ] | ||
| END | ||
| ``` | ||
|
huaxingao marked this conversation as resolved.
|
||
|
|
||
| ### Parameters | ||
|
|
||
| * **boolean_expression** | ||
|
|
||
| Specifies an expression with a return type of boolean. | ||
|
GuoPhilipse marked this conversation as resolved.
Outdated
|
||
|
|
||
| * **then_expression** | ||
|
|
||
| Specifies the then expression based on the `boolean_expression` condition, `then_expression` and `else_expression` should all be same type or coercible to a common type. | ||
|
GuoPhilipse marked this conversation as resolved.
Outdated
|
||
|
|
||
| * **else_expression** | ||
|
|
||
| Specifies the default expression, `then_expression` and `else_expression` should all be same type or coercible to a common type. | ||
|
GuoPhilipse marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### Examples | ||
|
|
||
| ```sql | ||
| CREATE TABLE person (id INT, name STRING, age INT); | ||
| INSERT INTO person VALUES | ||
| (100, 'John', 30), | ||
| (200, 'Mary', NULL), | ||
| (300, 'Mike', 80), | ||
| (400, 'Dan', 50); | ||
|
|
||
| SELECT id, CASE WHEN id > 200 THEN 'bigger' ELSE 'small' END FROM person; | ||
| +------+--------------------------------------------------+ | ||
| | id | CASE WHEN (id > 200) THEN bigger ELSE small END | | ||
| +------+--------------------------------------------------+ | ||
| | 100 | small | | ||
| | 200 | small | | ||
| | 300 | bigger | | ||
| | 400 | bigger | | ||
| +------+--------------------------------------------------+ | ||
|
|
||
| SELECT id, CASE id WHEN 100 then 'bigger' WHEN id > 300 THEN '300' ELSE 'small' END FROM person; | ||
| +------+-----------------------------------------------------------------------------------------------+ | ||
| | id | CASE WHEN (id = 100) THEN bigger WHEN (id = CAST((id > 300) AS INT)) THEN 300 ELSE small END | | ||
| +------+-----------------------------------------------------------------------------------------------+ | ||
| | 100 | bigger | | ||
| | 200 | small | | ||
| | 300 | small | | ||
| | 400 | small | | ||
| +------+-----------------------------------------------------------------------------------------------+ | ||
|
|
||
| SELECT * FROM person | ||
| WHERE | ||
| CASE 1 = 1 | ||
| WHEN 100 THEN 'big' | ||
| WHEN 200 THEN 'bigger' | ||
| WHEN 300 THEN 'biggest' | ||
| ELSE 'small' | ||
| END = 'small'; | ||
| +------+-------+-------+ | ||
| | id | name | age | | ||
| +------+-------+-------+ | ||
| | 100 | John | 30 | | ||
| | 200 | Mary | NULL | | ||
| | 300 | Mike | 80 | | ||
| | 400 | Dan | 50 | | ||
| +------+-------+-------+ | ||
| ``` | ||
|
|
||
| ### Related Statements | ||
|
|
||
| * [SELECT Main](sql-ref-syntax-qry-select.html) | ||
| * [WHERE Clause](sql-ref-syntax-qry-select-where.html) | ||
| * [GROUP BY Clause](sql-ref-syntax-qry-select-groupby.html) | ||
| * [HAVING Clause](sql-ref-syntax-qry-select-having.html) | ||
| * [ORDER BY Clause](sql-ref-syntax-qry-select-orderby.html) | ||
| * [SORT BY Clause](sql-ref-syntax-qry-select-sortby.html) | ||
| * [DISTRIBUTE BY Clause](sql-ref-syntax-qry-select-distribute-by.html) | ||
| * [LIMIT Clause](sql-ref-syntax-qry-select-limit.html) | ||
| * [PIVOT Clause](sql-ref-syntax-qry-select-pivot.html) | ||
| * [LATERAL VIEW Clause](sql-ref-syntax-qry-select-lateral-view.html) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,6 +81,8 @@ aggregate_name ( [ DISTINCT ] expression [ , ... ] ) [ FILTER ( WHERE boolean_ex | |
| * **aggregate_name** | ||
|
|
||
| Specifies an aggregate function name (MIN, MAX, COUNT, SUM, AVG, etc.). | ||
| Some aggregate function like `FIRST` and `LAST` have special usage as the following: | ||
| **Syntax:** `[ FIRST | LAST ] ( [ distinct ] expression [ IGNORE NULLS ] ) [ FILTER ( WHERE boolean_expression ) ]` | ||
|
GuoPhilipse marked this conversation as resolved.
Outdated
Member
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. We still need
Member
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. now have have aggregate functions above and examples in the end, looks enough,let me move it |
||
|
|
||
| * **DISTINCT** | ||
|
|
||
|
|
@@ -91,6 +93,18 @@ aggregate_name ( [ DISTINCT ] expression [ , ... ] ) [ FILTER ( WHERE boolean_ex | |
| Filters the input rows for which the `boolean_expression` in the `WHERE` clause evaluates | ||
| to true are passed to the aggregate function; other rows are discarded. | ||
|
|
||
| * **FIRST** | ||
|
|
||
| `FIRST` selects a first expression value from the data set. We can specify an optional `IGNORE NULL` clause to ignore NULL values. | ||
|
GuoPhilipse marked this conversation as resolved.
Outdated
|
||
|
|
||
| * **LAST** | ||
|
|
||
| `LAST` select a last expression value from the data set, we can specify `IGNORE NULLS` to ignore NULLS, it is optional. | ||
|
GuoPhilipse marked this conversation as resolved.
Outdated
|
||
|
|
||
| * **IGNORE NULLS** | ||
|
|
||
| `IGNORE NULLS` is to ignore null values, which is used in `FIRST` and `LAST` | ||
|
GuoPhilipse marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### Examples | ||
|
|
||
| ```sql | ||
|
|
@@ -260,6 +274,30 @@ SELECT city, car_model, sum(quantity) AS sum FROM dealer | |
| | San Jose| HondaAccord| 8| | ||
| | San Jose| HondaCivic| 5| | ||
| +---------+------------+---+ | ||
|
|
||
| --Prepare data for ignore nulls example | ||
| CREATE TABLE person (id INT, name STRING, age INT); | ||
| INSERT INTO person VALUES | ||
| (100, 'Mary', NULL), | ||
| (200, 'John', 30), | ||
| (300, 'Mike', 80), | ||
| (400, 'Dan', 50); | ||
|
|
||
| --Select the first row in cloumn age | ||
| SELECT FIRST(age) FROM person; | ||
| +--------------------+ | ||
| | first(age, false) | | ||
| +--------------------+ | ||
| | NULL | | ||
| +--------------------+ | ||
|
|
||
| --Get the first row in cloumn `age` ignore nulls,last row in column `id` and sum of cloumn `id`. | ||
| SELECT FIRST(age IGNORE NULLS), LAST(id), SUM(id) FROM person; | ||
| +-------------------+------------------+----------+ | ||
| | first(age, true) | last(id, false) | sum(id) | | ||
| +-------------------+------------------+----------+ | ||
| | 30 | 400 | 1000 | | ||
| +-------------------+------------------+----------+ | ||
| ``` | ||
|
|
||
| ### Related Statements | ||
|
|
@@ -272,3 +310,6 @@ SELECT city, car_model, sum(quantity) AS sum FROM dealer | |
| * [CLUSTER BY Clause](sql-ref-syntax-qry-select-clusterby.html) | ||
| * [DISTRIBUTE BY Clause](sql-ref-syntax-qry-select-distribute-by.html) | ||
| * [LIMIT Clause](sql-ref-syntax-qry-select-limit.html) | ||
| * [CASE Clause](sql-ref-syntax-qry-select-case.html) | ||
| * [PIVOT Clause](sql-ref-syntax-qry-select-pivot.html) | ||
| * [LATERAL VIEW Clause](sql-ref-syntax-qry-select-lateral-view.html) | ||
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.
The bucketSpec is still missing in CREATE HIVE FORMAT table, right?
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.
Any reason we did not add it? @huaxingao @GuoPhilipse
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.
Sorry, we missed that.
@GuoPhilipse Could you please have a follow-up to add
bucketSpec? Thanks!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.
Thank you!
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.
Yes, we need it, will add it soon.