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

first pass: --sample #8337

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion core/dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections.abc import Hashable
from dataclasses import dataclass, field
from typing import Optional, TypeVar, Any, Type, Dict, Iterator, Tuple, Set
import uuid

from dbt.contracts.graph.nodes import SourceDefinition, ManifestNode, ResultNode, ParsedNode
from dbt.contracts.relation import (
Expand Down Expand Up @@ -35,6 +36,7 @@
include_policy: Policy = field(default_factory=lambda: Policy())
quote_policy: Policy = field(default_factory=lambda: Policy())
dbt_created: bool = False
sample: Optional[int] = None

def _is_exactish_match(self, field: ComponentName, value: str) -> bool:
if self.dbt_created and self.quote_policy.get_part(field) is False:
Expand Down Expand Up @@ -180,7 +182,11 @@

def render(self) -> str:
# if there is nothing set, this will return the empty string.
return ".".join(part for _, part in self._render_iterator() if part is not None)
rendered_parts = ".".join(part for _, part in self._render_iterator() if part is not None)
if self.sample and rendered_parts:
alias = f"_dbt_sample_{uuid.uuid4().hex.upper()[:6]}"
return f"(select * from {rendered_parts} limit {self.sample}) {alias}"

Check warning on line 188 in core/dbt/adapters/base/relation.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/relation.py#L187-L188

Added lines #L187 - L188 were not covered by tests
return rendered_parts

def quoted(self, identifier):
return "{quote_char}{identifier}{quote_char}".format(
Expand Down
2 changes: 2 additions & 0 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ def docs_serve(ctx, **kwargs):
@p.profile
@p.profiles_dir
@p.project_dir
@p.sample
@p.select
@p.selector
@p.inline
Expand Down Expand Up @@ -551,6 +552,7 @@ def parse(ctx, **kwargs):
@p.profile
@p.profiles_dir
@p.project_dir
@p.sample
@p.select
@p.selector
@p.state
Expand Down
9 changes: 9 additions & 0 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,15 @@
default=(),
)

sample = click.option(
"--sample",
envvar="DBT_SAMPLE",
help="Limit by sample rows when resolving dbt ref and sources.",
type=click.INT,
default=None,
)


model_decls = ("-m", "--models", "--model")
select_decls = ("-s", "--select")
select_attrs = {
Expand Down
10 changes: 7 additions & 3 deletions core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,13 @@
def create_relation(self, target_model: ManifestNode) -> RelationProxy:
if target_model.is_ephemeral_model:
self.model.set_cte(target_model.unique_id, None)
return self.Relation.create_ephemeral_from_node(self.config, target_model)
return self.Relation.create_ephemeral_from_node(

Check warning on line 534 in core/dbt/context/providers.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/providers.py#L534

Added line #L534 was not covered by tests
self.config, target_model, sample=self.config.args.sample
)
else:
return self.Relation.create_from(self.config, target_model)
return self.Relation.create_from(

Check warning on line 538 in core/dbt/context/providers.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/providers.py#L538

Added line #L538 was not covered by tests
self.config, target_model, sample=self.config.args.sample
)

def validate(
self,
Expand Down Expand Up @@ -590,7 +594,7 @@
target_kind="source",
disabled=(isinstance(target_source, Disabled)),
)
return self.Relation.create_from_source(target_source)
return self.Relation.create_from_source(target_source, sample=self.config.args.sample)

Check warning on line 597 in core/dbt/context/providers.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/providers.py#L597

Added line #L597 was not covered by tests


# metric` implementations
Expand Down
Loading