Skip to content

Commit ef6c4ea

Browse files
gabriele-tomassettiadamretter
authored andcommitted
[feature] Complete intermediate AST support for CountExpression
1 parent cf562e7 commit ef6c4ea

File tree

3 files changed

+88
-12
lines changed

3 files changed

+88
-12
lines changed

exist-core/src/main/antlr/org/exist/xquery/parser/XQueryTree.g

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,21 +1773,24 @@ throws PermissionDeniedException, EXistException, XPathException
17731773
FLWORClause expr;
17741774
switch (clause.type) {
17751775
case LET:
1776-
expr= new LetExpr(context);
1776+
expr = new LetExpr(context);
17771777
expr.setASTNode(expr_AST_in);
17781778
break;
17791779
case GROUPBY:
1780-
expr = new GroupByClause(context);
1781-
break;
1782-
case ORDERBY:
1783-
expr = new OrderByClause(context, clause.orderSpecs);
1784-
break;
1785-
case WHERE:
1786-
expr = new WhereClause(context, new DebuggableExpression(clause.inputSequence));
1787-
break;
1788-
default:
1789-
expr= new ForExpr(context, clause.allowEmpty);
1790-
break;
1780+
expr = new GroupByClause(context);
1781+
break;
1782+
case ORDERBY:
1783+
expr = new OrderByClause(context, clause.orderSpecs);
1784+
break;
1785+
case WHERE:
1786+
expr = new WhereClause(context, new DebuggableExpression(clause.inputSequence));
1787+
break;
1788+
case COUNT:
1789+
expr = new CountClause(context, clause.varName);
1790+
break;
1791+
default:
1792+
expr = new ForExpr(context, clause.allowEmpty);
1793+
break;
17911794
}
17921795
expr.setASTNode(clause.ast);
17931796
if (clause.type == FLWORClause.ClauseType.FOR || clause.type == FLWORClause.ClauseType.LET) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* eXist-db Open Source Native XML Database
3+
* Copyright (C) 2001 The eXist-db Authors
4+
*
5+
6+
* http://www.exist-db.org
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
package org.exist.xquery;
23+
24+
import org.exist.xquery.util.ExpressionDumper;
25+
import org.exist.xquery.value.Item;
26+
import org.exist.xquery.value.Sequence;
27+
28+
/**
29+
* Implements a count clause inside a FLWOR expressions.
30+
*
31+
* @author <a href="mailto:[email protected]">Adam Retter</a>
32+
* @author <a href="mailto:[email protected]">Gabriele Tomassetti</a>
33+
*/
34+
public class CountClause extends AbstractFLWORClause {
35+
36+
final String varName;
37+
38+
public CountClause(final XQueryContext context, final String countName) {
39+
super(context);
40+
this.varName = countName;
41+
}
42+
43+
@Override
44+
public ClauseType getType() {
45+
return ClauseType.COUNT;
46+
}
47+
48+
public String getVarName() {
49+
return varName;
50+
}
51+
52+
@Override
53+
public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
54+
55+
}
56+
57+
@Override
58+
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
59+
return null;
60+
}
61+
62+
@Override
63+
public void dump(final ExpressionDumper dumper) {
64+
dumper.display("count", this.getLine());
65+
dumper.startIndent();
66+
dumper.display(this.varName);
67+
dumper.endIndent().nl();
68+
}
69+
}

exist-core/src/test/java/org/exist/xquery/CountExpressionTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.StringReader;
3333

3434
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
3536
import static org.junit.jupiter.api.Assertions.fail;
3637

3738
/**
@@ -76,5 +77,8 @@ public void countTest() throws RecognitionException, XPathException, TokenStream
7677
assertEquals(XQueryParser.LITERAL_count, ast.getNextSibling().getFirstChild().getNextSibling().getNextSibling().getType());
7778
// rank variable binding
7879
assertEquals(XQueryParser.VARIABLE_BINDING, ast.getNextSibling().getFirstChild().getNextSibling().getNextSibling().getFirstChild().getType());
80+
assertTrue(((ForExpr)expr.getFirst()).returnExpr instanceof OrderByClause);
81+
assertTrue(((OrderByClause)(((ForExpr)expr.getFirst()).returnExpr)).returnExpr instanceof CountClause);
82+
assertEquals("rank", ((CountClause)((OrderByClause)(((ForExpr)expr.getFirst()).returnExpr)).returnExpr).varName);
7983
}
8084
}

0 commit comments

Comments
 (0)