{"action":"created","comment":{"url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/comments/38548655","id":38548655,"diff_hunk":"@@ -1025,3 +1030,109 @@ def visit_copy_command(element, compiler, **kw):\n         ),\n         **kw\n     )\n+\n+\n+def gen_columns_from_children(root):\n+    \"\"\"\n+    Generates columns that are being used in child elements of the delete query\n+    this will be used to determine tables for the using clause.\n+    :param root: the delete query\n+    :return: a generator of columns\n+    \"\"\"\n+    if isinstance(root, (Delete, BinaryExpression, BooleanClauseList)):\n+        for child in root.get_children():\n+            yc = gen_columns_from_children(child)\n+            for it in yc:\n+                yield it\n+    elif isinstance(root, sa.Column):\n+        yield root\n+\n+\n+@compiles(Delete, 'redshift')\n+def visit_delete_stmt(element, compiler, **kwargs):\n+    \"\"\"\n+    Adds redshift-dialect specific compilation rule for the\n+    delete statement.\n+\n+    Redshift DELETE syntax can be found here:\n+    http://docs.aws.amazon.com/redshift/latest/dg/r_DELETE.html\n+\n+    .. :code-block: sql\n+\n+        DELETE [ FROM ] table_name\n+        [ { USING } table_name, ...]\n+        [ WHERE condition ]\n+\n+    By default, SqlAlchemy compiles DELETE statements with the\n+    syntax:\n+\n+    .. :code-block: sql\n+\n+        DELETE [ FROM ] table_name\n+        [ WHERE condition ]\n+\n+    problem illustration:\n+\n+    >>> from sqlalchemy import Table, Column, Integer, MetaData, delete\n+    >>> from redshift_sqlalchemy.dialect import RedshiftDialect\n+    >>> meta = MetaData()\n+    >>> table1 = Table(\n+    ... 'table_1',\n+    ... meta,\n+    ... Column('pk', Integer, primary_key=True)\n+    ... )\n+    ...\n+    >>> table2 = Table(\n+    ... 'table_2',\n+    ... meta,\n+    ... Column('pk', Integer, primary_key=True)\n+    ... )\n+    ...\n+    >>> del_stmt = delete(table1).where(table1.c.pk==table2.c.pk)\n+    >>> str(del_stmt.compile(dialect=RedshiftDialect()))\n+    'DELETE FROM table_1 USING table_2 WHERE table_1.pk = table_2.pk'\n+    >>> str(del_stmt)\n+    'DELETE FROM table_1 WHERE table_1.pk = table_2.pk'\n+    >>> del_stmt2 = delete(table1)\n+    >>> str(del_stmt2)\n+    'DELETE FROM table_1'\n+    >>> del_stmt3 = delete(table1).where(table1.c.pk > 1000)\n+    >>> str(del_stmt3)\n+    'DELETE FROM table_1 WHERE table_1.pk > :pk_1'\n+    >>> str(del_stmt3.compile(dialect=RedshiftDialect()))\n+    'DELETE FROM table_1 WHERE table_1.pk >  %(pk_1)s'\n+    \"\"\"\n+\n+    # Set empty strings for the default where clause and using clause\n+    whereclause = ''\n+    usingclause = ''\n+\n+    # determine if the delete query needs a ``USING`` injected\n+    # by inspecting the whereclause's children & their children...\n+    # first, the where clause text is buit, if applicable\n+    # then, the using clause text is built, if applicable\n+    # note:\n+    #   the tables in the using clause are sorted in the order in\n+    #   which they first appear in the where clause.\n+    delete_stmt_table = compiler.preparer.format_table(element.table)\n+    whereclause_tuple = element.get_children()\n+    if whereclause_tuple:\n+        usingclause_tables = []\n+        whereclause = ' WHERE {clause}'.format(\n+            clause=compiler.process(*whereclause_tuple, **kwargs)\n+        )\n+\n+        whereclause_columns = gen_columns_from_children(element)\n+        for col in whereclause_columns:\n+            table = compiler.preparer.format_table(col.table)\n+            if table != delete_stmt_table and table not in usingclause_tables:\n+                usingclause_tables.append(table)","path":"redshift_sqlalchemy/dialect.py","position":114,"original_position":114,"commit_id":"1d7040ba063d5c4ba3e879b74f26e8db7a4cfca3","original_commit_id":"1d7040ba063d5c4ba3e879b74f26e8db7a4cfca3","user":{"login":"haleemur","id":6108231,"avatar_url":"https://avatars.githubusercontent.com/u/6108231?v=3","gravatar_id":"","url":"https://api.github.com/users/haleemur","html_url":"https://github.com/haleemur","followers_url":"https://api.github.com/users/haleemur/followers","following_url":"https://api.github.com/users/haleemur/following{/other_user}","gists_url":"https://api.github.com/users/haleemur/gists{/gist_id}","starred_url":"https://api.github.com/users/haleemur/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/haleemur/subscriptions","organizations_url":"https://api.github.com/users/haleemur/orgs","repos_url":"https://api.github.com/users/haleemur/repos","events_url":"https://api.github.com/users/haleemur/events{/privacy}","received_events_url":"https://api.github.com/users/haleemur/received_events","type":"User","site_admin":false},"body":"@graingert and I thought it best to preserve the order in which the tables first appear in the `WHERE` clause when building the `USING` clause. That's why I'd prefer to just use list and append new tables to it.","created_at":"2015-09-02T15:47:20Z","updated_at":"2015-09-02T15:47:20Z","html_url":"https://github.com/graingert/redshift_sqlalchemy/pull/36#discussion_r38548655","pull_request_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/36","_links":{"self":{"href":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/comments/38548655"},"html":{"href":"https://github.com/graingert/redshift_sqlalchemy/pull/36#discussion_r38548655"},"pull_request":{"href":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/36"}}},"pull_request":{"url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/36","id":44001138,"html_url":"https://github.com/graingert/redshift_sqlalchemy/pull/36","diff_url":"https://github.com/graingert/redshift_sqlalchemy/pull/36.diff","patch_url":"https://github.com/graingert/redshift_sqlalchemy/pull/36.patch","issue_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/issues/36","number":36,"state":"open","locked":false,"title":"Fix DELETE statement for Redshift Dialect","user":{"login":"haleemur","id":6108231,"avatar_url":"https://avatars.githubusercontent.com/u/6108231?v=3","gravatar_id":"","url":"https://api.github.com/users/haleemur","html_url":"https://github.com/haleemur","followers_url":"https://api.github.com/users/haleemur/followers","following_url":"https://api.github.com/users/haleemur/following{/other_user}","gists_url":"https://api.github.com/users/haleemur/gists{/gist_id}","starred_url":"https://api.github.com/users/haleemur/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/haleemur/subscriptions","organizations_url":"https://api.github.com/users/haleemur/orgs","repos_url":"https://api.github.com/users/haleemur/repos","events_url":"https://api.github.com/users/haleemur/events{/privacy}","received_events_url":"https://api.github.com/users/haleemur/received_events","type":"User","site_admin":false},"body":"Pull request to solve Issue: [Fix DELETE statement for Redshift Dialect](https://github.com/graingert/redshift_sqlalchemy/issues/35).\r\n\r\nAdds support so that the following sqlalchemy query\r\n\r\n    stmt = delete(table1).where(table1.c.id==table2.c.id)\r\n\r\nis compiled as \r\n\r\n    DELETE FROM table1\r\n    USING table2\r\n    WHERE table1.id = table2.id\r\n\r\ninstead of \r\n\r\n    DELETE FROM table1\r\n    WHERE table1.id = table2.id\r\n","created_at":"2015-09-02T14:51:39Z","updated_at":"2015-09-02T15:47:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"68fa26904d0492a449ad65fdbd0bc5bd241ab2e8","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/36/commits","review_comments_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/36/comments","review_comment_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/comments{/number}","comments_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/issues/36/comments","statuses_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/statuses/1d7040ba063d5c4ba3e879b74f26e8db7a4cfca3","head":{"label":"haleemur:fix_redshift_delete_stmt","ref":"fix_redshift_delete_stmt","sha":"1d7040ba063d5c4ba3e879b74f26e8db7a4cfca3","user":{"login":"haleemur","id":6108231,"avatar_url":"https://avatars.githubusercontent.com/u/6108231?v=3","gravatar_id":"","url":"https://api.github.com/users/haleemur","html_url":"https://github.com/haleemur","followers_url":"https://api.github.com/users/haleemur/followers","following_url":"https://api.github.com/users/haleemur/following{/other_user}","gists_url":"https://api.github.com/users/haleemur/gists{/gist_id}","starred_url":"https://api.github.com/users/haleemur/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/haleemur/subscriptions","organizations_url":"https://api.github.com/users/haleemur/orgs","repos_url":"https://api.github.com/users/haleemur/repos","events_url":"https://api.github.com/users/haleemur/events{/privacy}","received_events_url":"https://api.github.com/users/haleemur/received_events","type":"User","site_admin":false},"repo":{"id":41505263,"name":"redshift_sqlalchemy","full_name":"haleemur/redshift_sqlalchemy","owner":{"login":"haleemur","id":6108231,"avatar_url":"https://avatars.githubusercontent.com/u/6108231?v=3","gravatar_id":"","url":"https://api.github.com/users/haleemur","html_url":"https://github.com/haleemur","followers_url":"https://api.github.com/users/haleemur/followers","following_url":"https://api.github.com/users/haleemur/following{/other_user}","gists_url":"https://api.github.com/users/haleemur/gists{/gist_id}","starred_url":"https://api.github.com/users/haleemur/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/haleemur/subscriptions","organizations_url":"https://api.github.com/users/haleemur/orgs","repos_url":"https://api.github.com/users/haleemur/repos","events_url":"https://api.github.com/users/haleemur/events{/privacy}","received_events_url":"https://api.github.com/users/haleemur/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/haleemur/redshift_sqlalchemy","description":"Amazon Redshift SQLAlchemy Dialect","fork":true,"url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy","forks_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/forks","keys_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/teams","hooks_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/hooks","issue_events_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/issues/events{/number}","events_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/events","assignees_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/assignees{/user}","branches_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/branches{/branch}","tags_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/tags","blobs_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/git/refs{/sha}","trees_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/statuses/{sha}","languages_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/languages","stargazers_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/stargazers","contributors_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/contributors","subscribers_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/subscribers","subscription_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/subscription","commits_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/commits{/sha}","git_commits_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/git/commits{/sha}","comments_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/comments{/number}","issue_comment_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/issues/comments{/number}","contents_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/contents/{+path}","compare_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/merges","archive_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/downloads","issues_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/issues{/number}","pulls_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/pulls{/number}","milestones_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/milestones{/number}","notifications_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/labels{/name}","releases_url":"https://api.github.com/repos/haleemur/redshift_sqlalchemy/releases{/id}","created_at":"2015-08-27T18:53:25Z","updated_at":"2015-08-27T18:53:26Z","pushed_at":"2015-09-02T15:00:08Z","git_url":"git://github.com/haleemur/redshift_sqlalchemy.git","ssh_url":"git@github.com:haleemur/redshift_sqlalchemy.git","clone_url":"https://github.com/haleemur/redshift_sqlalchemy.git","svn_url":"https://github.com/haleemur/redshift_sqlalchemy","homepage":null,"size":275,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"graingert:master","ref":"master","sha":"be53c70fbf5ac6e25175e5a7642e08111d00b32d","user":{"login":"graingert","id":413772,"avatar_url":"https://avatars.githubusercontent.com/u/413772?v=3","gravatar_id":"","url":"https://api.github.com/users/graingert","html_url":"https://github.com/graingert","followers_url":"https://api.github.com/users/graingert/followers","following_url":"https://api.github.com/users/graingert/following{/other_user}","gists_url":"https://api.github.com/users/graingert/gists{/gist_id}","starred_url":"https://api.github.com/users/graingert/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/graingert/subscriptions","organizations_url":"https://api.github.com/users/graingert/orgs","repos_url":"https://api.github.com/users/graingert/repos","events_url":"https://api.github.com/users/graingert/events{/privacy}","received_events_url":"https://api.github.com/users/graingert/received_events","type":"User","site_admin":false},"repo":{"id":35280897,"name":"redshift_sqlalchemy","full_name":"graingert/redshift_sqlalchemy","owner":{"login":"graingert","id":413772,"avatar_url":"https://avatars.githubusercontent.com/u/413772?v=3","gravatar_id":"","url":"https://api.github.com/users/graingert","html_url":"https://github.com/graingert","followers_url":"https://api.github.com/users/graingert/followers","following_url":"https://api.github.com/users/graingert/following{/other_user}","gists_url":"https://api.github.com/users/graingert/gists{/gist_id}","starred_url":"https://api.github.com/users/graingert/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/graingert/subscriptions","organizations_url":"https://api.github.com/users/graingert/orgs","repos_url":"https://api.github.com/users/graingert/repos","events_url":"https://api.github.com/users/graingert/events{/privacy}","received_events_url":"https://api.github.com/users/graingert/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/graingert/redshift_sqlalchemy","description":"Amazon Redshift SQLAlchemy Dialect","fork":true,"url":"https://api.github.com/repos/graingert/redshift_sqlalchemy","forks_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/forks","keys_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/teams","hooks_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/hooks","issue_events_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/issues/events{/number}","events_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/events","assignees_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/assignees{/user}","branches_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/branches{/branch}","tags_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/tags","blobs_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/git/refs{/sha}","trees_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/statuses/{sha}","languages_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/languages","stargazers_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/stargazers","contributors_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/contributors","subscribers_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/subscribers","subscription_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/subscription","commits_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/commits{/sha}","git_commits_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/git/commits{/sha}","comments_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/comments{/number}","issue_comment_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/issues/comments{/number}","contents_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/contents/{+path}","compare_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/merges","archive_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/downloads","issues_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/issues{/number}","pulls_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls{/number}","milestones_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/milestones{/number}","notifications_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/labels{/name}","releases_url":"https://api.github.com/repos/graingert/redshift_sqlalchemy/releases{/id}","created_at":"2015-05-08T13:29:24Z","updated_at":"2015-08-25T20:16:20Z","pushed_at":"2015-09-02T15:00:09Z","git_url":"git://github.com/graingert/redshift_sqlalchemy.git","ssh_url":"git@github.com:graingert/redshift_sqlalchemy.git","clone_url":"https://github.com/graingert/redshift_sqlalchemy.git","svn_url":"https://github.com/graingert/redshift_sqlalchemy","homepage":null,"size":734,"stargazers_count":4,"watchers_count":4,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":8,"forks":3,"open_issues":8,"watchers":4,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/36"},"html":{"href":"https://github.com/graingert/redshift_sqlalchemy/pull/36"},"issue":{"href":"https://api.github.com/repos/graingert/redshift_sqlalchemy/issues/36"},"comments":{"href":"https://api.github.com/repos/graingert/redshift_sqlalchemy/issues/36/comments"},"review_comments":{"href":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/36/comments"},"review_comment":{"href":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/graingert/redshift_sqlalchemy/pulls/36/commits"},"statuses":{"href":"https://api.github.com/repos/graingert/redshift_sqlalchemy/statuses/1d7040ba063d5c4ba3e879b74f26e8db7a4cfca3"}}}}