improved parser performance.

This commit is contained in:
wenshao
2017-08-26 22:53:51 +08:00
parent 76c441949f
commit a983b2685b
75 changed files with 2998 additions and 2053 deletions
@@ -15,6 +15,8 @@
*/
package com.alibaba.druid.sql.ast;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
public abstract class SQLExprImpl extends SQLObjectImpl implements SQLExpr {
public SQLExprImpl(){
@@ -30,4 +32,6 @@ public abstract class SQLExprImpl extends SQLObjectImpl implements SQLExpr {
public SQLDataType computeDataType() {
return null;
}
}
@@ -25,8 +25,7 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public abstract class SQLObjectImpl implements SQLObject {
private SQLObject parent;
protected SQLObject parent;
protected Map<String, Object> attributes;
public SQLObjectImpl(){
@@ -21,18 +21,23 @@ import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
import com.alibaba.druid.sql.visitor.ParameterizedVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.Utils;
public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Serializable {
private static final long serialVersionUID = 1L;
private SQLExpr left;
private SQLExpr right;
private SQLBinaryOperator operator;
private String dbType;
private static final long serialVersionUID = 1L;
protected SQLExpr left;
protected SQLExpr right;
protected SQLBinaryOperator operator;
protected String dbType;
private boolean bracket = false;
private boolean bracket = false;
// only for parameterized output
protected transient List<SQLObject> mergedList;
public SQLBinaryOpExpr(){
@@ -47,7 +52,11 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Seri
}
public SQLBinaryOpExpr(SQLExpr left, SQLBinaryOperator operator, SQLExpr right, String dbType){
setLeft(left);
if (left != null) {
left.setParent(this);
}
this.left = left;
setRight(right);
this.operator = operator;
@@ -150,24 +159,16 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Seri
return false;
}
SQLBinaryOpExpr other = (SQLBinaryOpExpr) obj;
if (left == null) {
if (other.left != null) {
return false;
}
} else if (!left.equals(other.left)) {
return false;
}
if (operator != other.operator) {
return false;
}
if (right == null) {
if (other.right != null) {
return false;
}
} else if (!right.equals(other.right)) {
return false;
}
return true;
return operator == other.operator
&& SQLExprUtils.equals(left, other.left)
&& SQLExprUtils.equals(right, other.right);
}
public boolean equals(SQLBinaryOpExpr other) {
return operator == other.operator
&& SQLExprUtils.equals(left, other.left)
&& SQLExprUtils.equals(right, other.right);
}
@@ -508,4 +509,129 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Seri
return false;
}
/**
* only for parameterized output
* @param v
* @param x
* @return
*/
public static SQLBinaryOpExpr merge(ParameterizedVisitor v, SQLBinaryOpExpr x) {
SQLObject parent = x.parent;
for (;;) {
if (x.right instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr rightBinary = (SQLBinaryOpExpr) x.right;
if (x.left instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr leftBinaryExpr = (SQLBinaryOpExpr) x.left;
if (SQLExprUtils.equals(leftBinaryExpr.right, rightBinary)) {
x = leftBinaryExpr;
v.incrementReplaceCunt();
continue;
}
}
SQLExpr mergedRight = merge(v, rightBinary);
if (mergedRight != x.right) {
x = new SQLBinaryOpExpr(x.left, x.operator, mergedRight);
v.incrementReplaceCunt();
}
x.setParent(parent);
}
break;
}
if (x.left instanceof SQLBinaryOpExpr) {
SQLExpr mergedLeft = merge(v, (SQLBinaryOpExpr) x.left);
if (mergedLeft != x.left) {
SQLBinaryOpExpr tmp = new SQLBinaryOpExpr(mergedLeft, x.operator, x.right);
tmp.setParent(parent);
x = tmp;
v.incrementReplaceCunt();
}
}
// ID = ? OR ID = ? => ID = ?
if (x.operator == SQLBinaryOperator.BooleanOr) {
if ((x.left instanceof SQLBinaryOpExpr) && (x.right instanceof SQLBinaryOpExpr)) {
SQLBinaryOpExpr leftBinary = (SQLBinaryOpExpr) x.left;
SQLBinaryOpExpr rightBinary = (SQLBinaryOpExpr) x.right;
if (mergeEqual(leftBinary, rightBinary)) {
v.incrementReplaceCunt();
leftBinary.setParent(x.parent);
leftBinary.addMergedItem(rightBinary);
return leftBinary;
}
if (SQLExprUtils.isLiteralExpr(leftBinary.left) //
&& leftBinary.operator == SQLBinaryOperator.BooleanOr) {
if (mergeEqual(leftBinary.right, x.right)) {
v.incrementReplaceCunt();
leftBinary.addMergedItem(rightBinary);
return leftBinary;
}
}
}
}
return x;
}
/**
* only for parameterized output
* @param item
* @return
*/
private void addMergedItem(SQLBinaryOpExpr item) {
if (mergedList == null) {
mergedList = new ArrayList<SQLObject>();
}
mergedList.add(item);
}
/**
* only for parameterized output
* @return
*/
public List<SQLObject> getMergedList() {
return mergedList;
}
/**
* only for parameterized output
* @param a
* @param b
* @return
*/
private static boolean mergeEqual(SQLExpr a, SQLExpr b) {
if (!(a instanceof SQLBinaryOpExpr)) {
return false;
}
if (!(b instanceof SQLBinaryOpExpr)) {
return false;
}
SQLBinaryOpExpr binaryA = (SQLBinaryOpExpr) a;
SQLBinaryOpExpr binaryB = (SQLBinaryOpExpr) b;
if (binaryA.getOperator() != SQLBinaryOperator.Equality) {
return false;
}
if (binaryB.getOperator() != SQLBinaryOperator.Equality) {
return false;
}
if (!(binaryA.getRight() instanceof SQLLiteralExpr || binaryA.getRight() instanceof SQLVariantRefExpr)) {
return false;
}
if (!(binaryB.getRight() instanceof SQLLiteralExpr || binaryB.getRight() instanceof SQLVariantRefExpr)) {
return false;
}
return binaryA.getLeft().toString().equals(binaryB.getLeft().toString());
}
}
@@ -0,0 +1,78 @@
package com.alibaba.druid.sql.ast.expr;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.util.ArrayList;
import java.util.List;
public class SQLBinaryOpExprGroup extends SQLExprImpl {
private final SQLBinaryOperator operator;
private final List<SQLExpr> items = new ArrayList<SQLExpr>();
private String dbType;
public SQLBinaryOpExprGroup(SQLBinaryOperator operator) {
this.operator = operator;
}
public SQLBinaryOpExprGroup(SQLBinaryOperator operator, String dbType) {
this.operator = operator;
this.dbType = dbType;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SQLBinaryOpExprGroup that = (SQLBinaryOpExprGroup) o;
if (operator != that.operator) return false;
return items.equals(that.items);
}
@Override
public int hashCode() {
int result = operator != null ? operator.hashCode() : 0;
result = 31 * result + items.hashCode();
return result;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.items);
}
visitor.endVisit(this);
}
@Override
public SQLExpr clone() {
SQLBinaryOpExprGroup x = new SQLBinaryOpExprGroup(operator);
for (SQLExpr item : items) {
SQLExpr item2 = item.clone();
item2.setParent(this);
x.items.add(item2);
}
return x;
}
public void add(SQLExpr item) {
if (item != null) {
item.setParent(this);
}
this.items.add(item);
}
public List<SQLExpr> getItems() {
return this.items;
}
public SQLBinaryOperator getOperator() {
return operator;
}
}
@@ -0,0 +1,65 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.ast.expr;
import com.alibaba.druid.sql.ast.SQLExpr;
public class SQLExprUtils {
public static boolean equals(SQLExpr a, SQLExpr b) {
if (a == b) {
return true;
}
if (a == null || b == null) {
return false;
}
Class<?> clazz_a = a.getClass();
Class<?> clazz_b = b.getClass();
if (clazz_a != clazz_b) {
return false;
}
if (clazz_a == SQLIdentifierExpr.class) {
SQLIdentifierExpr x_a = (SQLIdentifierExpr) a;
SQLIdentifierExpr x_b = (SQLIdentifierExpr) b;
return x_a.equals(x_b);
}
if (clazz_a == SQLBinaryOpExpr.class) {
SQLBinaryOpExpr x_a = (SQLBinaryOpExpr) a;
SQLBinaryOpExpr x_b = (SQLBinaryOpExpr) b;
return x_a.equals(x_b);
}
return a.equals(b);
}
public static boolean isLiteralExpr(SQLExpr expr) {
if (expr instanceof SQLLiteralExpr) {
return true;
}
if (expr instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binary = (SQLBinaryOpExpr) expr;
return isLiteralExpr(binary.left) && isLiteralExpr(binary.right);
}
return false;
}
}
@@ -21,12 +21,14 @@ import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.FNVUtils;
public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
protected String name;
private transient String lowerName;
protected transient long hash_lower;
private transient Boolean parameter;
private transient SQLColumnDefinition resolvedColumn;
@@ -40,6 +42,11 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
this.name = name;
}
public SQLIdentifierExpr(String name, long hash_lower){
this.name = name;
this.hash_lower = hash_lower;
}
public String getSimpleName() {
return name;
}
@@ -51,6 +58,7 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
public void setName(String name) {
this.name = name;
this.lowerName = null;
this.hash_lower = 0L;
}
public String getLowerName() {
@@ -60,6 +68,13 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
return lowerName;
}
public long hash_lower() {
if (hash_lower == 0) {
hash_lower = FNVUtils.fnv_64_lower(name);
}
return hash_lower;
}
public Boolean isParameter() {
return parameter;
}
@@ -108,6 +123,18 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
return true;
}
public boolean equals(SQLIdentifierExpr other) {
if (name == other.name) {
return true;
}
if (name == null || other.name == null) {
return false;
}
return name.equals(other.name);
}
public boolean equalsIgnoreCase(Object obj) {
if (this == obj) {
return true;
@@ -36,6 +36,9 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
private SQLExpr from;
private SQLExpr using;
private SQLExpr _for;
private String trimOption;
public SQLMethodInvokeExpr(){
@@ -255,4 +258,23 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
}
this.using = using;
}
public SQLExpr getFor() {
return _for;
}
public void setFor(SQLExpr x) {
if (x != null) {
x.setParent(this);
}
this._for = x;
}
public String getTrimOption() {
return trimOption;
}
public void setTrimOption(String trimOption) {
this.trimOption = trimOption;
}
}
@@ -20,6 +20,7 @@ import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -168,7 +169,14 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
return comment;
}
public void setComment(String comment) {
this.setComment(new SQLCharExpr(comment));
}
public void setComment(SQLExpr comment) {
if (comment != null) {
comment.setParent(this);
}
this.comment = comment;
}