Add `with_cte_expression`
ClickHouse supports two different sorts of CTEs:
- Normal, subquery based CTEs supported by most DBMS
- Expression-based CTEs
This PR adds support for (2) by adding a new adapter-specific method. Note the SQL syntax is flipped for (2).
Implementation is a hack - let me know if there are better solutions!
👋 @macobo
If it's time-sensitive, I am happy to merge it but maybe as undocumented function/macro to avoid anyone outside of Plausible using it until we verify there is no other way to support this type of CTEs.
Not in a rush right now - I thought this might simplify a task I'm working on, but I might be taking another approach.
Small note for if you do end up reaching out to ecto developers: Keeping opts passed to with_cte in the same format, not validating for extra keys and making sure they get passed to connection.ex#cte would be ideal from an extensibility point of view. This would allow something like:
q
|> with_cte(fragment("123"), as: "foo", expression: true)
And the adapter can take care of the rest
Definitely! But I don't think this alone would be enough for this example to work, since Ecto would try to ensure that "foo" is a valid CTE and fragment("123") is a valid CTE name and right now it fails at compile time before the adapter receives anything. with_cte("foo", as: fragment("123"), expression: true) would probably work however.