preserve CTE from a SELECT when doing INSERT...SELECT

Issue #3418 resolved
Mike Bayer repo owner created an issue

related to #2551 but of lesser scope, if the select() passed to insert.from_select() has a cte() inside of it, we should render that inline:

    INSERT INTO data (id, data) WITH anon_1 AS
    (SELECT data2.id AS id, data2.data AS data
    FROM data2
    WHERE data2.data = 1)
     SELECT data2.id, data2.data
    FROM data2, anon_1
    WHERE data2.data = anon_1.data

PG supports the WITH both on top and under the INSERT, and they have different semantics: http://www.postgresql.org/docs/9.4/interactive/sql-insert.html "It is possible for the query (SELECT statement) to also contain a WITH clause. In such a case both sets of with_query can be referenced within the query, but the second one takes precedence since it is more closely nested.".

Comments (4)

  1. Mike Bayer reporter
    • Added official support for a CTE used by the SELECT present inside of :meth:.Insert.from_select. This behavior worked accidentally up until 0.9.9, when it no longer worked due to unrelated changes as part of 🎫3248. Note that this is the rendering of the WITH clause after the INSERT, before the SELECT; the full functionality of CTEs rendered at the top level of INSERT, UPDATE, DELETE is a new feature targeted for a later release. fixes #3418

    → <<cset eb1bb84fbc10>>

  2. Mike Bayer reporter
    • Added official support for a CTE used by the SELECT present inside of :meth:.Insert.from_select. This behavior worked accidentally up until 0.9.9, when it no longer worked due to unrelated changes as part of 🎫3248. Note that this is the rendering of the WITH clause after the INSERT, before the SELECT; the full functionality of CTEs rendered at the top level of INSERT, UPDATE, DELETE is a new feature targeted for a later release. fixes #3418

    (cherry picked from commit eb1bb84fbc10c801c7269a3d38c9e0235327857e)

    → <<cset 87c605e3dc8b>>

  3. Log in to comment