Merge pull request #561 from yakolee/master

修复数值计算bug
This commit is contained in:
Yako
2014-06-12 13:27:22 +08:00
4 changed files with 70 additions and 8 deletions
@@ -22,7 +22,6 @@ import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLBooleanExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLHexExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
@@ -484,6 +483,11 @@ public class MySqlExprParser extends SQLExprParser {
lexer.nextToken();
return userName;
}
if(lexer.token() == Token.ERROR) {
throw new ParserException("syntax error, token: " + lexer.token() + " " + lexer.stringVal() + ", pos : "
+ lexer.pos());
}
return super.primaryRest(expr);
}
@@ -1239,6 +1239,14 @@ public class SQLEvalVisitorUtils {
return castToLong(list.get(0));
}
}
if (val instanceof Boolean) {
if (((Boolean) val).booleanValue()) {
return 1l;
} else {
return 0l;
}
}
return ((Number) val).longValue();
}
@@ -1322,8 +1330,12 @@ public class SQLEvalVisitorUtils {
return new BigInteger(val);
} catch (NumberFormatException e) {
}
return new BigDecimal(val);
try {
return new BigDecimal(val);
} catch (NumberFormatException e) {
return 0;
}
}
public static Date castToDate(Object val) {
@@ -1406,6 +1418,18 @@ public class SQLEvalVisitorUtils {
if (a == null || b == null) {
return null;
}
if(a == EVAL_VALUE_NULL || b == EVAL_VALUE_NULL) {
return null;
}
if (a instanceof String) {
a = castToNumber((String) a);
}
if (b instanceof String) {
b = castToNumber((String) b);
}
if (a instanceof Long || b instanceof Long) {
return castToLong(a).longValue() & castToLong(b).longValue();
@@ -1418,6 +1442,18 @@ public class SQLEvalVisitorUtils {
if (a == null || b == null) {
return null;
}
if(a == EVAL_VALUE_NULL || b == EVAL_VALUE_NULL) {
return null;
}
if (a instanceof String) {
a = castToNumber((String) a);
}
if (b instanceof String) {
b = castToNumber((String) b);
}
if (a instanceof Long || b instanceof Long) {
return castToLong(a).longValue() | castToLong(b).longValue();
@@ -1431,6 +1467,10 @@ public class SQLEvalVisitorUtils {
return null;
}
if(a == EVAL_VALUE_NULL || b == EVAL_VALUE_NULL) {
return null;
}
if (a instanceof String) {
a = castToNumber((String) a);
}
@@ -1475,7 +1515,18 @@ public class SQLEvalVisitorUtils {
}
if (a instanceof Long || b instanceof Long) {
return castToLong(a) / castToLong(b);
Long longA = castToLong(a);
Long longB = castToLong(b);
if (longB == 0) {
if (longA > 0) {
return Double.POSITIVE_INFINITY;
} else if (longA < 0) {
return Double.NEGATIVE_INFINITY;
} else {
return Double.NaN;
}
}
return longA / longB;
}
if (a instanceof Integer || b instanceof Integer) {
@@ -1648,6 +1699,10 @@ public class SQLEvalVisitorUtils {
if (a == null || b == null) {
return false;
}
if (a == EVAL_VALUE_NULL || b == EVAL_VALUE_NULL) {
return false;
}
if (a.equals(b)) {
return true;
@@ -42,8 +42,11 @@ public class Char implements Function {
int charCode = ((Number) paramValue).intValue();
buf.append((char) charCode);
} else if (paramValue instanceof String) {
int charCode = new BigDecimal((String)paramValue).intValue();
buf.append((char) charCode);
try {
int charCode = new BigDecimal((String) paramValue).intValue();
buf.append((char) charCode);
} catch (NumberFormatException e) {
}
} else {
return SQLEvalVisitor.EVAL_ERROR;
}
@@ -50,7 +50,7 @@ public class Substring implements Function {
if (paramSize == 2) {
if (index < 0) {
if (index <= 0) {
int lastIndex = str.length() + index;
return str.substring(lastIndex);
}
@@ -68,7 +68,7 @@ public class Substring implements Function {
int len = ((Number) param2Value).intValue();
String result;
if (index < 0) {
if (index <= 0) {
int lastIndex = str.length() + index;
result = str.substring(lastIndex);
} else {