You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have query which I wanted to convert from one database syntax to another.
Query 1: Select a+b as c ,a, b from test where c=100;
This query from teradata will not work in Postgres.
So I have to convert the query to
Output: Select a,b,c from (select a,b, a+b as c from test ) test where c=100;
I have created grammar rules to parse this rules.
K_SELECT - - Select
Select_List -- a,b,a+b as c
K_FROM - From
From_Clause -- test
where_clause - c=100.
I Parse the sql and uses match to match a rule and use a function for that rule to do transformation.
let mut s = String::new();
for i in sql_parse.into_inner() {
match i.as_rule() {
Rule::K_SELECT => { s.push_str(select_function(&i)) },
Rule::K_FROM => { s.push_str(from_function(&i)) },
Rule::select_list => {s.push_str(select_list_function(&i)) },
Rule::from_clause => {s.push_str(from_clause_function(&i))},
Rule::where_clause => {s.push_str(where_clause_function(&i))}
}
Now in the function select_function I might need to access the Pair for the Rule::where_clause and in sometimes I want to get access to select_list rule values from where_clause.
How to move back and forth in tree from the current position. Like when I am in where_clause_function, I should be able to access the select_list rule.
This discussion was converted from issue #452 on July 14, 2022 14:48.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi All,
I have query which I wanted to convert from one database syntax to another.
Query 1: Select a+b as c ,a, b from test where c=100;
This query from teradata will not work in Postgres.
So I have to convert the query to
Output: Select a,b,c from (select a,b, a+b as c from test ) test where c=100;
I have created grammar rules to parse this rules.
K_SELECT - - Select
Select_List -- a,b,a+b as c
K_FROM - From
From_Clause -- test
where_clause - c=100.
I Parse the sql and uses match to match a rule and use a function for that rule to do transformation.
let mut s = String::new();
for i in sql_parse.into_inner() {
match i.as_rule() {
Rule::K_SELECT => { s.push_str(select_function(&i)) },
Rule::K_FROM => { s.push_str(from_function(&i)) },
Rule::select_list => {s.push_str(select_list_function(&i)) },
Rule::from_clause => {s.push_str(from_clause_function(&i))},
Rule::where_clause => {s.push_str(where_clause_function(&i))}
}
Now in the function select_function I might need to access the Pair for the Rule::where_clause and in sometimes I want to get access to select_list rule values from where_clause.
How to move back and forth in tree from the current position. Like when I am in where_clause_function, I should be able to access the select_list rule.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions