-
-
Notifications
You must be signed in to change notification settings - Fork 220
Open
Description
Describe the bug
Column aliases are ignored when using PostgreSQL's :: cast syntax without parentheses.
Database Engine
PostgreSQL.
To Reproduce
Reproduce with the following query
-- query gives incorrect ast
select x::date as y from tbl; -- `as` is set to `null`
-- same query but gives correct ast
select (x::date) as y from tbl; -- `as` is set to `y`The query without parentheses yields the following ast
{
"tableList": [
"select::null::tbl"
],
"columnList": [
"select::null::x"
],
"ast": [
{
"with": null,
"type": "select",
"options": null,
"distinct": {
"type": null
},
"columns": [
{
"type": "expr",
"expr": {
"as": "y",
"symbol": "::",
"target": [
{
"dataType": "DATE"
}
],
"type": "cast",
"keyword": "cast",
"expr": {
"type": "column_ref",
"table": null,
"column": {
"expr": {
"type": "default",
"value": "x"
}
},
"collate": null
}
},
"as": null
}
],
"into": {
"position": null
},
"from": [
{
"db": null,
"table": "tbl",
"as": null
}
],
"where": null,
"groupby": null,
"having": null,
"orderby": null,
"limit": {
"seperator": "",
"value": []
},
"window": null
}
]
}ast.columns[0].expr.as = "y" // wrong location
ast.columns[0].as = null // should be "y"node-sql-parser version: 5.3.13
node version: 22
Expected behavior
I expect the ast for column x to have the as key to be set to y and not null. The wanted behaviour exists when using the exact same query but using parentheses around the column being casted.
Additional context
I've made a pull requests which includes test for this case (#2617).
Here's the ast for parsing a query the query with parentheses.
select (x::date) as y from tbl;ast for query above
{
"tableList": [
"select::null::tbl"
],
"columnList": [
"select::null::x"
],
"ast": [
{
"with": null,
"type": "select",
"options": null,
"distinct": {
"type": null
},
"columns": [
{
"type": "expr",
"expr": {
"as": null,
"symbol": "::",
"target": [
{
"dataType": "DATE"
}
],
"type": "cast",
"keyword": "cast",
"expr": {
"type": "column_ref",
"table": null,
"column": {
"expr": {
"type": "default",
"value": "x"
}
},
"collate": null
},
"parentheses": true
},
"as": "y"
}
],
"into": {
"position": null
},
"from": [
{
"db": null,
"table": "tbl",
"as": null
}
],
"where": null,
"groupby": null,
"having": null,
"orderby": null,
"limit": {
"seperator": "",
"value": []
},
"window": null
}
]
}Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working