You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "dylanhz (via GitHub)" <gi...@apache.org> on 2024/06/24 03:04:56 UTC

[PR] [FLINK-26940][Table SQL / API] Add SUBSTRING_INDEX supported in SQL & Table API [flink]

dylanhz opened a new pull request, #24972:
URL: https://github.com/apache/flink/pull/24972

   ## What is the purpose of the change
   Add SUBSTRING_INDEX supported in SQL & Table API.
   Examples:
   ```sql
   > SELECT substring_index('www.apache.org', '.', 2);
    www.apache 
   ```
   
   ## Brief change log
   SUBSTRING_INDEX for Table API and SQL
   
   
   ## Verifying this change
   This change added tests and can be verified as follows: `StringFunctionsITCase#substringIndexTestCases`
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): no
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: no
     - The serializers: no
     - The runtime per-record code paths (performance sensitive): no
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
     - The S3 file system connector: no
   
   ## Documentation
   
     - Does this pull request introduce a new feature? yes
     - If yes, how is the feature documented? docs
   


-- 
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-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-26940][Table SQL / API] Add SUBSTRING_INDEX supported in SQL & Table API [flink]

Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on PR #24972:
URL: https://github.com/apache/flink/pull/24972#issuecomment-2227414443

   @flinkbot run azure


-- 
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-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-26940][table] Add the built-in function SUBSTRING_INDEX [flink]

Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on code in PR #24972:
URL: https://github.com/apache/flink/pull/24972#discussion_r1703604984


##########
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:
   BTW, I do find a bug in the next row, `beginIndex < endIndex` should be `beginIndex <= endIndex`. Corresponding test cases have been added.



-- 
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-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-26940][table] Add the built-in function SUBSTRING_INDEX [flink]

Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on code in PR #24972:
URL: https://github.com/apache/flink/pull/24972#discussion_r1703496621


##########
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:
   Yes. Here is an example from Spark:
   ```SQL
   select substring_index('www.apache.org', '.', 3);
   www.apache.org
   ```



-- 
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-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-26940][table] Add the built-in function SUBSTRING_INDEX [flink]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #24972:
URL: https://github.com/apache/flink/pull/24972#issuecomment-2853417755

   This PR has been closed since it has not had any activity in 120 days. 
   If you feel like this was a mistake, or you would like to continue working on it, 
   please feel free to re-open the PR and ask for a review.


-- 
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-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-26940][Table SQL / API] Add SUBSTRING_INDEX supported in SQL & Table API [flink]

Posted by "lsyldliu (via GitHub)" <gi...@apache.org>.
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-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-26940][table] Add the built-in function SUBSTRING_INDEX [flink]

Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on code in PR #24972:
URL: https://github.com/apache/flink/pull/24972#discussion_r1703500287


##########
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:
   I believe it is unnecessary, as the current code would return `-1` directly in this case, indicating no matching substring found, which aligns with the expected behavior.



-- 
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-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-26940][table] Add the built-in function SUBSTRING_INDEX [flink]

Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on code in PR #24972:
URL: https://github.com/apache/flink/pull/24972#discussion_r1703511359


##########
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:
   Currently we don't have `find` and `rfind` for BinaryStringData. I think it would be great to introduce them into BinaryStringDataUtil  because they are commonly used for string matching.
   
   



-- 
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-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-26940][table] Add the built-in function SUBSTRING_INDEX [flink]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #24972:
URL: https://github.com/apache/flink/pull/24972#issuecomment-2781251502

   This PR is being marked as stale since it has not had any activity in the last 90 days. 
   If you would like to keep this PR alive, please leave a comment asking for a review. 
   If the PR has merge conflicts, update it with the latest from the base branch.
   
   If you are having difficulty finding a reviewer, please reach out to the 
   community, contact details can be found here: https://flink.apache.org/what-is-flink/community/
   
   If this PR is no longer valid or desired, please feel free to close it. 
   If no activity occurs in the next 30 days, it will be automatically closed.


-- 
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-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-26940][Table SQL / API] Add SUBSTRING_INDEX supported in SQL & Table API [flink]

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #24972:
URL: https://github.com/apache/flink/pull/24972#issuecomment-2185520911

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "47da25a4c3a730d937531bd64136c3577619e147",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "47da25a4c3a730d937531bd64136c3577619e147",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 47da25a4c3a730d937531bd64136c3577619e147 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-26940][table] Add the built-in function SUBSTRING_INDEX [flink]

Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on code in PR #24972:
URL: https://github.com/apache/flink/pull/24972#discussion_r1703502900


##########
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:
   `expr` starts with `delim` and `count = 1`. Here is an example:
   ```SQL
   select substring_index('www.apache.org', 'w', 1);
    
   ```



-- 
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-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-26940][table] Add the built-in function SUBSTRING_INDEX [flink]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #24972: [FLINK-26940][table] Add the built-in function SUBSTRING_INDEX
URL: https://github.com/apache/flink/pull/24972


-- 
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-unsubscribe@flink.apache.org

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