Skip to content

Commit

Permalink
merge of within_group and analytic
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Sep 23, 2017
1 parent 1035232 commit 26faba8
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class AnalyticExpression implements Expression {
private boolean allColumns = false;
private WindowElement windowElement;
private KeepExpression keep = null;
private AnalyticType type = AnalyticType.OVER;

@Override
public void accept(ExpressionVisitor expressionVisitor) {
Expand Down Expand Up @@ -116,6 +117,14 @@ public void setWindowElement(WindowElement windowElement) {
this.windowElement = windowElement;
}

public AnalyticType getType() {
return type;
}

public void setType(AnalyticType type) {
this.type = type;
}

@Override
public String toString() {
StringBuilder b = new StringBuilder();
Expand All @@ -136,7 +145,15 @@ public String toString() {
if (keep != null) {
b.append(keep.toString()).append(" ");
}
b.append("OVER (");

switch (type) {
case WITHIN_GROUP:
b.append("WITHIN GROUP");
break;
default:
b.append("OVER");
}
b.append(" (");

toStringPartitionBy(b);
toStringOrderByElements(b);
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/AnalyticType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2017 JSQLParser
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
/*
* Copyright (C) 2017 JSQLParser.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package net.sf.jsqlparser.expression;

/**
*
* @author Tobias Warneke (t.warneke@gmx.net)
*/
public enum AnalyticType {
OVER,
WITHIN_GROUP
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ public interface ExpressionVisitor {

void visit(AnalyticExpression aexpr);

void visit(WithinGroupExpression wgexpr);

void visit(ExtractExpression eexpr);

void visit(IntervalExpression iexpr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,6 @@ public void visit(RegExpMySQLOperator expr) {
visitBinaryExpression(expr);
}

@Override
public void visit(WithinGroupExpression wgexpr) {
wgexpr.getExprList().accept(this);
for (OrderByElement element : wgexpr.getOrderByElements()) {
element.getExpression().accept(this);
}
}

@Override
public void visit(UserVariable var) {

Expand Down

This file was deleted.

5 changes: 0 additions & 5 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
import net.sf.jsqlparser.expression.TimestampValue;
import net.sf.jsqlparser.expression.UserVariable;
import net.sf.jsqlparser.expression.WhenClause;
import net.sf.jsqlparser.expression.WithinGroupExpression;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseOr;
Expand Down Expand Up @@ -564,10 +563,6 @@ public void visit(SelectExpressionItem item) {
item.getExpression().accept(this);
}

@Override
public void visit(WithinGroupExpression wgexpr) {
}

@Override
public void visit(UserVariable var) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import net.sf.jsqlparser.expression.UserVariable;
import net.sf.jsqlparser.expression.WhenClause;
import net.sf.jsqlparser.expression.WindowElement;
import net.sf.jsqlparser.expression.WithinGroupExpression;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseOr;
Expand Down Expand Up @@ -600,7 +599,15 @@ public void visit(AnalyticExpression aexpr) {
keep.accept(this);
buffer.append(" ");
}
buffer.append("OVER (");

switch (aexpr.getType()) {
case WITHIN_GROUP:
buffer.append("WITHIN GROUP");
break;
default:
buffer.append("OVER");
}
buffer.append(" (");

if (partitionExpressionList != null && !partitionExpressionList.getExpressions().isEmpty()) {
buffer.append("PARTITION BY ");
Expand Down Expand Up @@ -686,11 +693,6 @@ public void visit(JsonOperator jsonExpr) {
visitBinaryExpression(jsonExpr, " " + jsonExpr.getStringExpression() + " ");
}

@Override
public void visit(WithinGroupExpression wgexpr) {
buffer.append(wgexpr.toString());
}

@Override
public void visit(UserVariable var) {
buffer.append(var.toString());
Expand Down
29 changes: 5 additions & 24 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2347,9 +2347,7 @@ Expression PrimaryExpression():

| LOOKAHEAD(3) [sign="+" | sign="-"] retval=NumericBind()

| LOOKAHEAD(AnalyticExpression()) retval=AnalyticExpression()

| LOOKAHEAD(WithinGroupExpression()) retval=WithinGroupExpression()
| LOOKAHEAD(3) retval=AnalyticExpression()

| LOOKAHEAD(3) retval=ExtractExpression()

Expand Down Expand Up @@ -2482,26 +2480,6 @@ IntervalExpression IntervalExpression() : {
}
}

WithinGroupExpression WithinGroupExpression() :
{
Token token = null;
List<OrderByElement> orderByElements = null;
WithinGroupExpression result = new WithinGroupExpression();
ExpressionList exprList;
}
{
token = <S_IDENTIFIER> "(" exprList = SimpleExpressionList() ")"
<K_WITHIN> <K_GROUP>
"(" orderByElements = OrderByElements() ")"

{
result.setName(token.image);
result.setExprList(exprList);
result.setOrderByElements(orderByElements);
return result;
}
}

KeepExpression KeepExpression() : {
KeepExpression keep = new KeepExpression();
Token token;
Expand Down Expand Up @@ -2536,7 +2514,10 @@ AnalyticExpression AnalyticExpression() :

[ keep=KeepExpression() ]

<K_OVER> "("
(<K_OVER> {retval.setType(AnalyticType.OVER);}
| <K_WITHIN> <K_GROUP> {retval.setType(AnalyticType.WITHIN_GROUP);} )

"("
[<K_PARTITION> <K_BY> expressionList=SimpleExpressionList() ]
[olist=OrderByElements() [windowElement = WindowElement() ] ]

Expand Down

0 comments on commit 26faba8

Please sign in to comment.