Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/dialect/databricks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
// specific language governing permissions and limitations
// under the License.

use crate::dialect::Dialect;
use crate::dialect::{Dialect, Precedence};
use crate::parser::{Parser, ParserError};
use crate::tokenizer::Token;

/// A [`Dialect`] for [Databricks SQL](https://www.databricks.com/)
///
Expand All @@ -25,6 +27,21 @@ use crate::dialect::Dialect;
pub struct DatabricksDialect;

impl Dialect for DatabricksDialect {
/// `<<` and `>>`, binding tighter than `&` and looser than `+`, as Spark's
/// grammar orders them.
fn supports_bitwise_shift_operators(&self) -> bool {
true
}

fn get_next_precedence(&self, parser: &Parser) -> Option<Result<u8, ParserError>> {
match parser.peek_token_ref().token {
Token::ShiftLeft | Token::ShiftRight => {
Some(Ok(self.prec_value(Precedence::Ampersand) + 1))
}
_ => None,
}
}

// see https://docs.databricks.com/en/sql/language-manual/sql-ref-identifiers.html

fn is_delimited_identifier_start(&self, ch: char) -> bool {
Expand Down
18 changes: 17 additions & 1 deletion src/dialect/spark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
use alloc::boxed::Box;

use crate::ast::{BinaryOperator, Expr};
use crate::dialect::Dialect;
use crate::dialect::{Dialect, Precedence};
use crate::keywords::Keyword;
use crate::parser::{Parser, ParserError};
use crate::tokenizer::Token;

/// A [`Dialect`] for [Apache Spark SQL](https://spark.apache.org/docs/latest/sql-ref.html).
///
Expand All @@ -31,6 +32,21 @@ use crate::parser::{Parser, ParserError};
pub struct SparkSqlDialect;

impl Dialect for SparkSqlDialect {
/// `<<` and `>>`, binding tighter than `&` and looser than `+`, as Spark's
/// grammar orders them.
fn supports_bitwise_shift_operators(&self) -> bool {
true
}

fn get_next_precedence(&self, parser: &Parser) -> Option<Result<u8, ParserError>> {
match parser.peek_token_ref().token {
Token::ShiftLeft | Token::ShiftRight => {
Some(Ok(self.prec_value(Precedence::Ampersand) + 1))
}
_ => None,
}
}

// See https://spark.apache.org/docs/latest/sql-ref-identifier.html
fn is_delimited_identifier_start(&self, ch: char) -> bool {
matches!(ch, '`')
Expand Down
39 changes: 39 additions & 0 deletions tests/sqlparser_spark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,42 @@ fn test_describe_table_is_not_a_table_query() {
// EXPLAIN is unaffected.
spark().one_statement_parses_to("EXPLAIN TABLE t", "EXPLAIN SELECT * FROM t");
}

#[test]
fn test_bitwise_shift_operators() {
spark().verified_stmt("SELECT 4 >> 1, 1 << 2");
// Tighter than `&`, looser than `+`.
match spark().verified_expr("1 & 3 << 1") {
Expr::BinaryOp {
op: BinaryOperator::BitwiseAnd,
right,
..
} => assert!(matches!(
*right,
Expr::BinaryOp {
op: BinaryOperator::PGBitwiseShiftLeft,
..
}
)),
other => panic!("Expected & over <<, got {other:?}"),
}
match spark().verified_expr("1 << 1 + 1") {
Expr::BinaryOp {
op: BinaryOperator::PGBitwiseShiftLeft,
right,
..
} => assert!(matches!(
*right,
Expr::BinaryOp {
op: BinaryOperator::Plus,
..
}
)),
other => panic!("Expected << over +, got {other:?}"),
}
spark().verified_stmt("SELECT (1 & 3) << 1");
match spark().verified_expr("a >> b") {
Expr::BinaryOp { op, .. } => assert_eq!(op, BinaryOperator::PGBitwiseShiftRight),
other => panic!("Expected a shift, got {other:?}"),
}
}
Loading