custom data tests with CTEs fail
See original GitHub issuebackground
Per @alittlesliceoftom’s Slack thread.
Steps to reproduce:
- write a data test that uses CTE clause,
- dbt wraps the data test’s query into another CTE.
- Synapse returns an error because it does not allow for nested CTEs.
Example
tests/example.sql
WITH cte_test AS (
SELECT * FROM {{ref('interesting_moments')}}
)
select TOP 0 * FROM cte_test
target/compiled/ava_its_data/tests/example.sql
This doesn’t work as Synapse does not allow a single WITH
clause inside of
with dbt__CTE__INTERNAL_test as (
WITH cte_test AS (
SELECT * FROM "june_sql_pool"."ajs_mkt"."interesting_moments"
)
select TOP 0 * FROM cte_test
)select count(*) from dbt__CTE__INTERNAL_test
potential workarounds
haven’t thought much about the best solution yet…
add the wrapped CTE to the end of the CTE chain?
Synapse doesn’t support nested CTEs, but it does supported “chained referencing” of CTEs inside a single WITH clause. So the example below works, but I don’t know how I would conditionally do this only when the data test has a CTE
WITH
cte_test AS (
SELECT * FROM "june_sql_pool"."ajs_mkt"."interesting_moments"
),
dbt__CTE__INTERNAL_test AS (
select TOP 0 * FROM cte_test
)
select count(*) from dbt__CTE__INTERNAL_test
create and dispose of a view/ temp table instead of a CTE?
CREATE VIEW dbt__CTE__INTERNAL_test as
WITH cte_test AS (
SELECT * FROM "june_sql_pool"."ajs_mkt"."interesting_moments"
)
select TOP 0 * FROM cte_test;
select count(*) from dbt__CTE__INTERNAL_test
DROP VIEW dbt__CTE__INTERNAL_test
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
custom data tests with CTEs fail · Issue #172 - GitHub
write a data test that uses CTE clause,; dbt wraps the data test's query into another CTE. Synapse returns an error because it...
Read more >Use Common Table Expressions (CTE) to Keep Your SQL ...
Let's take an example using data in the demo schema of Mode's Public Warehouse, which involves a hypothetical paper company.
Read more >Inserts and Updates with CTEs in SQL Server (Common Table ...
We can quickly create insert and update statements with common table expressions and organize our data easily.
Read more >Create custom tests or override defaults - YouTube
One such way is to create custom schema tests to tests your models, as well as override the default tests such as not_null...
Read more >Solved: Custom query with CTE not supported (bug?)
Solved: Is creating a custom query using a CTE officially not supported or am I just duing it wrong? It works in Query...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@swanderz I had a chance to mess around with this, check out dbt-msft/dbt-synapse#29. I tried implementing your second idea for potential workarounds.
close by #167