Skip to content

Commit

Permalink
tp: Add TableList as macro argument.
Browse files Browse the repository at this point in the history
Change-Id: Id8780ce6b6f2d15d8b621f2d91d31db58a8b5e7c
  • Loading branch information
aMayzner committed Jul 22, 2024
1 parent e127b95 commit b10f480
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -13338,6 +13338,7 @@ genrule {
"src/trace_processor/perfetto_sql/stdlib/linux/memory/process.sql",
"src/trace_processor/perfetto_sql/stdlib/linux/perf/samples.sql",
"src/trace_processor/perfetto_sql/stdlib/metasql/column_list.sql",
"src/trace_processor/perfetto_sql/stdlib/metasql/table_list.sql",
"src/trace_processor/perfetto_sql/stdlib/pkvm/hypervisor.sql",
"src/trace_processor/perfetto_sql/stdlib/prelude/casts.sql",
"src/trace_processor/perfetto_sql/stdlib/prelude/slices.sql",
Expand Down
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2776,6 +2776,7 @@ perfetto_filegroup(
name = "src_trace_processor_perfetto_sql_stdlib_metasql_metasql",
srcs = [
"src/trace_processor/perfetto_sql/stdlib/metasql/column_list.sql",
"src/trace_processor/perfetto_sql/stdlib/metasql/table_list.sql",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,14 @@ SqlSource RewriteToDummySql(const SqlSource& source) {
SqlSource::FromTraceProcessorImplementation("SELECT 0 WHERE 0"));
}

constexpr std::array<const char*, 6> kTokensAllowedInMacro({
constexpr std::array<const char*, 7> kTokensAllowedInMacro({
"_ColumnNameList",
"_Macro",
"_SqlFragment",
"_TableNameList",
"ColumnName",
"Expr",
"TableOrSubquery",
"_ColumnNameList",
"_SqlFragment",
"_Macro",
});

bool IsTokenAllowedInMacro(const std::string& view) {
Expand Down
5 changes: 4 additions & 1 deletion src/trace_processor/perfetto_sql/stdlib/metasql/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
import("../../../../../gn/perfetto_sql.gni")

perfetto_sql_source_set("metasql") {
sources = [ "column_list.sql" ]
sources = [
"column_list.sql",
"table_list.sql",
]
}
23 changes: 23 additions & 0 deletions src/trace_processor/perfetto_sql/stdlib/metasql/table_list.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--
-- Copyright 2024 The Android Open Source Project
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-- Given a list of table names, applies an arbitrary macro to each table
-- and joins the result with a comma.
CREATE PERFETTO MACRO _metasql_map_join_table_list(
tables _TableNameList,
map_macro _Macro
)
RETURNS _SqlFragment
AS __intrinsic_token_map_join!($tables, $map_macro, __intrinsic_token_comma!());
3 changes: 3 additions & 0 deletions test/trace_processor/diff_tests/include_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
from diff_tests.stdlib.linux.cpu import LinuxCpu
from diff_tests.stdlib.linux.memory import Memory
from diff_tests.stdlib.metasql.column_list import ColumnListTests
from diff_tests.stdlib.metasql.table_list import TableListTests
from diff_tests.stdlib.pkvm.tests import Pkvm
from diff_tests.stdlib.prelude.math_functions_tests import PreludeMathFunctions
from diff_tests.stdlib.prelude.pprof_functions_tests import PreludePprofFunctions
Expand Down Expand Up @@ -294,6 +295,8 @@ def fetch_all_diff_tests(index_path: str) -> List['testing.TestCase']:
'DynamicTables').fetch(),
*ColumnListTests(index_path, 'stdlib/column_list',
'ColumnListTests').fetch(),
*TableListTests(index_path, 'stdlib/table_list',
'TableListTests').fetch(),
*Memory(index_path, 'stdlib/linux', 'Memory').fetch(),
*PreludeMathFunctions(index_path, 'stdlib/prelude',
'PreludeMathFunctions').fetch(),
Expand Down
48 changes: 48 additions & 0 deletions test/trace_processor/diff_tests/stdlib/metasql/table_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
# Copyright (C) 2024 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License a
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from python.generators.diff_tests.testing import Csv, TextProto
from python.generators.diff_tests.testing import DiffTestBlueprint
from python.generators.diff_tests.testing import TestSuite


class TableListTests(TestSuite):

def test_table_list_result_columns(self):
return DiffTestBlueprint(
trace=TextProto(''),
query="""
INCLUDE PERFETTO MODULE metasql.table_list;
CREATE PERFETTO MACRO mac(t TableOrSubquery)
RETURNS TableOrSubquery AS
(SELECT * FROM $t);
WITH foo AS (
SELECT 0 AS a
),
bar AS (
SELECT 1 AS b
),
baz AS (
SELECT 2 AS c
)
SELECT a + b + c
FROM _metasql_map_join_table_list!((foo, bar, baz), mac);
""",
out=Csv("""
"a + b + c"
3
"""))

0 comments on commit b10f480

Please sign in to comment.