Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbt data tests error with 'with' clause usage #26

Closed
wongio opened this issue Jun 3, 2020 · 9 comments · Fixed by #167
Closed

dbt data tests error with 'with' clause usage #26

wongio opened this issue Jun 3, 2020 · 9 comments · Fixed by #167

Comments

@wongio
Copy link

wongio commented Jun 3, 2020

-- assert count of rows

with cte as (
select count(distinct([category])) as cnt
from table
)
select cnt
from cte
where cnt <> 2

('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near the keyword 'with'. (156) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon. (319); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near ')'. (102)")

I then add a semi-colon before the with and I get this error.

('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near ';'. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near ')'. (102)")

Any thoughts on why this is occurring?

@wongio
Copy link
Author

wongio commented Jun 4, 2020

using dbt 0.15.2

If I don't put a with clause it works fine.

@b-per
Copy link

b-per commented Jul 1, 2020

I have the same error on my side, using dbt 0.15.2 as well.
WITH works fine in models but not in data tests

@b-per
Copy link

b-per commented Jul 1, 2020

I updated the connections.py file as per this commit and it is showing me the query that is failing.

The problem seems to be that dbt uses a different function to run data tests (here) and the SQL query that is called is in the form of:

select count(*) as errors from (
<query for the test>
) sbq

And SQL Server throws errors with CTEs if the WITH statement is not the first one in the query. So fixing this issue would require changing things in the dbt code base or making dbt-sqlserver call data tests differently than the way dbt does it.

As dbt-sqlserver is not officially supported I don't know if it would be useful to raise an issue in the dbt repo directly.

@yp-s19tech
Copy link

Also experiencing the same issue

@mikaelene
Copy link
Collaborator

Hi all, this is a known limitation for me. I also don’t know how important it is to fix? Can you use a sub query instead?

There are some issues with the sql server dialect that makes it hard to create this adapter. The most annoying is that you can’t use ‘create table as’. Like in all other dbs. Right now there is an issue in dbt core to make it possible to implement tests at all in versions greater then v0.15.x. Which will make it possible to upgrade this adapter along with dbt.

If no one objects, I am prioritising that first and the possibility’s for test with ‘with’ later.

@yp-s19tech
Copy link

@mikaelene I managed to work around the issue by using a subquery, at the cost of readability:

select my_subquery.COL_A, my_subquery.a_counts from (select COL_A, count(*) as a_counts from {{ ref('FIRST_TBL') }} group by COL_A ) as my_subquery WHERE my_subquery.a_counts = 0

@ghost
Copy link

ghost commented Mar 2, 2021

It would probably be possible to support WITH by creating a view, running the view and then dropping the view again, but that would require to run multiple SQL queries or to build everything into a procedure returning results of a temporary table ...

dataders added a commit to dataders/dbt-ml-preprocessing that referenced this issue Mar 12, 2021
I can't use this macro within a CTE because TSQL doesn't let you have nested CTEs (see dbt-msft/dbt-sqlserver#26 and https://github.com/dbt-msft/dbt-synapse/issues/25)
jamesweakley pushed a commit to omnata-labs/dbt-ml-preprocessing that referenced this issue Mar 18, 2021
I can't use this macro within a CTE because TSQL doesn't let you have nested CTEs (see dbt-msft/dbt-sqlserver#26 and https://github.com/dbt-msft/dbt-synapse/issues/25)
@dataders
Copy link
Collaborator

fixed by #167!

@dodangquyen22
Copy link

Hi all, I have same issue. I using dbt test and get this error: "Incorrect syntax near the keyword 'with'" . Env i use: Python 3.12, dbt-sqlserver 1.8.4. I would like to know how to solve it, thanks everyone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants