nateab commented on code in PR #27577:
URL: https://github.com/apache/flink/pull/27577#discussion_r2791825814


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/RegexpSplitFunction.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.ArrayData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+
+import javax.annotation.Nullable;
+
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+/**
+ * Implementation of {@link BuiltInFunctionDefinitions#REGEXP_SPLIT}.
+ *
+ * <p>Splits a string by a regular expression pattern and returns an array of 
substrings.
+ *
+ * <p>Examples:
+ *
+ * <pre>{@code
+ * REGEXP_SPLIT('Hello123World456', '[0-9]+') = ['Hello', 'World', '']
+ * REGEXP_SPLIT('a,b;c', '[,;]') = ['a', 'b', 'c']
+ * REGEXP_SPLIT('one  two   three', '\\s+') = ['one', 'two', 'three']
+ * }</pre>
+ */
+@Internal
+public class RegexpSplitFunction extends BuiltInScalarFunction {
+
+    private transient Pattern cachedPattern;
+    private transient String cachedRegex;
+
+    public RegexpSplitFunction(SpecializedFunction.SpecializedContext context) 
{
+        super(BuiltInFunctionDefinitions.REGEXP_SPLIT, context);
+    }
+
+    public @Nullable ArrayData eval(@Nullable StringData str, @Nullable 
StringData regex) {
+        if (str == null || regex == null) {
+            return null;
+        }
+
+        String regexStr = regex.toString();
+        if (regexStr.isEmpty()) {
+            // If regex is empty, split by each character
+            String strValue = str.toString();
+            StringData[] result = new StringData[strValue.length()];
+            for (int i = 0; i < strValue.length(); i++) {
+                result[i] = 
StringData.fromString(String.valueOf(strValue.charAt(i)));
+            }
+            return new GenericArrayData(result);
+        }
+
+        try {
+            // Cache the compiled pattern to improve performance

Review Comment:
   Every other `REGEXP_*` function uses `SqlFunctionUtils.getRegexpMatcher()` 
which delegates to the shared `REGEXP_PATTERN_CACHE` (a `ThreadLocalCache`)
   
   see for example 
https://github.com/apache/flink/blob/master/flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/RegexpSubstrFunction.java#L42



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/RegexpSplitFunction.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.ArrayData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+
+import javax.annotation.Nullable;
+
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+/**
+ * Implementation of {@link BuiltInFunctionDefinitions#REGEXP_SPLIT}.

Review Comment:
   also see https://issues.apache.org/jira/browse/FLINK-6810 for general 
instructions on what else you need to add in order to contribute builtin 
functions, for example which docs to add, what other considerations to make



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/RegexpFunctionsITCase.java:
##########
@@ -387,4 +388,94 @@ private Stream<TestSetSpec> regexpSubstrTestCases() {
                                 "Invalid input arguments. Expected signatures 
are:\n"
                                         + "REGEXP_SUBSTR(str 
<CHARACTER_STRING>, regex <CHARACTER_STRING>)"));
     }
+
+    private Stream<TestSetSpec> regexpSplitTestCases() {
+        return Stream.of(
+                
TestSetSpec.forFunction(BuiltInFunctionDefinitions.REGEXP_SPLIT)
+                        .onFieldsWithData(
+                                "Hello123World456",
+                                null,
+                                "a,b;c|d",
+                                "one  two   three",
+                                123,
+                                "12345",
+                                ",123,,,123,")
+                        .andDataTypes(
+                                DataTypes.STRING().notNull(),
+                                DataTypes.STRING(),
+                                DataTypes.STRING().notNull(),
+                                DataTypes.STRING().notNull(),
+                                DataTypes.INT().notNull(),
+                                DataTypes.STRING().notNull(),
+                                DataTypes.STRING().notNull())
+                        // Basic regex split
+                        .testResult(
+                                $("f0").regexpSplit("[0-9]+"),
+                                "REGEXP_SPLIT(f0, '[0-9]+')",
+                                new String[] {"Hello", "World", ""},
+                                DataTypes.ARRAY(DataTypes.STRING()).notNull())
+                        // null input test
+                        .testResult(
+                                $("f0").regexpSplit(null),
+                                "REGEXP_SPLIT(f0, NULL)",
+                                null,
+                                DataTypes.ARRAY(DataTypes.STRING()))
+                        // Empty regex - split by character
+                        .testResult(
+                                $("f5").regexpSplit(""),
+                                "REGEXP_SPLIT(f5, '')",
+                                new String[] {"1", "2", "3", "4", "5"},
+                                DataTypes.ARRAY(DataTypes.STRING()).notNull())
+                        // null string input
+                        .testResult(
+                                $("f1").regexpSplit("[0-9]+"),
+                                "REGEXP_SPLIT(f1, '[0-9]+')",
+                                null,
+                                DataTypes.ARRAY(DataTypes.STRING()))
+                        // null string and null pattern
+                        .testResult(
+                                $("f1").regexpSplit(null),
+                                "REGEXP_SPLIT(f1, null)",
+                                null,
+                                DataTypes.ARRAY(DataTypes.STRING()))
+                        // Multi-character delimiter regex
+                        .testResult(
+                                $("f2").regexpSplit("[,;|]"),
+                                "REGEXP_SPLIT(f2, '[,;|]')",
+                                new String[] {"a", "b", "c", "d"},
+                                DataTypes.ARRAY(DataTypes.STRING()).notNull())
+                        // Whitespace regex
+                        .testResult(
+                                $("f3").regexpSplit("\\s+"),
+                                "REGEXP_SPLIT(f3, '\\\\s+')",
+                                new String[] {"one", "two", "three"},
+                                DataTypes.ARRAY(DataTypes.STRING()).notNull())
+                        // No match - return original string
+                        .testResult(
+                                $("f5").regexpSplit("[a-z]+"),
+                                "REGEXP_SPLIT(f5, '[a-z]+')",
+                                new String[] {"12345"},
+                                DataTypes.ARRAY(DataTypes.STRING()).notNull())
+                        // Invalid regex - return null
+                        .testResult(
+                                $("f0").regexpSplit("("),
+                                "REGEXP_SPLIT(f0, '(')",
+                                null,
+                                DataTypes.ARRAY(DataTypes.STRING()).notNull())

Review Comment:
   this seems inconsistent, since we expect the return value to be null?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to