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
75 changes: 75 additions & 0 deletions src/40select.js
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way to structure this functionality

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,80 @@ 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
54 changes: 54 additions & 0 deletions src/47over.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,57 @@ yy.Over = class Over {
return s;
}
};

yy.PositionalWindowFunc = class PositionalWindowFunc {
constructor(params) {
Object.assign(this, params);
}

toString() {
let s = this.funcid + '(';
if (this.args && this.args.length) {
s += this.args.map(a => a.toString()).join(',');
}
s += ')';
if (this.over) s += ' ' + this.over.toString();
return s;
}

findAggregator(query) {
const defaultArg = this.args && this.args[2];
let defaultValue = null;
if (defaultArg) {
if (defaultArg.value != null) {
defaultValue = defaultArg.value;
} else if (defaultArg.op === '-' && defaultArg.right) {
defaultValue = -defaultArg.right.value;
}
}

query.windowfns.push({
funcid: this.funcid,
as: this.as,
expressionColumnId: this.args && this.args[0] ? this.args[0].columnid : null,
offset: this.args && this.args[1] ? this.args[1].value : 1,
defaultValue: defaultValue,
partitionColumns:
this.over && this.over.partition
? this.over.partition.map(p => p.columnid || p.toString())
: [],
orderColumns:
this.over && this.over.order
? this.over.order.map(o => ({
columnid:
o.expression && o.expression.columnid
? o.expression.columnid
: o.columnid || o.toString(),
direction: o.direction || 'ASC',
}))
: [],
});
}

toJS() {
return 'undefined';
}
};
5 changes: 4 additions & 1 deletion src/alasqlparser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -1544,8 +1544,11 @@ FuncValue
{
var funcid = $1;
var exprlist = $4;
if(exprlist.length > 1 && (funcid.toUpperCase() == 'MIN' || funcid.toUpperCase() == 'MAX')) {
var fidU = funcid.toUpperCase();
if(exprlist.length > 1 && (fidU == 'MIN' || fidU == 'MAX')) {
$$ = new yy.FuncValue({funcid: funcid, args: exprlist, over: $6});
} else if(fidU == 'LEAD' || fidU == 'LAG' || fidU == 'FIRST_VALUE' || fidU == 'LAST_VALUE') {
$$ = new yy.PositionalWindowFunc({funcid: fidU, args: exprlist, over: $6});
} else if(alasql.aggr[$1]) {
$$ = new yy.AggrValue({aggregatorid: 'REDUCE',
funcid: funcid, expression: exprlist[0], args: exprlist, distinct:($3=='DISTINCT'), over: $6 });
Expand Down
5 changes: 4 additions & 1 deletion src/alasqlparser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading