Skip to content

Commit 1914d72

Browse files
committed
[Oracle] Lower StringConcat precedence
1 parent ca2d333 commit 1914d72

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed

src/ast/query.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ impl SetExpr {
174174
None
175175
}
176176
}
177+
178+
/// If this `SetExpr` is a `SELECT`, returns a mutable [`Select`].
179+
pub fn as_select_mut(&mut self) -> Option<&mut Select> {
180+
if let Self::Select(select) = self {
181+
Some(&mut **select)
182+
} else {
183+
None
184+
}
185+
}
177186
}
178187

179188
impl fmt::Display for SetExpr {

src/dialect/oracle.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use super::Dialect;
18+
use log::debug;
19+
20+
use crate::{
21+
parser::{Parser, ParserError},
22+
tokenizer::Token,
23+
};
24+
25+
use super::{Dialect, Precedence};
1926

2027
/// A [`Dialect`] for [Oracle Databases](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/index.html)
2128
#[derive(Debug)]
@@ -75,6 +82,35 @@ impl Dialect for OracleDialect {
7582
true
7683
}
7784

85+
fn get_next_precedence(&self, _parser: &Parser) -> Option<Result<u8, ParserError>> {
86+
let t = _parser.peek_token();
87+
debug!("get_next_precedence() {t:?}");
88+
89+
match t.token {
90+
Token::StringConcat => Some(Ok(self.prec_value(Precedence::PlusMinus))),
91+
_ => None,
92+
}
93+
}
94+
95+
fn prec_value(&self, prec: Precedence) -> u8 {
96+
match prec {
97+
Precedence::Period => 100,
98+
Precedence::DoubleColon => 50,
99+
Precedence::AtTz => 41,
100+
Precedence::MulDivModOp => 40,
101+
Precedence::PlusMinus => 30,
102+
Precedence::Xor => 24,
103+
Precedence::Ampersand => 23,
104+
Precedence::Caret => 22,
105+
Precedence::Pipe => 21,
106+
Precedence::Between | Precedence::Eq | Precedence::Like | Precedence::Is => 20,
107+
Precedence::PgOther => 16,
108+
Precedence::UnaryNot => 15,
109+
Precedence::And => 10,
110+
Precedence::Or => 5,
111+
}
112+
}
113+
78114
fn supports_group_by_expr(&self) -> bool {
79115
true
80116
}

tests/sqlparser_oracle.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
#![warn(clippy::all)]
19+
//! Test SQL syntax, specific to [sqlparser::dialect::OracleDialect].
20+
21+
extern crate core;
22+
23+
#[cfg(test)]
24+
use pretty_assertions::assert_eq;
25+
26+
use sqlparser::{
27+
ast::{Expr, SelectItem, Value},
28+
dialect::OracleDialect,
29+
};
30+
#[cfg(test)]
31+
use test_utils::TestedDialects;
32+
33+
mod test_utils;
34+
35+
#[test]
36+
fn muldiv_have_higher_precedence_than_strconcat() {
37+
// ~ oracle: `||` has a lower precedence than `*` and `/`
38+
let sql = "SELECT 3 / 5 || 'asdf' || 7 * 9 FROM dual";
39+
let mut query = oracle_dialect().verified_query(sql);
40+
nest_binary_ops(&mut query.body.as_select_mut().expect("not a SELECT").projection[0]);
41+
assert_eq!(
42+
&format!("{query}"),
43+
"SELECT (((3 / 5) || 'asdf') || (7 * 9)) FROM dual"
44+
);
45+
}
46+
47+
#[test]
48+
fn plusminus_have_same_precedence_as_strconcat() {
49+
// ~ oracle: `+`, `-`, and `||` have the same precedence and parse from left-to-right
50+
let sql = "SELECT 3 + 5 || '.3' || 7 - 9 FROM dual";
51+
let mut query = oracle_dialect().verified_query(sql);
52+
nest_binary_ops(&mut query.body.as_select_mut().expect("not a SELECT").projection[0]);
53+
assert_eq!(
54+
&format!("{query}"),
55+
"SELECT ((((3 + 5) || '.3') || 7) - 9) FROM dual"
56+
);
57+
}
58+
59+
fn oracle_dialect() -> TestedDialects {
60+
TestedDialects::new(vec![Box::new(OracleDialect)])
61+
}
62+
63+
/// Wraps [Expr::BinaryExpr]s in `item` with a [Expr::Nested] recursively.
64+
fn nest_binary_ops(item: &mut SelectItem) {
65+
// ~ idealy, we could use `VisitorMut` at this point
66+
fn nest(expr: &mut Expr) {
67+
// ~ ideally we could use VisitorMut here
68+
if let Expr::BinaryOp { left, op: _, right } = expr {
69+
nest(&mut *left);
70+
nest(&mut *right);
71+
let inner = std::mem::replace(expr, Expr::Value(Value::Null.into()));
72+
*expr = Expr::Nested(Box::new(inner));
73+
}
74+
}
75+
match item {
76+
SelectItem::UnnamedExpr(expr) => nest(expr),
77+
SelectItem::ExprWithAlias { expr, alias: _ } => nest(expr),
78+
SelectItem::QualifiedWildcard(_, _) => {}
79+
SelectItem::Wildcard(_) => {}
80+
}
81+
}

0 commit comments

Comments
 (0)