lincoln-lil commented on code in PR #25137:
URL: https://github.com/apache/flink/pull/25137#discussion_r1722955742


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/RegexpInstrFunction.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext;
+
+import javax.annotation.Nullable;
+
+import java.util.regex.Matcher;
+import java.util.regex.PatternSyntaxException;
+
+import static 
org.apache.flink.table.runtime.functions.SqlFunctionUtils.REGEXP_PATTERN_CACHE;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#REGEXP_INSTR}. */
+@Internal
+public class RegexpInstrFunction extends BuiltInScalarFunction {
+
+    public RegexpInstrFunction(SpecializedContext context) {
+        super(BuiltInFunctionDefinitions.REGEXP_INSTR, context);
+    }
+
+    public @Nullable Integer eval(@Nullable StringData str, @Nullable 
StringData regex) {
+        if (str == null || regex == null) {

Review Comment:
   We can reuse the code (L42~L51) for the 4 regex xx functions.



##########
flink-python/pyflink/table/expression.py:
##########
@@ -1254,6 +1254,17 @@ def regexp_extract_all(self, regex, extract_index=None) 
-> 'Expression':
         else:
             return _ternary_op("regexpExtractAll")(self, regex, extract_index)
 
+    def regexp_instr(self, regex) -> 'Expression':
+        """
+        Returns the position of the first substring in str that matches regex.
+        Result indexes begin at 1, 0 if there is no match.
+        null if any of the arguments are null or regex is invalid.
+
+        :param regex: A STRING expression with a matching pattern.
+        :return: An INTEGER representation of matched substring index.

Review Comment:
   -> 'An INTEGER representation of the first matched substring index.'



##########
docs/data/sql_functions_zh.yml:
##########
@@ -423,6 +423,16 @@ string:
       str <CHAR | VARCHAR>, regex <CHAR | VARCHAR>, extractIndex <TINYINT | 
SMALLINT | INTEGER | BIGINT>
       
       返回一个 ARRAY<STRING>,表示所有匹配的子串。如果任何参数为 `NULL` 或非法,则返回 `NULL`。
+  - sql: REGEXP_INSTR(str, regex)
+    table: str.regexpInstr(regex)
+    description: |
+      返回 str 中第一个匹配 regex 的子字符串的索引。
+      
+      结果索引从 1 开始,如果匹配失败则返回 0。
+      
+      str <CHAR | VARCHAR>, regex <CHAR | VARCHAR>
+      
+      返回一个 INTEGER 表示匹配成功的子串索引。如果任何参数为 `NULL` 或 regex 非法,则返回 `NULL`。

Review Comment:
   返回一个 INTEGER 表示 str 中第一个匹配 regex 的子字符串的索引



##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/BaseExpressions.java:
##########
@@ -1208,6 +1209,19 @@ public OutType regexpExtractAll(InType regex) {
                 unresolvedCall(REGEXP_EXTRACT_ALL, toExpr(), 
objectToExpression(regex)));
     }
 
+    /**
+     * Returns the position of the first substring in {@code str} that matches 
{@code regex}. <br>
+     * Result indexes begin at 1, 0 if there is no match. <br>
+     *
+     * @param regex A STRING expression with a matching pattern.
+     * @return An INTEGER representation of matched substring index. <br>

Review Comment:
   -> 'An INTEGER representation of the first matched substring index.'



##########
docs/data/sql_functions.yml:
##########
@@ -352,6 +352,16 @@ string:
       str <CHAR | VARCHAR>, regex <CHAR | VARCHAR>, extractIndex <TINYINT | 
SMALLINT | INTEGER | BIGINT>
       
       Returns an ARRAY<STRING> representation of all the matched substrings. 
`NULL` if any of the arguments are `NULL` or invalid.
+  - sql: REGEXP_INSTR(str, regex)
+    table: str.regexpInstr(regex)
+    description: |
+      Returns the position of the first substring in str that matches regex.
+      
+      Result indexes begin at 1, 0 if there is no match.
+      
+      str <CHAR | VARCHAR>, regex <CHAR | VARCHAR>
+      
+      Returns an INTEGER representation of matched substring index. `NULL` if 
any of the arguments are `NULL` or regex is invalid.

Review Comment:
   -> 'Returns an INTEGER representation of the first matched substring index.'



-- 
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