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

pg_query could not detect length of RawStmt(s) with quoted characters #191

Open
valerysvl opened this issue May 22, 2023 · 3 comments
Open
Labels

Comments

@valerysvl
Copy link

Example query with 2 stataments:

UPDATE _ScheduledJobs23816 SET _RestartAttemptNumber = CAST(0 AS NUMERIC), _State = CAST(1 AS NUMERIC), _StartTime = '2023-05-16 11:22:52'::timestamp, _FinishTime = '2023-05-16 11:21:27'::timestamp
WHERE _ScheduledJobs23816._ID = '\244\331\340\376\225\250\343\343A\005Z3\\270c\\273'::bytea; UPDATE _ScheduledJobs23816 SET _RestartAttemptNumber = CAST(0 AS NUMERIC), _State = CAST(1 AS NUMERIC), _StartTime = '2023-05-16 11:22:52'::timestamp, _FinishTime = '2023-05-16 11:21:27'::timestamp WHERE _ScheduledJobs23816._ID = '\\244\\331\\340\\376\\225\\250\\343\\343A\\005Z3\270c\273'::bytea;

pg_query can not detect on RawStmt list:

  1. correct stmt_length of both statements,
  2. correct stmt_location on 2nd statement

Used pg_query for PG15.1

@valerysvl valerysvl changed the title pg_query could detect length of RawStmt(s) with quoted characters pg_query could not detect length of RawStmt(s) with quoted characters May 22, 2023
@lfittl
Copy link
Member

lfittl commented May 22, 2023

@valerysvl Thanks for the report!

Could you clarify what you are expecting, and what you are seeing?

From a quick test using pg_query, this appears to work correctly:

irb(main):021:0> puts x
UPDATE _ScheduledJobs23816 SET _RestartAttemptNumber = CAST(0 AS NUMERIC), _State = CAST(1 AS NUMERIC), _StartTime = '2023-05-16 11:22:52'::timestamp, _FinishTime = '2023-05-16 11:21:27'::timestamp
WHERE _ScheduledJobs23816._ID = '\244\331\340\376\225\250\343\343A\005`Z3\270c\273'::bytea;
UPDATE _ScheduledJobs23816 SET _RestartAttemptNumber = CAST(0 AS NUMERIC), _State = CAST(1 AS NUMERIC), _StartTime = '2023-05-16 11:22:52'::timestamp, _FinishTime = '2023-05-16 11:21:27'::timestamp
WHERE _ScheduledJobs23816._ID = '\244\331\340\376\225\250\343\343A\005`Z3\270c\273'::bytea;

irb(main):015:0> PgQuery.parse(x).tree.stmts.map {|s| [s.stmt_location, s.stmt_len] }
=> [[0, 288], [289, 289]]

irb(main):018:0> x[..288]
=> "UPDATE _ScheduledJobs23816 SET _RestartAttemptNumber = CAST(0 AS NUMERIC), _State = CAST(1 AS NUMERIC), _StartTime = '2023-05-16 11:22:52'::timestamp, _FinishTime = '2023-05-16 11:21:27'::timestamp\nWHERE _ScheduledJobs23816._ID = '\\244\\331\\340\\376\\225\\250\\343\\343A\\005`Z3\\270c\\273'::bytea;"

irb(main):019:0> x[289..][..289]
=> "\nUPDATE _ScheduledJobs23816 SET _RestartAttemptNumber = CAST(0 AS NUMERIC), _State = CAST(1 AS NUMERIC), _StartTime = '2023-05-16 11:22:52'::timestamp, _FinishTime = '2023-05-16 11:21:27'::timestamp\nWHERE _ScheduledJobs23816._ID = '\\244\\331\\340\\376\\225\\250\\343\\343A\\005`Z3\\270c\\273'::bytea;"

@valerysvl
Copy link
Author

after parsing
protobuf_result = pg_query_parse_protobuf(source_query);

and following unpacking
unpacked = pg_query__parse_result__unpack(NULL, pgquery_pbparse_result.parse_tree.len, pgquery_pbparse_result.parse_tree.data)

I am expecting stms_location as correct offset on source_query

unpacked.stmts[0].stmt_len = 289;
unpacked.stmts[0].stmt_location = 0;
unpacked.stmts[1].stmt_len = 289;
unpacked.stmts[1].stmt_location = 289;
unpacked.stmts[2].stmt_len = 289;
unpacked.stmts[2].stmt_location = 579;
unpacked.stmts[3].stmt_len = 289;
unpacked.stmts[3].stmt_location = 867;
...

@lfittl
Copy link
Member

lfittl commented May 22, 2023

@valerysvl Its not clear to me what you mean with "correct offset on source_query" - from the data provided the len and location appear to be correct.

Could you provide a fully self-contained example as a reproducer?

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