From 686e86250ea6c4ad26dccbf0389c801f0df7a952 Mon Sep 17 00:00:00 2001 From: "jie.zou" Date: Tue, 2 Apr 2019 11:10:28 +0800 Subject: [PATCH] #11217 fix substring function in calcite query engine --- .../apache/calcite/runtime/SqlFunctions.java | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java index 533a15a243b7..f629c4c01919 100644 --- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java +++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java @@ -133,11 +133,21 @@ private SqlFunctions() { /** SQL SUBSTRING(string FROM ... FOR ...) function. */ // override - public static String substring(String s, int from, int for_) { - if (s == null) { + public static String substring(String c, int s, int l) { + if (c == null) { return null; } - return s.substring(from - 1, Math.min(from - 1 + for_, s.length())); + int lc = c.length(); + if (s < 0) { + s += lc + 1; + } + int e = s + l; + if (s > lc || e < 1) { + return ""; + } + int s1 = Math.max(s, 1); + int e1 = Math.min(s1 + l, lc + 1); + return c.substring(s1 - 1, e1 - 1); } public static String substring(String s, long from, long for_) { @@ -149,8 +159,9 @@ public static String substring(String s, long from, long for_) { } /** SQL SUBSTRING(string FROM ...) function. */ + // override public static String substring(String s, int from) { - return s.substring(from - 1); + return substring(s, from, s.length() + 1); } public static String substring(String s, long from) { @@ -161,8 +172,22 @@ public static String substring(String s, long from) { } /** SQL SUBSTRING(binary FROM ... FOR ...) function. */ - public static ByteString substring(ByteString b, int from, int for_) { - return b.substring(from - 1, Math.min(from - 1 + for_, b.length())); + // override + public static ByteString substring(ByteString c, int s, int l) { + if (c == null) { + return null; + } + int lc = c.length(); + if (s < 0) { + s += lc + 1; + } + int e = s + l; + if (s > lc || e < 1) { + return ByteString.EMPTY; + } + int s1 = Math.max(s, 1); + int e1 = Math.min(s1 + l, lc + 1); + return c.substring(s1 - 1, e1 - 1); } public static ByteString substring(ByteString b, long from, long for_) { @@ -174,8 +199,9 @@ public static ByteString substring(ByteString b, long from, long for_) { } /** SQL SUBSTRING(binary FROM ...) function. */ + // override public static ByteString substring(ByteString b, int from) { - return b.substring(from - 1); + return substring(b, from, b.length() + 1); } public static ByteString substring(ByteString b, long from) {