Skip to main content

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
18use crate::ast::Expr;
19use crate::dialect::Dialect;
20use crate::parser::{Parser, ParserError};
21
22/// A [`Dialect`] for [Apache Spark SQL](https://spark.apache.org/docs/latest/sql-ref.html).
23///
24/// See <https://spark.apache.org/docs/latest/sql-ref-syntax.html>.
25#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
27pub struct SparkSqlDialect;
28
29impl Dialect for SparkSqlDialect {
30    // See https://spark.apache.org/docs/latest/sql-ref-identifier.html
31    fn is_delimited_identifier_start(&self, ch: char) -> bool {
32        matches!(ch, '`')
33    }
34
35    /// Aliases are identifiers, see `named_expression` in
36    /// <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select.html> and
37    /// <https://spark.apache.org/docs/latest/sql-ref-identifier.html>
38    fn supports_string_literal_column_alias(&self, _explicit: bool) -> bool {
39        false
40    }
41
42    /// Aliases are identifiers, see <https://spark.apache.org/docs/latest/sql-ref-identifier.html>
43    fn supports_string_literal_table_alias(&self, _explicit: bool) -> bool {
44        false
45    }
46
47    fn supports_comparison_in_between_low_bound(&self) -> bool {
48        true
49    }
50
51    /// See <https://spark.apache.org/docs/latest/sql-ref-identifier.html>
52    fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
53        Some('`')
54    }
55
56    fn is_identifier_start(&self, ch: char) -> bool {
57        matches!(ch, 'a'..='z' | 'A'..='Z' | '_')
58    }
59
60    fn is_identifier_part(&self, ch: char) -> bool {
61        matches!(ch, 'a'..='z' | 'A'..='Z' | '0'..='9' | '_')
62    }
63
64    /// See <https://spark.apache.org/docs/latest/sql-ref-functions-builtin-agg.html>
65    fn supports_filter_during_aggregation(&self) -> bool {
66        true
67    }
68
69    /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-groupby.html>
70    fn supports_group_by_expr(&self) -> bool {
71        true
72    }
73
74    /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-groupby.html>
75    fn supports_group_by_with_modifier(&self) -> bool {
76        true
77    }
78
79    /// See <https://spark.apache.org/docs/latest/sql-ref-functions-builtin-higher-order-func.html>
80    fn supports_lambda_functions(&self) -> bool {
81        true
82    }
83
84    /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select.html>
85    fn supports_select_wildcard_except(&self) -> bool {
86        true
87    }
88
89    /// See <https://spark.apache.org/docs/latest/sql-ref-datatypes.html>
90    fn supports_struct_literal(&self) -> bool {
91        true
92    }
93
94    fn supports_nested_comments(&self) -> bool {
95        true
96    }
97
98    /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-ddl-create-table-datasource.html>
99    fn supports_create_table_using(&self) -> bool {
100        true
101    }
102
103    /// `LONG` is an alias for `BIGINT` in Spark SQL.
104    ///
105    /// See <https://spark.apache.org/docs/latest/sql-ref-datatypes.html>
106    fn supports_long_type_as_bigint(&self) -> bool {
107        true
108    }
109
110    /// See <https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select.html>
111    fn supports_values_as_table_factor(&self) -> bool {
112        true
113    }
114
115    fn require_interval_qualifier(&self) -> bool {
116        true
117    }
118
119    fn supports_bang_not_operator(&self) -> bool {
120        true
121    }
122
123    fn supports_select_item_multi_column_alias(&self) -> bool {
124        true
125    }
126
127    fn supports_cte_without_as(&self) -> bool {
128        true
129    }
130
131    /// See <https://spark.apache.org/docs/latest/sql-ref-datatypes.html>
132    fn supports_map_literal_with_angle_brackets(&self) -> bool {
133        true
134    }
135
136    /// See:
137    /// - <https://spark.apache.org/docs/latest/sql-pipe-syntax.html>
138    /// - <https://issues.apache.org/jira/browse/SPARK-49528>
139    fn supports_pipe_operator(&self) -> bool {
140        true
141    }
142
143    /// Parse the `DIV` keyword as integer division.
144    ///
145    /// Example: `SELECT 10 DIV 3` returns `3`.
146    ///
147    /// See <https://spark.apache.org/docs/latest/sql-ref-functions-builtin-math.html>
148    fn parse_infix(
149        &self,
150        parser: &mut Parser,
151        expr: &Expr,
152        precedence: u8,
153    ) -> Option<Result<Expr, ParserError>> {
154        super::parse_div_infix(parser, expr, precedence)
155    }
156}