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 15import {SourceDataset, UnionDataset} from './dataset'; 16import {LONG, NUM, STR} from './query_result'; 17 18test('get query for simple dataset', () => { 19 const dataset = new SourceDataset({ 20 src: 'slice', 21 schema: {id: NUM}, 22 }); 23 24 expect(dataset.query()).toEqual('select id from (slice)'); 25}); 26 27test("get query for simple dataset with 'eq' filter", () => { 28 const dataset = new SourceDataset({ 29 src: 'slice', 30 schema: {id: NUM}, 31 filter: { 32 col: 'id', 33 eq: 123, 34 }, 35 }); 36 37 expect(dataset.query()).toEqual('select id from (slice) where id = 123'); 38}); 39 40test("get query for simple dataset with an 'in' filter", () => { 41 const dataset = new SourceDataset({ 42 src: 'slice', 43 schema: {id: NUM}, 44 filter: { 45 col: 'id', 46 in: [123, 456], 47 }, 48 }); 49 50 expect(dataset.query()).toEqual( 51 'select id from (slice) where id in (123,456)', 52 ); 53}); 54 55test('get query for union dataset', () => { 56 const dataset = new UnionDataset([ 57 new SourceDataset({ 58 src: 'slice', 59 schema: {id: NUM}, 60 filter: { 61 col: 'id', 62 eq: 123, 63 }, 64 }), 65 new SourceDataset({ 66 src: 'slice', 67 schema: {id: NUM}, 68 filter: { 69 col: 'id', 70 eq: 456, 71 }, 72 }), 73 ]); 74 75 expect(dataset.query()).toEqual( 76 'select id from (slice) where id = 123 union all select id from (slice) where id = 456', 77 ); 78}); 79 80test('doesImplement', () => { 81 const dataset = new SourceDataset({ 82 src: 'slice', 83 schema: {id: NUM, ts: LONG}, 84 }); 85 86 expect(dataset.implements({id: NUM})).toBe(true); 87 expect(dataset.implements({id: NUM, ts: LONG})).toBe(true); 88 expect(dataset.implements({id: NUM, ts: LONG, name: STR})).toBe(false); 89 expect(dataset.implements({id: LONG})).toBe(false); 90}); 91 92test('find the schema of a simple dataset', () => { 93 const dataset = new SourceDataset({ 94 src: 'slice', 95 schema: {id: NUM, ts: LONG}, 96 }); 97 98 expect(dataset.schema).toMatchObject({id: NUM, ts: LONG}); 99}); 100 101test('find the schema of a union where source sets differ in their names', () => { 102 const dataset = new UnionDataset([ 103 new SourceDataset({ 104 src: 'slice', 105 schema: {foo: NUM}, 106 }), 107 new SourceDataset({ 108 src: 'slice', 109 schema: {bar: NUM}, 110 }), 111 ]); 112 113 expect(dataset.schema).toMatchObject({}); 114}); 115 116test('find the schema of a union with differing source sets', () => { 117 const dataset = new UnionDataset([ 118 new SourceDataset({ 119 src: 'slice', 120 schema: {foo: NUM}, 121 }), 122 new SourceDataset({ 123 src: 'slice', 124 schema: {foo: LONG}, 125 }), 126 ]); 127 128 expect(dataset.schema).toMatchObject({}); 129}); 130 131test('find the schema of a union with one column in common', () => { 132 const dataset = new UnionDataset([ 133 new SourceDataset({ 134 src: 'slice', 135 schema: {foo: NUM, bar: NUM}, 136 }), 137 new SourceDataset({ 138 src: 'slice', 139 schema: {foo: NUM, baz: NUM}, 140 }), 141 ]); 142 143 expect(dataset.schema).toMatchObject({foo: NUM}); 144}); 145 146test('optimize a union dataset', () => { 147 const dataset = new UnionDataset([ 148 new SourceDataset({ 149 src: 'slice', 150 schema: {}, 151 filter: { 152 col: 'track_id', 153 eq: 123, 154 }, 155 }), 156 new SourceDataset({ 157 src: 'slice', 158 schema: {}, 159 filter: { 160 col: 'track_id', 161 eq: 456, 162 }, 163 }), 164 ]); 165 166 expect(dataset.optimize()).toEqual({ 167 src: 'slice', 168 schema: {}, 169 filter: { 170 col: 'track_id', 171 in: [123, 456], 172 }, 173 }); 174}); 175 176test('optimize a union dataset with different types of filters', () => { 177 const dataset = new UnionDataset([ 178 new SourceDataset({ 179 src: 'slice', 180 schema: {}, 181 filter: { 182 col: 'track_id', 183 eq: 123, 184 }, 185 }), 186 new SourceDataset({ 187 src: 'slice', 188 schema: {}, 189 filter: { 190 col: 'track_id', 191 in: [456, 789], 192 }, 193 }), 194 ]); 195 196 expect(dataset.optimize()).toEqual({ 197 src: 'slice', 198 schema: {}, 199 filter: { 200 col: 'track_id', 201 in: [123, 456, 789], 202 }, 203 }); 204}); 205 206test('optimize a union dataset with different schemas', () => { 207 const dataset = new UnionDataset([ 208 new SourceDataset({ 209 src: 'slice', 210 schema: {foo: NUM}, 211 }), 212 new SourceDataset({ 213 src: 'slice', 214 schema: {bar: NUM}, 215 }), 216 ]); 217 218 expect(dataset.optimize()).toEqual({ 219 src: 'slice', 220 // The resultant schema is the combination of the union's member's schemas, 221 // as we know the source is the same as we know we can get all of the 'seen' 222 // columns from the source. 223 schema: { 224 foo: NUM, 225 bar: NUM, 226 }, 227 }); 228}); 229