Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#11217 fix substring function in calcite query enginr #23

Open
wants to merge 1 commit into
base: kycalcite-1.16.0.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

substring(xx, 0, 1) result not expected

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_) {
Expand All @@ -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) {
Expand All @@ -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_) {
Expand All @@ -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) {
Expand Down