question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

custom data tests with CTEs fail

See original GitHub issue

background

Per @alittlesliceoftom’s Slack thread.

Steps to reproduce:

  1. write a data test that uses CTE clause,
  2. dbt wraps the data test’s query into another CTE.
  3. 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:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jtcohen6commented, Nov 25, 2020

@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.

0reactions
dataderscommented, Nov 30, 2021

close by #167

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found