-
Notifications
You must be signed in to change notification settings - Fork 126
/
column.js
785 lines (713 loc) · 18.7 KB
/
column.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
* Copyright (c) 2015-2019 Snowflake Computing Inc. All rights reserved.
*/
var Util = require('../../util');
var Errors = require('../../errors');
var BigNumber = require('bignumber.js');
const GlobalConfig = require('../../global_config');
const Logger = require('../../logger');
var SfTimestamp = require('./sf_timestamp');
var SqlTypes = require('./data_types').SqlTypes;
var bigInt = require('big-integer');
var NULL_UPPERCASE = 'NULL';
/**
* Creates a new Column.
*
* @param {Object} options
* @param {Number} index
* @param {Object} statementParameters
* @param {String} resultVersion
*
* @constructor
*/
function Column(options, index, statementParameters, resultVersion)
{
var name = options.overriddenName || options.name;
var nullable = options.nullable;
var scale = options.scale;
var type = options.type;
var precision = options.precision;
/**
* Returns the name of this column.
*
* @return {String}
*/
this.getName = function ()
{
return name;
};
/**
* Returns the index of this column.
*
* @return {Number}
*/
this.getIndex = function ()
{
return index;
};
/**
* Returns the id of this column.
*
* @return {Number}
*/
this.getId = function ()
{
// use the index as the id for now
return index;
};
/**
* Determines if this column is nullable.
*
* @returns {Boolean}
*/
this.isNullable = function ()
{
return nullable;
};
/**
* Returns the scale associated with this column.
*
* @returns {Number}
*/
this.getScale = function ()
{
return scale;
};
/**
* Returns the type associated with this column.
*
* @returns {String}
*/
this.getType = function ()
{
return type;
};
/**
* Returns the precision associated with this column
*
* @returns {Number}
*/
this.getPrecision = function ()
{
return precision;
};
// add methods that make it easy to check if the column is of a specific type
this.isString = createFnIsColumnOfType(type, SqlTypes.isString, SqlTypes);
this.isBinary = createFnIsColumnOfType(type, SqlTypes.isBinary, SqlTypes);
this.isNumber = createFnIsColumnOfType(type, SqlTypes.isNumber, SqlTypes);
this.isBoolean = createFnIsColumnOfType(type, SqlTypes.isBoolean, SqlTypes);
this.isDate = createFnIsColumnOfType(type, SqlTypes.isDate, SqlTypes);
this.isTime = createFnIsColumnOfType(type, SqlTypes.isTime, SqlTypes);
this.isTimestamp = createFnIsColumnOfType(type, SqlTypes.isTimestamp, SqlTypes);
this.isTimestampLtz = createFnIsColumnOfType(type, SqlTypes.isTimestampLtz, SqlTypes);
this.isTimestampNtz = createFnIsColumnOfType(type, SqlTypes.isTimestampNtz, SqlTypes);
this.isTimestampTz = createFnIsColumnOfType(type, SqlTypes.isTimestampTz, SqlTypes);
this.isVariant = createFnIsColumnOfType(type, SqlTypes.isVariant, SqlTypes);
this.isObject = createFnIsColumnOfType(type, SqlTypes.isObject, SqlTypes);
this.isArray = createFnIsColumnOfType(type, SqlTypes.isArray, SqlTypes);
var convert;
var toString;
var toValue;
var format;
if (this.isNumber())
{
let integerAs = statementParameters['JS_TREAT_INTEGER_AS_BIGINT'];
if (!integerAs)
{
convert = convertRawNumber;
}
else
{
if (this.getScale() > 0 || this.getType() === SqlTypes.values.REAL)
{
convert = convertRawNumber;
}
// This is a integer so represent it as a big int
else
{
convert = convertRawBigInt;
}
}
toValue = toValueFromNumber;
toString = toStringFromNumber;
}
else if (this.isTime())
{
convert = convertRawTime;
toValue = toValueFromTime;
toString = toStringFromTime;
format = statementParameters['TIME_OUTPUT_FORMAT'];
}
else
{
toValue = noop;
if (this.isBoolean())
{
convert = convertRawBoolean;
toString = toStringFromBoolean;
}
else if (this.isDate())
{
convert = convertRawDate;
toString = toStringFromDate;
format = statementParameters['DATE_OUTPUT_FORMAT'];
}
else if (this.isTimestamp())
{
if (this.isTimestampLtz())
{
convert = convertRawTimestampLtz;
toString = toStringFromTimestamp;
format = statementParameters['TIMESTAMP_LTZ_OUTPUT_FORMAT'];
}
else if (this.isTimestampNtz())
{
convert = convertRawTimestampNtz;
toString = toStringFromTimestamp;
format = statementParameters['TIMESTAMP_NTZ_OUTPUT_FORMAT'];
}
else if (this.isTimestampTz())
{
convert = convertRawTimestampTz;
toString = toStringFromTimestamp;
format = statementParameters['TIMESTAMP_TZ_OUTPUT_FORMAT'];
}
// if we don't have a type-specific timezone, use the default format
if (!format)
{
format = statementParameters['TIMESTAMP_OUTPUT_FORMAT'];
}
}
else if (this.isBinary())
{
convert = convertRawBinary;
toString = toStringFromBinary;
format = statementParameters['BINARY_OUTPUT_FORMAT'];
}
else if (this.isVariant())
{
convert = convertRawVariant;
toString = toStringFromVariant;
}
else
{
// column is of type string, so leave value as is
convert = noop;
toString = toStringFromString;
}
}
// create a private context to pass to the extract function
var context =
{
convert: convert,
toValue: toValue,
toString: toString,
format: format,
resultVersion: resultVersion,
statementParameters: statementParameters
};
/**
* Returns the value of this column in a row.
*
* @param {Object} row
*
* @returns {*}
*/
this.getRowValue = function (row)
{
return extractFromRow.call(this, row, context, false);
};
/**
* Returns the value of this in a row as a String.
*
* @param {Object} row
*
* @returns {String}
*/
this.getRowValueAsString = function (row)
{
return extractFromRow.call(this, row, context, true);
};
}
/**
* Returns a function that can be used to determine if a column is of a given
* type.
*
* @param {String} columnType the column type.
* @param {Function} columnComparisonFn the column comparison function.
* @param {Object} scope the scope in which to invoke the column comparison
* function.
*
* @returns {Function}
*/
function createFnIsColumnOfType(columnType, columnComparisonFn, scope)
{
return function ()
{
return columnComparisonFn.call(scope, columnType);
};
}
/**
* Converts a raw column value of type Number. The returned value is an object
* that contains the raw string version of the value as well as the
* post-processed version of the value obtained after casting to Number.
*
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Object}
*/
function convertRawNumber(rawColumnValue, column, context)
{
return {
raw: rawColumnValue,
processed: Number(rawColumnValue)
};
}
/**
* Converts a raw column value that is an integer. The returned value is an object
* that contains the raw string version of the value as well as the post-processed
* version of the value obtained after casting to bigInt
*
* @param rawColumnValue
* @param column
* @param context
* @returns {{processed: bigInt.BigInteger, raw: *}}
*/
function convertRawBigInt(rawColumnValue, column, context)
{
return {
raw: rawColumnValue,
processed: bigInt(rawColumnValue)
};
}
/**
* Converts a raw column value of type Boolean to a boolean (true, false,
* or null).
*
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Boolean}
*/
function convertRawBoolean(rawColumnValue, column, context)
{
var ret;
if ((rawColumnValue === '1') || (rawColumnValue === 'TRUE'))
{
ret = true;
}
else if ((rawColumnValue === '0') || (rawColumnValue === 'FALSE'))
{
ret = false;
}
return ret;
}
/**
* Converts a raw column value of type Date to a Snowflake Date.
*
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Date}
*/
function convertRawDate(rawColumnValue, column, context)
{
return new SfTimestamp(
Number(rawColumnValue) * 86400, // convert to seconds
0, // no nano seconds
0, // no scale required
'UTC', // use utc as the timezone
context.format).toSfDate();
}
/**
* Converts a raw column value of type Time to a Snowflake Time.
*
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Object}
*/
function convertRawTime(rawColumnValue, column, context)
{
var columnScale = column.getScale();
// the values might be big so use BigNumber to do arithmetic
var valFracSecsBig =
new BigNumber(rawColumnValue).times(Math.pow(10, columnScale));
return convertRawTimestampHelper(
valFracSecsBig,
columnScale,
'UTC',
context.format).toSfTime();
}
/**
* Converts a raw column value of type TIMESTAMP_LTZ to a Snowflake Date.
*
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Date}
*/
function convertRawTimestampLtz(rawColumnValue, column, context)
{
var columnScale = column.getScale();
// the values might be big so use BigNumber to do arithmetic
var valFracSecsBig =
new BigNumber(rawColumnValue).times(Math.pow(10, columnScale));
// create a new snowflake date
return convertRawTimestampHelper(
valFracSecsBig,
columnScale,
context.statementParameters['TIMEZONE'],
context.format).toSfDate();
}
/**
* Converts a raw column value of type TIMESTAMP_NTZ to a Snowflake Date.
*
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Date}
*/
function convertRawTimestampNtz(rawColumnValue, column, context)
{
var columnScale = column.getScale();
// the values might be big so use BigNumber to do arithmetic
var valFracSecsBig =
new BigNumber(rawColumnValue).times(Math.pow(10, columnScale));
// create a new snowflake date
return convertRawTimestampHelper(
valFracSecsBig,
columnScale,
'UTC', // it's _ntz, so use UTC for timezone
context.format).toSfDate();
}
/**
* Converts a raw column value of type TIMESTAMP_TZ to a Snowflake Date.
*
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Date}
*/
function convertRawTimestampTz(rawColumnValue, column, context)
{
var valFracSecsBig;
var valFracSecsWithTzBig;
var timezoneBig;
var timezone;
var timestampAndTZIndex;
// compute the scale factor
var columnScale = column.getScale();
var scaleFactor = Math.pow(10, columnScale);
var resultVersion = context.resultVersion;
if (resultVersion === '0' || resultVersion === undefined)
{
// the values might be big so use BigNumber to do arithmetic
valFracSecsBig =
new BigNumber(rawColumnValue).times(scaleFactor);
// for _tz, the timezone is baked into the value
valFracSecsWithTzBig = valFracSecsBig;
// extract everything but the lowest 14 bits to get the fractional seconds
valFracSecsBig =
valFracSecsWithTzBig.dividedBy(16384).floor();
// extract the lowest 14 bits to get the timezone
if (valFracSecsWithTzBig.greaterThanOrEqualTo(0))
{
timezoneBig = valFracSecsWithTzBig.modulo(16384);
}
else
{
timezoneBig =
valFracSecsWithTzBig.modulo(16384).plus(16384);
}
}
else
{
// split the value into number of seconds and timezone index
timestampAndTZIndex = rawColumnValue.split(' ');
// the values might be big so use BigNumber to do arithmetic
valFracSecsBig =
new BigNumber(timestampAndTZIndex[0]).times(scaleFactor);
timezoneBig = new BigNumber(timestampAndTZIndex[1]);
}
timezone = timezoneBig.toNumber();
// assert that timezone is valid
Errors.assertInternal(timezone >= 0 && timezone <= 2880);
// subtract 24 hours from the timezone to map [0, 48] to
// [-24, 24], and convert the result to a number
timezone = timezone - 1440;
// create a new snowflake date
return convertRawTimestampHelper(
valFracSecsBig,
columnScale,
timezone,
context.format).toSfDate();
}
/**
* Helper function for the convertRawTimestamp*() functions.
* Returns an instance of SfTimestamp.
*
* @param {Object} epochFracSecsBig
* @param {Number} scale
* @param {String | Number} timezone
* @param {String} format
*
* @returns {Object}
*/
function convertRawTimestampHelper(
epochFracSecsBig,
scale,
timezone,
format)
{
// compute the scale factor
var scaleFactor = Math.pow(10, scale);
// split the value into epoch seconds + nanoseconds; for example,
// 1365148923.123456789 will be split into 1365148923 (epoch seconds)
// and 123456789 (nano seconds)
var valSecBig = epochFracSecsBig.dividedBy(scaleFactor).floor();
var fractionsBig = epochFracSecsBig.minus(valSecBig.times(scaleFactor));
var valSecNanoBig = fractionsBig.times(Math.pow(10, 9 - scale));
// create a new snowflake date from the information
return new SfTimestamp(
valSecBig.toNumber(),
valSecNanoBig.toNumber(),
scale,
timezone,
format);
}
/**
* Converts a raw column value of type Variant to a JavaScript value.
*
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Object | Array}
*/
function convertRawVariant(rawColumnValue, column, context) {
// if the input is a non-empty string, convert it to a json object
if (Util.string.isNotNullOrEmpty(rawColumnValue)) {
try {
return GlobalConfig.jsonColumnVariantParser(rawColumnValue);
} catch (jsonParseError) {
try {
return GlobalConfig.xmlColumnVariantParser(rawColumnValue);
} catch (xmlParseError) {
Logger.getInstance().debug("Variant cannot be parsed neither as JSON: %s nor as XML: %s", jsonParseError.message, xmlParseError.message);
throw new Errors.VariantParseError(jsonParseError, xmlParseError);
}
}
}
}
/**
* Converts a raw column value of type Binary to a Buffer.
*
* @param {String} rawColumnValue
* @param {Object} column
* @param {Object} context
*
* @returns {Buffer}
*/
function convertRawBinary(rawColumnValue, column, context)
{
// Ensure the format is valid.
var format = context.format.toUpperCase();
Errors.assertInternal(format === "HEX" || format === "BASE64");
// Decode hex string sent by GS.
var buffer = Buffer.from(rawColumnValue, "HEX");
if (format === "HEX")
{
buffer.toStringSf = function ()
{
// The raw value is already an uppercase hex string, so just return it.
// Note that buffer.toString("HEX") returns a lowercase hex string, but we
// want upper case.
return rawColumnValue;
}
}
else
{
buffer.toStringSf = function ()
{
return this.toString("BASE64");
}
}
buffer.getFormat = function ()
{
return format;
};
return buffer;
}
/**
* Returns the input value as is.
*
* @param {*} value
*
* @returns {*}
*/
function noop(value)
{
return value;
}
/**
* The toValue() function for a column of type Number.
*
* @param {*} columnValue
*
* @returns {Number}
*/
function toValueFromNumber(columnValue)
{
return columnValue ? columnValue.processed : columnValue;
}
/**
* The toValue() function for a column of type Time.
*
* @param {*} columnValue
*
* @returns {String}
*/
function toValueFromTime(columnValue)
{
// there's no native javascript type that can be used to represent time, so
// just convert to string
return toStringFromTime(columnValue);
}
/**
* The toString() function for a column of type Number.
*
* @param {Number} columnValue
*
* @returns {String}
*/
function toStringFromNumber(columnValue)
{
return (columnValue !== null) ? columnValue.raw : NULL_UPPERCASE;
}
/**
* The toString() function for a column of type Boolean.
*
* @param {Boolean} columnValue
*
* @returns {String}
*/
function toStringFromBoolean(columnValue)
{
return (columnValue !== null) ? String(columnValue).toUpperCase() :
NULL_UPPERCASE;
}
/**
* The toString() function for a column of type Date.
*
* @param {Date} columnValue
*
* @returns {String}
*/
function toStringFromDate(columnValue)
{
return (columnValue !== null) ? columnValue.toJSON() : NULL_UPPERCASE;
}
/**
* The toString() function for a column of type Time.
*
* @param {Object} columnValue
*
* @returns {String}
*/
function toStringFromTime(columnValue)
{
return (columnValue !== null) ? columnValue.toJSON() : NULL_UPPERCASE;
}
/**
* The toString() function for a column of type Timestamp.
*
* @param {Date} columnValue
*
* @returns {String}
*/
function toStringFromTimestamp(columnValue)
{
return (columnValue !== null) ? columnValue.toJSON() : NULL_UPPERCASE;
}
/**
* The toString() function for a column of type Variant.
*
* @param {Object} columnValue
*
* @returns {String}
*/
function toStringFromVariant(columnValue)
{
return (columnValue !== null) ? JSON.stringify(columnValue) : NULL_UPPERCASE;
}
/**
* The toString() function for a column of type String.
*
* @param {String} columnValue
*
* @returns {String}
*/
function toStringFromString(columnValue)
{
return (columnValue !== null) ? columnValue : NULL_UPPERCASE;
}
/**
* The toString() function for a column of type Binary.
*
* @param {Buffer} columnValue
*
* @returns {String}
*/
function toStringFromBinary(columnValue)
{
return (columnValue !== null) ? columnValue.toStringSf() : NULL_UPPERCASE;
}
/**
* Extracts the value of a column from a given row.
*
* @param {Object} row
* @param {Object} context
* @param {Boolean} asString
*
* @returns {*}
*/
function extractFromRow(row, context, asString)
{
var map = row._arrayProcessedColumns;
var values = row.values;
// get the value
var columnIndex = this.getIndex();
var ret = values[columnIndex];
// if we want the value as a string, and the column is of type variant, and we
// haven't already processed the value before, we don't need to process the
// value, so only process if none of the aforementioned conditions are true
if (!(asString && this.isVariant() && !map[columnIndex]))
{
// if the column value has not been processed yet, process it, put it back
// in the values array, and remember that the value has been processed
if (!map[columnIndex])
{
if (ret !== null)
{
ret = values[columnIndex] =
context.convert(values[columnIndex], this, context);
}
map[columnIndex] = true;
}
// use the appropriate extraction function depending on whether
// we want the value or a string representation of the value
var extractFn = !asString ? context.toValue : context.toString;
ret = extractFn(ret);
}
return ret;
}
module.exports = Column;