Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tumbling window #1

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
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
63 changes: 61 additions & 2 deletions exist-core/src/main/antlr/org/exist/xquery/parser/XQuery.g
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ imaginaryTokenDefinitions
PRAGMA
GTEQ
SEQUENCE
TUMBLING_WINDOW
CURRENT_ITEM
PREVIOUS_ITEM
NEXT_ITEM
WINDOW_VARS
;

// === XPointer ===
Expand Down Expand Up @@ -692,7 +697,7 @@ expr throws XPathException

exprSingle throws XPathException
:
( ( "for" | "let" ) DOLLAR ) => flworExpr
( ( "for" | "let" ) ("tumbling" | "sliding" | DOLLAR ) ) => flworExpr
| ( "try" LCURLY ) => tryCatchExpr
| ( ( "some" | "every" ) DOLLAR ) => quantifiedExpr
| ( "if" LPAREN ) => ifExpr
Expand Down Expand Up @@ -799,7 +804,9 @@ flworExpr throws XPathException

initialClause throws XPathException
:
( forClause | letClause )
( ( "for" DOLLAR ) => forClause
| ( "for" ( "tumbling" | "sliding" ) ) => windowClause
| letClause )
;

intermediateClause throws XPathException
Expand All @@ -822,6 +829,11 @@ letClause throws XPathException
"let"^ letVarBinding ( COMMA! letVarBinding )*
;

windowClause throws XPathException
:
"for"! ("tumbling"|"sliding") "window"^ inVarBinding windowStartCondition ( windowEndCondition )?
;

inVarBinding throws XPathException
{ String varName; }
:
Expand All @@ -846,6 +858,37 @@ allowingEmpty
"allowing"! "empty"
;

windowStartCondition throws XPathException
:
"start"^ windowVars "when" exprSingle
;

windowEndCondition throws XPathException
:
( "only" )? "end"^ windowVars "when" exprSingle
;

windowVars throws XPathException
{ String currentItemName = null, previousItemName = null, nextItemName = null; String varName = null; }
:
( DOLLAR! currentItemName=eqName! )?
( "at"! DOLLAR! varName=varName)?
( "previous"! DOLLAR! previousItemName=eqName! )?
( "next"! DOLLAR! nextItemName=eqName! )?
{
windowVars_AST = (org.exist.xquery.parser.XQueryAST)astFactory.create(WINDOW_VARS);
if (currentItemName != null)
windowVars_AST.addChild(astFactory.create(CURRENT_ITEM,currentItemName));
if (varName != null)
windowVars_AST.addChild(astFactory.create(POSITIONAL_VAR,varName));
if (previousItemName != null)
windowVars_AST.addChild(astFactory.create(PREVIOUS_ITEM,previousItemName));
if (nextItemName != null)
windowVars_AST.addChild(astFactory.create(NEXT_ITEM,nextItemName));
currentAST.root = (org.exist.xquery.parser.XQueryAST) windowVars_AST;
}
;

letVarBinding throws XPathException
{ String varName; }
:
Expand Down Expand Up @@ -2224,6 +2267,22 @@ reservedKeywords returns [String name]
"map" { name = "map"; }
|
"array" { name = "array"; }
|
"tumbling" { name = "tumbling"; }
|
"window" { name = "window"; }
|
"start" { name = "start"; }
|
"end" { name = "end"; }
|
"only" { name = "only"; }
|
"previous" { name = "previous"; }
|
"next" { name = "next"; }
|
"sliding" { name = "sliding"; }
;

/**
Expand Down
214 changes: 201 additions & 13 deletions exist-core/src/main/antlr/org/exist/xquery/parser/XQueryTree.g
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ options {
FLWORClause.ClauseType type = FLWORClause.ClauseType.FOR;
List<GroupSpec> groupSpecs = null;
List<OrderSpec> orderSpecs = null;
List<WindowCondition> windowConditions = null;
WindowExpr.WindowType windowType = null;
boolean allowEmpty = false;
}

Expand Down Expand Up @@ -1589,6 +1591,188 @@ throws PermissionDeniedException, EXistException, XPathException
)
)+
)
|
#(
wc:"window"
{
ForLetClause clause= new ForLetClause();
clause.type = FLWORClause.ClauseType.WINDOW;
clause.windowConditions = new ArrayList<WindowCondition>(2);
}
(
"tumbling"
{
clause.windowType = WindowExpr.WindowType.TUMBLING_WINDOW;
}
|
"sliding"
{
clause.windowType = WindowExpr.WindowType.SLIDING_WINDOW;
}
)?
// invarBinding
(
#(
windowWarName:VARIABLE_BINDING
{
clause.ast = windowWarName;
PathExpr inputSequence= new PathExpr(context);
}
(
#(
"as"
{ clause.sequenceType= new SequenceType(); }
sequenceType [clause.sequenceType]
)
)?
step=expr [inputSequence]
{
clause.varName= windowWarName.getText();
clause.inputSequence= inputSequence;
clauses.add(clause);
}
)
)
// windowStartCondition
#(
"start"
{
PathExpr whenExpr = new PathExpr(context);
QName currentItemName = null;
QName previousItemName = null;
QName nextItemName = null;
String windowStartPosVar = null;
}
#(
// WINDOW_VARS
WINDOW_VARS
(
currentItem:CURRENT_ITEM
{
if (currentItem != null && currentItem.getText() != null) {
try {
currentItemName = QName.parse(staticContext, currentItem.getText());
} catch (final IllegalQNameException iqe) {
throw new XPathException(currentItem.getLine(), currentItem.getColumn(), ErrorCodes.XPST0081, "No namespace defined for prefix " + currentItem.getText());
}
}
}
)?
(
startPosVar:POSITIONAL_VAR
{
windowStartPosVar = startPosVar.getText();
}
)?
(
previousItem:PREVIOUS_ITEM
{
if (previousItem != null && previousItem.getText() != null) {
try {
previousItemName= QName.parse(staticContext, previousItem.getText());
} catch (final IllegalQNameException iqe) {
throw new XPathException(previousItem.getLine(), previousItem.getColumn(), ErrorCodes.XPST0081, "No namespace defined for prefix " + previousItem.getText());
}
}
}
)?
(
nextItem:NEXT_ITEM
{
if (nextItem != null && nextItem.getText() != null) {
try {
nextItemName = QName.parse(staticContext, nextItem.getText());
} catch (final IllegalQNameException iqe) {
throw new XPathException(nextItem.getLine(), nextItem.getColumn(), ErrorCodes.XPST0081, "No namespace defined for prefix " + nextItem.getText());
}
}
}
)?
)
"when"
step=expr [whenExpr]
{
WindowCondition windowCondition = new WindowCondition(
context, whenExpr, currentItemName, previousItemName, nextItemName, windowStartPosVar, false
);
clause.windowConditions.add(windowCondition);
}
)
// windowEndCondition
(
{
PathExpr endWhenExpr = new PathExpr(context);
QName endCurrentItemName = null;
QName endPreviousItemName = null;
QName endNextItemName = null;
String windowEndPosVar = null;
Boolean only = false;
}
#(
"end"
(
"only"
{
only = true;
}
)?
#(
// WINDOW_VARS
WINDOW_VARS
(
endCurrentItem:CURRENT_ITEM
{
if (endCurrentItem != null && endCurrentItem.getText() != null) {
try {
endCurrentItemName = QName.parse(staticContext, endCurrentItem.getText());
} catch (final IllegalQNameException iqe) {
throw new XPathException(endCurrentItem.getLine(), endCurrentItem.getColumn(), ErrorCodes.XPST0081, "No namespace defined for prefix " + endCurrentItem.getText());
}
}
}
)?
(
endPosVar:POSITIONAL_VAR
{
windowEndPosVar = endPosVar.getText();
}
)?
(
endPreviousItem:PREVIOUS_ITEM
{
if (endPreviousItem != null && endPreviousItem.getText() != null) {
try {
endPreviousItemName= QName.parse(staticContext, endPreviousItem.getText());
} catch (final IllegalQNameException iqe) {
throw new XPathException(endPreviousItem.getLine(), endPreviousItem.getColumn(), ErrorCodes.XPST0081, "No namespace defined for prefix " + endPreviousItem.getText());
}
}
}
)?
(
endNextItem:NEXT_ITEM
{
if (endNextItem != null && endNextItem.getText() != null) {
try {
endNextItemName = QName.parse(staticContext, endNextItem.getText());
} catch (final IllegalQNameException iqe) {
throw new XPathException(endNextItem.getLine(), endNextItem.getColumn(), ErrorCodes.XPST0081, "No namespace defined for prefix " + endNextItem.getText());
}
}
}
)?
)
"when"
step=expr [endWhenExpr]
{
WindowCondition endWindowCondition = new WindowCondition(
context, endWhenExpr, endCurrentItemName, endPreviousItemName, endNextItemName, windowEndPosVar, only
);
clause.windowConditions.add(endWindowCondition);
}
)
)?
)
|
// XQuery 3.0 group by clause
#(
Expand Down Expand Up @@ -1720,20 +1904,24 @@ throws PermissionDeniedException, EXistException, XPathException
expr= new LetExpr(context);
break;
case GROUPBY:
expr = new GroupByClause(context);
break;
case ORDERBY:
expr = new OrderByClause(context, clause.orderSpecs);
break;
case WHERE:
expr = new WhereClause(context, new DebuggableExpression(clause.inputSequence));
break;
default:
expr= new ForExpr(context, clause.allowEmpty);
break;
expr = new GroupByClause(context);
break;
case ORDERBY:
expr = new OrderByClause(context, clause.orderSpecs);
break;
case WHERE:
expr = new WhereClause(context, new DebuggableExpression(clause.inputSequence));
break;
case WINDOW:
expr = new WindowExpr(context, clause.windowType, clause.windowConditions.get(0), clause.windowConditions.size() > 1 ? clause.windowConditions.get(1) : null);
break;
default:
expr= new ForExpr(context, clause.allowEmpty);
break;
}
expr.setASTNode(clause.ast);
if (clause.type == FLWORClause.ClauseType.FOR || clause.type == FLWORClause.ClauseType.LET) {
if (clause.type == FLWORClause.ClauseType.FOR || clause.type == FLWORClause.ClauseType.LET
|| clause.type == FLWORClause.ClauseType.WINDOW) {
final BindingExpression bind = (BindingExpression)expr;
bind.setVariable(clause.varName);
bind.setSequenceType(clause.sequenceType);
Expand All @@ -1750,7 +1938,7 @@ throws PermissionDeniedException, EXistException, XPathException
}
((GroupByClause)expr).setGroupSpecs(specs);
}
}
}
if (!(action instanceof FLWORClause))
expr.setReturnExpression(new DebuggableExpression(action));
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ public void visitLetExpression(LetExpr letExpr) {
//Nothing to do
}

@Override
public void visitWindowExpression(WindowExpr letExpr) {
//Nothing to do
}

@Override
public void visitOrderByClause(OrderByClause orderBy) {
// Nothing to do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public void visitLetExpression(LetExpr letExpr) {
letExpr.getReturnExpression().accept(this);
}

public void visitWindowExpression(WindowExpr windowExpr) {
windowExpr.getInputSequence().accept(this);
windowExpr.getReturnExpression().accept(this);
}

@Override
public void visitOrderByClause(OrderByClause orderBy) {
for (OrderSpec spec: orderBy.getOrderSpecs()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ public interface ExpressionVisitor {
void visitVariableDeclaration(VariableDeclaration decl);

void visitSimpleMapOperator(OpSimpleMap simpleMap);

void visitWindowExpression(WindowExpr windowExpr);
}
2 changes: 1 addition & 1 deletion exist-core/src/main/java/org/exist/xquery/FLWORClause.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public interface FLWORClause extends Expression {

enum ClauseType {
FOR, LET, GROUPBY, ORDERBY, WHERE, SOME, EVERY
FOR, LET, GROUPBY, ORDERBY, WHERE, SOME, EVERY, WINDOW
}

/**
Expand Down
Loading