gda icon indicating copy to clipboard operation
gda copied to clipboard

The parser cannot parse update query correctly with `order by`

Open hyhuang1218 opened this issue 3 years ago • 1 comments

Sometimes, we call update_all on an object of ActiveRelation (which also orders the record by id), but the parser cannot get the correct information from the sql if it includes order by in the query. E.g User.where("created_at < ?", Time.now.utc + 1.day).order(:id).update_all(created_at: Time.now.utc).

How to reproduce:

[7] pry(main)> parser = GDA::SQL::Parser.new
=> #<GDA::SQL::Parser:0x0000000124ad5168>
[8] pry(main)> sql = "update users set users.created_at = '2023-01-09 20:19:21.793637' where users.is_not_deleted = true and (created_at < '2023-01-10 20:19:21.723448') order by users.id asc"
=> "update users set users.created_at = '2023-01-09 20:19:21.793637' where users.is_not_deleted = true and (created_at < '2023-01-10 20:19:21.723448') order by users.id asc"
[9] pry(main)> parser.parse(sql).ast
=> #<GDA::Nodes::Unknown:0x0000000124afc060>

This returns Update node type

If I remove order by users.id asc, it works:

[11] pry(main)> sql = "update users set users.created_at = '2023-01-09 20:19:21.793637' where users.is_not_deleted = true and (created_at < '2023-01-10 20:19:21.723448')"
=> "update users set users.created_at = '2023-01-09 20:19:21.793637' where users.is_not_deleted = true and (created_at < '2023-01-10 20:19:21.723448')"
[12] pry(main)> parser.parse(sql).ast
=> #<GDA::Nodes::Update:0x0000000124ee5758>

hyhuang1218 avatar Jan 09 '23 20:01 hyhuang1218

I don't think order by is supported for update statements on SQLite3. What database are you using, and does this statement work on that database? That said, I'm not sure what order by is supposed to do on an update statement.

tenderlove avatar Mar 20 '23 19:03 tenderlove