// math.gl // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors import * as arrow from 'apache-arrow'; import {queryArrowSQL} from '@math.gl/sql/query'; import {expect, test} from 'vitest'; test('evaluates whole-result window aggregates after filtering and before pagination', () => { const input = new arrow.Table({ id: arrow.vectorFromArray([1, 2, 3, 4], new arrow.Int32()), value: arrow.vectorFromArray([null, 3, 9, 20], new arrow.Int32()) }); const tables = new Map([['input', input]]); const result = queryArrowSQL( 'SELECT id, COUNT(*) OVER () AS rows, COUNT(value) OVER () AS present, SUM(value) OVER () AS total FROM input WHERE id < 4 ORDER BY total DESC, id DESC LIMIT 1 OFFSET 1', tables ); expect(Array.from(result, row => row.toJSON())).toEqual([ {id: 2, rows: 3n, present: 2n, total: 12n} ]); const empty = queryArrowSQL('SELECT COUNT(*) OVER () AS rows FROM input WHERE id < 0', tables); expect(empty.numRows).toBe(0); expect(empty.schema.fields.map(field => field.name)).toEqual(['rows']); expect(() => queryArrowSQL('SELECT COUNT(*) OVER (ORDER BY id) FROM input', tables)).toThrow(); }); test('keeps integer-like column names in schema order when appending windows', () => { const type = new arrow.Int32(); const schema = new arrow.Schema( [new arrow.Field('station', type), new arrow.Field('2020', type)], new Map([['source', 'stations']]) ); const batches = [ [11, 101], [12, 102] ].map(values => { const children = values.map(value => arrow.vectorFromArray([value], type).data[0]!); return new arrow.RecordBatch( schema, new arrow.Data(new arrow.Struct(schema.fields), 0, 1, 0, undefined, children) ); }); const tables = new Map([['input', new arrow.Table(schema, batches)]]); const control = queryArrowSQL('SELECT station, "2020" FROM input', tables); const window = queryArrowSQL('SELECT station, "2020", COUNT(*) OVER () AS n FROM input', tables); expect(window.schema.fields.map(field => field.name)).toEqual(['station', '2020', 'n']); expect(window.schema.metadata).toEqual(schema.metadata); for (const result of [control, window]) { expect(Array.from(result.getChild('station')!)).toEqual([11, 12]); expect(Array.from(result.getChild('2020')!)).toEqual([101, 102]); } expect(Array.from(window.getChild('n')!)).toEqual([2n, 2n]); }); test('matches exact composite keys with duplicates and null components', () => { const left = new arrow.Table({ id: arrow.vectorFromArray([1, 2, 3], new arrow.Int32()), key: arrow.vectorFromArray([7, 7, null], new arrow.Int32()), region: arrow.vectorFromArray(['east', 'west', 'east'], new arrow.Utf8()) }); const right = new arrow.Table({ key: arrow.vectorFromArray([7, 7, 7, null], new arrow.Int32()), region: arrow.vectorFromArray(['east', 'east', 'north', 'east'], new arrow.Utf8()), score: arrow.vectorFromArray([4, 5, 20, 99], new arrow.Int32()) }); const tables = new Map([ ['left_table', left], ['right_table', right] ]); const result = queryArrowSQL( 'SELECT l.id, r.score AS score FROM left_table l LEFT JOIN right_table r ON r.region = l.region AND l.key = r.key ORDER BY id, score', tables ); expect(Array.from(result, row => row.toJSON())).toEqual([ {id: 1, score: 4}, {id: 1, score: 5}, {id: 2, score: null}, {id: 3, score: null} ]); }); test('correlates sibling expansions to their original parent', () => { const list = new arrow.List(new arrow.Field('item', new arrow.Int32())); const parents = new arrow.Table({ id: arrow.vectorFromArray([1, 2], new arrow.Int32()), numbers: arrow.vectorFromArray([[7, 8], [9]], list), labels: arrow.vectorFromArray([[10, 20], []], list) }); const result = queryArrowSQL( 'SELECT p.id, u.value AS number, v.value AS label FROM parents p CROSS JOIN UNNEST(p.numbers) AS u(value) LEFT JOIN UNNEST(p.labels) AS v(value) ON TRUE ORDER BY id, number, label', new Map([['parents', parents]]) ); expect(Array.from(result, row => row.toJSON())).toEqual([ {id: 1, number: 7, label: 10}, {id: 1, number: 7, label: 20}, {id: 1, number: 8, label: 10}, {id: 1, number: 8, label: 20}, {id: 2, number: 9, label: null} ]); });