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: support create transform #99

Merged
merged 1 commit into from
Dec 29, 2023
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
24 changes: 24 additions & 0 deletions crates/codegen/src/get_node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,30 @@ fn custom_handlers(node: &Node) -> TokenStream {
panic!("Encountered multiple defined func_name elements in CreateConversionStmt");
}
},
"CreateTransformStmt" => quote! {
tokens.push(TokenProperty::from(Token::Create));
if n.replace {
tokens.push(TokenProperty::from(Token::Or));
tokens.push(TokenProperty::from(Token::Replace));
}
tokens.push(TokenProperty::from(Token::Transform));
if n.type_name.is_some() {
tokens.push(TokenProperty::from(Token::For));
}
tokens.push(TokenProperty::from(Token::Language));
if n.fromsql.is_some() {
tokens.push(TokenProperty::from(Token::From));
tokens.push(TokenProperty::from(Token::SqlP));
tokens.push(TokenProperty::from(Token::With));
tokens.push(TokenProperty::from(Token::Function));
}
if n.tosql.is_some() {
tokens.push(TokenProperty::from(Token::To));
tokens.push(TokenProperty::from(Token::SqlP));
tokens.push(TokenProperty::from(Token::With));
tokens.push(TokenProperty::from(Token::Function));
}
},
_ => quote! {},
}
}
Expand Down
28 changes: 28 additions & 0 deletions crates/parser/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,32 @@ mod tests {
],
)
}

#[test]
fn test_create_transform() {
test_get_node_properties(
"CREATE OR REPLACE TRANSFORM FOR hstore LANGUAGE plpython3u (
FROM SQL WITH FUNCTION hstore_to_plpython(internal),
TO SQL WITH FUNCTION plpython_to_hstore(internal)
);",
SyntaxKind::CreateTransformStmt,
vec![
TokenProperty::from(SyntaxKind::Create),
TokenProperty::from(SyntaxKind::Or),
TokenProperty::from(SyntaxKind::Replace),
TokenProperty::from(SyntaxKind::Transform),
TokenProperty::from(SyntaxKind::For),
TokenProperty::from(SyntaxKind::Language),
TokenProperty::from(SyntaxKind::From),
TokenProperty::from(SyntaxKind::SqlP),
TokenProperty::from(SyntaxKind::With),
TokenProperty::from(SyntaxKind::Function),
TokenProperty::from(SyntaxKind::To),
TokenProperty::from(SyntaxKind::SqlP),
TokenProperty::from(SyntaxKind::With),
TokenProperty::from(SyntaxKind::Function),
TokenProperty::from("plpython3u".to_string()),
],
)
}
}
9 changes: 9 additions & 0 deletions crates/parser/tests/data/statements/valid/0056.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TRANSFORM FOR hstore LANGUAGE plpython3u (
FROM SQL WITH FUNCTION hstore_to_plpython(internal),
TO SQL WITH FUNCTION plpython_to_hstore(internal)
);

CREATE OR REPLACE TRANSFORM FOR hstore LANGUAGE plpython3u (
FROM SQL WITH FUNCTION hstore_to_plpython(internal),
TO SQL WITH FUNCTION plpython_to_hstore(internal)
);
251 changes: 251 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
---
source: crates/parser/tests/statement_parser_test.rs
description: "CREATE TRANSFORM FOR hstore LANGUAGE plpython3u (\n FROM SQL WITH FUNCTION hstore_to_plpython(internal),\n TO SQL WITH FUNCTION plpython_to_hstore(internal)\n);"
---
Parse {
cst: [email protected]
[email protected]
[email protected] "CREATE"
[email protected] " "
[email protected] "TRANSFORM"
[email protected] " "
[email protected] "FOR"
[email protected] " "
[email protected]
[email protected] "hstore"
[email protected] " "
[email protected] "LANGUAGE"
[email protected] " "
[email protected] "plpython3u"
[email protected] " "
[email protected] "("
[email protected] "\n"
[email protected] " "
[email protected] "FROM"
[email protected] " "
[email protected] "SQL"
[email protected] " "
[email protected] "WITH"
[email protected] " "
[email protected] "FUNCTION"
[email protected] " "
[email protected]
[email protected] "hstore_to_plpython"
[email protected] "("
[email protected]
[email protected] "internal"
[email protected] ")"
[email protected] ","
[email protected] "\n"
[email protected] " "
[email protected] "TO"
[email protected] " "
[email protected] "SQL"
[email protected] " "
[email protected] "WITH"
[email protected] " "
[email protected] "FUNCTION"
[email protected] " "
[email protected]
[email protected] "plpython_to_hstore"
[email protected] "("
[email protected]
[email protected] "internal"
[email protected] ")"
[email protected] "\n"
[email protected] ")"
[email protected] ";"
,
errors: [],
stmts: [
RawStmt {
stmt: CreateTransformStmt(
CreateTransformStmt {
replace: false,
type_name: Some(
TypeName {
names: [
Node {
node: Some(
String(
String {
sval: "hstore",
},
),
),
},
],
type_oid: 0,
setof: false,
pct_type: false,
typmods: [],
typemod: -1,
array_bounds: [],
location: 21,
},
),
lang: "plpython3u",
fromsql: Some(
ObjectWithArgs {
objname: [
Node {
node: Some(
String(
String {
sval: "hstore_to_plpython",
},
),
),
},
],
objargs: [
Node {
node: Some(
TypeName(
TypeName {
names: [
Node {
node: Some(
String(
String {
sval: "internal",
},
),
),
},
],
type_oid: 0,
setof: false,
pct_type: false,
typmods: [],
typemod: -1,
array_bounds: [],
location: 96,
},
),
),
},
],
objfuncargs: [
Node {
node: Some(
FunctionParameter(
FunctionParameter {
name: "",
arg_type: Some(
TypeName {
names: [
Node {
node: Some(
String(
String {
sval: "internal",
},
),
),
},
],
type_oid: 0,
setof: false,
pct_type: false,
typmods: [],
typemod: -1,
array_bounds: [],
location: 96,
},
),
mode: FuncParamDefault,
defexpr: None,
},
),
),
},
],
args_unspecified: false,
},
),
tosql: Some(
ObjectWithArgs {
objname: [
Node {
node: Some(
String(
String {
sval: "plpython_to_hstore",
},
),
),
},
],
objargs: [
Node {
node: Some(
TypeName(
TypeName {
names: [
Node {
node: Some(
String(
String {
sval: "internal",
},
),
),
},
],
type_oid: 0,
setof: false,
pct_type: false,
typmods: [],
typemod: -1,
array_bounds: [],
location: 151,
},
),
),
},
],
objfuncargs: [
Node {
node: Some(
FunctionParameter(
FunctionParameter {
name: "",
arg_type: Some(
TypeName {
names: [
Node {
node: Some(
String(
String {
sval: "internal",
},
),
),
},
],
type_oid: 0,
setof: false,
pct_type: false,
typmods: [],
typemod: -1,
array_bounds: [],
location: 151,
},
),
mode: FuncParamDefault,
defexpr: None,
},
),
),
},
],
args_unspecified: false,
},
),
},
),
range: 0..162,
},
],
}
Loading