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

[V1.3.latest] Added ability OPTION clause when creating a table #460

Open
wants to merge 3 commits into
base: v1.3.latest
Choose a base branch
from

Conversation

djagoda881
Copy link

@djagoda881 djagoda881 commented Dec 15, 2023

In some cases, users need to use the OPTION() clause in their dbt models that appears in SQL Server. The problem is that dbt does not provide this capability. A good reproducing example is the recursive code below, fetching data from the database.
test table calendar containing a wide range of dates
image

WITH cte
  AS (SELECT [date]
        FROM calendar
       WHERE DATE = '2020-01-01'
      UNION ALL
      SELECT DATEADD(day, 1, [date]) as [date]
        FROM cte
       WHERE DATEADD(day, 1, [date]) 
       < '2021-01-01'
    --    < '2020-02-28'
       )
SELECT *
  FROM cte
  OPTION (MAXRECURSION 0);

In the case of the condition < '2021-01-01', the recursion exceeds 100. Therefore, if you do not use the OPTION clause (MAXRECURSION 0), in this scenario, the recursion will stop at 100, not retrieving all the data in the query. However, in the case of table creation, an error will occur. MAXRECURSIONis just one example; there are many more options.

The use of the option clause in dbt. Below, I'm providing code that demonstrates the use of the option parameter in dbt.

{{
    config({
        "materialized": 'table',
        "option_clause": "MAXRECURSION 0"
    })

}}
WITH cte
  AS (SELECT [date]
        FROM calendar
       WHERE DATE = '2020-01-01'
      UNION ALL
      SELECT DATEADD(day, 1, [date]) as [date]
        FROM cte
       WHERE DATEADD(day, 1, [date]) 
    < '2021-01-01'
    -- < '2020-02-28'
       )
SELECT *
  FROM cte

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 this pull request may close these issues.

None yet

1 participant