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 d602f7df43..31a86b2c52 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java @@ -931,7 +931,7 @@ public class SQLEvalVisitorUtils { return ((Number) val).intValue() == 1; } - throw new IllegalArgumentException(); + throw new IllegalArgumentException(val.getClass() + " not supported."); } public static String castToString(Object val) { @@ -1165,7 +1165,7 @@ public class SQLEvalVisitorUtils { return castToByte(a) + castToByte(b); } - throw new IllegalArgumentException(); + throw new IllegalArgumentException(a.getClass() + " and " + b.getClass() + " not supported."); } public static Object div(Object a, Object b) { @@ -1197,7 +1197,7 @@ public class SQLEvalVisitorUtils { return castToByte(a) / castToByte(b); } - throw new IllegalArgumentException(); + throw new IllegalArgumentException(a.getClass() + " and " + b.getClass() + " not supported."); } public static boolean gt(Object a, Object b) { @@ -1209,6 +1209,10 @@ public class SQLEvalVisitorUtils { return true; } + if (a instanceof String || b instanceof String) { + return ((String) a).compareTo((String) b) > 0; + } + if (a instanceof BigDecimal || b instanceof BigDecimal) { return castToDecimal(a).compareTo(castToDecimal(b)) > 0; } @@ -1252,7 +1256,7 @@ public class SQLEvalVisitorUtils { return d1.compareTo(d2) > 0; } - throw new IllegalArgumentException(); + throw new IllegalArgumentException(a.getClass() + " and " + b.getClass() + " not supported."); } public static boolean gteq(Object a, Object b) { @@ -1272,6 +1276,10 @@ public class SQLEvalVisitorUtils { return false; } + if (a instanceof String || b instanceof String) { + return ((String) a).compareTo((String) b) < 0; + } + if (a instanceof BigDecimal || b instanceof BigDecimal) { return castToDecimal(a).compareTo(castToDecimal(b)) < 0; } @@ -1315,7 +1323,7 @@ public class SQLEvalVisitorUtils { return d1.compareTo(d2) < 0; } - throw new IllegalArgumentException(); + throw new IllegalArgumentException(a.getClass() + " and " + b.getClass() + " not supported."); } public static boolean lteq(Object a, Object b) { @@ -1339,6 +1347,10 @@ public class SQLEvalVisitorUtils { return true; } + if (a instanceof String || b instanceof String) { + return castToString(a).equals(castToString(b)); + } + if (a instanceof BigDecimal || b instanceof BigDecimal) { return castToDecimal(a).compareTo(castToDecimal(b)) == 0; } @@ -1378,11 +1390,7 @@ public class SQLEvalVisitorUtils { return d1.equals(d2); } - if (a instanceof String || b instanceof String) { - return castToString(a).equals(castToString(b)); - } - - throw new IllegalArgumentException(); + throw new IllegalArgumentException(a.getClass() + " and " + b.getClass() + " not supported."); } public static Object add(Object a, Object b) { @@ -1430,7 +1438,7 @@ public class SQLEvalVisitorUtils { return castToString(a) + castToString(b); } - throw new IllegalArgumentException(); + throw new IllegalArgumentException(a.getClass() + " and " + b.getClass() + " not supported."); } public static Object sub(Object a, Object b) { @@ -1514,7 +1522,7 @@ public class SQLEvalVisitorUtils { return castToByte(a) * castToByte(b); } - throw new IllegalArgumentException(); + throw new IllegalArgumentException(a.getClass() + " and " + b.getClass() + " not supported."); } public static boolean like(String input, String pattern) { 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 4ade056894..9e11949825 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 @@ -51,7 +51,7 @@ public class Substring implements Function { if (paramSize == 2) { if (index < 0) { - int lastIndex = str.length() + index ; + int lastIndex = str.length() + index; return str.substring(lastIndex); } @@ -66,15 +66,18 @@ public class Substring implements Function { } int len = ((Number) param2Value).intValue(); - + String result; if (index < 0) { - int lastIndex = str.length() + index ; + int lastIndex = str.length() + index; result = str.substring(lastIndex); } else { result = str.substring(index - 1); } - + + if (len > result.length()) { + return result; + } return result.substring(0, len); } }