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 think this might be the intended behaviour. What syntax do you expect to select *, t.*, count(*) to parse to? What syntax do you think it should parse to? Then we can see what looks like the best solution.
We are parsing SQL into a different DSL, so as a workaround, I special-cased BinOp "." to our our own column naming which has a Name similar to simple-sql-parser's except that it includes the Star option:
data Name = Name String | Star
Our DSL doesn't view Star as a naked scalar expression, but rather becomes part of an Iden [Name].
Here is the reason why it parses that way. When you parse e.g.
create table a.b ( ...
You can easily parse the a.b as a [Name] or something like that, because nothing else can appear there.
When you parse:
select a.b from ...;
What does a.b mean?
It can mean b is in the a namespace, so Iden [Name] seems sensible. But it could also mean b is a member of the struct that a is, so this is not a Iden [Name]. The code represents it as a binary
operator ('.').
The parser has to decide to parse all of these as Iden [Name], or as dot operators (or whatever syntax you decide for the general '.'/ member selector/ however you want to interpret it). The idea is that
something else can fix these up in a later pass when it's doing type checking or something like that.
jake=# create table a (x int, y int);
CREATE TABLE
jake=# select a.x from a;
x
---
(0 rows)
jake=# create type a1 as (x int, y int);
CREATE TYPE
jake=# create table b (z a1);
CREATE TABLE
jake=# select z.x from b;
ERROR: missing FROM-clause entry for table "z"
LINE 1: select z.x from b;
^
jake=# select (z).x from b;
x
---
(0 rows)
I'm not a fan, but maybe this is how everyone expects SQL to work? It seems to me very much like this is making the user's life more difficult to avoid some relatively easy coding. I imagine dealing with this was difficult in the mid 90s (in that period's C parser generators and compiler state of the art) when I assume this rule was decided on in Postgres, but we are not C programmers in 1993 so at least that excuse is not available to us.
It may be interesting to see if other databases have a similarly slightly odd syntax rule about this. I also have no idea what the ANSI standard position is.
I think the '*' handling partly depends on how to handle the Iden/member selector ambiguity. I think because of how messy SQL syntax is, there are often no right answers, just compromises to choose between.
Activity
JakeWheat commentedon Aug 9, 2019
I think this might be the intended behaviour. What syntax do you expect to
select *, t.*, count(*)to parse to? What syntax do you think it should parse to? Then we can see what looks like the best solution.agentm commentedon Aug 9, 2019
I don't expect the "." in "t.*" to be a binary operator any more than "t.a" is a binary function.
It looks like you originally had a
Star2ScalarExprto capture this which is commented out in the tests. I would be OK with that.agentm commentedon Aug 10, 2019
We are parsing SQL into a different DSL, so as a workaround, I special-cased
BinOp "."to our our own column naming which has aNamesimilar to simple-sql-parser's except that it includes the Star option:data Name = Name String | StarOur DSL doesn't view Star as a naked scalar expression, but rather becomes part of an
Iden [Name].JakeWheat commentedon Aug 13, 2019
Here is the reason why it parses that way. When you parse e.g.
You can easily parse the
a.bas a[Name]or something like that, because nothing else can appear there.When you parse:
What does
a.bmean?It can mean b is in the a namespace, so
Iden [Name]seems sensible. But it could also mean b is a member of the struct that a is, so this is not aIden [Name]. The code represents it as a binaryoperator ('.').
The parser has to decide to parse all of these as
Iden [Name], or as dot operators (or whatever syntax you decide for the general '.'/ member selector/ however you want to interpret it). The idea is thatsomething else can fix these up in a later pass when it's doing type checking or something like that.
Which one would you choose?
Here's what Postgres does:
https://www.postgresql.org/docs/current/sql-expressions.html#FIELD-SELECTION
I'm not a fan, but maybe this is how everyone expects SQL to work? It seems to me very much like this is making the user's life more difficult to avoid some relatively easy coding. I imagine dealing with this was difficult in the mid 90s (in that period's C parser generators and compiler state of the art) when I assume this rule was decided on in Postgres, but we are not C programmers in 1993 so at least that excuse is not available to us.
It may be interesting to see if other databases have a similarly slightly odd syntax rule about this. I also have no idea what the ANSI standard position is.
I think the '*' handling partly depends on how to handle the Iden/member selector ambiguity. I think because of how messy SQL syntax is, there are often no right answers, just compromises to choose between.