--- /dev/null
+.. change::
+ :tags: bug, sql
+ :tickets: 6752
+
+ Fix issue in :class:`_sql.CTE` where new :meth:`_sql.HasCTE.add_cte` method
+ added in version 1.4.21 / :ticket:`6752` failed to function correctly for
+ "compound select" structures such as :func:`_sql.union`,
+ :func:`_sql.union_all`, :func:`_sql.except`, etc. Pull request courtesy
+ Eric Masseran.
\ No newline at end of file
if toplevel and not self.compile_state:
self.compile_state = compile_state
+ compound_stmt = compile_state.statement
+
entry = self._default_stack_entry if toplevel else self.stack[-1]
need_result_map = toplevel or (
not compound_index
}
)
+ if compound_stmt._independent_ctes:
+ for cte in compound_stmt._independent_ctes:
+ cte._compiler_dispatch(self, **kwargs)
+
keyword = self.compound_keywords.get(cs.keyword)
text = (" " + keyword + " ").join(
checkparams={"param_1": 10, "price_1": 50, "price_2": 45},
)
+ def test_compound_select_uses_independent_cte(self):
+ products = table("products", column("id"), column("price"))
+
+ upd_cte = (
+ products.update().values(price=10).where(products.c.price > 50)
+ ).cte()
+
+ stmt = (
+ products.select()
+ .where(products.c.price < 45)
+ .union(products.select().where(products.c.price > 90))
+ .add_cte(upd_cte)
+ )
+
+ self.assert_compile(
+ stmt,
+ "WITH anon_1 AS (UPDATE products SET price=:param_1 "
+ "WHERE products.price > :price_1) "
+ "SELECT products.id, products.price "
+ "FROM products WHERE products.price < :price_2 "
+ "UNION "
+ "SELECT products.id, products.price "
+ "FROM products WHERE products.price > :price_3",
+ checkparams={
+ "param_1": 10,
+ "price_1": 50,
+ "price_2": 45,
+ "price_3": 90,
+ },
+ )
+
def test_insert_uses_independent_cte(self):
products = table("products", column("id"), column("price"))