sqlparser/dialect/spark.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#[cfg(not(feature = "std"))]
19use alloc::boxed::Box;
20
21use crate::ast::{BinaryOperator, Expr};
22use crate::dialect::Dialect;
23use crate::keywords::Keyword;
24use crate::parser::{Parser, ParserError};
25
26/// A [`Dialect`] for [Apache Spark SQL](https://spark.apache.org/docs/latest/sql-ref.html).
27///
28/// See <https://spark.apache.org/docs/latest/sql-ref-syntax.html>.
29#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
30#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
31pub struct SparkSqlDialect;
32
33impl Dialect for SparkSqlDialect {
34 // See https://spark.apache.org/docs/latest/sql-ref-identifier.html
35 fn is_delimited_identifier_start(&self, ch: char) -> bool {
36 matches!(ch, '`')
37 }
38
39 /// See <https://spark.apache.org/docs/latest/sql-ref-identifier.html>
40 fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
41 Some('`')
42 }
43
44 fn is_identifier_start(&self, ch: char) -> bool {
45 matches!(ch, 'a'..='z' | 'A'..='Z' | '_')
46 }
47
48 fn is_identifier_part(&self, ch: char) -> bool {
49 matches!(ch, 'a'..='z' | 'A'..='Z' | '0'..='9' | '_')
50 }
51
52 /// See <https://spark.apache.org/docs/latest/sql-ref-functions-builtin-agg.html>
53 fn supports_filter_during_aggregation(&self) -> bool {
54 true
55 }
56
57 /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-groupby.html>
58 fn supports_group_by_expr(&self) -> bool {
59 true
60 }
61
62 /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-groupby.html>
63 fn supports_group_by_with_modifier(&self) -> bool {
64 true
65 }
66
67 /// See <https://spark.apache.org/docs/latest/sql-ref-functions-builtin-higher-order-func.html>
68 fn supports_lambda_functions(&self) -> bool {
69 true
70 }
71
72 /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select.html>
73 fn supports_select_wildcard_except(&self) -> bool {
74 true
75 }
76
77 /// See <https://spark.apache.org/docs/latest/sql-ref-datatypes.html>
78 fn supports_struct_literal(&self) -> bool {
79 true
80 }
81
82 fn supports_nested_comments(&self) -> bool {
83 true
84 }
85
86 /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-ddl-create-table-datasource.html>
87 fn supports_create_table_using(&self) -> bool {
88 true
89 }
90
91 /// `LONG` is an alias for `BIGINT` in Spark SQL.
92 ///
93 /// See <https://spark.apache.org/docs/latest/sql-ref-datatypes.html>
94 fn supports_long_type_as_bigint(&self) -> bool {
95 true
96 }
97
98 /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select.html>
99 fn supports_values_as_table_factor(&self) -> bool {
100 true
101 }
102
103 fn require_interval_qualifier(&self) -> bool {
104 true
105 }
106
107 fn supports_bang_not_operator(&self) -> bool {
108 true
109 }
110
111 fn supports_select_item_multi_column_alias(&self) -> bool {
112 true
113 }
114
115 fn supports_cte_without_as(&self) -> bool {
116 true
117 }
118
119 /// See <https://spark.apache.org/docs/latest/sql-ref-datatypes.html>
120 fn supports_map_literal_with_angle_brackets(&self) -> bool {
121 true
122 }
123
124 /// See:
125 /// - <https://spark.apache.org/docs/latest/sql-pipe-syntax.html>
126 /// - <https://issues.apache.org/jira/browse/SPARK-49528>
127 fn supports_pipe_operator(&self) -> bool {
128 true
129 }
130
131 /// Parse the `DIV` keyword as integer division.
132 ///
133 /// Example: `SELECT 10 DIV 3` returns `3`.
134 ///
135 /// See <https://spark.apache.org/docs/latest/sql-ref-functions-builtin-math.html>
136 fn parse_infix(
137 &self,
138 parser: &mut Parser,
139 expr: &Expr,
140 precedence: u8,
141 ) -> Option<Result<Expr, ParserError>> {
142 if parser.parse_keyword(Keyword::DIV) {
143 let left = Box::new(expr.clone());
144 let right = Box::new(match parser.parse_subexpr(precedence) {
145 Ok(expr) => expr,
146 Err(e) => return Some(Err(e)),
147 });
148 Some(Ok(Expr::BinaryOp {
149 left,
150 op: BinaryOperator::MyIntegerDivide,
151 right,
152 }))
153 } else {
154 None
155 }
156 }
157}