-
Notifications
You must be signed in to change notification settings - Fork 693
Expand file tree
/
Copy path40select.js
More file actions
executable file
·795 lines (710 loc) · 22.6 KB
/
40select.js
File metadata and controls
executable file
·795 lines (710 loc) · 22.6 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
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
786
787
788
789
790
791
792
793
794
795
/*
//
// Select run-time part for Alasql.js
// Date: 03.11.2014
// (c) 2014, Andrey Gershun
//
*/
//
// Main part of SELECT procedure
//
/* global yy */
yy.Select = class Select {
constructor(params) {
Object.assign(this, params);
}
toString() {
var s;
s = '';
if (this.explain) {
s += 'EXPLAIN ';
}
s += 'SELECT ';
if (this.modifier) {
s += this.modifier + ' ';
}
if (this.distinct) {
s += 'DISTINCT ';
}
if (this.top) {
s += 'TOP ' + this.top.value + ' ';
if (this.percent) {
s += 'PERCENT ';
}
}
s += this.columns
.map(function (col) {
var s;
s = col.toString();
if (typeof col.as !== 'undefined') {
s += ' AS ' + col.as;
}
return s;
})
.join(', ');
if (this.from) {
s +=
' FROM ' +
this.from
.map(function (f) {
var ss;
ss = f.toString();
if (f.as) {
ss += ' AS ' + f.as;
}
return ss;
})
.join(',');
}
if (this.joins) {
s += this.joins
.map(function (jn) {
var ss;
ss = ' ';
if (jn.joinmode) {
ss += jn.joinmode + ' ';
}
if (jn.table) {
ss += 'JOIN ' + jn.table.toString();
} else if (jn.select) {
ss += 'JOIN (' + jn.select.toString() + ')';
} else if (jn instanceof alasql.yy.Apply) {
ss += jn.toString();
} else {
throw new Error('Wrong type in JOIN mode');
}
if (jn.as) {
ss += ' AS ' + jn.as;
}
if (jn.using) {
ss += ' USING ' + jn.using.toString();
}
if (jn.on) {
ss += ' ON ' + jn.on.toString();
}
return ss;
})
.join('');
}
if (this.where) {
s += ' WHERE ' + this.where.toString();
}
if (this.group && this.group.length > 0) {
s +=
' GROUP BY ' +
this.group
.map(function (grp) {
return grp.toString();
})
.join(', ');
}
if (this.having) {
s += ' HAVING ' + this.having.toString();
}
if (this.order && this.order.length > 0) {
s +=
' ORDER BY ' +
this.order
.map(function (ord) {
return ord.toString();
})
.join(', ');
}
if (this.limit) {
s += ' LIMIT ' + this.limit.value;
}
if (this.offset) {
s += ' OFFSET ' + this.offset.value;
}
if (this.union) {
s += ' UNION ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.union.toString();
}
if (this.unionall) {
s += ' UNION ALL ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.unionall.toString();
}
if (this.except) {
s += ' EXCEPT ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.except.toString();
}
if (this.intersect) {
s += ' INTERSECT ' + (this.corresponding ? 'CORRESPONDING ' : '') + this.intersect.toString();
}
return s;
}
/**
Select statement in expression
*/
toJS(context) {
var s =
'alasql.utils.flatArray(this.queriesfn[' +
(this.queriesidx - 1) +
'](this.params,null,' +
context +
'))[0]';
// var s = '(ee=alasql.utils.flatArray(this.queriesfn['+(this.queriesidx-1)+'](this.params,null,'+context+')),console.log(999,ee),ee[0])';
return s;
}
// Compile SELECT statement
compile(databaseid, params) {
var db = alasql.databases[databaseid];
// Create variable for query
var query = new Query();
// Array with columns to be removed
query.removeKeys = [];
query.aggrKeys = [];
query.explain = this.explain; // Explain
query.explaination = [];
query.explid = 1;
query.modifier = this.modifier;
query.database = db;
// 0. Precompile whereexists
this.compileWhereExists(query);
// 0. Precompile queries for IN, NOT IN, ANY and ALL operators
this.compileQueries(query);
query.defcols = this.compileDefCols(query, databaseid);
// 1. Compile FROM clause
query.fromfn = this.compileFrom(query);
// 2. Compile JOIN clauses
if (this.joins) {
this.compileJoins(query);
}
// todo?: 3. Compile SELECT clause
// For ROWNUM()
query.rownums = [];
query.grouprownums = [];
query.windowaggrs = []; // For window aggregate functions (COUNT/MAX/MIN/SUM/AVG with OVER)
// Check if INTO OBJECT() is used - this affects how arrow expressions are compiled
if (this.into instanceof yy.FuncValue && this.into.funcid.toUpperCase() === 'OBJECT') {
query.intoObject = true;
}
this.compileSelectGroup0(query);
if (this.group || query.selectGroup.length > 0) {
query.selectgfns = this.compileSelectGroup1(query);
} else {
query.selectfns = this.compileSelect1(query, params);
}
// Remove columns clause
this.compileRemoveColumns(query);
// 5. Optimize WHERE and JOINS
if (this.where) {
this.compileWhereJoins(query);
}
// 4. Compile WHERE clause
query.wherefn = this.compileWhere(query);
// 6. Compile GROUP BY
if (this.group || query.selectGroup.length > 0) {
query.groupfn = this.compileGroup(query);
}
// 6. Compile HAVING
if (this.having) {
query.havingfn = this.compileHaving(query);
}
// 8. Compile ORDER BY clause
if (this.order) {
query.orderfn = this.compileOrder(query, params);
// Copy orderColumns to query for union handling
query.orderColumns = this.orderColumns;
}
if (this.group || query.selectGroup.length > 0) {
query.selectgfn = this.compileSelectGroup2(query);
} else {
query.selectfn = this.compileSelect2(query, params);
}
// 7. Compile DISTINCT, LIMIT and OFFSET
query.distinct = this.distinct;
// 9. Compile PIVOT clause
if (this.pivot) query.pivotfn = this.compilePivot(query);
if (this.unpivot) query.pivotfn = this.compileUnpivot(query);
// 10. Compile TOP/LIMIT/OFFSET/FETCH clause
if (this.top) {
query.limit = this.top.value;
} else if (this.limit) {
query.limit = this.limit.value;
if (this.offset) {
query.offset = this.offset.value;
}
}
query.percent = this.percent;
// 9. Compile ordering function for UNION and UNIONALL
query.corresponding = this.corresponding; // If CORRESPONDING flag exists
if (this.union) {
query.unionfn = this.union.compile(databaseid);
// ORDER BY is now at the top level, not in the union clause
if (!query.orderfn && this.union.order) {
query.orderfn = this.union.compileOrder(query, params);
}
} else if (this.unionall) {
query.unionallfn = this.unionall.compile(databaseid);
// ORDER BY is now at the top level, not in the unionall clause
if (!query.orderfn && this.unionall.order) {
query.orderfn = this.unionall.compileOrder(query, params);
}
} else if (this.except) {
query.exceptfn = this.except.compile(databaseid);
// ORDER BY is now at the top level, not in the except clause
if (!query.orderfn && this.except.order) {
query.orderfn = this.except.compileOrder(query, params);
}
} else if (this.intersect) {
query.intersectfn = this.intersect.compile(databaseid);
// ORDER BY is now at the top level, not in the intersect clause
if (!query.orderfn && this.intersect.order) {
query.orderfn = this.intersect.compileOrder(query, params);
}
}
// SELECT INTO
if (this.into) {
if (this.into instanceof yy.Table) {
// Save into the table in database
if (
alasql.options.autocommit &&
alasql.databases[this.into.databaseid || databaseid].engineid
) {
// For external database when AUTOCOMMIT is ONs
query.intoallfns = `return alasql
.engines[${JSON.stringify(alasql.databases[this.into.databaseid || databaseid].engineid)}]
.intoTable(
${JSON.stringify(this.into.databaseid || databaseid)},
${JSON.stringify(this.into.tableid)},
this.data,
columns,
cb
);`;
} else {
// Into AlaSQL tables
query.intofns = `alasql
.databases[${JSON.stringify(this.into.databaseid || databaseid)}]
.tables[${JSON.stringify(this.into.tableid)}]
.data.push(r);
`;
}
} else if (this.into instanceof yy.VarValue) {
//
// Save into local variable
// SELECT * INTO @VAR1 FROM ?
//
query.intoallfns = `
alasql.vars[${JSON.stringify(this.into.variable)}]=this.data;
res=this.data.length;
if(cb) res = cb(res);
return res;
`;
} else if (this.into instanceof yy.FuncValue) {
//
// If this is INTO() function, then call it
// with one or two parameters
//
var funcid = this.into.funcid.toUpperCase();
var qs = 'return alasql.into[' + JSON.stringify(funcid) + '](';
if (this.into.args && this.into.args.length > 0) {
qs += this.into.args[0].toJS() + ',';
if (this.into.args.length > 1) {
qs += this.into.args[1].toJS() + ',';
} else {
qs += 'undefined,';
}
} else {
qs += 'undefined, undefined,';
}
query.intoallfns = qs + 'this.data,columns,cb)';
// Mark that OBJECT should preserve array results
if (funcid === 'OBJECT') {
query.preserveArrayResult = true;
}
} else if (this.into instanceof yy.ParamValue) {
//
// Save data into parameters array
// like alasql('SELECT * INTO ? FROM ?',[outdata,srcdata]);
// or SELECT * INTO $variable FROM ?
//
// Distinguish between ? (numeric param - push to array) and $variable (string param - replace array)
if (typeof this.into.param === 'string') {
// $variable syntax - replace the array
query.intoallfns = `
if(!params[${JSON.stringify(this.into.param)}]) params[${JSON.stringify(this.into.param)}]=[];
params[${JSON.stringify(this.into.param)}]=this.data;
res=this.data.length;
if(cb) res = cb(res);
return res;
`;
} else {
// ? syntax - push to existing array
query.intofns = `params[${JSON.stringify(this.into.param)}].push(r)`;
}
}
if (query.intofns) {
// Create intofn function
query.intofn = new Function('r,i,params,alasql', 'var y;' + query.intofns);
} else if (query.intoallfns) {
// Create intoallfn function
query.intoallfn = new Function('columns,cb,params,alasql', 'var y;' + query.intoallfns);
}
}
// Now, compile all togeather into one function with query object in scope
var statement = function (params, cb, oldscope) {
query.params = params;
// Note the callback function has the data and error reversed due to existing code in promiseExec which has the
// err and data swapped. This trickles down into alasql.exec and further. Rather than risk breaking the whole thing,
// the (data, err) standard is maintained here.
var res1 = queryfn(query, oldscope, function (res, err) {
if (err) {
if (cb) {
return cb(null, err);
}
throw err;
}
if (query.rownums.length > 0) {
for (var i = 0, ilen = res.length; i < ilen; i++) {
for (var j = 0, jlen = query.rownums.length; j < jlen; j++) {
res[i][query.rownums[j]] = i + 1;
}
}
}
// Handle GROUP_ROW_NUMBER() and ROW_NUMBER() OVER (PARTITION BY ...) - restart numbering when grouping column(s) change
if (query.grouprownums && query.grouprownums.length > 0) {
for (var j = 0, jlen = query.grouprownums.length; j < jlen; j++) {
var config = query.grouprownums[j];
var partitionColumns;
// Determine which columns to partition by
if (config.partitionColumns && config.partitionColumns.length > 0) {
// Use explicit PARTITION BY columns
partitionColumns = config.partitionColumns;
} else {
// Fall back to first column for GROUP_ROW_NUMBER()
var columnKeys = Object.keys(res[0] || {});
partitionColumns = [columnKeys[0]];
}
var prevValues = null;
var rowNum = 0;
for (var i = 0, ilen = res.length; i < ilen; i++) {
// Get current partition key (combination of all partition columns)
var currentValues = partitionColumns
.map(function (col) {
return res[i][col];
})
.join('|');
// Reset counter when partition changes
if (i === 0 || currentValues !== prevValues) {
rowNum = 1;
} else {
rowNum++;
}
res[i][config.as] = rowNum;
prevValues = currentValues;
}
}
}
// Window offset functions: LEAD/LAG/FIRST_VALUE/LAST_VALUE
// Scans results linearly to compute values based on relative row positions
if (query.windowFuncs && query.windowFuncs.length > 0) {
for (var j = 0; j < query.windowFuncs.length; j++) {
var wf = query.windowFuncs[j];
var partCols = wf.partitionColumns || [];
var exprCol = wf.args[0] && wf.args[0].columnid;
// Parse offset and default value arguments (handles negative literals like -1)
var getArg = function (a) {
if (!a) return undefined;
if (a.value !== undefined) return a.value;
if (a.op === '-' && a.right && a.right.value !== undefined) return -a.right.value;
return undefined;
};
var offset = getArg(wf.args[1]);
if (offset === undefined) offset = 1;
var defVal = getArg(wf.args[2]);
if (defVal === undefined) defVal = null;
// Track partition boundaries as we scan
var prevPart = null;
var partStart = 0;
// Scan rows, processing each partition when boundaries change
for (var i = 0; i <= res.length; i++) {
var currPart =
i < res.length && partCols.length > 0
? partCols
.map(function (c) {
return res[i][c];
})
.join('|')
: null;
// When partition ends, compute window function for all rows in partition
if (i === res.length || (prevPart !== null && currPart !== prevPart)) {
for (var k = partStart; k < i; k++) {
var targetIdx;
if (wf.funcid === 'LEAD') {
targetIdx = k + offset;
res[k][wf.as] = targetIdx < i && exprCol ? res[targetIdx][exprCol] : defVal;
} else if (wf.funcid === 'LAG') {
targetIdx = k - offset;
res[k][wf.as] =
targetIdx >= partStart && exprCol ? res[targetIdx][exprCol] : defVal;
} else if (wf.funcid === 'FIRST_VALUE') {
res[k][wf.as] = exprCol ? res[partStart][exprCol] : null;
} else if (wf.funcid === 'LAST_VALUE') {
res[k][wf.as] = exprCol ? res[i - 1][exprCol] : null;
}
}
partStart = i;
}
prevPart = currPart;
}
}
}
// Handle window aggregate functions - COUNT/MAX/MIN/SUM/AVG with OVER (PARTITION BY ...)
if (query.windowaggrs && query.windowaggrs.length > 0) {
for (var j = 0, jlen = query.windowaggrs.length; j < jlen; j++) {
var config = query.windowaggrs[j];
var partitions = {};
// Group rows by partition
for (var i = 0, ilen = res.length; i < ilen; i++) {
var partitionKey =
config.partitionColumns && config.partitionColumns.length > 0
? config.partitionColumns
.map(function (col) {
return res[i][col];
})
.join('|')
: '__all__';
if (!partitions[partitionKey]) partitions[partitionKey] = [];
partitions[partitionKey].push(i);
}
// Calculate and assign aggregate for each partition
for (var partitionKey in partitions) {
var rowIndices = partitions[partitionKey];
var values = [];
var colId = config.expression && config.expression.columnid;
// Collect values from partition rows
if (config.aggregatorid !== 'COUNT' || (colId && colId !== '*')) {
for (var k = 0; k < rowIndices.length; k++) {
var val = res[rowIndices[k]][colId];
if (val != null) values.push(val);
}
}
// Calculate aggregate
var aggregateValue;
switch (config.aggregatorid) {
case 'COUNT':
aggregateValue = colId && colId !== '*' ? values.length : rowIndices.length;
break;
case 'SUM':
aggregateValue = values.reduce(function (sum, v) {
return sum + v;
}, 0);
break;
case 'AVG':
aggregateValue =
values.length > 0
? values.reduce(function (sum, v) {
return sum + v;
}, 0) / values.length
: null;
break;
case 'MAX':
aggregateValue = values.length > 0 ? Math.max.apply(null, values) : null;
break;
case 'MIN':
aggregateValue = values.length > 0 ? Math.min.apply(null, values) : null;
break;
}
// Assign aggregate value to all rows in partition
for (var k = 0; k < rowIndices.length; k++) {
res[rowIndices[k]][config.as] = aggregateValue;
}
}
}
}
var res2 = modify(query, res);
if (cb) {
cb(res2);
}
return res2;
});
return res1;
};
statement.query = query;
return statement;
}
execute(databaseid, params, cb) {
return this.compile(databaseid)(params, cb);
// throw new Error('Insert statement is should be compiled')
}
compileWhereExists(query) {
if (!this.exists) return;
query.existsfn = this.exists.map(function (ex) {
var nq = ex.compile(query.database.databaseid);
nq.query.modifier = 'RECORDSET';
return nq;
});
}
compileQueries(query) {
if (!this.queries) return;
// Helper function to detect if a subquery might be correlated
// A subquery is correlated if it references tables not in its own FROM clause
const isCorrelated = (subquery, outerQuery) => {
if (!subquery.from) return false;
// Get table names from subquery's FROM clause
const subqueryTables = new Set();
subquery.from.forEach(f => {
if (f.tableid) subqueryTables.add(f.tableid);
if (f.as) subqueryTables.add(f.as);
});
// Check if WHERE clause references tables not in subquery's FROM
const referencesExternal = node => {
if (!node) return false;
// Check Column nodes for tableid using instanceof
if (node instanceof yy.Column) {
if (node.tableid && !subqueryTables.has(node.tableid)) {
return true;
}
}
// Recursively check own properties only (not inherited)
for (let key of Object.keys(node)) {
if (node[key] && typeof node[key] === 'object') {
if (referencesExternal(node[key])) return true;
}
}
return false;
};
return referencesExternal(subquery.where) || referencesExternal(subquery.columns);
};
query.queriesfn = this.queries.map(function (q, idx) {
var nq = q.compile(query.database.databaseid);
nq.query.modifier = 'RECORDSET';
// Mark as correlated if it references external tables
nq.query.isCorrelated = isCorrelated(q, query);
// If the nested query has its own queries, ensure they're compiled too
// This handles nested subqueries properly
if (q.queries && q.queries.length > 0) {
nq.query.queriesfn = q.queries.map(function (qq) {
var nnq = qq.compile(query.database.databaseid);
nnq.query.modifier = 'RECORDSET';
return nnq;
});
}
return nq;
});
}
};
/**
Modify res according modifier
@function
@param {object} query Query object
@param res {object|number|string|boolean} res Data to be converted
*/
function modify(query, res) {
// jshint ignore:line
/* If source is a primitive value then return it */
if (
typeof res === 'undefined' ||
typeof res === 'number' ||
typeof res === 'string' ||
typeof res === 'boolean'
) {
return res;
}
var modifier = query.modifier || alasql.options.modifier;
var columns = query.columns;
// If dirtyColumns is true, we need to merge columns from data with existing columns
// This happens when SELECT * is used with dynamic data sources (like parameters)
if (query.dirtyColumns && res.length > 0) {
var allcol = {};
// First, scan the data to find all column names
for (var i = Math.min(res.length, alasql.options.columnlookup || 10) - 1; 0 <= i; i--) {
for (var key in res[i]) {
allcol[key] = true;
}
}
// Create columns from data
var dataColumns = Object.keys(allcol).map(function (columnid) {
return {columnid: columnid};
});
// If we don't have any columns yet, just use the data columns
if (!columns || columns.length === 0) {
columns = dataColumns;
} else {
// We have some columns (e.g., from explicit column expressions),
// merge them with data columns, avoiding duplicates
var existingColumnIds = {};
columns.forEach(function (col) {
existingColumnIds[col.columnid] = true;
});
// Add data columns that aren't already in the list
dataColumns.forEach(function (col) {
if (!existingColumnIds[col.columnid]) {
columns.push(col);
}
});
}
} else if (typeof columns === 'undefined' || columns.length === 0) {
// Try to create columns
if (res.length > 0) {
var allcol = {};
for (var i = Math.min(res.length, alasql.options.columnlookup || 10) - 1; 0 <= i; i--) {
for (var key in res[i]) {
allcol[key] = true;
}
}
columns = Object.keys(allcol).map(function (columnid) {
return {columnid: columnid};
});
} else {
// Cannot recognize columns
columns = [];
if (query && query.sources) {
query.sources.forEach(source => {
if (source && source.columns && Array.isArray(source.columns)) {
columns = columns.concat(source.columns);
}
});
}
}
}
switch (modifier) {
case 'VALUE':
if (res.length === 0) return undefined;
const keyValue = columns && columns.length > 0 ? columns[0].columnid : Object.keys(res[0])[0];
return res[0][keyValue];
case 'ROW':
if (res.length === 0) return undefined;
return Object.values(res[0]);
case 'COLUMN':
if (res.length === 0) return [];
let key;
if (columns && columns.length > 0) {
key = columns[0].columnid;
} else {
key = Object.keys(res[0])[0];
}
let ar = [];
for (var i = 0, ilen = res.length; i < ilen; i++) {
ar.push(res[i][key]);
}
// Apply DISTINCT if specified
if (query.distinct) {
ar = alasql.utils.distinctArray(ar);
}
return ar;
case 'MATRIX':
if (res.length === 0) return undefined;
return res.map(row => columns.map(col => row[col.columnid]));
case 'INDEX':
if (res.length === 0) return undefined;
const keyIndex = columns && columns.length > 0 ? columns[0].columnid : Object.keys(res[0])[0];
const valIndex = columns && columns.length > 1 ? columns[1].columnid : Object.keys(res[0])[1];
return res.reduce((acc, row) => ({...acc, [row[keyIndex]]: row[valIndex]}), {});
case 'RECORDSET':
// Assuming alasql.Recordset is available in the scope
return new alasql.Recordset({columns: columns, data: res});
case 'TEXTSTRING':
if (res.length === 0) return undefined;
const keyTextString =
columns && columns.length > 0 ? columns[0].columnid : Object.keys(res[0])[0];
return res.map(row => row[keyTextString]).join('\n');
case 'ALASQL_DETAILS':
// Returns both data and column metadata in a structured format
// Useful for internal operations that need both data and column info in one call
return {
data: res,
columns: columns,
length: res.length,
};
}
return res;
}