1// Copyright (C) 2025 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 15import protos from '../../protos'; 16import { 17 createQueryResult, 18 LONG_NULL, 19 NUM, 20 STR, 21} from '../../trace_processor/query_result'; 22import {fromSqlBool, rows} from './utils'; 23 24const T = protos.QueryResult.CellsBatch.CellType; 25 26test('rows', () => { 27 // Manually construct a QueryResult which is equivalent to running the 28 // following SQL query: 29 // 30 // SELECT 31 // column1, 32 // column2, 33 // column3 34 // FROM ( 35 // VALUES 36 // ('A', 10, 100), 37 // ('B', 20, 200), 38 // ('C', 30, NULL) 39 // ) 40 // ORDER BY column1 ASC 41 const batch = protos.QueryResult.CellsBatch.create({ 42 cells: [ 43 [T.CELL_STRING, T.CELL_FLOAT64, T.CELL_VARINT], 44 [T.CELL_STRING, T.CELL_FLOAT64, T.CELL_VARINT], 45 [T.CELL_STRING, T.CELL_FLOAT64, T.CELL_NULL], 46 ].flat(), 47 stringCells: ['A', 'B', 'C'].join('\0'), 48 float64Cells: [10, 20, 30], 49 varintCells: [100, 200], 50 isLastBatch: true, 51 }); 52 const resultProto = protos.QueryResult.create({ 53 columnNames: ['column1', 'column2', 'column3'], 54 batch: [batch], 55 }); 56 const queryResult = createQueryResult({query: 'Some query'}); 57 queryResult.appendResultBatch( 58 protos.QueryResult.encode(resultProto).finish(), 59 ); 60 61 expect( 62 rows(queryResult, {column1: STR, column2: NUM, column3: LONG_NULL}), 63 ).toStrictEqual([ 64 {column1: 'A', column2: 10, column3: 100n}, 65 {column1: 'B', column2: 20, column3: 200n}, 66 {column1: 'C', column2: 30, column3: null}, 67 ]); 68}); 69 70test('fromSqlBool', () => { 71 expect(fromSqlBool(null)).toBeUndefined(); 72 expect(fromSqlBool(-1)).toStrictEqual(true); 73 expect(fromSqlBool(0)).toStrictEqual(false); 74 expect(fromSqlBool(0.1)).toStrictEqual(true); 75 expect(fromSqlBool(1)).toStrictEqual(true); 76}); 77