Skip to content

Support postgres style CREATE FUNCTION in GenericDialect #1159

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

Merged
merged 2 commits into from
Mar 1, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3317,7 +3317,7 @@ impl<'a> Parser<'a> {
return_type: None,
params,
})
} else if dialect_of!(self is PostgreSqlDialect) {
} else if dialect_of!(self is PostgreSqlDialect | GenericDialect) {
let name = self.parse_object_name(false)?;
self.expect_token(&Token::LParen)?;
let args = if self.consume_token(&Token::RParen) {
Expand Down
10 changes: 8 additions & 2 deletions tests/sqlparser_hive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use sqlparser::ast::{
CreateFunctionBody, CreateFunctionUsing, Expr, Function, FunctionDefinition, Ident, ObjectName,
SelectItem, Statement, TableFactor, UnaryOperator, Value,
};
use sqlparser::dialect::{GenericDialect, HiveDialect};
use sqlparser::dialect::{GenericDialect, HiveDialect, MsSqlDialect};
use sqlparser::parser::{ParserError, ParserOptions};
use sqlparser::test_utils::*;

Expand Down Expand Up @@ -285,8 +285,14 @@ fn parse_create_function() {
_ => unreachable!(),
}

// Test error in dialect that doesn't support parsing CREATE FUNCTION
let unsupported_dialects = TestedDialects {
dialects: vec![Box::new(MsSqlDialect {})],
options: None,
};

assert_eq!(
generic(None).parse_sql_statements(sql).unwrap_err(),
unsupported_dialects.parse_sql_statements(sql).unwrap_err(),
ParserError::ParserError(
"Expected an object type after CREATE, found: FUNCTION".to_string()
)
Expand Down
2 changes: 1 addition & 1 deletion tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3257,7 +3257,7 @@ fn parse_similar_to() {
fn parse_create_function() {
let sql = "CREATE FUNCTION add(INTEGER, INTEGER) RETURNS INTEGER LANGUAGE SQL IMMUTABLE AS 'select $1 + $2;'";
assert_eq!(
pg().verified_stmt(sql),
pg_and_generic().verified_stmt(sql),
Statement::CreateFunction {
or_replace: false,
temporary: false,
Expand Down