From 0060b1581caab719828da13fb02cd8e3fa3ab466 Mon Sep 17 00:00:00 2001 From: Anna Mayzner Date: Fri, 16 Aug 2024 12:06:48 +0000 Subject: [PATCH] stdlib: Check banned DROP in stdlib Change-Id: I81a717434f96353c545777d07167c89d7984cc2f --- python/generators/sql_processing/utils.py | 15 +++++++++++++-- .../perfetto_sql/stdlib/chrome/histograms.sql | 2 -- tools/check_sql_modules.py | 11 +++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/python/generators/sql_processing/utils.py b/python/generators/sql_processing/utils.py index d8408d7545..a265cc9b32 100644 --- a/python/generators/sql_processing/utils.py +++ b/python/generators/sql_processing/utils.py @@ -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_\.]*\*;') @@ -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 = [] diff --git a/src/trace_processor/perfetto_sql/stdlib/chrome/histograms.sql b/src/trace_processor/perfetto_sql/stdlib/chrome/histograms.sql index b7bb5256aa..6aded778b6 100644 --- a/src/trace_processor/perfetto_sql/stdlib/chrome/histograms.sql +++ b/src/trace_processor/perfetto_sql/stdlib/chrome/histograms.sql @@ -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( diff --git a/tools/check_sql_modules.py b/tools/check_sql_modules.py index 77656976f9..5d1f7141e5 100755 --- a/tools/check_sql_modules.py +++ b/tools/check_sql_modules.py @@ -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 @@ -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))