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 fn is_identifier_start(&self, ch: char) -> bool {
40 matches!(ch, 'a'..='z' | 'A'..='Z' | '_')
41 }
42
43 fn is_identifier_part(&self, ch: char) -> bool {
44 matches!(ch, 'a'..='z' | 'A'..='Z' | '0'..='9' | '_')
45 }
46
47 /// See <https://spark.apache.org/docs/latest/sql-ref-functions-builtin-agg.html>
48 fn supports_filter_during_aggregation(&self) -> bool {
49 true
50 }
51
52 /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-groupby.html>
53 fn supports_group_by_expr(&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_with_modifier(&self) -> bool {
59 true
60 }
61
62 /// See <https://spark.apache.org/docs/latest/sql-ref-functions-builtin-higher-order-func.html>
63 fn supports_lambda_functions(&self) -> bool {
64 true
65 }
66
67 /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select.html>
68 fn supports_select_wildcard_except(&self) -> bool {
69 true
70 }
71
72 /// See <https://spark.apache.org/docs/latest/sql-ref-datatypes.html>
73 fn supports_struct_literal(&self) -> bool {
74 true
75 }
76
77 fn supports_nested_comments(&self) -> bool {
78 true
79 }
80
81 /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-ddl-create-table-datasource.html>
82 fn supports_create_table_using(&self) -> bool {
83 true
84 }
85
86 /// `LONG` is an alias for `BIGINT` in Spark SQL.
87 ///
88 /// See <https://spark.apache.org/docs/latest/sql-ref-datatypes.html>
89 fn supports_long_type_as_bigint(&self) -> bool {
90 true
91 }
92
93 /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select.html>
94 fn supports_values_as_table_factor(&self) -> bool {
95 true
96 }
97
98 fn require_interval_qualifier(&self) -> bool {
99 true
100 }
101
102 fn supports_bang_not_operator(&self) -> bool {
103 true
104 }
105
106 fn supports_select_item_multi_column_alias(&self) -> bool {
107 true
108 }
109
110 fn supports_cte_without_as(&self) -> bool {
111 true
112 }
113
114 /// See <https://spark.apache.org/docs/latest/sql-ref-datatypes.html>
115 fn supports_map_literal_with_angle_brackets(&self) -> bool {
116 true
117 }
118
119 /// See:
120 /// - <https://spark.apache.org/docs/latest/sql-pipe-syntax.html>
121 /// - <https://issues.apache.org/jira/browse/SPARK-49528>
122 fn supports_pipe_operator(&self) -> bool {
123 true
124 }
125
126 /// Parse the `DIV` keyword as integer division.
127 ///
128 /// Example: `SELECT 10 DIV 3` returns `3`.
129 ///
130 /// See <https://spark.apache.org/docs/latest/sql-ref-functions-builtin-math.html>
131 fn parse_infix(
132 &self,
133 parser: &mut Parser,
134 expr: &Expr,
135 _precedence: u8,
136 ) -> Option<Result<Expr, ParserError>> {
137 if parser.parse_keyword(Keyword::DIV) {
138 let left = Box::new(expr.clone());
139 let right = Box::new(match parser.parse_expr() {
140 Ok(expr) => expr,
141 Err(e) => return Some(Err(e)),
142 });
143 Some(Ok(Expr::BinaryOp {
144 left,
145 op: BinaryOperator::MyIntegerDivide,
146 right,
147 }))
148 } else {
149 None
150 }
151 }
152}