Skip to content

Commit

Permalink
refactor EventTimeFilter.render to _render_event_time_filtered
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Sep 5, 2024
1 parent e00e4a4 commit 62df5fe
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,6 @@ class EventTimeFilter(FakeAPIObject, Hashable):
start: Optional[datetime] = None
end: Optional[datetime] = None

def render(self) -> str:
"""
Returns "" if start and end are both None
"""
filter = ""
if self.start and self.end:
filter = f"{self.field_name} >= '{self.start}' and {self.field_name} < '{self.end}'"
elif self.start:
filter = f"{self.field_name} >= '{self.start}'"
elif self.end:
filter = f"{self.field_name} < '{self.end}'"

return filter

def __hash__(self) -> int:
return hash(self.render())

Expand Down Expand Up @@ -261,12 +247,26 @@ def render_event_time_filtered(self, rendered: Optional[str] = None) -> str:
if self.event_time_filter is None:
return rendered

filter = self.event_time_filter.render()
filter = self._render_event_time_filtered(self.event_time_filter)
if not filter:
return rendered

return f"(select * from {rendered} where {filter}){self._render_subquery_alias(namespace='et_filter')}"

def _render_event_time_filtered(self, event_time_filter: EventTimeFilter) -> str:
"""
Returns "" if start and end are both None
"""
filter = ""
if event_time_filter.start and event_time_filter.end:
filter = f"{event_time_filter.field_name} >= '{event_time_filter.start}' and {event_time_filter.field_name} < '{event_time_filter.end}'"
elif event_time_filter.start:
filter = f"{event_time_filter.field_name} >= '{event_time_filter.start}'"
elif event_time_filter.end:
filter = f"{event_time_filter.field_name} < '{event_time_filter.end}'"

return filter

def quoted(self, identifier):
return "{quote_char}{identifier}{quote_char}".format(
quote_char=self.quote_character,
Expand Down Expand Up @@ -361,6 +361,8 @@ def __hash__(self) -> int:
def __str__(self) -> str:
rendered = self.render() if self.limit is None else self.render_limited()

# Limited subquery is wrapped by the event time filter subquery, and not the other way around.
# This is because in the context of resolving limited refs, we care more about performance than reliably producing a sample of a certain size.
if self.event_time_filter:
rendered = self.render_event_time_filtered(rendered)

Expand Down

0 comments on commit 62df5fe

Please sign in to comment.