From f8d29879310705ca0c9dd8da02c83c09a8792420 Mon Sep 17 00:00:00 2001 From: yakolee Date: Fri, 6 Jun 2014 18:43:49 +0800 Subject: [PATCH] bugfix --- .../sql/visitor/SQLEvalVisitorUtils.java | 25 ++++++++++++++++++- .../sql/visitor/functions/Substring.java | 4 +-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java index a6f626cce2..7564d5f2c2 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java @@ -1418,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(); @@ -1503,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) { diff --git a/src/main/java/com/alibaba/druid/sql/visitor/functions/Substring.java b/src/main/java/com/alibaba/druid/sql/visitor/functions/Substring.java index 9e11949825..fe6ecb9914 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/functions/Substring.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/functions/Substring.java @@ -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 {