-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy patharrow.test.ts
More file actions
213 lines (180 loc) · 7.49 KB
/
arrow.test.ts
File metadata and controls
213 lines (180 loc) · 7.49 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { expect } from 'chai';
import sinon from 'sinon';
import { DBSQLClient } from '../../lib';
import { ClientConfig } from '../../lib/contracts/IClientContext';
import IDBSQLSession from '../../lib/contracts/IDBSQLSession';
import ArrowResultHandler from '../../lib/result/ArrowResultHandler';
import ArrowResultConverter from '../../lib/result/ArrowResultConverter';
import ResultSlicer from '../../lib/result/ResultSlicer';
import config from './utils/config';
const fixtures = require('../fixtures/compatibility');
const { expected: expectedColumn } = require('../fixtures/compatibility/column');
const { expected: expectedArrow } = require('../fixtures/compatibility/arrow');
const { expected: expectedArrowNativeTypes } = require('../fixtures/compatibility/arrow_native_types');
const { fixArrowResult } = fixtures;
async function openSession(customConfig: Partial<ClientConfig> = {}) {
const client = new DBSQLClient();
const clientConfig = client.getConfig();
sinon.stub(client, 'getConfig').returns({
...clientConfig,
...customConfig,
});
const connection = await client.connect({
host: config.host,
path: config.path,
token: config.token,
});
return connection.openSession({
initialCatalog: config.catalog,
initialSchema: config.schema,
});
}
async function execute(session: IDBSQLSession, statement: string) {
const operation = await session.executeStatement(statement);
const result = await operation.fetchAll();
await operation.close();
return result;
}
async function deleteTable(session: IDBSQLSession, tableName: string) {
await execute(session, `DROP TABLE IF EXISTS ${tableName}`);
}
async function initializeTable(session: IDBSQLSession, tableName: string) {
await deleteTable(session, tableName);
const createTable = fixtures.createTableSql.replace(/\$\{table_name\}/g, tableName);
await execute(session, createTable);
const insertData = fixtures.insertDataSql.replace(/\$\{table_name\}/g, tableName);
await execute(session, insertData);
}
describe('Arrow support', () => {
const tableName = `dbsql_nodejs_sdk_e2e_arrow_${config.tableSuffix}`;
function createTest(
testBody: (session: IDBSQLSession) => void | Promise<void>,
customConfig: Partial<ClientConfig> = {},
) {
return async () => {
const session = await openSession(customConfig);
try {
await initializeTable(session, tableName);
await testBody(session);
} finally {
await deleteTable(session, tableName);
await session.close();
}
};
}
it(
'should not use arrow if disabled',
createTest(
async (session) => {
const operation = await session.executeStatement(`SELECT * FROM ${tableName}`);
const result = await operation.fetchAll();
expect(result).to.deep.equal(expectedColumn);
// @ts-expect-error TS2339: Property getResultHandler does not exist on type IOperation
const resultHandler = await operation.getResultHandler();
expect(resultHandler).to.be.instanceof(ResultSlicer);
expect(resultHandler.source).to.be.not.instanceof(ArrowResultConverter);
await operation.close();
},
{
arrowEnabled: false,
useLZ4Compression: false,
},
),
);
it(
'should use arrow with native types disabled',
createTest(
async (session) => {
const operation = await session.executeStatement(`SELECT * FROM ${tableName}`);
const result = await operation.fetchAll();
expect(fixArrowResult(result)).to.deep.equal(expectedArrow);
// @ts-expect-error TS2339: Property getResultHandler does not exist on type IOperation
const resultHandler = await operation.getResultHandler();
expect(resultHandler).to.be.instanceof(ResultSlicer);
expect(resultHandler.source).to.be.instanceof(ArrowResultConverter);
expect(resultHandler.source.source).to.be.instanceof(ArrowResultHandler);
await operation.close();
},
{
arrowEnabled: true,
useArrowNativeTypes: false,
useLZ4Compression: false,
},
),
);
it(
'should use arrow with native types enabled',
createTest(
async (session) => {
const operation = await session.executeStatement(`SELECT * FROM ${tableName}`);
const result = await operation.fetchAll();
expect(fixArrowResult(result)).to.deep.equal(expectedArrowNativeTypes);
// @ts-expect-error TS2339: Property getResultHandler does not exist on type IOperation
const resultHandler = await operation.getResultHandler();
expect(resultHandler).to.be.instanceof(ResultSlicer);
expect(resultHandler.source).to.be.instanceof(ArrowResultConverter);
expect(resultHandler.source.source).to.be.instanceof(ArrowResultHandler);
await operation.close();
},
{
arrowEnabled: true,
useArrowNativeTypes: true,
useLZ4Compression: false,
},
),
);
it('should handle multiple batches in response', async () => {
const rowsCount = 10000;
const session = await openSession({
arrowEnabled: true,
useLZ4Compression: false,
});
const operation = await session.executeStatement(`
SELECT *
FROM range(0, ${rowsCount}) AS t1
LEFT JOIN (SELECT 1) AS t2
`);
// We use some internals here to check that server returned response with multiple batches
// @ts-expect-error TS2339: Property getResultHandler does not exist on type IOperation
const resultHandler = await operation.getResultHandler();
expect(resultHandler).to.be.instanceof(ResultSlicer);
expect(resultHandler.source).to.be.instanceof(ArrowResultConverter);
expect(resultHandler.source.source).to.be.instanceof(ArrowResultHandler);
// @ts-expect-error TS2339: Property _data does not exist on type IOperation
sinon.spy(operation._data, 'fetchNext');
const result = await resultHandler.fetchNext({ limit: rowsCount });
// @ts-expect-error TS2339: Property _data does not exist on type IOperation
expect(operation._data.fetchNext.callCount).to.be.eq(1);
// @ts-expect-error TS2339: Property _data does not exist on type IOperation
const rawData = await operation._data.fetchNext.firstCall.returnValue;
// We don't know exact count of batches returned, it depends on server's configuration,
// but with much enough rows there should be more than one result batch
expect(rawData.arrowBatches?.length).to.be.gt(1);
expect(result.length).to.be.eq(rowsCount);
});
it(
'should handle LZ4 compressed data',
createTest(
async (session) => {
const operation = await session.executeStatement(
`SELECT * FROM ${tableName}`,
{ useCloudFetch: false }, // Explicitly disable cloud fetch to test LZ4 compression
);
const result = await operation.fetchAll();
expect(fixArrowResult(result)).to.deep.equal(expectedArrow);
// @ts-expect-error TS2339: Property getResultHandler does not exist on type IOperation
const resultHandler = await operation.getResultHandler();
expect(resultHandler).to.be.instanceof(ResultSlicer);
expect(resultHandler.source).to.be.instanceof(ArrowResultConverter);
expect(resultHandler.source.source).to.be.instanceof(ArrowResultHandler);
expect(resultHandler.source.source.isLZ4Compressed).to.be.true;
await operation.close();
},
{
arrowEnabled: true,
useArrowNativeTypes: false,
useLZ4Compression: true,
},
),
);
});