Skip to content

Commit 02a07cd

Browse files
srielaucloud-fan
authored andcommitted
[SPARK-43205][DOC] identifier clause docs
### What changes were proposed in this pull request? Document the IDENTIFIER() clause ### Why are the changes needed? Docs are good! ### Does this PR introduce _any_ user-facing change? ### How was this patch tested? <!-- <img width="892" alt="Screenshot 2023-08-15 at 4 26 27 PM" src="https://github.com/apache/spark/assets/3514644/6ce43330-668e-4c84-b72b-bf1e2679d736"> If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible. If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future. If tests were not added, please describe why they were not added and/or why it was difficult to add. If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks. --> See attached <img width="892" alt="Screenshot 2023-08-15 at 4 26 27 PM" src="https://github.com/apache/spark/assets/3514644/55823375-8d1a-4473-bf19-74796d273416"> <img width="747" alt="Screenshot 2023-08-15 at 4 45 23 PM" src="https://github.com/apache/spark/assets/3514644/0ee852a9-6a11-4c87-bed9-43531c55fc31"> Closes #42506 from srielau/SPARK-43205-3.5-IDENTIFIER-clause-docs. Authored-by: srielau <[email protected]> Signed-off-by: Wenchen Fan <[email protected]> (cherry picked from commit 7786d0b) Signed-off-by: Wenchen Fan <[email protected]>
1 parent f807bd2 commit 02a07cd

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

docs/sql-ref-identifier-clause.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
layout: global
3+
title: Identifier clause
4+
displayTitle: IDENTIFIER clause
5+
license: |
6+
Licensed to the Apache Software Foundation (ASF) under one or more
7+
contributor license agreements. See the NOTICE file distributed with
8+
this work for additional information regarding copyright ownership.
9+
The ASF licenses this file to You under the Apache License, Version 2.0
10+
(the "License"); you may not use this file except in compliance with
11+
the License. You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
---
21+
22+
### Description
23+
24+
Converts a constant `STRING` expression into a SQL object name.
25+
The purpose of this clause is to allow for templating of identifiers in SQL statements without opening up the risk of SQL injection attacks.
26+
Typically, this clause is used with a parameter marker as argument.
27+
28+
### Syntax
29+
30+
```sql
31+
IDENTIFIER ( strExpr )
32+
```
33+
34+
### Parameters
35+
36+
- **strExpr**: A constant `STRING` expression. Typically, the expression includes a parameter marker.
37+
38+
### Returns
39+
40+
A (qualified) identifier which can be used as a:
41+
42+
- qualified table name
43+
- namespace name
44+
- function name
45+
- qualified column or attribute reference
46+
47+
### Examples
48+
49+
These examples use named parameter markers to templatize queries.
50+
51+
```scala
52+
// Creation of a table using parameter marker.
53+
spark.sql("CREATE TABLE IDENTIFIER(:mytab)(c1 INT)", args = Map("mytab" -> "tab1")).show()
54+
55+
spark.sql("DESCRIBE IDENTIFIER(:mytab)", args = Map("mytab" -> "tab1")).show()
56+
+--------+---------+-------+
57+
|col_name|data_type|comment|
58+
+--------+---------+-------+
59+
| c1| int| NULL|
60+
+--------+---------+-------+
61+
62+
// Altering a table with a fixed schema and a parameterized table name.
63+
spark.sql("ALTER TABLE IDENTIFIER('default.' || :mytab) ADD COLUMN c2 INT", args = Map("mytab" -> "tab1")).show()
64+
65+
spark.sql("DESCRIBE IDENTIFIER(:mytab)", args = Map("mytab" -> "default.tab1")).show()
66+
+--------+---------+-------+
67+
|col_name|data_type|comment|
68+
+--------+---------+-------+
69+
| c1| int| NULL|
70+
| c2| int| NULL|
71+
+--------+---------+-------+
72+
73+
// A parameterized reference to a table in a query. This table name is qualified and uses back-ticks.
74+
spark.sql("SELECT * FROM IDENTIFIER(:mytab)", args = Map("mytab" -> "`default`.`tab1`")).show()
75+
+---+---+
76+
| c1| c2|
77+
+---+---+
78+
+---+---+
79+
80+
81+
// You cannot qualify the IDENTIFIER clause or use it as a qualifier itself.
82+
spark.sql("SELECT * FROM myschema.IDENTIFIER(:mytab)", args = Map("mytab" -> "`tab1`")).show()
83+
[INVALID_SQL_SYNTAX.INVALID_TABLE_VALUED_FUNC_NAME] `myschema`.`IDENTIFIER`.
84+
85+
spark.sql("SELECT * FROM IDENTIFIER(:myschema).mytab", args = Map("mychema" -> "`default`")).show()
86+
[PARSE_SYNTAX_ERROR]
87+
88+
// Dropping a table with separate schema and table parameters.
89+
spark.sql("DROP TABLE IDENTIFIER(:myschema || '.' || :mytab)", args = Map("myschema" -> "default", "mytab" -> "tab1")).show()
90+
91+
// A parameterized column reference
92+
spark.sql("SELECT IDENTIFIER(:col) FROM VALUES(1) AS T(c1)", args = Map("col" -> "t.c1")).show()
93+
+---+
94+
| c1|
95+
+---+
96+
| 1|
97+
+---+
98+
99+
// Passing in a function name as a parameter
100+
spark.sql("SELECT IDENTIFIER(:func)(-1)", args = Map("func" -> "abs")).show();
101+
+-------+
102+
|abs(-1)|
103+
+-------+
104+
| 1|
105+
+-------+
106+
```

docs/sql-ref.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Spark SQL is Apache Spark's module for working with structured data. This guide
3232
* [User-Defined Aggregate Functions (UDAFs)](sql-ref-functions-udf-aggregate.html)
3333
* [Integration with Hive UDFs/UDAFs/UDTFs](sql-ref-functions-udf-hive.html)
3434
* [Identifiers](sql-ref-identifier.html)
35+
* [IDENTIFIER clause](sql-ref-identifier-clause.html)
3536
* [Literals](sql-ref-literals.html)
3637
* [Null Semantics](sql-ref-null-semantics.html)
3738
* [SQL Syntax](sql-ref-syntax.html)

0 commit comments

Comments
 (0)