Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/40select.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ yy.Select = class Select {
query.rownums = [];
query.grouprownums = [];
query.windowaggrs = []; // For window aggregate functions (COUNT/MAX/MIN/SUM/AVG with OVER)
query.windowfns = []; // For positional window functions (LEAD/LAG/FIRST_VALUE/LAST_VALUE)

// Check if INTO OBJECT() is used - this affects how arrow expressions are compiled
if (this.into instanceof yy.FuncValue && this.into.funcid.toUpperCase() === 'OBJECT') {
Expand Down Expand Up @@ -509,6 +510,83 @@ yy.Select = class Select {
}
}

// Handle positional window functions - LEAD/LAG/FIRST_VALUE/LAST_VALUE
if (query.windowfns && query.windowfns.length > 0) {
for (var j = 0, jlen = query.windowfns.length; j < jlen; j++) {
var wfConfig = query.windowfns[j];
var partitions = {};

// Group rows by partition key
for (var i = 0, ilen = res.length; i < ilen; i++) {
var partitionKey =
wfConfig.partitionColumns && wfConfig.partitionColumns.length > 0
? wfConfig.partitionColumns
.map(function (col) {
return res[i][col];
})
.join('|')
: '__all__';

if (!partitions[partitionKey]) partitions[partitionKey] = [];
partitions[partitionKey].push(i);
}

// Process each partition
for (var partitionKey in partitions) {
var rowIndices = partitions[partitionKey];

// Sort row indices within partition by ORDER BY columns
if (wfConfig.orderColumns && wfConfig.orderColumns.length > 0) {
rowIndices.sort(function (a, b) {
for (var oi = 0; oi < wfConfig.orderColumns.length; oi++) {
var ocol = wfConfig.orderColumns[oi];
var va = res[a][ocol.columnid];
var vb = res[b][ocol.columnid];
if (va == null && vb == null) continue;
if (va == null) return ocol.direction === 'ASC' ? -1 : 1;
if (vb == null) return ocol.direction === 'ASC' ? 1 : -1;
if (va < vb) return ocol.direction === 'ASC' ? -1 : 1;
if (va > vb) return ocol.direction === 'ASC' ? 1 : -1;
}
return 0;
});
}

// Compute values for each row in the partition
for (var k = 0; k < rowIndices.length; k++) {
var idx = rowIndices[k];
var colId = wfConfig.expressionColumnId;
var value;

switch (wfConfig.funcid) {
case 'LEAD':
var leadIdx = k + wfConfig.offset;
value =
leadIdx < rowIndices.length
? res[rowIndices[leadIdx]][colId]
: wfConfig.defaultValue;
break;
case 'LAG':
var lagIdx = k - wfConfig.offset;
value =
lagIdx >= 0
? res[rowIndices[lagIdx]][colId]
: wfConfig.defaultValue;
break;
case 'FIRST_VALUE':
value = res[rowIndices[0]][colId];
break;
case 'LAST_VALUE':
value = res[rowIndices[rowIndices.length - 1]][colId];
break;
}

res[idx][wfConfig.as] = value;
}
}
}
}

var res2 = modify(query, res);

if (cb) {
Expand Down
47 changes: 47 additions & 0 deletions src/424select.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,53 @@ yy.Select.prototype.compileSelectGroup0 = function (query) {
if (col.funcid && col.funcid.toUpperCase() === 'GROUP_ROW_NUMBER') {
query.grouprownums.push({as: col.as, columnIndex: 0}); // Track which column to use for grouping
}

// Detect positional window functions: LEAD, LAG, FIRST_VALUE, LAST_VALUE
if (col.funcid) {
var fid = col.funcid.toUpperCase();
if (
fid === 'LEAD' ||
fid === 'LAG' ||
fid === 'FIRST_VALUE' ||
fid === 'LAST_VALUE'
) {
var wfConfig = {
funcid: fid,
as: col.as,
expressionColumnId:
col.args && col.args[0] ? col.args[0].columnid : null,
offset:
col.args && col.args[1] ? col.args[1].value : 1,
defaultValue:
col.args && col.args[2]
? col.args[2].value != null
? col.args[2].value
: col.args[2].op === '-' && col.args[2].right
? -col.args[2].right.value
: null
: null,
partitionColumns:
col.over && col.over.partition
? col.over.partition.map(function (p) {
return p.columnid || p.toString();
})
: [],
orderColumns:
col.over && col.over.order
? col.over.order.map(function (o) {
return {
columnid:
o.expression && o.expression.columnid
? o.expression.columnid
: o.columnid || o.toString(),
direction: o.direction || 'ASC',
};
})
: [],
};
query.windowfns.push(wfConfig);
}
}
// console.log("colas:",colas);
// }
} else {
Expand Down
12 changes: 12 additions & 0 deletions src/55functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ stdlib.ROW_NUMBER = function () {
stdlib.GROUP_ROW_NUMBER = function () {
return '1';
};
stdlib.LEAD = function () {
return 'undefined';
};
stdlib.LAG = function () {
return 'undefined';
};
stdlib.FIRST_VALUE = function () {
return 'undefined';
};
stdlib.LAST_VALUE = function () {
return 'undefined';
};

stdlib.SQRT = function (s) {
return 'Math.sqrt(' + s + ')';
Expand Down
Loading