Skip to content

Commit

Permalink
Revert "Define event_monitoring_live_v1 views in view.sql files (#…
Browse files Browse the repository at this point in the history
…4576)" (#4680)

This reverts commit 2c4cc5e.
  • Loading branch information
scholtzan authored Dec 11, 2023
1 parent 6e2e8f6 commit c31ae16
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
27 changes: 16 additions & 11 deletions bigquery_etl/view/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@

# Regex matching CREATE VIEW statement so it can be removed to get the view query
CREATE_VIEW_PATTERN = re.compile(
r"CREATE(?:\s+OR\s+REPLACE)?(?:\s+MATERIALIZED)?\s+VIEW(?:\s+IF\s+NOT\s+EXISTS)?\s+[^\s]+\s+AS",
re.IGNORECASE,
r"CREATE\s+OR\s+REPLACE\s+VIEW\s+[^\s]+\s+AS", re.IGNORECASE
)


Expand Down Expand Up @@ -185,16 +184,22 @@ def _valid_fully_qualified_references(self):

def _valid_view_naming(self):
"""Validate that the created view naming matches the directory structure."""
sql = sqlparse.format(self.content, strip_comments=True).strip()
if view_statement_match := re.match(
r"CREATE(?:\s+OR\s+REPLACE)?(?:\s+MATERIALIZED)?\s+VIEW(?:\s+IF\s+NOT\s+EXISTS)?"
r"\s+(?P<view_id>(?:(?:`?[\w-]+`?\.)?`?\w+`?\.)?`?\w+`?)",
sql,
re.IGNORECASE,
):
target_view = view_statement_match["view_id"].replace("`", "")
parsed = sqlparse.parse(self.content)[0]
tokens = [
t
for t in parsed.tokens
if not (t.is_whitespace or isinstance(t, sqlparse.sql.Comment))
]
is_view_statement = (
" ".join(tokens[0].normalized.split()) == "CREATE OR REPLACE"
and tokens[1].normalized == "VIEW"
)
if is_view_statement:
target_view = str(tokens[2]).strip().split()[0]
try:
[project_id, dataset_id, view_id] = target_view.split(".")
[project_id, dataset_id, view_id] = target_view.replace("`", "").split(
"."
)
if not (
self.name == view_id
and self.dataset == dataset_id
Expand Down
2 changes: 1 addition & 1 deletion bqetl_project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ dry_run:
- sql/moz-fx-data-shared-prod/org_mozilla_firefox_beta_derived/experiment_events_live_v1/init.sql
- sql/moz-fx-data-shared-prod/telemetry_derived/experiment_enrollment_cumulative_population_estimate_v1/view.sql
- sql/moz-fx-data-shared-prod/telemetry/experiment_enrollment_cumulative_population_estimate/view.sql
- sql/moz-fx-data-shared-prod/**/event_monitoring_live_v1/view.sql
- sql/moz-fx-data-shared-prod/**/event_monitoring_live_v1/init.sql
- sql/moz-fx-data-shared-prod/monitoring/event_monitoring_live/view.sql
# Already exists (and lacks an "OR REPLACE" clause)
- sql/moz-fx-data-shared-prod/org_mozilla_firefox_derived/clients_first_seen_v1/init.sql
Expand Down
29 changes: 15 additions & 14 deletions sql_generators/glean_usage/event_monitoring_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class EventMonitoringLive(GleanTable):

def __init__(self) -> None:
"""Initialize materialized view generation."""
self.no_init = True
self.no_init = False
self.per_app_id_enabled = True
self.per_app_enabled = False
self.across_apps_enabled = True
Expand All @@ -38,10 +38,9 @@ def __init__(self) -> None:
def generate_per_app_id(
self, project_id, baseline_table, output_dir=None, use_cloud_function=True, app_info=[]
):
"""Generate per-app_id views."""
tables = table_names_from_baseline(baseline_table, include_project_id=False)

view_filename = f"{self.target_table_id}.view.sql"
init_filename = f"{self.target_table_id}.init.sql"
metadata_filename = f"{self.target_table_id}.metadata.yaml"

table = tables[f"{self.prefix}"]
Expand Down Expand Up @@ -69,21 +68,23 @@ def generate_per_app_id(
Artifact = namedtuple("Artifact", "table_id basename sql")
artifacts = []

view_sql = render(
view_filename, template_folder=PATH / "templates", **render_kwargs
)
metadata = render(
metadata_filename,
template_folder=PATH / "templates",
format=False,
**render_kwargs,
)
artifacts.append(Artifact(table, "metadata.yaml", metadata))
if not self.no_init:
init_sql = render(
init_filename, template_folder=PATH / "templates", **render_kwargs
)
metadata = render(
metadata_filename,
template_folder=PATH / "templates",
format=False,
**render_kwargs,
)
artifacts.append(Artifact(table, "metadata.yaml", metadata))

skip_existing_artifact = self.skip_existing(output_dir, project_id)

if output_dir:
artifacts.append(Artifact(table, "view.sql", view_sql))
if not self.no_init:
artifacts.append(Artifact(table, "init.sql", init_sql))

for artifact in artifacts:
destination = (
Expand Down
3 changes: 1 addition & 2 deletions sql_generators/stable_views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ def write_view_if_not_exists(target_project: str, sql_dir: Path, schema: SchemaF
from sql_generators.stable_views import VIEW_METADATA_TEMPLATE, VIEW_QUERY_TEMPLATE

VIEW_CREATE_REGEX = re.compile(
r"CREATE(?:\s+OR\s+REPLACE)?(?:\s+MATERIALIZED)?\s+VIEW(?:\s+IF\s+NOT\s+EXISTS)?\s+[^\s]+\s+AS",
re.IGNORECASE,
r"CREATE OR REPLACE VIEW\n\s*[^\s]+\s*\nAS", re.IGNORECASE
)

SKIP_VIEW_SCHEMA = {
Expand Down

0 comments on commit c31ae16

Please sign in to comment.