Oracle: qualify quotes USER, UID, ORA_ROWSCN and CONNECT_BY_ISLEAF/ISCYCLE as if they were columns
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Domain
- backend-api-design, databases
Research direction
Start with Oracle dialect definitions, especially Oracle.PSEUDOCOLUMNS and the no-parentheses handling used for SYSDATE, then trace how qualify resolves these names against the schema. Reproduce with the supplied snippet and Oracle dialect. Done when the five names remain unquoted and are not treated as columns; an open linked pull request is already addressing this issue.
Written by the indexing model from the issue text.
Description
Oracle's PSEUDOCOLUMNS covers ROWNUM, ROWID, LEVEL, OBJECT_ID and OBJECT_VALUE. It does not cover a few other pseudo-columns and no-paren functions. qualify therefore treats them as ordinary columns and quotes them. A quoted name means a column with that name to Oracle, so the rewritten query fails with ORA-00904.
LEVEL shows the expected behaviour in the same query.
Fully reproducible code snippet
Read and write dialect are both oracle. Checked on 30.36.0 and on main (a7124cf).
import sqlglot
from sqlglot.optimizer.qualify import qualify
schema = {"t": {"id": "INT", "parent_id": "INT"}}
for sql in [
"SELECT USER, UID FROM t",
"SELECT ORA_ROWSCN FROM t",
"SELECT id, LEVEL, CONNECT_BY_ISLEAF, CONNECT_BY_ISCYCLE FROM t "
"START WITH parent_id IS NULL CONNECT BY NOCYCLE PRIOR id = parent_id",
]:
tree = qualify(sqlglot.parse_one(sql, read="oracle"), schema=schema,
dialect="oracle", validate_qualify_columns=False)
print(tree.sql(dialect="oracle"))
Output:
SELECT "USER" AS "USER", "UID" AS "UID" FROM "T" "T"
SELECT "ORA_ROWSCN" AS "ORA_ROWSCN" FROM "T" "T"
SELECT "T"."ID" AS "ID", LEVEL AS "LEVEL", "CONNECT_BY_ISLEAF" AS "CONNECT_BY_ISLEAF", "CONNECT_BY_ISCYCLE" AS "CONNECT_BY_ISCYCLE" FROM "T" "T" START WITH ...
Expected: USER, UID, ORA_ROWSCN, CONNECT_BY_ISLEAF and CONNECT_BY_ISCYCLE stay unquoted, the way LEVEL does. They should also not be treated as columns of t.
A possible fix: add ORA_ROWSCN, CONNECT_BY_ISLEAF and CONNECT_BY_ISCYCLE to Oracle.PSEUDOCOLUMNS; the last two only apply under CONNECT BY, like LEVEL. USER and UID are functions rather than pseudo-columns, so they may fit better as no-paren functions, the way SYSDATE is handled. Happy to open a PR.
Official Documentation
- Pseudocolumns: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Pseudocolumns.html
- ORA_ROWSCN: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/ORA_ROWSCN-Pseudocolumn.html
- Hierarchical query pseudocolumns: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Hierarchical-Query-Pseudocolumns.html
- USER: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/USER.html
- UID: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/UID.html
- Dominant language
- Python
- Stars
- 9.8k
- Forks
- 1.5k
- Avg merge
- 10h 54m
- Merged PRs (30d)
- 310
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tobymao/sqlglot
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
ClickHouse: quantile is read as the exact Quantile, though ClickHouse's quantile is approximateOpen
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
ClickHouse: DAY_OF_WEEK is read as DayOfWeek, so every call is written back as toDayOfWeek(t, 3)Open
Difficulty 3/5 1-2 days Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
INTERVAL '1 day ago' loses "ago" and becomes a positive intervalPossibly taken @treysp claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
daniel-ospina/tortoise#15014 ·
Maintainers usually reply within 1 day
-
area:api p2 product:business triage:active type:feat
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
cuadrao-ai/argus#2656 ·
Maintainers usually reply within 1 day
-
documentation good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
good first issue tests
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
yeab7505/tenasig#20 · 1 comment ·
Maintainers usually reply within 1 day