|
| 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"))] |
| 19 | +use alloc::boxed::Box; |
| 20 | + |
| 21 | +use crate::ast::{BinaryOperator, Expr}; |
| 22 | +use crate::dialect::Dialect; |
| 23 | +use crate::keywords::Keyword; |
| 24 | +use 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))] |
| 31 | +pub struct SparkSqlDialect; |
| 32 | + |
| 33 | +impl 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 | + /// Parse the `DIV` keyword as integer division. |
| 120 | + /// |
| 121 | + /// Example: `SELECT 10 DIV 3` returns `3`. |
| 122 | + /// |
| 123 | + /// See <https://spark.apache.org/docs/latest/sql-ref-functions-builtin-math.html> |
| 124 | + fn parse_infix( |
| 125 | + &self, |
| 126 | + parser: &mut Parser, |
| 127 | + expr: &Expr, |
| 128 | + _precedence: u8, |
| 129 | + ) -> Option<Result<Expr, ParserError>> { |
| 130 | + if parser.parse_keyword(Keyword::DIV) { |
| 131 | + let left = Box::new(expr.clone()); |
| 132 | + let right = Box::new(match parser.parse_expr() { |
| 133 | + Ok(expr) => expr, |
| 134 | + Err(e) => return Some(Err(e)), |
| 135 | + }); |
| 136 | + Some(Ok(Expr::BinaryOp { |
| 137 | + left, |
| 138 | + op: BinaryOperator::MyIntegerDivide, |
| 139 | + right, |
| 140 | + })) |
| 141 | + } else { |
| 142 | + None |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments