lsyldliu commented on code in PR #24972:
URL: https://github.com/apache/flink/pull/24972#discussion_r1703139469


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/SubstringIndexFunction.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.apache.commons.lang3.StringUtils;
+
+import javax.annotation.Nullable;
+
+import java.util.Arrays;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#SUBSTRING_INDEX}. */
+@Internal
+public class SubstringIndexFunction extends BuiltInScalarFunction {
+    public SubstringIndexFunction(SpecializedFunction.SpecializedContext 
context) {
+        super(BuiltInFunctionDefinitions.SUBSTRING_INDEX, context);
+    }
+
+    public @Nullable StringData eval(
+            @Nullable StringData expr, @Nullable StringData delim, @Nullable 
Integer count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+
+        if (count == 0) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String str = expr.toString();
+        if (str.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String delimiter = delim.toString();
+        if (delimiter.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+
+        try {
+            if (count > 0) {
+                // get index of the count-th occurrences of delimiter
+                int idx = StringUtils.ordinalIndexOf(str, delimiter, count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(0, idx));
+                }
+            } else {
+                // get index of the last -count-th occurrences of delimiter
+                int idx = StringUtils.lastOrdinalIndexOf(str, delimiter, 
-count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(idx + 
delimiter.length()));
+                }
+            }
+        } catch (Throwable t) {
+            throw new FlinkRuntimeException(t);
+        }
+
+        // can not find enough delimiter
+        return expr;
+    }
+
+    public @Nullable byte[] eval(
+            @Nullable byte[] expr, @Nullable byte[] delim, @Nullable Integer 
count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+        if (expr.length == 0 || delim.length == 0 || count == 0) {
+            return new byte[] {};
+        }
+
+        if (count > 0) {
+            int idx = -1;
+            while (count > 0) {
+                // get index of the next occurrence of delim
+                idx = find(expr, idx + 1, delim);
+                if (idx >= 0) {
+                    --count;
+                } else {
+                    // can not find enough delim
+                    return expr;
+                }
+            }
+            if (idx == 0) {
+                return new byte[] {};
+            }
+            return Arrays.copyOfRange(expr, 0, idx);
+
+        } else {
+            int idx = expr.length - delim.length + 1;
+            while (count < 0) {
+                // get index of the previous occurrence of delim
+                idx = rfind(expr, idx - 1, delim);
+                if (idx >= 0) {
+                    ++count;
+                } else {
+                    // can not find enough delim
+                    return expr;
+                }
+            }
+            if (idx + delim.length == expr.length) {
+                return new byte[] {};

Review Comment:
   ```suggestion
                   return new byte[0];
   ```



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/SubstringIndexFunction.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.apache.commons.lang3.StringUtils;
+
+import javax.annotation.Nullable;
+
+import java.util.Arrays;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#SUBSTRING_INDEX}. */
+@Internal
+public class SubstringIndexFunction extends BuiltInScalarFunction {
+    public SubstringIndexFunction(SpecializedFunction.SpecializedContext 
context) {
+        super(BuiltInFunctionDefinitions.SUBSTRING_INDEX, context);
+    }
+
+    public @Nullable StringData eval(
+            @Nullable StringData expr, @Nullable StringData delim, @Nullable 
Integer count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+
+        if (count == 0) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String str = expr.toString();
+        if (str.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String delimiter = delim.toString();
+        if (delimiter.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+
+        try {
+            if (count > 0) {
+                // get index of the count-th occurrences of delimiter
+                int idx = StringUtils.ordinalIndexOf(str, delimiter, count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(0, idx));
+                }
+            } else {
+                // get index of the last -count-th occurrences of delimiter
+                int idx = StringUtils.lastOrdinalIndexOf(str, delimiter, 
-count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(idx + 
delimiter.length()));
+                }
+            }
+        } catch (Throwable t) {
+            throw new FlinkRuntimeException(t);
+        }
+
+        // can not find enough delimiter
+        return expr;
+    }
+
+    public @Nullable byte[] eval(
+            @Nullable byte[] expr, @Nullable byte[] delim, @Nullable Integer 
count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+        if (expr.length == 0 || delim.length == 0 || count == 0) {
+            return new byte[] {};
+        }
+
+        if (count > 0) {
+            int idx = -1;
+            while (count > 0) {
+                // get index of the next occurrence of delim
+                idx = find(expr, idx + 1, delim);
+                if (idx >= 0) {
+                    --count;
+                } else {
+                    // can not find enough delim
+                    return expr;
+                }
+            }
+            if (idx == 0) {
+                return new byte[] {};
+            }
+            return Arrays.copyOfRange(expr, 0, idx);
+
+        } else {
+            int idx = expr.length - delim.length + 1;
+            while (count < 0) {
+                // get index of the previous occurrence of delim
+                idx = rfind(expr, idx - 1, delim);
+                if (idx >= 0) {
+                    ++count;
+                } else {
+                    // can not find enough delim
+                    return expr;
+                }
+            }
+            if (idx + delim.length == expr.length) {
+                return new byte[] {};
+            }
+            return Arrays.copyOfRange(expr, idx + delim.length, expr.length);
+        }
+    }
+
+    private int find(byte[] str, int beginIdx, byte[] target) {

Review Comment:
   Change these util methods to static would be better.



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/SubstringIndexFunction.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.apache.commons.lang3.StringUtils;
+
+import javax.annotation.Nullable;
+
+import java.util.Arrays;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#SUBSTRING_INDEX}. */
+@Internal
+public class SubstringIndexFunction extends BuiltInScalarFunction {
+    public SubstringIndexFunction(SpecializedFunction.SpecializedContext 
context) {
+        super(BuiltInFunctionDefinitions.SUBSTRING_INDEX, context);
+    }
+
+    public @Nullable StringData eval(
+            @Nullable StringData expr, @Nullable StringData delim, @Nullable 
Integer count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+
+        if (count == 0) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String str = expr.toString();
+        if (str.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String delimiter = delim.toString();
+        if (delimiter.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+
+        try {
+            if (count > 0) {
+                // get index of the count-th occurrences of delimiter
+                int idx = StringUtils.ordinalIndexOf(str, delimiter, count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(0, idx));
+                }
+            } else {
+                // get index of the last -count-th occurrences of delimiter
+                int idx = StringUtils.lastOrdinalIndexOf(str, delimiter, 
-count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(idx + 
delimiter.length()));
+                }
+            }
+        } catch (Throwable t) {
+            throw new FlinkRuntimeException(t);
+        }
+
+        // can not find enough delimiter
+        return expr;
+    }
+
+    public @Nullable byte[] eval(
+            @Nullable byte[] expr, @Nullable byte[] delim, @Nullable Integer 
count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+        if (expr.length == 0 || delim.length == 0 || count == 0) {
+            return new byte[] {};
+        }
+
+        if (count > 0) {
+            int idx = -1;
+            while (count > 0) {
+                // get index of the next occurrence of delim
+                idx = find(expr, idx + 1, delim);
+                if (idx >= 0) {
+                    --count;
+                } else {
+                    // can not find enough delim
+                    return expr;
+                }
+            }
+            if (idx == 0) {
+                return new byte[] {};
+            }
+            return Arrays.copyOfRange(expr, 0, idx);
+
+        } else {
+            int idx = expr.length - delim.length + 1;
+            while (count < 0) {
+                // get index of the previous occurrence of delim
+                idx = rfind(expr, idx - 1, delim);
+                if (idx >= 0) {
+                    ++count;
+                } else {
+                    // can not find enough delim
+                    return expr;
+                }
+            }
+            if (idx + delim.length == expr.length) {
+                return new byte[] {};
+            }
+            return Arrays.copyOfRange(expr, idx + delim.length, expr.length);
+        }
+    }
+
+    private int find(byte[] str, int beginIdx, byte[] target) {
+        final int endIdx = str.length - target.length;

Review Comment:
   Do we should check the str.length > target.length?



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/SubstringIndexFunction.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.apache.commons.lang3.StringUtils;
+
+import javax.annotation.Nullable;
+
+import java.util.Arrays;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#SUBSTRING_INDEX}. */
+@Internal
+public class SubstringIndexFunction extends BuiltInScalarFunction {
+    public SubstringIndexFunction(SpecializedFunction.SpecializedContext 
context) {
+        super(BuiltInFunctionDefinitions.SUBSTRING_INDEX, context);
+    }
+
+    public @Nullable StringData eval(
+            @Nullable StringData expr, @Nullable StringData delim, @Nullable 
Integer count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+
+        if (count == 0) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String str = expr.toString();
+        if (str.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String delimiter = delim.toString();
+        if (delimiter.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+
+        try {
+            if (count > 0) {
+                // get index of the count-th occurrences of delimiter
+                int idx = StringUtils.ordinalIndexOf(str, delimiter, count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(0, idx));
+                }
+            } else {
+                // get index of the last -count-th occurrences of delimiter
+                int idx = StringUtils.lastOrdinalIndexOf(str, delimiter, 
-count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(idx + 
delimiter.length()));
+                }
+            }
+        } catch (Throwable t) {
+            throw new FlinkRuntimeException(t);
+        }
+
+        // can not find enough delimiter

Review Comment:
   The semantics of this function is to return the original string if the 
specified number of delimiter cannot be found?



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/SubstringIndexFunction.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.apache.commons.lang3.StringUtils;
+
+import javax.annotation.Nullable;
+
+import java.util.Arrays;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#SUBSTRING_INDEX}. */
+@Internal
+public class SubstringIndexFunction extends BuiltInScalarFunction {
+    public SubstringIndexFunction(SpecializedFunction.SpecializedContext 
context) {
+        super(BuiltInFunctionDefinitions.SUBSTRING_INDEX, context);
+    }
+
+    public @Nullable StringData eval(
+            @Nullable StringData expr, @Nullable StringData delim, @Nullable 
Integer count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+
+        if (count == 0) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String str = expr.toString();

Review Comment:
   Can use the BinaryStringData to judge and find the substring instead of 
materialized it in advance?



##########
docs/data/sql_functions.yml:
##########
@@ -387,6 +387,14 @@ string:
   - sql: SUBSTR(string, integer1[, integer2])
     table: STRING.substr(INTEGER1[, INTEGER2])
     description: Returns a substring of string starting from position integer1 
with length integer2 (to the end by default).
+  - sql: SUBSTRING_INDEX(expr, delim, count)
+    table: expr.substringIndex(delim, count)
+    description: | 
+      expr <CHARACTER_STRING>, delim <CHARACTER_STRING>, count <INTEGER>
+      expr <BINARY_STRING>, delim <BINARY_STRING>, count <INTEGER>
+      Returns the substring of expr before count occurrences of delim. expr 
and delim allow binary type, and the result matches the type of expr. null if 
any of its arguments are null. 

Review Comment:
   null -> `NULL`



##########
docs/data/sql_functions_zh.yml:
##########
@@ -487,6 +487,14 @@ string:
   - sql: SUBSTR(string, integer1[, integer2])
     table: STRING.substr(INTEGER1[, INTEGER2])
     description: 返回字符串的子字符串,从位置 integer1 开始,长度为 integer2(默认到末尾)。
+  - sql: SUBSTRING_INDEX(expr, delim, count)
+    table: expr.substringIndex(delim, count)
+    description: |
+      expr <CHARACTER_STRING>, delim <CHARACTER_STRING>, count <INTEGER>
+      expr <BINARY_STRING>, delim <BINARY_STRING>, count <INTEGER>
+      返回 expr 中分隔符 delim 出现第 count 次前的子串。expr 和 delim 允许 binary 类型,并且返回值类型与 
expr 一致。任意参数为 null 时返回 null。

Review Comment:
   null -> `NULL`



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/SubstringIndexFunction.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.apache.commons.lang3.StringUtils;
+
+import javax.annotation.Nullable;
+
+import java.util.Arrays;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#SUBSTRING_INDEX}. */
+@Internal
+public class SubstringIndexFunction extends BuiltInScalarFunction {
+    public SubstringIndexFunction(SpecializedFunction.SpecializedContext 
context) {
+        super(BuiltInFunctionDefinitions.SUBSTRING_INDEX, context);
+    }
+
+    public @Nullable StringData eval(
+            @Nullable StringData expr, @Nullable StringData delim, @Nullable 
Integer count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+
+        if (count == 0) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String str = expr.toString();
+        if (str.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String delimiter = delim.toString();
+        if (delimiter.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+
+        try {
+            if (count > 0) {
+                // get index of the count-th occurrences of delimiter
+                int idx = StringUtils.ordinalIndexOf(str, delimiter, count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(0, idx));
+                }
+            } else {
+                // get index of the last -count-th occurrences of delimiter
+                int idx = StringUtils.lastOrdinalIndexOf(str, delimiter, 
-count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(idx + 
delimiter.length()));
+                }
+            }
+        } catch (Throwable t) {
+            throw new FlinkRuntimeException(t);
+        }
+
+        // can not find enough delimiter
+        return expr;
+    }
+
+    public @Nullable byte[] eval(
+            @Nullable byte[] expr, @Nullable byte[] delim, @Nullable Integer 
count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+        if (expr.length == 0 || delim.length == 0 || count == 0) {
+            return new byte[] {};
+        }
+
+        if (count > 0) {
+            int idx = -1;
+            while (count > 0) {
+                // get index of the next occurrence of delim
+                idx = find(expr, idx + 1, delim);
+                if (idx >= 0) {
+                    --count;
+                } else {
+                    // can not find enough delim
+                    return expr;
+                }
+            }
+            if (idx == 0) {

Review Comment:
   Can you help explain this logic to me? What case will will occur idx == 0, 
then return a empty byte array?



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/SubstringIndexFunction.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.apache.commons.lang3.StringUtils;
+
+import javax.annotation.Nullable;
+
+import java.util.Arrays;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#SUBSTRING_INDEX}. */
+@Internal
+public class SubstringIndexFunction extends BuiltInScalarFunction {
+    public SubstringIndexFunction(SpecializedFunction.SpecializedContext 
context) {
+        super(BuiltInFunctionDefinitions.SUBSTRING_INDEX, context);
+    }
+
+    public @Nullable StringData eval(
+            @Nullable StringData expr, @Nullable StringData delim, @Nullable 
Integer count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+
+        if (count == 0) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String str = expr.toString();
+        if (str.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+        String delimiter = delim.toString();
+        if (delimiter.isEmpty()) {
+            return BinaryStringData.EMPTY_UTF8;
+        }
+
+        try {
+            if (count > 0) {
+                // get index of the count-th occurrences of delimiter
+                int idx = StringUtils.ordinalIndexOf(str, delimiter, count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(0, idx));
+                }
+            } else {
+                // get index of the last -count-th occurrences of delimiter
+                int idx = StringUtils.lastOrdinalIndexOf(str, delimiter, 
-count);
+                if (idx != StringUtils.INDEX_NOT_FOUND) {
+                    return StringData.fromString(str.substring(idx + 
delimiter.length()));
+                }
+            }
+        } catch (Throwable t) {
+            throw new FlinkRuntimeException(t);
+        }
+
+        // can not find enough delimiter
+        return expr;
+    }
+
+    public @Nullable byte[] eval(
+            @Nullable byte[] expr, @Nullable byte[] delim, @Nullable Integer 
count) {
+        if (expr == null || delim == null || count == null) {
+            return null;
+        }
+        if (expr.length == 0 || delim.length == 0 || count == 0) {
+            return new byte[] {};
+        }
+
+        if (count > 0) {
+            int idx = -1;
+            while (count > 0) {
+                // get index of the next occurrence of delim
+                idx = find(expr, idx + 1, delim);
+                if (idx >= 0) {
+                    --count;
+                } else {
+                    // can not find enough delim
+                    return expr;
+                }
+            }
+            if (idx == 0) {
+                return new byte[] {};

Review Comment:
   ```suggestion
                   return new byte[0];
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to