Support postgres style CREATE FUNCTION in GenericDialect (#1159)

This commit is contained in:
Andrew Lamb 2024-03-01 13:43:29 -05:00 committed by GitHub
parent a511c47bd0
commit 9db9d22480
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 10 additions and 4 deletions

View file

@ -3317,7 +3317,7 @@ impl<'a> Parser<'a> {
return_type: None, return_type: None,
params, params,
}) })
} else if dialect_of!(self is PostgreSqlDialect) { } else if dialect_of!(self is PostgreSqlDialect | GenericDialect) {
let name = self.parse_object_name(false)?; let name = self.parse_object_name(false)?;
self.expect_token(&Token::LParen)?; self.expect_token(&Token::LParen)?;
let args = if self.consume_token(&Token::RParen) { let args = if self.consume_token(&Token::RParen) {

View file

@ -19,7 +19,7 @@ use sqlparser::ast::{
CreateFunctionBody, CreateFunctionUsing, Expr, Function, FunctionDefinition, Ident, ObjectName, CreateFunctionBody, CreateFunctionUsing, Expr, Function, FunctionDefinition, Ident, ObjectName,
SelectItem, Statement, TableFactor, UnaryOperator, Value, SelectItem, Statement, TableFactor, UnaryOperator, Value,
}; };
use sqlparser::dialect::{GenericDialect, HiveDialect}; use sqlparser::dialect::{GenericDialect, HiveDialect, MsSqlDialect};
use sqlparser::parser::{ParserError, ParserOptions}; use sqlparser::parser::{ParserError, ParserOptions};
use sqlparser::test_utils::*; use sqlparser::test_utils::*;
@ -285,8 +285,14 @@ fn parse_create_function() {
_ => unreachable!(), _ => 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!( assert_eq!(
generic(None).parse_sql_statements(sql).unwrap_err(), unsupported_dialects.parse_sql_statements(sql).unwrap_err(),
ParserError::ParserError( ParserError::ParserError(
"Expected an object type after CREATE, found: FUNCTION".to_string() "Expected an object type after CREATE, found: FUNCTION".to_string()
) )

View file

@ -3257,7 +3257,7 @@ fn parse_similar_to() {
fn parse_create_function() { fn parse_create_function() {
let sql = "CREATE FUNCTION add(INTEGER, INTEGER) RETURNS INTEGER LANGUAGE SQL IMMUTABLE AS 'select $1 + $2;'"; let sql = "CREATE FUNCTION add(INTEGER, INTEGER) RETURNS INTEGER LANGUAGE SQL IMMUTABLE AS 'select $1 + $2;'";
assert_eq!( assert_eq!(
pg().verified_stmt(sql), pg_and_generic().verified_stmt(sql),
Statement::CreateFunction { Statement::CreateFunction {
or_replace: false, or_replace: false,
temporary: false, temporary: false,