Skip to content

Commit

Permalink
feat: Parse special keywords as functions (current_user, user, etc) (#…
Browse files Browse the repository at this point in the history
…561)

* feat: Parse special keywors as functions (current_user, user, etc)

* explain special field
  • Loading branch information
ovr committed Aug 11, 2022
1 parent b6e36ad commit 54a29e8
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 17 deletions.
27 changes: 18 additions & 9 deletions src/ast/mod.rs
Expand Up @@ -2321,20 +2321,29 @@ pub struct Function {
pub over: Option<WindowSpec>,
// aggregate functions may specify eg `COUNT(DISTINCT x)`
pub distinct: bool,
// Some functions must be called without trailing parentheses, for example Postgres
// do it for current_catalog, current_schema, etc. This flags is used for formatting.
pub special: bool,
}

impl fmt::Display for Function {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}({}{})",
self.name,
if self.distinct { "DISTINCT " } else { "" },
display_comma_separated(&self.args),
)?;
if let Some(o) = &self.over {
write!(f, " OVER ({})", o)?;
if self.special {
write!(f, "{}", self.name)?;
} else {
write!(
f,
"{}({}{})",
self.name,
if self.distinct { "DISTINCT " } else { "" },
display_comma_separated(&self.args),
)?;

if let Some(o) = &self.over {
write!(f, " OVER ({})", o)?;
}
}

Ok(())
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/parser.rs
Expand Up @@ -421,6 +421,20 @@ impl<'a> Parser<'a> {
self.prev_token();
Ok(Expr::Value(self.parse_value()?))
}
Keyword::CURRENT_CATALOG
| Keyword::CURRENT_USER
| Keyword::SESSION_USER
| Keyword::USER
if dialect_of!(self is PostgreSqlDialect | GenericDialect) =>
{
Ok(Expr::Function(Function {
name: ObjectName(vec![w.to_ident()]),
args: vec![],
over: None,
distinct: false,
special: true,
}))
}
Keyword::CURRENT_TIMESTAMP | Keyword::CURRENT_TIME | Keyword::CURRENT_DATE => {
self.parse_time_functions(ObjectName(vec![w.to_ident()]))
}
Expand Down Expand Up @@ -598,6 +612,7 @@ impl<'a> Parser<'a> {
args,
over,
distinct,
special: false,
}))
}

Expand All @@ -612,6 +627,7 @@ impl<'a> Parser<'a> {
args,
over: None,
distinct: false,
special: false,
}))
}

Expand Down
19 changes: 17 additions & 2 deletions tests/sqlparser_common.rs
Expand Up @@ -572,6 +572,7 @@ fn parse_select_count_wildcard() {
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Wildcard)],
over: None,
distinct: false,
special: false,
}),
expr_from_projection(only(&select.projection))
);
Expand All @@ -590,6 +591,7 @@ fn parse_select_count_distinct() {
}))],
over: None,
distinct: true,
special: false,
}),
expr_from_projection(only(&select.projection))
);
Expand Down Expand Up @@ -1414,6 +1416,7 @@ fn parse_select_having() {
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Wildcard)],
over: None,
distinct: false,
special: false,
})),
op: BinaryOperator::Gt,
right: Box::new(Expr::Value(number("1")))
Expand Down Expand Up @@ -1445,7 +1448,8 @@ fn parse_select_qualify() {
}],
window_frame: None
}),
distinct: false
distinct: false,
special: false
})),
op: BinaryOperator::Eq,
right: Box::new(Expr::Value(number("1")))
Expand Down Expand Up @@ -2532,6 +2536,7 @@ fn parse_scalar_function_in_projection() {
))],
over: None,
distinct: false,
special: false,
}),
expr_from_projection(only(&select.projection))
);
Expand Down Expand Up @@ -2610,6 +2615,7 @@ fn parse_named_argument_function() {
],
over: None,
distinct: false,
special: false,
}),
expr_from_projection(only(&select.projection))
);
Expand Down Expand Up @@ -2643,6 +2649,7 @@ fn parse_window_functions() {
window_frame: None,
}),
distinct: false,
special: false,
}),
expr_from_projection(&select.projection[0])
);
Expand Down Expand Up @@ -2906,7 +2913,8 @@ fn parse_at_timezone() {
}]),
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(zero.clone()))],
over: None,
distinct: false
distinct: false,
special: false,
})),
time_zone: "UTC-06:00".to_string()
},
Expand All @@ -2932,6 +2940,7 @@ fn parse_at_timezone() {
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(zero,),),],
over: None,
distinct: false,
special: false
},)),
time_zone: "UTC-06:00".to_string(),
},),),
Expand All @@ -2941,6 +2950,7 @@ fn parse_at_timezone() {
],
over: None,
distinct: false,
special: false
},),
alias: Ident {
value: "hour".to_string(),
Expand Down Expand Up @@ -2976,6 +2986,7 @@ fn parse_table_function() {
)))],
over: None,
distinct: false,
special: false,
});
assert_eq!(expr, expected_expr);
assert_eq!(alias, table_alias("a"))
Expand Down Expand Up @@ -3148,6 +3159,7 @@ fn parse_delimited_identifiers() {
args: vec![],
over: None,
distinct: false,
special: false,
}),
expr_from_projection(&select.projection[1]),
);
Expand Down Expand Up @@ -5125,6 +5137,7 @@ fn parse_time_functions() {
args: vec![],
over: None,
distinct: false,
special: false,
}),
expr_from_projection(&select.projection[0])
);
Expand All @@ -5140,6 +5153,7 @@ fn parse_time_functions() {
args: vec![],
over: None,
distinct: false,
special: false,
}),
expr_from_projection(&select.projection[0])
);
Expand All @@ -5155,6 +5169,7 @@ fn parse_time_functions() {
args: vec![],
over: None,
distinct: false,
special: false,
}),
expr_from_projection(&select.projection[0])
);
Expand Down
15 changes: 10 additions & 5 deletions tests/sqlparser_mysql.rs
Expand Up @@ -686,7 +686,8 @@ fn parse_insert_with_on_duplicate_update() {
Expr::Identifier(Ident::new("description"))
))],
over: None,
distinct: false
distinct: false,
special: false,
})
},
Assignment {
Expand All @@ -697,7 +698,8 @@ fn parse_insert_with_on_duplicate_update() {
Expr::Identifier(Ident::new("perm_create"))
))],
over: None,
distinct: false
distinct: false,
special: false,
})
},
Assignment {
Expand All @@ -708,7 +710,8 @@ fn parse_insert_with_on_duplicate_update() {
Expr::Identifier(Ident::new("perm_read"))
))],
over: None,
distinct: false
distinct: false,
special: false,
})
},
Assignment {
Expand All @@ -719,7 +722,8 @@ fn parse_insert_with_on_duplicate_update() {
Expr::Identifier(Ident::new("perm_update"))
))],
over: None,
distinct: false
distinct: false,
special: false,
})
},
Assignment {
Expand All @@ -730,7 +734,8 @@ fn parse_insert_with_on_duplicate_update() {
Expr::Identifier(Ident::new("perm_delete"))
))],
over: None,
distinct: false
distinct: false,
special: false,
})
},
])),
Expand Down
47 changes: 47 additions & 0 deletions tests/sqlparser_postgres.rs
Expand Up @@ -1400,6 +1400,7 @@ fn test_composite_value() {
)))],
over: None,
distinct: false,
special: false
}))))
}),
select.projection[0]
Expand Down Expand Up @@ -1542,6 +1543,52 @@ fn parse_declare() {
pg_and_generic().verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" BINARY INSENSITIVE SCROLL CURSOR WITH HOLD FOR SELECT * FROM table_name LIMIT 2222");
}

#[test]
fn parse_current_functions() {
let sql = "SELECT CURRENT_CATALOG, CURRENT_USER, SESSION_USER, USER";
let select = pg_and_generic().verified_only_select(sql);
assert_eq!(
&Expr::Function(Function {
name: ObjectName(vec![Ident::new("CURRENT_CATALOG")]),
args: vec![],
over: None,
distinct: false,
special: true,
}),
expr_from_projection(&select.projection[0])
);
assert_eq!(
&Expr::Function(Function {
name: ObjectName(vec![Ident::new("CURRENT_USER")]),
args: vec![],
over: None,
distinct: false,
special: true,
}),
expr_from_projection(&select.projection[1])
);
assert_eq!(
&Expr::Function(Function {
name: ObjectName(vec![Ident::new("SESSION_USER")]),
args: vec![],
over: None,
distinct: false,
special: true,
}),
expr_from_projection(&select.projection[2])
);
assert_eq!(
&Expr::Function(Function {
name: ObjectName(vec![Ident::new("USER")]),
args: vec![],
over: None,
distinct: false,
special: true,
}),
expr_from_projection(&select.projection[3])
);
}

#[test]
fn parse_fetch() {
pg_and_generic().verified_stmt("FETCH 2048 IN \"SQL_CUR0x7fa44801bc00\"");
Expand Down
4 changes: 3 additions & 1 deletion tests/sqpparser_clickhouse.rs
Expand Up @@ -51,6 +51,7 @@ fn parse_map_access_expr() {
],
over: None,
distinct: false,
special: false,
})],
})],
into: None,
Expand Down Expand Up @@ -85,7 +86,8 @@ fn parse_map_access_expr() {
))),
],
over: None,
distinct: false
distinct: false,
special: false,
})]
}),
op: BinaryOperator::NotEq,
Expand Down

0 comments on commit 54a29e8

Please sign in to comment.