{"action":"created","issue":{"url":"https://api.github.com/repos/andialbrecht/sqlparse/issues/276","repository_url":"https://api.github.com/repos/andialbrecht/sqlparse","labels_url":"https://api.github.com/repos/andialbrecht/sqlparse/issues/276/labels{/name}","comments_url":"https://api.github.com/repos/andialbrecht/sqlparse/issues/276/comments","events_url":"https://api.github.com/repos/andialbrecht/sqlparse/issues/276/events","html_url":"https://github.com/andialbrecht/sqlparse/issues/276","id":167477021,"number":276,"title":"Subselects and comma craziness","user":{"login":"notpeter","id":145113,"avatar_url":"https://avatars.githubusercontent.com/u/145113?v=3","gravatar_id":"","url":"https://api.github.com/users/notpeter","html_url":"https://github.com/notpeter","followers_url":"https://api.github.com/users/notpeter/followers","following_url":"https://api.github.com/users/notpeter/following{/other_user}","gists_url":"https://api.github.com/users/notpeter/gists{/gist_id}","starred_url":"https://api.github.com/users/notpeter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/notpeter/subscriptions","organizations_url":"https://api.github.com/users/notpeter/orgs","repos_url":"https://api.github.com/users/notpeter/repos","events_url":"https://api.github.com/users/notpeter/events{/privacy}","received_events_url":"https://api.github.com/users/notpeter/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-07-25T22:05:07Z","updated_at":"2016-07-26T07:22:36Z","closed_at":null,"body":"I'm having some issues where subselects aren't handled properly.  Specifically, while sqlparse properly formats the query alone without, once it's placed inside a subselect things fall apart quickly...for example:\r\n![image](https://cloud.githubusercontent.com/assets/145113/17119135/0077657e-5279-11e6-98ef-e07f4aa46891.png)\r\n\r\nFull Query:\r\n````\r\nSELECT * FROM\r\n  (SELECT n.nspname,\r\n          c.relname,\r\n          a.attname,\r\n          a.atttypid,\r\n          a.attnotnull OR (t.typtype = 'd' AND t.typnotnull) AS attnotnull,\r\n          a.atttypmod,\r\n          a.attlen,\r\n          row_number() OVER (PARTITION BY a.attrelid ORDER BY a.attnum) AS attnum,\r\n          pg_catalog.pg_get_expr(def.adbin, def.adrelid) AS adsrc,\r\n          dsc.description,\r\n          t.typbasetype,\r\n          t.typtype\r\n   FROM pg_catalog.pg_namespace n\r\n   JOIN pg_catalog.pg_class c ON (c.relnamespace = n.oid)\r\n   JOIN pg_catalog.pg_attribute a ON (a.attrelid=c.oid)\r\n   JOIN pg_catalog.pg_type t ON (a.atttypid = t.oid)\r\n   LEFT JOIN pg_catalog.pg_attrdef def ON (a.attrelid=def.adrelid AND a.attnum = def.adnum)\r\n   LEFT JOIN pg_catalog.pg_description dsc ON (c.oid=dsc.objoid AND a.attnum = dsc.objsubid)\r\n   LEFT JOIN pg_catalog.pg_class dc ON (dc.oid=dsc.classoid AND dc.relname='pg_class')\r\n   LEFT JOIN pg_catalog.pg_namespace dn ON (dc.relnamespace=dn.oid AND dn.nspname='pg_catalog')\r\n   WHERE c.relkind IN ('r','v','f','m')\r\n         AND a.attnum > 0\r\n         AND NOT a.attisdropped\r\n         AND n.nspname LIKE 'public'\r\n         AND c.relname LIKE 'XXXXXXXXX') c\r\nWHERE\r\n   TRUE\r\nORDER BY\r\n   nspname,\r\n   c.relname,\r\n   attnum\r\n````\r\n\r\nFor anyone wondering, this is the query which the PostgreSQL JDBC drivers to return the list of foreign keys associated with a given table ('XXXXXXXX' above)."},"comment":{"url":"https://api.github.com/repos/andialbrecht/sqlparse/issues/comments/235183443","html_url":"https://github.com/andialbrecht/sqlparse/issues/276#issuecomment-235183443","issue_url":"https://api.github.com/repos/andialbrecht/sqlparse/issues/276","id":235183443,"user":{"login":"andialbrecht","id":25632,"avatar_url":"https://avatars.githubusercontent.com/u/25632?v=3","gravatar_id":"","url":"https://api.github.com/users/andialbrecht","html_url":"https://github.com/andialbrecht","followers_url":"https://api.github.com/users/andialbrecht/followers","following_url":"https://api.github.com/users/andialbrecht/following{/other_user}","gists_url":"https://api.github.com/users/andialbrecht/gists{/gist_id}","starred_url":"https://api.github.com/users/andialbrecht/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andialbrecht/subscriptions","organizations_url":"https://api.github.com/users/andialbrecht/orgs","repos_url":"https://api.github.com/users/andialbrecht/repos","events_url":"https://api.github.com/users/andialbrecht/events{/privacy}","received_events_url":"https://api.github.com/users/andialbrecht/received_events","type":"User","site_admin":false},"created_at":"2016-07-26T07:22:36Z","updated_at":"2016-07-26T07:22:36Z","body":"Which version of sqlparse are you using? This looks much like a bug that was already fixed. With the current version the formatted statement looks good:\r\n\r\n```\r\n>>> print(sqlparse.format(sql, reindent=True))\r\nSELECT *\r\nFROM\r\n  (SELECT n.nspname,\r\n          c.relname,\r\n          a.attname,\r\n          a.atttypid,\r\n          a.attnotnull\r\n   OR (t.typtype = 'd'\r\n       AND t.typnotnull) AS attnotnull,\r\n      a.atttypmod,\r\n      a.attlen,\r\n      row_number() OVER (PARTITION BY a.attrelid\r\n                         ORDER BY a.attnum) AS attnum,\r\n      pg_catalog.pg_get_expr(def.adbin, def.adrelid) AS adsrc,\r\n      dsc.description,\r\n      t.typbasetype,\r\n      t.typtype\r\n   FROM pg_catalog.pg_namespace n\r\n   JOIN pg_catalog.pg_class c ON (c.relnamespace = n.oid)\r\n   JOIN pg_catalog.pg_attribute a ON (a.attrelid=c.oid)\r\n   JOIN pg_catalog.pg_type t ON (a.atttypid = t.oid)\r\n   LEFT JOIN pg_catalog.pg_attrdef def ON (a.attrelid=def.adrelid\r\n                                           AND a.attnum = def.adnum)\r\n   LEFT JOIN pg_catalog.pg_description dsc ON (c.oid=dsc.objoid\r\n                                               AND a.attnum = dsc.objsubid)\r\n   LEFT JOIN pg_catalog.pg_class dc ON (dc.oid=dsc.classoid\r\n                                        AND dc.relname='pg_class')\r\n   LEFT JOIN pg_catalog.pg_namespace dn ON (dc.relnamespace=dn.oid\r\n                                            AND dn.nspname='pg_catalog')\r\n   WHERE c.relkind IN ('r',\r\n                       'v',\r\n                       'f',\r\n                       'm')\r\n     AND a.attnum > 0\r\n     AND NOT a.attisdropped\r\n     AND n.nspname LIKE 'public'\r\n     AND c.relname LIKE 'XXXXXXXXX') c\r\nWHERE TRUE\r\nORDER BY nspname,\r\n         c.relname,\r\n         attnum\r\n\r\n>>>\r\n```"}}
{"actor":{"display_login":"andialbrecht"}}