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 @@ -188,6 +188,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 @@ -528,6 +529,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
55 changes: 55 additions & 0 deletions src/47over.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,58 @@ 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 || this.nick,
expressionColumnId: this.args && this.args[0] ? this.args[0].columnid : null,
offset:
this.args && this.args[1] != null && this.args[1].value != null ? 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';
}
};
23 changes: 22 additions & 1 deletion src/alasqlparser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ DATABASE(S)? return 'DATABASE'
'FALSE' return 'FALSE'
'FETCH' return 'FETCH'
'FIRST' return 'FIRST'
'FIRST_VALUE'\s*/'(' return 'FIRST_VALUE'
'FOR' return 'FOR'
'FOREIGN' return 'FOREIGN'
'FROM' return 'FROM'
Expand Down Expand Up @@ -165,7 +166,10 @@ DATABASE(S)? return 'DATABASE'
'ITERATE' return 'ITERATE'
'JOIN' return 'JOIN'
'KEY' return 'KEY'
'LAG'\s*/'(' return 'LAG'
'LAST' return 'LAST'
'LAST_VALUE'\s*/'(' return 'LAST_VALUE'
'LEAD'\s*/'(' return 'LEAD'
'LET' return 'LET'
'LEAVE' return 'LEAVE'
'LEFT' return 'LEFT'
Expand Down Expand Up @@ -1544,7 +1548,8 @@ 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(alasql.aggr[$1]) {
$$ = new yy.AggrValue({aggregatorid: 'REDUCE',
Expand All @@ -1553,6 +1558,14 @@ FuncValue
$$ = new yy.FuncValue({funcid: funcid, args: exprlist, over: $6});
};
}
| LEAD LPAR ExprList RPAR OverClause
{ $$ = new yy.PositionalWindowFunc({funcid: 'LEAD', args: $3, over: $5}); }
| LAG LPAR ExprList RPAR OverClause
{ $$ = new yy.PositionalWindowFunc({funcid: 'LAG', args: $3, over: $5}); }
| FIRST_VALUE LPAR ExprList RPAR OverClause
{ $$ = new yy.PositionalWindowFunc({funcid: 'FIRST_VALUE', args: $3, over: $5}); }
| LAST_VALUE LPAR ExprList RPAR OverClause
{ $$ = new yy.PositionalWindowFunc({funcid: 'LAST_VALUE', args: $3, over: $5}); }
| Literal LPAR RPAR OverClause
{ $$ = new yy.FuncValue({ funcid: $1, over: $4 }) }
| IF LPAR ExprList RPAR
Expand Down Expand Up @@ -3353,6 +3366,7 @@ NonReserved
|FILE
|FINAL
|FIRST
|FIRST_VALUE
|FLAG
|FOLLOWING
|FORTRAN
Expand Down Expand Up @@ -3387,7 +3401,10 @@ NonReserved
|KEY
|KEY_MEMBER
|KEY_TYPE
|LAG
|LAST
|LAST_VALUE
|LEAD
|LENGTH
|LEVEL
|LIBRARY
Expand Down Expand Up @@ -3632,6 +3649,7 @@ var nonReserved = ["A"
,"FILE"
,"FINAL"
,"FIRST"
,"FIRST_VALUE"
,"FLAG"
,"FOLLOWING"
,"FORTRAN"
Expand Down Expand Up @@ -3666,7 +3684,10 @@ var nonReserved = ["A"
,"KEY"
,"KEY_MEMBER"
,"KEY_TYPE"
,"LAG"
,"LAST"
,"LAST_VALUE"
,"LEAD"
,"LENGTH"
,"LEVEL"
,"LIBRARY"
Expand Down
Loading