Problem parsing Oracle queries #2565
-
I'm getting parse errors while parsing Oracle queries with:
I'm using 'oracle' as dialect. |
Beta Was this translation helpful? Give feedback.
Answered by
georgesittas
Nov 14, 2023
Replies: 2 comments
-
Please create descriptive issues with reproducible examples. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
georgesittas
-
Here is reproduceable example. ---------------
-- SQL Code ---
---------------
-- Oracle Database 19c Enterprise Edition Release 19.0.0.0.0
-- Database version
select * from v$version;
/
-- drop table if exists
drop table employees;
/
-- create hierarchical table
create table employees as
select '1_JONES' as last_name, 1 as employee_id, null as manager_id from dual union all
select '2_BLAKE' as last_name, 2 as employee_id, 1 as manaer_id from dual union all
select '3_CLARK' as last_name, 3 as employee_id, 2 as manaer_id from dual union all
select '4_Marko' as last_name, 4 as employee_id, 2 as manaer_id from dual union all
select '5_Ivan' as last_name, 5 as employee_id, 3 as manaer_id from dual union all
select '6_Ante' as last_name, 6 as employee_id, 4 as manaer_id from dual;
/
commit;
/
-- check inserted data
SELECT employee_id, last_name, manager_id
FROM employees
;
/
-- write hierarchical query
-- https://docs.oracle.com/cd/B13789_01/server.101/b10759/queries003.htm#i2060615
SELECT
employee_id,
last_name,
manager_id mng_id,
level as lvl,
CONNECT_BY_ROOT last_name mng_name,
1 as one
FROM employees
CONNECT BY PRIOR employee_id = manager_id
;
/ ### Python code ###
#pip show sqlglot
#Name: sqlglot
#Version: 18.0.1
from sqlglot import parse_one, expressions
ok_sql = """
SELECT
employee_id,
last_name,
manager_id mng_id,
level as lvl,
CONNECT_BY_ROOT last_name, -- not parsing if alias exists
1 as one
FROM employees
CONNECT BY PRIOR employee_id = manager_id
"""
nok_sql = """
SELECT
employee_id,
last_name,
manager_id mng_id,
level as lvl,
CONNECT_BY_ROOT last_name mng_name_alias, -- not parsing if alias exists
1 as one
FROM employees
CONNECT BY PRIOR employee_id = manager_id
"""
# OK
parsed = parse_one(ok_sql, dialect = 'oracle')
# NOK - Parser error: Invalid expression / Unexpected token. Line 7, Col: 41.
# parsed = parse_one(nok_sql, dialect = 'oracle')
print(parsed.sql(pretty=True)) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please create descriptive issues with reproducible examples.