Skip to content

fix interval precedence for interval expressions #1396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Usage:
$ cargo run --example cli FILENAME.sql [--dialectname]

To print the parse results as JSON:
$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
$ cargo run --features json_example --example cli FILENAME.sql [--dialectname]

"#,
);
Expand Down
2 changes: 2 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ pub trait Dialect: Debug + Any {
Precedence::UnaryNot => 15,
Precedence::And => 10,
Precedence::Or => 5,
Precedence::Interval => self.prec_unknown(),
}
}

Expand Down Expand Up @@ -522,6 +523,7 @@ pub enum Precedence {
UnaryNot,
And,
Or,
Interval,
}

impl dyn Dialect {
Expand Down
3 changes: 3 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::tokenizer::Token;
#[derive(Debug)]
pub struct PostgreSqlDialect {}

// matches <https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-PRECEDENCE>
const INTERVAL_COLON_PREC: u8 = 150;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the point of this PR — interval binds more tightly than anything else in postgres:

select pg_typeof(INTERVAL '1'::text)
--> text

E.g. postgres evaluates that as (interval '1')::text, not as interval ('1'::text) as was the in sqlparser-rs until this PR.

const DOUBLE_COLON_PREC: u8 = 140;
const BRACKET_PREC: u8 = 130;
const COLLATE_PREC: u8 = 120;
Expand Down Expand Up @@ -136,6 +138,7 @@ impl Dialect for PostgreSqlDialect {

fn prec_value(&self, prec: Precedence) -> u8 {
match prec {
Precedence::Interval => INTERVAL_COLON_PREC,
Precedence::DoubleColon => DOUBLE_COLON_PREC,
Precedence::AtTz => AT_TZ_PREC,
Precedence::MulDivModOp => MUL_DIV_MOD_OP_PREC,
Expand Down
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ impl<'a> Parser<'a> {
}

pub fn parse_interval_expr(&mut self) -> Result<Expr, ParserError> {
let precedence = self.dialect.prec_unknown();
let precedence = self.dialect.prec_value(Precedence::Interval);
let mut expr = self.parse_prefix()?;

loop {
Expand Down
3 changes: 2 additions & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4955,7 +4955,8 @@ fn parse_interval() {
);

let sql = "SELECT INTERVAL 1 + 1 DAY";
let select = verified_only_select(sql);
let all_except_pg = all_dialects_except(|d| d.is::<PostgreSqlDialect>());
let select = all_except_pg.verified_only_select(sql);
assert_eq!(
&Expr::Interval(Interval {
value: Box::new(Expr::BinaryOp {
Expand Down
46 changes: 46 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4949,6 +4949,52 @@ fn test_unicode_string_literal() {
}
}

#[test]
fn interval_precedence_gt() {
let expr = pg().verified_expr("INTERVAL '1 second' > x");
assert_eq!(
expr,
Expr::BinaryOp {
left: Box::new(Expr::Interval(Interval {
value: Box::new(Expr::Value(Value::SingleQuotedString(
"1 second".to_string()
))),
leading_field: None,
leading_precision: None,
last_field: None,
fractional_seconds_precision: None,
},)),
op: BinaryOperator::Gt,
right: Box::new(Expr::Identifier(Ident {
value: "x".to_string(),
quote_style: None,
})),
}
)
}

#[test]
fn interval_precedence_double_colon() {
let expr = pg().verified_expr("INTERVAL '1 second'::TEXT");
assert_eq!(
expr,
Expr::Cast {
kind: CastKind::DoubleColon,
expr: Box::new(Expr::Interval(Interval {
value: Box::new(Expr::Value(Value::SingleQuotedString(
"1 second".to_string()
))),
leading_field: None,
leading_precision: None,
last_field: None,
fractional_seconds_precision: None,
})),
data_type: DataType::Text,
format: None,
}
)
}

fn check_arrow_precedence(sql: &str, arrow_operator: BinaryOperator) {
assert_eq!(
pg().verified_expr(sql),
Expand Down
Loading