Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues parsing a column named user #92

Open
chayim opened this issue Jul 5, 2023 · 2 comments
Open

Issues parsing a column named user #92

chayim opened this issue Jul 5, 2023 · 2 comments
Labels

Comments

@chayim
Copy link

chayim commented Jul 5, 2023

First off thank you - I love this library!

In my case, I wrap several queries, and run them through a parser like a bunch of other folks. My simplified example looks like:

res, _ := pg_query.Parse(query)
stmt := res.Stmts[0].Stmt.GetSelectStmt()
for _, f := range stmt.GetTargetList() {
    sp := f.GetResTarget().GetVal().GetColumnRef().GetFields()[0]
    fmt.Println(sp.GetString_().Sval)
}

The following query of course, works: select username, password from foo where user = 'somehuman'

target_list:{res_target:{val:{column_ref:{fields:{string:{sval:\"username\"}} location:7}} location:7}} target_list:{res_target:{val:{column_ref:{fields:{string:{sval:\"password\"}} location:17}} location:17}} from_clause:{range_var:{relname:\"foo\" inh:true relpersistence:\"p\" location:31}} where_clause:{a_expr:{kind:AEXPR_OP name:{string:{sval:\">\"}} lexpr:{column_ref:{fields:{string:{sval:\"x\"}} location:41}} rexpr:{a_const:{ival:{ival:17} location:45}} location:43}} limit_option:LIMIT_OPTION_DEFAULT op:SETOP_NONE" time="2023-07-05T11:55:54+03:00" level=debug msg="column_ref:{fields:{string:{sval:\"username\"}} location:7

The second, I change username to user, when attempting to call f.GetResTarget().GetVal().GetColumnRef().GetFields()[0] as clearly there's an indexing issue, do the the following parsed version of the object.

target_list:{res_target:{val:{sqlvalue_function:{op:SVFOP_USER typmod:-1 location:7}} location:7}} target_list:{res_target:{val:{column_ref:{fields:{string:{sval:\"password\"}} location:13}} location:13}} from_clause:{range_var:{relname:\"foo\" inh:true relpersistence:\"p\" location:27}} where_clause:{a_expr:{kind:AEXPR_OP name:{string:{sval:\">\"}} lexpr:{column_ref:{fields:{string:{sval:\"x\"}} location:37}} rexpr:{a_const:{ival:{ival:17} location:41}} location:39}} limit_option:LIMIT_OPTION_DEFAULT op:SETOP_NONE"

Is this an underlying libpq issue? I would have assumed my query would parse, and I'd just have a column named user

@chayim chayim changed the title Either libpq parsing bug, or pg_query Issues parsing a column named user Jul 5, 2023
@chayim chayim changed the title Issues parsing a column named user Issues parsing a column named user Jul 5, 2023
@lfittl
Copy link
Member

lfittl commented Jul 6, 2023

I think the problem here is that user is a keyword (see https://www.postgresql.org/docs/current/sql-keywords-appendix.html), and so you need to escape it like this: "user" (in Postgres syntax," is used to escape identifiers, ' is used for values).

@lfittl lfittl added the question label Jul 6, 2023
@chayim
Copy link
Author

chayim commented Jul 6, 2023

Interesting, thank you. So I looked at 3 options that work with the postgres cli:

  1. I wrapped user in backticks `user`. That fails to parse
  2. As you mentioned, single quotes fail in the exact same way as mentioned above.
  3. Double quotes work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants