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

Inline limit in SQL sent from dbt show #8641

Merged
merged 10 commits into from
Sep 27, 2023
Merged

Conversation

MichelleArk
Copy link
Contributor

@MichelleArk MichelleArk commented Sep 13, 2023

resolves #8496
resolves #8417

Problem

Solution

Adapter zone tests:

Checklist

  • I have read the contributing guide and understand what's expected of me
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • This PR has no interface changes (e.g. macros, cli, logs, json artifacts, config files, adapter interface, etc) or this PR has already received feedback and approval from Product or DX

@cla-bot cla-bot bot added the cla:yes label Sep 13, 2023
@github-actions
Copy link
Contributor

Thank you for your pull request! We could not find a changelog entry for this change. For details on how to document a change, see the contributing guide.

@codecov
Copy link

codecov bot commented Sep 13, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.01% ⚠️

Comparison is base (5182e3c) 86.61% compared to head (81f68e6) 86.60%.
Report is 13 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8641      +/-   ##
==========================================
- Coverage   86.61%   86.60%   -0.01%     
==========================================
  Files         175      176       +1     
  Lines       25595    25675      +80     
==========================================
+ Hits        22168    22235      +67     
- Misses       3427     3440      +13     
Flag Coverage Δ
integration 83.35% <100.00%> (-0.11%) ⬇️
unit 65.15% <33.33%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
core/dbt/task/show.py 100.00% <100.00%> (+1.81%) ⬆️

... and 26 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MichelleArk MichelleArk mentioned this pull request Sep 13, 2023
4 tasks
@MichelleArk MichelleArk changed the title first pass: add limit in show sql Inline limit in SQL sent from dbt show Sep 18, 2023
Copy link
Contributor

@jtcohen6 jtcohen6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really happy about this change! It hews closer to our existing patterns, and so it's easier to reason about, and would be easier to extend with more things (beyond limit + sql_header)

I left two quick comments about the show macro - feel free to take 'em or leave 'em

core/dbt/include/global_project/macros/adapters/show.sql Outdated Show resolved Hide resolved
Copy link
Member

@aranke aranke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, few nits

select *
from (
{{ sql }}
) as model_limit_subq
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: do we need the alias here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep! here's what happens without:

Runtime Error
Database Error in sql_operation inline_query (from remote system.sql)
subquery in FROM must have an alias
LINE 5: from (
^
HINT: For example, FROM (SELECT ...) [AS] foo.

{%- set sql_header = sql_header -%}
{{ sql_header if sql_header is not none }}
{%- if limit is not none -%}
{{ get_limit_subquery_sql(compiled_code, limit) }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens here if limit = -1 (get all rows)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adapter_response, execute_result = self.adapter.execute(
compiled_node.compiled_code, fetch=True, limit=limit
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no longer passing limit through self.adapter.execute or making use of fetchmany, so closing #8471 as part of this PR as well

Copy link
Contributor

@dataders dataders left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my spidey-sense tells me there's a potential UI footgun here, but the only thing I can think of is the one @colin-rogers-dbt surfaced (paraphrased):

the addition LIMIT at the outermost level is easiest, but not all query planners are created equal in that some will not propogate the LIMIT inwards. The result being that the query will still take as long to execute as without the limit.

Another thought was that not all databases support limit, e.g. TSQL and it's TOP function. That said, default__get_limit_subquery_sql(), seems easily overridable to support the different syntax. something like the below

{% macro sqlserver__get_limit_subquery_sql(sql, limit) %}
    select top {{ limit }} *
    from (
        {{ sql }}
    ) as model_limit_subq
{% endmacro %}

all of that is to say that this is certainly an improvement over the current state, and that we should endeavor to make this behavior as clear as possible to our users both in the CLI help docs as well as on our docs site

@MichelleArk MichelleArk added the user docs [docs.getdbt.com] Needs better documentation label Sep 25, 2023
@MichelleArk MichelleArk merged commit a2d4424 into main Sep 27, 2023
55 checks passed
@MichelleArk MichelleArk deleted the 8496/limit-in-show-query branch September 27, 2023 14:56
@FishtownBuildBot
Copy link
Collaborator

Opened a new issue in dbt-labs/docs.getdbt.com: dbt-labs/docs.getdbt.com#4141

@github-actions
Copy link
Contributor

The backport to 1.5.latest failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-1.5.latest 1.5.latest
# Navigate to the new working tree
cd .worktrees/backport-1.5.latest
# Create a new branch
git switch --create backport-8641-to-1.5.latest
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 a2d4424f922c2ba0f5884a5b39400b11901e165a
# Push it to GitHub
git push --set-upstream origin backport-8641-to-1.5.latest
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-1.5.latest

Then, create a pull request where the base branch is 1.5.latest and the compare/head branch is backport-8641-to-1.5.latest.

github-actions bot pushed a commit that referenced this pull request Sep 27, 2023
MichelleArk added a commit that referenced this pull request Sep 27, 2023
MichelleArk added a commit that referenced this pull request Sep 27, 2023
MichelleArk added a commit that referenced this pull request Sep 27, 2023
(cherry picked from commit a2d4424)

Co-authored-by: Michelle Ark <MichelleArk@users.noreply.github.com>
QMalcolm pushed a commit that referenced this pull request Oct 9, 2023
@aranke aranke mentioned this pull request Jul 12, 2024
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 1.5.latest backport 1.6.latest cla:yes user docs [docs.getdbt.com] Needs better documentation
Projects
None yet
5 participants