Skip to content

Commit e780b81

Browse files
authored
convert bigint to date also (#109)
1 parent e51016a commit e780b81

File tree

4 files changed

+367
-3
lines changed

4 files changed

+367
-3
lines changed

.changeset/little-parrots-lay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'druid-query-toolkit': patch
3+
---
4+
5+
inflateDatesForIndexes can deal with bigint

package-lock.json

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/query-result/query-result.spec.ts

Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,361 @@ describe('QueryResult', () => {
130130
});
131131
});
132132

133+
describe('#inflateDatesFromSqlTypes', () => {
134+
it('inflates columns with TIMESTAMP sqlType', () => {
135+
expect(
136+
new QueryResult({
137+
header: Column.fromColumnNamesAndTypeArrays(
138+
['name', 'timestamp', 'count'],
139+
[],
140+
['VARCHAR', 'TIMESTAMP', 'BIGINT'],
141+
),
142+
rows: [
143+
['Alice', '2016-06-27T00:00:00.000Z', 876],
144+
['Bob', '2016-06-27T01:00:00.000Z', 870],
145+
['Charlie', '2016-06-27T02:00:00.000Z', 960],
146+
],
147+
}).inflateDatesFromSqlTypes(),
148+
).toMatchInlineSnapshot(`
149+
QueryResult {
150+
"header": Array [
151+
Column {
152+
"name": "name",
153+
"nativeType": undefined,
154+
"sqlType": "VARCHAR",
155+
},
156+
Column {
157+
"name": "timestamp",
158+
"nativeType": undefined,
159+
"sqlType": "TIMESTAMP",
160+
},
161+
Column {
162+
"name": "count",
163+
"nativeType": undefined,
164+
"sqlType": "BIGINT",
165+
},
166+
],
167+
"query": undefined,
168+
"queryDuration": undefined,
169+
"queryId": undefined,
170+
"resultContext": undefined,
171+
"rows": Array [
172+
Array [
173+
"Alice",
174+
2016-06-27T00:00:00.000Z,
175+
876,
176+
],
177+
Array [
178+
"Bob",
179+
2016-06-27T01:00:00.000Z,
180+
870,
181+
],
182+
Array [
183+
"Charlie",
184+
2016-06-27T02:00:00.000Z,
185+
960,
186+
],
187+
],
188+
"sqlQuery": undefined,
189+
"sqlQueryId": undefined,
190+
}
191+
`);
192+
});
193+
194+
it('does not inflate nulls in TIMESTAMP columns', () => {
195+
expect(
196+
new QueryResult({
197+
header: Column.fromColumnNamesAndTypeArrays(
198+
['name', 'timestamp', 'count'],
199+
[],
200+
['VARCHAR', 'TIMESTAMP', 'BIGINT'],
201+
),
202+
rows: [
203+
['Alice', '2016-06-27T00:00:00.000Z', 876],
204+
['Bob', null, 870],
205+
['Charlie', '2016-06-27T02:00:00.000Z', 960],
206+
],
207+
}).inflateDatesFromSqlTypes(),
208+
).toMatchInlineSnapshot(`
209+
QueryResult {
210+
"header": Array [
211+
Column {
212+
"name": "name",
213+
"nativeType": undefined,
214+
"sqlType": "VARCHAR",
215+
},
216+
Column {
217+
"name": "timestamp",
218+
"nativeType": undefined,
219+
"sqlType": "TIMESTAMP",
220+
},
221+
Column {
222+
"name": "count",
223+
"nativeType": undefined,
224+
"sqlType": "BIGINT",
225+
},
226+
],
227+
"query": undefined,
228+
"queryDuration": undefined,
229+
"queryId": undefined,
230+
"resultContext": undefined,
231+
"rows": Array [
232+
Array [
233+
"Alice",
234+
2016-06-27T00:00:00.000Z,
235+
876,
236+
],
237+
Array [
238+
"Bob",
239+
null,
240+
870,
241+
],
242+
Array [
243+
"Charlie",
244+
2016-06-27T02:00:00.000Z,
245+
960,
246+
],
247+
],
248+
"sqlQuery": undefined,
249+
"sqlQueryId": undefined,
250+
}
251+
`);
252+
});
253+
254+
it('handles multiple TIMESTAMP columns', () => {
255+
expect(
256+
new QueryResult({
257+
header: Column.fromColumnNamesAndTypeArrays(
258+
['created', 'name', 'updated'],
259+
[],
260+
['TIMESTAMP', 'VARCHAR', 'TIMESTAMP'],
261+
),
262+
rows: [
263+
['2016-06-27T00:00:00.000Z', 'Alice', '2016-06-28T00:00:00.000Z'],
264+
['2016-06-27T01:00:00.000Z', 'Bob', '2016-06-28T01:00:00.000Z'],
265+
],
266+
}).inflateDatesFromSqlTypes(),
267+
).toMatchInlineSnapshot(`
268+
QueryResult {
269+
"header": Array [
270+
Column {
271+
"name": "created",
272+
"nativeType": undefined,
273+
"sqlType": "TIMESTAMP",
274+
},
275+
Column {
276+
"name": "name",
277+
"nativeType": undefined,
278+
"sqlType": "VARCHAR",
279+
},
280+
Column {
281+
"name": "updated",
282+
"nativeType": undefined,
283+
"sqlType": "TIMESTAMP",
284+
},
285+
],
286+
"query": undefined,
287+
"queryDuration": undefined,
288+
"queryId": undefined,
289+
"resultContext": undefined,
290+
"rows": Array [
291+
Array [
292+
2016-06-27T00:00:00.000Z,
293+
"Alice",
294+
2016-06-28T00:00:00.000Z,
295+
],
296+
Array [
297+
2016-06-27T01:00:00.000Z,
298+
"Bob",
299+
2016-06-28T01:00:00.000Z,
300+
],
301+
],
302+
"sqlQuery": undefined,
303+
"sqlQueryId": undefined,
304+
}
305+
`);
306+
});
307+
308+
it('does not inflate columns without TIMESTAMP sqlType', () => {
309+
expect(
310+
new QueryResult({
311+
header: Column.fromColumnNamesAndTypeArrays(
312+
['name', 'date_string', 'count'],
313+
[],
314+
['VARCHAR', 'VARCHAR', 'BIGINT'],
315+
),
316+
rows: [
317+
['Alice', '2016-06-27T00:00:00.000Z', 876],
318+
['Bob', '2016-06-27T01:00:00.000Z', 870],
319+
],
320+
}).inflateDatesFromSqlTypes(),
321+
).toMatchInlineSnapshot(`
322+
QueryResult {
323+
"header": Array [
324+
Column {
325+
"name": "name",
326+
"nativeType": undefined,
327+
"sqlType": "VARCHAR",
328+
},
329+
Column {
330+
"name": "date_string",
331+
"nativeType": undefined,
332+
"sqlType": "VARCHAR",
333+
},
334+
Column {
335+
"name": "count",
336+
"nativeType": undefined,
337+
"sqlType": "BIGINT",
338+
},
339+
],
340+
"query": undefined,
341+
"queryDuration": undefined,
342+
"queryId": undefined,
343+
"resultContext": undefined,
344+
"rows": Array [
345+
Array [
346+
"Alice",
347+
"2016-06-27T00:00:00.000Z",
348+
876,
349+
],
350+
Array [
351+
"Bob",
352+
"2016-06-27T01:00:00.000Z",
353+
870,
354+
],
355+
],
356+
"sqlQuery": undefined,
357+
"sqlQueryId": undefined,
358+
}
359+
`);
360+
});
361+
362+
it('returns same instance when no TIMESTAMP columns present', () => {
363+
const result = new QueryResult({
364+
header: Column.fromColumnNamesAndTypeArrays(['name', 'count'], [], ['VARCHAR', 'BIGINT']),
365+
rows: [
366+
['Alice', 876],
367+
['Bob', 870],
368+
],
369+
});
370+
371+
const inflated = result.inflateDatesFromSqlTypes();
372+
expect(inflated).toBe(result);
373+
});
374+
375+
it('inflates TIMESTAMP columns with millisecond numbers', () => {
376+
expect(
377+
new QueryResult({
378+
header: Column.fromColumnNamesAndTypeArrays(
379+
['__time', 'name', 'count'],
380+
[],
381+
['TIMESTAMP', 'VARCHAR', 'BIGINT'],
382+
),
383+
rows: [
384+
[1467072000000, 'Alice', 876],
385+
[1467075600000, 'Bob', 870],
386+
[1467079200000n, 'Charlie', 960], // test with bigint literal
387+
],
388+
}).inflateDatesFromSqlTypes(),
389+
).toMatchInlineSnapshot(`
390+
QueryResult {
391+
"header": Array [
392+
Column {
393+
"name": "__time",
394+
"nativeType": undefined,
395+
"sqlType": "TIMESTAMP",
396+
},
397+
Column {
398+
"name": "name",
399+
"nativeType": undefined,
400+
"sqlType": "VARCHAR",
401+
},
402+
Column {
403+
"name": "count",
404+
"nativeType": undefined,
405+
"sqlType": "BIGINT",
406+
},
407+
],
408+
"query": undefined,
409+
"queryDuration": undefined,
410+
"queryId": undefined,
411+
"resultContext": undefined,
412+
"rows": Array [
413+
Array [
414+
2016-06-28T00:00:00.000Z,
415+
"Alice",
416+
876,
417+
],
418+
Array [
419+
2016-06-28T01:00:00.000Z,
420+
"Bob",
421+
870,
422+
],
423+
Array [
424+
2016-06-28T02:00:00.000Z,
425+
"Charlie",
426+
960,
427+
],
428+
],
429+
"sqlQuery": undefined,
430+
"sqlQueryId": undefined,
431+
}
432+
`);
433+
});
434+
435+
it('handles mixed string and millisecond TIMESTAMP values', () => {
436+
expect(
437+
new QueryResult({
438+
header: Column.fromColumnNamesAndTypeArrays(
439+
['timestamp', 'name'],
440+
[],
441+
['TIMESTAMP', 'VARCHAR'],
442+
),
443+
rows: [
444+
['2016-06-27T00:00:00.000Z', 'Alice'],
445+
[1467075600000, 'Bob'],
446+
['2016-06-27T02:00:00.000Z', 'Charlie'],
447+
],
448+
}).inflateDatesFromSqlTypes(),
449+
).toMatchInlineSnapshot(`
450+
QueryResult {
451+
"header": Array [
452+
Column {
453+
"name": "timestamp",
454+
"nativeType": undefined,
455+
"sqlType": "TIMESTAMP",
456+
},
457+
Column {
458+
"name": "name",
459+
"nativeType": undefined,
460+
"sqlType": "VARCHAR",
461+
},
462+
],
463+
"query": undefined,
464+
"queryDuration": undefined,
465+
"queryId": undefined,
466+
"resultContext": undefined,
467+
"rows": Array [
468+
Array [
469+
2016-06-27T00:00:00.000Z,
470+
"Alice",
471+
],
472+
Array [
473+
2016-06-28T01:00:00.000Z,
474+
"Bob",
475+
],
476+
Array [
477+
2016-06-27T02:00:00.000Z,
478+
"Charlie",
479+
],
480+
],
481+
"sqlQuery": undefined,
482+
"sqlQueryId": undefined,
483+
}
484+
`);
485+
});
486+
});
487+
133488
describe('#toObjectArray', () => {
134489
it('works', () => {
135490
expect(testQueryResult.toObjectArray()).toEqual([

0 commit comments

Comments
 (0)