|
| 1 | +// Copyright (C) 2024 The Android Open Source Project |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import m from 'mithril'; |
| 16 | +import {Icons} from '../../../../../base/semantic_icons'; |
| 17 | +import {MenuItem} from '../../../../../widgets/menu'; |
| 18 | +import {TableColumn, RenderedCell, TableManager} from '../table_column'; |
| 19 | +import {SqlTableState} from '../state'; |
| 20 | +import { |
| 21 | + PerfettoSqlType, |
| 22 | + PerfettoSqlTypes, |
| 23 | + typesEqual, |
| 24 | + underlyingSqlType, |
| 25 | +} from '../../../../../trace_processor/perfetto_sql_type'; |
| 26 | +import {SqlColumn, sqlColumnId, SqlExpression} from '../sql_column'; |
| 27 | +import {SqlValue} from '../../../../../trace_processor/query_result'; |
| 28 | +import {createTableColumn} from '../create_column'; |
| 29 | + |
| 30 | +type CastParams = { |
| 31 | + type: PerfettoSqlType; |
| 32 | +}; |
| 33 | + |
| 34 | +const CASTS = { |
| 35 | + int: { |
| 36 | + type: PerfettoSqlTypes.INT, |
| 37 | + }, |
| 38 | + double: { |
| 39 | + type: PerfettoSqlTypes.DOUBLE, |
| 40 | + }, |
| 41 | + string: { |
| 42 | + type: PerfettoSqlTypes.STRING, |
| 43 | + }, |
| 44 | + boolean: { |
| 45 | + type: PerfettoSqlTypes.BOOLEAN, |
| 46 | + }, |
| 47 | + timestamp: { |
| 48 | + type: PerfettoSqlTypes.TIMESTAMP, |
| 49 | + }, |
| 50 | + duration: { |
| 51 | + type: PerfettoSqlTypes.DURATION, |
| 52 | + }, |
| 53 | + slice_id: { |
| 54 | + type: { |
| 55 | + kind: 'joinid', |
| 56 | + source: { |
| 57 | + table: 'slice', |
| 58 | + column: 'id', |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + utid: { |
| 63 | + type: { |
| 64 | + kind: 'joinid', |
| 65 | + source: { |
| 66 | + table: 'thread', |
| 67 | + column: 'id', |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + upid: { |
| 72 | + type: { |
| 73 | + kind: 'joinid', |
| 74 | + source: { |
| 75 | + table: 'process', |
| 76 | + column: 'id', |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | +} satisfies Record<string, CastParams>; |
| 81 | + |
| 82 | +// CastColumn wraps another column and provides casting functionality |
| 83 | +export class CastColumn implements TableColumn { |
| 84 | + public readonly column: SqlColumn; |
| 85 | + constructor( |
| 86 | + public readonly wrappedColumn: TableColumn, |
| 87 | + public readonly sourceColumn: TableColumn, |
| 88 | + public readonly type: PerfettoSqlType | undefined, |
| 89 | + ) { |
| 90 | + this.column = wrappedColumn.column; |
| 91 | + } |
| 92 | + |
| 93 | + getTitle(): string | undefined { |
| 94 | + return this.wrappedColumn.getTitle?.(); |
| 95 | + } |
| 96 | + |
| 97 | + renderCell( |
| 98 | + value: SqlValue, |
| 99 | + tableManager?: TableManager, |
| 100 | + supportingValues?: {} | undefined, |
| 101 | + ): RenderedCell { |
| 102 | + // Delegate rendering to the appropriate column type based on the cast type |
| 103 | + // This allows proper formatting for timestamps, durations, etc. |
| 104 | + return this.wrappedColumn.renderCell(value, tableManager, supportingValues); |
| 105 | + } |
| 106 | + |
| 107 | + supportingColumns() { |
| 108 | + return this.wrappedColumn.supportingColumns?.() || (() => {}); |
| 109 | + } |
| 110 | + |
| 111 | + listDerivedColumns(manager: TableManager) { |
| 112 | + return this.wrappedColumn.listDerivedColumns?.(manager); |
| 113 | + } |
| 114 | + |
| 115 | + getColumnSpecificMenuItems(args: { |
| 116 | + replaceColumn: (column: TableColumn) => void; |
| 117 | + }): m.Children { |
| 118 | + return m(MenuItem, { |
| 119 | + label: 'Remove cast', |
| 120 | + icon: Icons.Undo, |
| 121 | + onclick: () => args.replaceColumn(this.sourceColumn), |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +export function renderCastColumnMenu( |
| 127 | + column: TableColumn, |
| 128 | + columnIndex: number, |
| 129 | + state: SqlTableState, |
| 130 | +): m.Children { |
| 131 | + return Object.entries(CASTS) |
| 132 | + .filter(([_, params]) => { |
| 133 | + if (column.type === undefined) { |
| 134 | + return true; |
| 135 | + } |
| 136 | + return !typesEqual(params.type, column.type); |
| 137 | + }) |
| 138 | + .map(([label, params]) => |
| 139 | + m(MenuItem, { |
| 140 | + label, |
| 141 | + onclick: () => { |
| 142 | + // If this is already a CastColumn, get the original source column. |
| 143 | + const columnToCast: TableColumn = |
| 144 | + column instanceof CastColumn ? column.sourceColumn : column; |
| 145 | + |
| 146 | + const castExpression = (() => { |
| 147 | + if ( |
| 148 | + columnToCast.type !== undefined && |
| 149 | + underlyingSqlType(columnToCast.type) === |
| 150 | + underlyingSqlType(params.type) |
| 151 | + ) { |
| 152 | + // If the underlying types are the same, there is no need for a SQL cast, we only need to reinterpret the data. |
| 153 | + return (cols: string[]) => cols[0]; |
| 154 | + } |
| 155 | + return (cols: string[]) => |
| 156 | + `CAST(${cols[0]} AS ${underlyingSqlType(params.type)})`; |
| 157 | + })(); |
| 158 | + |
| 159 | + // Create a CastColumn wrapping the source column |
| 160 | + const castColumn = new CastColumn( |
| 161 | + createTableColumn({ |
| 162 | + column: new SqlExpression( |
| 163 | + castExpression, |
| 164 | + [columnToCast.column], |
| 165 | + `cast<${label}>(${sqlColumnId(columnToCast.column)})`, |
| 166 | + ), |
| 167 | + trace: state.trace, |
| 168 | + type: params.type, |
| 169 | + }), |
| 170 | + columnToCast, |
| 171 | + params.type, |
| 172 | + ); |
| 173 | + |
| 174 | + return state.replaceColumnAtIndex(columnIndex, castColumn); |
| 175 | + }, |
| 176 | + }), |
| 177 | + ); |
| 178 | +} |
0 commit comments