Skip to content

SELECT t.* is parsed as operator #24

Open

Description

@agentm

SELECT t.* is parsed as BinOp (Iden [Name Nothing "t"]) [Name Nothing "."] Star but it should be an Iden.

Is the intention for Star never to be part of Iden Name list?

The example in the docs star, as in select *, t.*, count(*) seems to imply that there is a way to represent a Star with an Iden, but I don't see how.

Activity

  1. JakeWheat commented on Aug 9, 2019

    @JakeWheat
    Owner

    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.

  2. agentm commented on Aug 9, 2019

    @agentm
    Author

    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 Star2 ScalarExpr to capture this which is commented out in the tests. I would be OK with that.

  3. agentm commented on Aug 10, 2019

    @agentm
    Author

    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].

  4. JakeWheat commented on Aug 13, 2019

    @JakeWheat
    Owner

    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.

    Which one would you choose?

    Here's what Postgres does:

    https://www.postgresql.org/docs/current/sql-expressions.html#FIELD-SELECTION

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions