Skip to content

Commit

Permalink
Merge "stdlib: Check banned DROP in stdlib" into main
Browse files Browse the repository at this point in the history
  • Loading branch information
aMayzner authored and Gerrit Code Review committed Aug 16, 2024
2 parents d2e1d77 + 0060b15 commit 4c65a51
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
15 changes: 13 additions & 2 deletions python/generators/sql_processing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def update_pattern(pattern):

CREATE_VIEW_AS_PATTERN = update_pattern(fr'^CREATE VIEW ({NAME}) AS')

DROP_TABLE_VIEW_PATTERN = update_pattern(fr'^DROP (TABLE|VIEW) IF EXISTS '
fr'({NAME});$')
DROP_TABLE_VIEW_PATTERN = update_pattern(
fr'^DROP (VIEW|TABLE|INDEX) (?:IF EXISTS)? ({NAME});$')

INCLUDE_ALL_PATTERN = update_pattern(
fr'^INCLUDE PERFETTO MODULE [a-zA-Z0-9_\.]*\*;')
Expand Down Expand Up @@ -216,6 +216,17 @@ def check_banned_create_view_as(sql: str, filename: str) -> List[str]:
return errors


# Given SQL string check whether there is usage of DROP TABLE/VIEW/MACRO/INDEX.
def check_banned_drop(sql: str, filename: str) -> List[str]:
errors = []
for _, matches in match_pattern(DROP_TABLE_VIEW_PATTERN, sql).items():
sql_type = matches[0]
name = matches[2]
errors.append(f"Dropping object {sql_type} '{name}' is banned.\n"
f"Offending file: {filename}\n")
return errors


# Given SQL string check whether there is usage of CREATE VIEW {name} AS.
def check_banned_include_all(sql: str, filename: str) -> List[str]:
errors = []
Expand Down
2 changes: 0 additions & 2 deletions src/trace_processor/perfetto_sql/stdlib/chrome/histograms.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
-- Use of this source code is governed by a BSD-style license that can be
-- found in the LICENSE file.

DROP VIEW IF EXISTS chrome_histograms;

-- A helper view on top of the histogram events emitted by Chrome.
-- Requires "disabled-by-default-histogram_samples" Chrome category.
CREATE PERFETTO TABLE chrome_histograms(
Expand Down
11 changes: 7 additions & 4 deletions tools/check_sql_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from python.generators.sql_processing.utils import check_banned_create_table_as
from python.generators.sql_processing.utils import check_banned_create_view_as
from python.generators.sql_processing.utils import check_banned_words
from python.generators.sql_processing.utils import check_banned_drop
from python.generators.sql_processing.utils import check_banned_include_all


Expand Down Expand Up @@ -100,14 +101,16 @@ def main():
errors.append(f"INSERT INTO table is not allowed in standard library.\n"
f"Offending file: {path}\n")

filepath = path.split(ROOT_DIR)[1]

errors += parsed.errors
errors += check_banned_words(sql, path)
errors += check_banned_create_table_as(
sql,
path.split(ROOT_DIR)[1],
sql, filepath,
args.stdlib_sources.split(ROOT_DIR)[1])
errors += check_banned_create_view_as(sql, path.split(ROOT_DIR)[1])
errors += check_banned_include_all(sql, path.split(ROOT_DIR)[1])
errors += check_banned_create_view_as(sql, filepath)
errors += check_banned_include_all(sql, filepath)
errors += check_banned_drop(sql, filepath)

if errors:
sys.stderr.write("\n".join(errors))
Expand Down

0 comments on commit 4c65a51

Please sign in to comment.