Skip to content

Commit

Permalink
first pass: --sample
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Nov 1, 2023
1 parent bb21403 commit 8c99a48
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
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, Union, FrozenSet
import uuid

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

# register relation types that can be renamed for the purpose of replacing relations using stages and backups
# adding a relation type here also requires defining the associated rename macro
Expand Down Expand Up @@ -192,7 +194,11 @@ def _render_iterator(self) -> Iterator[Tuple[Optional[ComponentName], Optional[s

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 200 in core/dbt/adapters/base/relation.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/adapters/base/relation.py#L199-L200

Added lines #L199 - L200 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 @@ -342,6 +342,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 @@ -599,6 +600,7 @@ def parse(ctx, **kwargs):
@p.profile
@p.profiles_dir
@p.project_dir
@p.sample
@p.select
@p.selector
@p.state
Expand Down
8 changes: 8 additions & 0 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,14 @@
hidden=True,
)

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 resolve(
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 @@ def resolve(self, source_name: str, table_name: str):
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

0 comments on commit 8c99a48

Please sign in to comment.