summaryrefslogtreecommitdiffstats
path: root/src/arrow/js/test/unit/table/serialize-tests.ts
blob: 5eb21176362a1e060ae9d790500c4902224cdbfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

import '../../jest-extensions';
import * as generate from '../../generate-test-data';
import {
    Table, Schema, Field, DataType, Dictionary, Int32, Float32, Utf8, Null, Int32Vector
} from 'apache-arrow';

const toSchema = (...xs: [string, DataType][]) => new Schema(xs.map((x) => new Field(...x)));
const schema1 = toSchema(['a', new Int32()], ['b', new Float32()], ['c', new Dictionary(new Utf8(), new Int32())]);
const schema2 = toSchema(['d', new Int32()], ['e', new Float32()], ['f', new Utf8()]);
const nullSchema = new Schema([new Field('null', new Null())]);

schema1.metadata.set('foo', 'bar');

function createTable<T extends { [key: string]: DataType } = any>(schema: Schema<T>, chunkLengths: number[]) {
    return generate.table(chunkLengths, schema).table;
}

describe('Table#serialize()', () => {

    test(`doesn't swap the order of buffers that share the same underlying ArrayBuffer but are in a different order`, () => {
        const values = new Int32Array([0, 1, 2, 3, 4, 5, 6, 7]);
        const expected = values.slice();
        const x = Int32Vector.from(values.subarray(4, 8)); // back
        const y = Int32Vector.from(values.subarray(0, 4)); // front
        const source = Table.new([x, y], ['x', 'y']);
        const table = Table.from(source.serialize());
        expect(table.getColumn('x').toArray()).toEqual(expected.subarray(4, 8));
        expect(table.getColumn('y').toArray()).toEqual(expected.subarray(0, 4));
    });

    test(`Table#empty round-trips through serialization`, () => {
        const source = Table.empty();
        source.schema.metadata.set('foo', 'bar');
        expect(source).toHaveLength(0);
        expect(source.numCols).toBe(0);
        const result = Table.from(source.serialize());
        expect(result).toEqualTable(source);
        expect(result.schema.metadata.get('foo')).toEqual('bar');
    });

    test(`Schema metadata round-trips through serialization`, () => {
        const source = createTable(schema1, [20]);
        expect(source).toHaveLength(20);
        expect(source.numCols).toBe(3);
        const result = Table.from(source.serialize());
        expect(result).toEqualTable(source);
        expect(result.schema.metadata.get('foo')).toEqual('bar');
    });

    test(`Table#assign an empty Table to a Table with a zero-length Null column round-trips through serialization`, () => {
        const table1 = new Table(nullSchema);
        const table2 = Table.empty();
        const source = table1.assign(table2);
        expect(source).toHaveLength(0);
        expect(source.numCols).toBe(1);
        const result = Table.from(source.serialize());
        expect(result).toEqualTable(source);
    });

    const chunkLengths = [] as number[];
    for (let i = -1; ++i < 3;) {
        chunkLengths[i * 2] = (Math.random() * 100) | 0;
        chunkLengths[i * 2 + 1] = 0;
        const table = <T extends { [key: string]: DataType } = any>(schema: Schema<T>) => createTable(schema, chunkLengths);
        test(`Table#select round-trips through serialization`, () => {
            const source = table(schema1).select('a', 'c');
            expect(source.numCols).toBe(2);
            const result = Table.from(source.serialize());
            expect(result).toEqualTable(source);
        });
        test(`Table#selectAt round-trips through serialization`, () => {
            const source = table(schema1).selectAt(0, 2);
            expect(source.numCols).toBe(2);
            const result = Table.from(source.serialize());
            expect(result).toEqualTable(source);
        });
        test(`Table#assign round-trips through serialization`, () => {
            const source = table(schema1).assign(table(schema2));
            expect(source.numCols).toBe(6);
            const result = Table.from(source.serialize());
            expect(result).toEqualTable(source);
            expect(result.schema.metadata.get('foo')).toEqual('bar');
        });
        test(`Table#assign with an empty table round-trips through serialization`, () => {
            const table1 = table(schema1);
            const source = table1.assign(Table.empty());
            expect(source.numCols).toBe(table1.numCols);
            expect(source).toHaveLength(table1.length);
            const result = Table.from(source.serialize());
            expect(result).toEqualTable(source);
            expect(result.schema.metadata.get('foo')).toEqual('bar');
        });
        test(`Table#assign with a zero-length Null column round-trips through serialization`, () => {
            const table1 = new Table(nullSchema);
            const table2 = table(schema1);
            const source = table1.assign(table2);
            expect(source).toHaveLength(table2.length);
            expect(source.numCols).toBe(4);
            const result = Table.from(source.serialize());
            expect(result).toEqualTable(source);
            expect(result.schema.metadata.get('foo')).toEqual('bar');
        });
        test(`Table#assign with different lengths and number of chunks round-trips through serialization`, () => {
            const table1 = table(schema1);
            const table2 = createTable(schema2, [102, 4, 10, 97, 10, 2, 4]);
            const source = table1.assign(table2);
            expect(source.numCols).toBe(6);
            expect(source).toHaveLength(Math.max(table1.length, table2.length));
            const result = Table.from(source.serialize());
            expect(result).toEqualTable(source);
            expect(result.schema.metadata.get('foo')).toEqual('bar');
        });
        test(`Table#select with Table#assign the result of Table#selectAt round-trips through serialization`, () => {
            const table1 = table(schema1);
            const table2 = table(schema2);
            const source = table1.select('a', 'c').assign(table2.selectAt(2));
            expect(source.numCols).toBe(3);
            const result = Table.from(source.serialize());
            expect(result).toEqualTable(source);
            expect(result.schema.metadata.get('foo')).toEqual('bar');
        });
        test(`Table#slice round-trips through serialization`, () => {
            const table1 = table(schema1);
            const length = table1.length;
            const [begin, end] = [length * .25, length * .75].map((x) => x | 0);
            const source = table1.slice(begin, end);
            expect(source.numCols).toBe(3);
            expect(source).toHaveLength(end - begin);
            const result = Table.from(source.serialize());
            expect(result).toEqualTable(source);
            expect(result.schema.metadata.get('foo')).toEqual('bar');
        });
        test(`Table#concat of two slices round-trips through serialization`, () => {
            const table1 = table(schema1);
            const length = table1.length;
            const [begin1, end1] = [length * .10, length * .20].map((x) => x | 0);
            const [begin2, end2] = [length * .80, length * .90].map((x) => x | 0);
            const slice1 = table1.slice(begin1, end1);
            const slice2 = table1.slice(begin2, end2);
            const source = slice1.concat(slice2);
            expect(slice1).toHaveLength(end1 - begin1);
            expect(slice2).toHaveLength(end2 - begin2);
            expect(source).toHaveLength((end1 - begin1) + (end2 - begin2));
            [slice1, slice2, source].forEach((x) => expect(x.numCols).toBe(3));
            const result = Table.from(source.serialize());
            expect(result).toEqualTable(source);
            expect(result.schema.metadata.get('foo')).toEqual('bar');
        });
    }
});