|
| 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