You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2025/12/10 11:50:52 UTC

(datafusion-sqlparser-rs) 01/01: [Oracle] Lower StringConcat precedence (#2115)

This is an automated email from the ASF dual-hosted git repository.

github-bot pushed a commit to branch gh-readonly-queue/main/pr-2115-ca2d333dff851d344ce5b307c6ec4911a707703e
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git

commit adbfc46177aad76b7d67d85fba0dea38a4f777cb
Author: xitep <xi...@users.noreply.github.com>
AuthorDate: Wed Dec 10 12:50:26 2025 +0100

    [Oracle] Lower StringConcat precedence (#2115)
---
 src/dialect/oracle.rs     |  19 ++++++++-
 tests/sqlparser_oracle.rs | 105 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+), 1 deletion(-)

diff --git a/src/dialect/oracle.rs b/src/dialect/oracle.rs
index 0d6aee5e..f8bb0e15 100644
--- a/src/dialect/oracle.rs
+++ b/src/dialect/oracle.rs
@@ -15,7 +15,14 @@
 // specific language governing permissions and limitations
 // under the License.
 
-use super::Dialect;
+use log::debug;
+
+use crate::{
+    parser::{Parser, ParserError},
+    tokenizer::Token,
+};
+
+use super::{Dialect, Precedence};
 
 /// A [`Dialect`] for [Oracle Databases](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/index.html)
 #[derive(Debug)]
@@ -75,6 +82,16 @@ impl Dialect for OracleDialect {
         true
     }
 
+    fn get_next_precedence(&self, parser: &Parser) -> Option<Result<u8, ParserError>> {
+        let t = parser.peek_token();
+        debug!("get_next_precedence() {t:?}");
+
+        match t.token {
+            Token::StringConcat => Some(Ok(self.prec_value(Precedence::PlusMinus))),
+            _ => None,
+        }
+    }
+
     fn supports_group_by_expr(&self) -> bool {
         true
     }
diff --git a/tests/sqlparser_oracle.rs b/tests/sqlparser_oracle.rs
new file mode 100644
index 00000000..09fd4191
--- /dev/null
+++ b/tests/sqlparser_oracle.rs
@@ -0,0 +1,105 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Test SQL syntax, specific to [sqlparser::dialect::OracleDialect].
+
+#[cfg(test)]
+use pretty_assertions::assert_eq;
+
+use sqlparser::{
+    ast::{BinaryOperator, Expr, Value, ValueWithSpan},
+    dialect::OracleDialect,
+    tokenizer::Span,
+};
+use test_utils::{expr_from_projection, number, TestedDialects};
+
+mod test_utils;
+
+fn oracle() -> TestedDialects {
+    TestedDialects::new(vec![Box::new(OracleDialect)])
+}
+
+/// Oracle: `||` has a lower precedence than `*` and `/`
+#[test]
+fn muldiv_have_higher_precedence_than_strconcat() {
+    // ...............  A .. B ...... C .. D ...........
+    let sql = "SELECT 3 / 5 || 'asdf' || 7 * 9 FROM dual";
+    let select = oracle().verified_only_select(sql);
+    assert_eq!(1, select.projection.len());
+    assert_eq!(
+        expr_from_projection(&select.projection[0]),
+        // (C || D)
+        &Expr::BinaryOp {
+            // (A || B)
+            left: Box::new(Expr::BinaryOp {
+                // A
+                left: Box::new(Expr::BinaryOp {
+                    left: Box::new(Expr::Value(number("3").into())),
+                    op: BinaryOperator::Divide,
+                    right: Box::new(Expr::Value(number("5").into())),
+                }),
+                op: BinaryOperator::StringConcat,
+                right: Box::new(Expr::Value(ValueWithSpan {
+                    value: Value::SingleQuotedString("asdf".into()),
+                    span: Span::empty(),
+                })),
+            }),
+            op: BinaryOperator::StringConcat,
+            // D
+            right: Box::new(Expr::BinaryOp {
+                left: Box::new(Expr::Value(number("7").into())),
+                op: BinaryOperator::Multiply,
+                right: Box::new(Expr::Value(number("9").into())),
+            }),
+        }
+    );
+}
+
+/// Oracle: `+`, `-`, and `||` have the same precedence and parse from left-to-right
+#[test]
+fn plusminus_have_same_precedence_as_strconcat() {
+    // ................ A .. B .... C .. D ............
+    let sql = "SELECT 3 + 5 || '.3' || 7 - 9 FROM dual";
+    let select = oracle().verified_only_select(sql);
+    assert_eq!(1, select.projection.len());
+    assert_eq!(
+        expr_from_projection(&select.projection[0]),
+        // D
+        &Expr::BinaryOp {
+            left: Box::new(Expr::BinaryOp {
+                // B
+                left: Box::new(Expr::BinaryOp {
+                    // A
+                    left: Box::new(Expr::BinaryOp {
+                        left: Box::new(Expr::Value(number("3").into())),
+                        op: BinaryOperator::Plus,
+                        right: Box::new(Expr::Value(number("5").into())),
+                    }),
+                    op: BinaryOperator::StringConcat,
+                    right: Box::new(Expr::Value(ValueWithSpan {
+                        value: Value::SingleQuotedString(".3".into()),
+                        span: Span::empty(),
+                    })),
+                }),
+                op: BinaryOperator::StringConcat,
+                right: Box::new(Expr::Value(number("7").into())),
+            }),
+            op: BinaryOperator::Minus,
+            right: Box::new(Expr::Value(number("9").into()))
+        }
+    );
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org