bug fixed for issue #2658

This commit is contained in:
wenshao
2018-07-16 14:44:56 +08:00
parent 00a9b92ca1
commit 6572e9a20c
6 changed files with 59 additions and 2 deletions
@@ -17,6 +17,9 @@ package com.alibaba.druid.sql.ast;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import java.util.Collections;
import java.util.List;
public abstract class SQLExprImpl extends SQLObjectImpl implements SQLExpr {
public SQLExprImpl(){
@@ -27,11 +30,16 @@ public abstract class SQLExprImpl extends SQLObjectImpl implements SQLExpr {
public abstract int hashCode();
public abstract SQLExpr clone();
public SQLExpr clone() {
throw new UnsupportedOperationException();
}
public SQLDataType computeDataType() {
return null;
}
public List<SQLObject> getChildren() {
return Collections.emptyList();
}
}
@@ -35,6 +35,7 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
protected SQLAggregateOption option;
protected final List<SQLExpr> arguments = new ArrayList<SQLExpr>();
protected SQLKeep keep;
protected SQLExpr filter;
protected SQLOver over;
protected SQLName overRef;
protected SQLOrderBy withinGroup;
@@ -186,6 +187,7 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
if (option != that.option) return false;
if (arguments != null ? !arguments.equals(that.arguments) : that.arguments != null) return false;
if (keep != null ? !keep.equals(that.keep) : that.keep != null) return false;
if (filter != null ? !filter.equals(that.filter) : that.filter != null) return false;
if (over != null ? !over.equals(that.over) : that.over != null) return false;
if (overRef != null ? !overRef.equals(that.overRef) : that.overRef != null) return false;
if (withinGroup != null ? !withinGroup.equals(that.withinGroup) : that.withinGroup != null) return false;
@@ -199,6 +201,7 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
result = 31 * result + (option != null ? option.hashCode() : 0);
result = 31 * result + (arguments != null ? arguments.hashCode() : 0);
result = 31 * result + (keep != null ? keep.hashCode() : 0);
result = 31 * result + (filter != null ? filter.hashCode() : 0);
result = 31 * result + (over != null ? over.hashCode() : 0);
result = 31 * result + (overRef != null ? overRef.hashCode() : 0);
result = 31 * result + (withinGroup != null ? withinGroup.hashCode() : 0);
@@ -219,6 +222,10 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
x.setKeep(keep.clone());
}
if (filter != null) {
x.setFilter(filter.clone());
}
if (over != null) {
x.setOver(over.clone());
}
@@ -236,6 +243,17 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
return x;
}
public SQLExpr getFilter() {
return filter;
}
public void setFilter(SQLExpr x) {
if (x != null) {
x.setParent(this);
}
this.filter = x;
}
public SQLDataType computeDataType() {
long hash = methodNameHashCod64();
@@ -277,6 +295,11 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
return true;
}
if (filter == expr) {
this.filter = target;
target.setParent(this);
}
return false;
}
}
@@ -334,4 +334,15 @@ public class PGExprParser extends SQLExprParser {
}
return alias;
}
protected void filter(SQLAggregateExpr x) {
if (lexer.identifierEquals(FnvHash.Constants.FILTER)) {
lexer.nextToken();
accept(Token.LPAREN);
accept(Token.WHERE);
SQLExpr filter = this.expr();
accept(Token.RPAREN);
x.setFilter(filter);
}
}
}
@@ -1445,14 +1445,21 @@ public class SQLExprParser extends SQLParser {
accept(Token.RPAREN);
if (lexer.identifierEquals(FnvHash.Constants.FILTER)) {
filter(aggregateExpr);
}
if (lexer.token == Token.OVER) {
over(aggregateExpr);
}
return aggregateExpr;
}
protected void filter(SQLAggregateExpr aggregateExpr) {
}
protected void over(SQLAggregateExpr aggregateExpr) {
lexer.nextToken();
@@ -1562,6 +1562,13 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
over.accept(this);
}
final SQLExpr filter = x.getFilter();
if (filter != null) {
print0(ucase ? "FILTER (WHERE " : "filter (where ");
printExpr(filter);
print(')');
}
this.parameterized = parameterized;
return false;
}