Myracle commented on code in PR #27577: URL: https://github.com/apache/flink/pull/27577#discussion_r2797154205
########## 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: @nateab Thanks for your time to review. The suggestions are valuable and I have modified the code. -- 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]
