Skip to content
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

feat: Parse special keywords as functions (current_user, user, etc) #561

Merged
merged 2 commits into from Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/ast/mod.rs
Expand Up @@ -2292,20 +2292,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,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder why a special field is needed ? Wouldn't it be enough to match on the name?

If we do need a special field, can you please add a /// comment explaining what it means and what it is used for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I use this field to skip formating (arguments).

image

Right now, Dialect is not available in formating, because we use fmt::Display for implementing to_string() method.

}

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 @@ -611,7 +611,8 @@ fn parse_insert_with_on_duplicate_update() {
Expr::Identifier(Ident::new("description"))
))],
over: None,
distinct: false
distinct: false,
special: false,
})
},
Assignment {
Expand All @@ -622,7 +623,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 @@ -633,7 +635,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 @@ -644,7 +647,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 @@ -655,7 +659,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