Skip to content

Commit 1c499e2

Browse files
authored
test: harden Arrow precision parity for nested and dictionary values (#637)
test: cover nested and dictionary Arrow numeric parity Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
1 parent d9bd91f commit 1c499e2

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

‎tests/unit/result/ArrowRowMaterialization.test.ts‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,127 @@ describe('Arrow row materialization', () => {
318318
);
319319
});
320320

321+
for (const numeric of [
322+
{
323+
name: 'BIGINT',
324+
type: () => new arrow.Int64(),
325+
thriftType: TTypeId.BIGINT_TYPE,
326+
values: [BigInt('9007199254740993'), BigInt('-9223372036854775808'), BigInt(0)],
327+
expected: [BigInt('9007199254740993'), BigInt('-9223372036854775808'), BigInt(0)],
328+
},
329+
{
330+
name: 'DECIMAL',
331+
type: () => new arrow.Decimal(4, 38, 128),
332+
thriftType: TTypeId.DECIMAL_TYPE,
333+
values: [decimal('90071992547409931234'), decimal('-12500'), decimal('0')],
334+
expected: ['9007199254740993.1234', '-1.2500', '0.0000'],
335+
// Dictionary decimals retain the legacy unscaled string representation.
336+
dictionaryExpected: ['90071992547409931234', '-12500', '0'],
337+
},
338+
]) {
339+
for (const dictionaryEncoded of [false, true]) {
340+
it(`matches legacy precise ${numeric.name} in scalars and containers (${rowMode}, dictionary=${dictionaryEncoded})`, async () => {
341+
const type = () =>
342+
dictionaryEncoded ? new arrow.Dictionary(numeric.type(), new arrow.Int32()) : numeric.type();
343+
const [positive, negative, zero] = numeric.values;
344+
const [expectedPositive, expectedNegative, expectedZero] =
345+
(dictionaryEncoded && numeric.dictionaryExpected) || numeric.expected;
346+
const columns: Column[] = [
347+
{
348+
field: new arrow.Field('scalar', type(), true),
349+
thriftType: numeric.thriftType,
350+
values: [positive, null, negative, positive, zero],
351+
expected: [expectedPositive, null, expectedNegative, expectedPositive, expectedZero],
352+
},
353+
{
354+
field: new arrow.Field('struct', new arrow.Struct([new arrow.Field('value', type(), true)]), true),
355+
thriftType: TTypeId.STRUCT_TYPE,
356+
values: [{ value: positive }, null, { value: null }, { value: negative }, { value: zero }],
357+
expected: [
358+
{ value: expectedPositive },
359+
null,
360+
{ value: null },
361+
{ value: expectedNegative },
362+
{ value: expectedZero },
363+
],
364+
},
365+
{
366+
field: new arrow.Field('list', new arrow.List(new arrow.Field('element', type(), true)), true),
367+
thriftType: TTypeId.ARRAY_TYPE,
368+
values: [[positive, null, negative, positive, zero], null, [], [null], [negative]],
369+
expected: [
370+
[expectedPositive, null, expectedNegative, expectedPositive, expectedZero],
371+
null,
372+
[],
373+
[null],
374+
[expectedNegative],
375+
],
376+
},
377+
{
378+
field: new arrow.Field(
379+
'map',
380+
new arrow.Map_(
381+
new arrow.Field(
382+
'entries',
383+
new arrow.Struct<{ key: arrow.Utf8; value: ReturnType<typeof type> }>([
384+
new arrow.Field('key', new arrow.Utf8(), false),
385+
new arrow.Field('value', type(), true),
386+
]),
387+
false,
388+
),
389+
),
390+
true,
391+
),
392+
thriftType: TTypeId.MAP_TYPE,
393+
values: [
394+
new Map<string, any>([
395+
['positive', positive],
396+
['missing', null],
397+
['negative', negative],
398+
['repeated', positive],
399+
['zero', zero],
400+
]),
401+
null,
402+
new Map(),
403+
new Map([['missing', null]]),
404+
new Map([['negative', negative]]),
405+
],
406+
expected: [
407+
{
408+
positive: expectedPositive,
409+
missing: null,
410+
negative: expectedNegative,
411+
repeated: expectedPositive,
412+
zero: expectedZero,
413+
},
414+
null,
415+
{},
416+
{ missing: null },
417+
{ negative: expectedNegative },
418+
],
419+
},
420+
];
421+
const converter = converterFor(columns, rowMode, true);
422+
const rows = await converter.fetchNext({ limit: 100 });
423+
const expected = columns[0].values.map((_, rowIndex) => {
424+
const values = columns.map((column) => column.expected[rowIndex]);
425+
return rowMode === 'array'
426+
? values
427+
: Object.fromEntries(columns.map((column, index) => [column.field.name, values[index]]));
428+
});
429+
deepStrictEqual(rows, expected);
430+
const table = arrow.tableFromIPC(ipc(columns));
431+
const legacyRows = (converter as any).getObjectRows(table.schema, table.toArray());
432+
deepStrictEqual(
433+
rows,
434+
rowMode === 'array'
435+
? legacyRows.map((row: any) => columns.map((column) => row[column.field.name]))
436+
: legacyRows,
437+
);
438+
});
439+
}
440+
}
441+
321442
for (const type of [new arrow.Binary(), new arrow.FixedSizeBinary(2)]) {
322443
it(`copies ${type} values out of the batch (${rowMode})`, async () => {
323444
const columns: Column[] = [

0 commit comments

Comments
 (0)