Skip to content

Commit

Permalink
Merge "tp: SelectColumn.alias can be optional" into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Treehugger Robot authored and Gerrit Code Review committed Jan 23, 2025
2 parents b311b40 + d9fda54 commit a829742
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
3 changes: 2 additions & 1 deletion protos/perfetto/perfetto_sql/structured_query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ message PerfettoSqlStructuredQuery {
// The existing name of the column from the source. Required.
optional string column_name = 1;

// The new name of the column. Required.
// The new name of the column. If not set, the name of the column is
// `column_name`. Optional.
optional string alias = 2;
}
repeated SelectColumn select_columns = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,16 @@ base::StatusOr<std::string> GeneratorImpl::SelectColumnsNoAggregates(
}
std::string sql;
for (auto it = select_columns; it; ++it) {
StructuredQuery::SelectColumn::Decoder alias(*it);
StructuredQuery::SelectColumn::Decoder column(*it);
if (!sql.empty()) {
sql += ", ";
}
sql += alias.column_name().ToStdString() + " AS " +
alias.alias().ToStdString();
if (column.has_alias()) {
sql += column.column_name().ToStdString() + " AS " +
column.alias().ToStdString();
} else {
sql += column.column_name().ToStdString();
}
}
return sql;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,36 @@ TEST(StructuredQueryGeneratorTest, Smoke2) {
"linux.memory.process", "slices.slices"));
}

TEST(StructuredQueryGeneratorTest, ColumnSelection) {
StructuredQueryGenerator gen;
auto proto = ToProto(R"(
id: "table_source_thread_slice"
table: {
table_name: "thread_slice"
module_name: "slices.with_context"
column_names: "id"
column_names: "ts"
column_names: "dur"
}
select_columns: {column_name: "id"}
select_columns: {
column_name: "dur"
alias: "cheese"
}
select_columns: {column_name: "ts"}
)");
auto ret = gen.Generate(proto.data(), proto.size());
ASSERT_OK_AND_ASSIGN(std::string res, ret);
ASSERT_THAT(res.c_str(), EqualsIgnoringWhitespace(R"(
WITH sq_table_source_thread_slice AS
(SELECT
id,
dur AS cheese,
ts
FROM thread_slice)
SELECT * FROM sq_table_source_thread_slice
)"));
}

} // namespace
} // namespace perfetto::trace_processor::perfetto_sql::generator

0 comments on commit a829742

Please sign in to comment.