Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 230 additions & 24 deletions be/src/exprs/function/function_regexp.cpp

Large diffs are not rendered by default.

259 changes: 227 additions & 32 deletions be/test/exprs/function/function_like_test.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BigIntType;
import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.VarcharType;

Expand All @@ -36,21 +36,33 @@
* ScalarFunction 'regexp_extract_all'. This class is generated by GenerateFunction.
*/
public class RegexpExtractAll extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {
implements ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT)
.args(VarcharType.SYSTEM_DEFAULT, VarcharType.SYSTEM_DEFAULT),
FunctionSignature.ret(StringType.INSTANCE).args(StringType.INSTANCE, StringType.INSTANCE)
FunctionSignature.ret(StringType.INSTANCE).args(StringType.INSTANCE, StringType.INSTANCE),
FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT)
.args(VarcharType.SYSTEM_DEFAULT, VarcharType.SYSTEM_DEFAULT, BigIntType.INSTANCE),
FunctionSignature.ret(StringType.INSTANCE)
.args(StringType.INSTANCE, StringType.INSTANCE, BigIntType.INSTANCE)
);

/**
* constructor with 2 arguments.
* constructor with 2 arguments. The optional group index follows Spark semantics
* and defaults to 1 (the first capturing group) in the BE.
*/
public RegexpExtractAll(Expression arg0, Expression arg1) {
super("regexp_extract_all", arg0, arg1);
}

/**
* constructor with 3 arguments.
*/
public RegexpExtractAll(Expression arg0, Expression arg1, Expression arg2) {
super("regexp_extract_all", arg0, arg1, arg2);
}

/** constructor for withChildren and reuse signature */
private RegexpExtractAll(ScalarFunctionParams functionParams) {
super(functionParams);
Expand All @@ -61,7 +73,7 @@ private RegexpExtractAll(ScalarFunctionParams functionParams) {
*/
@Override
public RegexpExtractAll withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 2);
Preconditions.checkArgument(children.size() == 2 || children.size() == 3);
return new RegexpExtractAll(getFunctionParams(children));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.ArrayType;
import org.apache.doris.nereids.types.BigIntType;
import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.VarcharType;

Expand All @@ -38,22 +38,34 @@
* Returns all matches of a regex pattern as an Array&lt;String&gt; instead of a string-formatted array.
*/
public class RegexpExtractAllArray extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {
implements ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(ArrayType.of(VarcharType.SYSTEM_DEFAULT))
.args(VarcharType.SYSTEM_DEFAULT, VarcharType.SYSTEM_DEFAULT),
FunctionSignature.ret(ArrayType.of(StringType.INSTANCE))
.args(StringType.INSTANCE, StringType.INSTANCE)
.args(StringType.INSTANCE, StringType.INSTANCE),
FunctionSignature.ret(ArrayType.of(VarcharType.SYSTEM_DEFAULT))

@linrrzqqq linrrzqqq Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better not to change the parameter limit from two to a hard limit of three. I think support variable parameters is better.

Doris's version upgrades are generally rolling upgrades, first upgrading the backend (be) and then the frontend (fe).An old FE sends two arguments to a new BE, while an old FE pads two-argument SQL calls to three arguments and therefore cannot execute them on an new BE.

Could we keep the original two-argument expression shape and make the new BE accept both 2 and 3 arguments? When only two arguments are provided, the BE can simply use group index 1 as the default. This would preserve existing two-argument queries during a rolling upgrade

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! Reworked in d387667: the FE no longer pads, and the BE now registers both a 2-arg and a 3-arg variadic form (mirroring regexp_replace's ThreeParamTypes/FourParamTypes), so an old FE keeps working against a new BE during rolling upgrades.

.args(VarcharType.SYSTEM_DEFAULT, VarcharType.SYSTEM_DEFAULT, BigIntType.INSTANCE),
FunctionSignature.ret(ArrayType.of(StringType.INSTANCE))
.args(StringType.INSTANCE, StringType.INSTANCE, BigIntType.INSTANCE)
);

/**
* constructor with 2 arguments.
* constructor with 2 arguments. The optional group index follows Spark semantics
* and defaults to 1 (the first capturing group) in the BE.
*/
public RegexpExtractAllArray(Expression arg0, Expression arg1) {
super("regexp_extract_all_array", arg0, arg1);
}

/**
* constructor with 3 arguments.
*/
public RegexpExtractAllArray(Expression arg0, Expression arg1, Expression arg2) {
super("regexp_extract_all_array", arg0, arg1, arg2);
}

/** constructor for withChildren and reuse signature */
private RegexpExtractAllArray(ScalarFunctionParams functionParams) {
super(functionParams);
Expand All @@ -64,7 +76,7 @@ private RegexpExtractAllArray(ScalarFunctionParams functionParams) {
*/
@Override
public RegexpExtractAllArray withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 2);
Preconditions.checkArgument(children.size() == 2 || children.size() == 3);
return new RegexpExtractAllArray(getFunctionParams(children));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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.doris.nereids.trees.expressions.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
import org.apache.doris.nereids.types.BigIntType;
import org.apache.doris.nereids.types.VarcharType;

import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
* Unit tests for the optional group-index argument of regexp_extract_all
* and regexp_extract_all_array.
*/
public class RegexpExtractAllTest {

@Test
public void testTwoAndThreeArgumentForms() {
Expression str = new VarcharLiteral("abc");
Expression pattern = new VarcharLiteral("(b)");
Expression index = new BigIntLiteral(2);

RegexpExtractAll twoArg = new RegexpExtractAll(str, pattern);
Assertions.assertEquals("regexp_extract_all", twoArg.getName());
Assertions.assertEquals(2, twoArg.arity());

RegexpExtractAll threeArg = new RegexpExtractAll(str, pattern, index);
Assertions.assertEquals(3, threeArg.arity());
Assertions.assertEquals(BigIntType.INSTANCE, threeArg.child(2).getDataType());
}

@Test
public void testSignatures() {
Expression str = new VarcharLiteral("abc");
Expression pattern = new VarcharLiteral("(b)");

List<FunctionSignature> signatures = new RegexpExtractAll(str, pattern).getSignatures();
Assertions.assertEquals(4, signatures.size());
// two-argument forms come first for backward compatibility
Assertions.assertEquals(2, signatures.get(0).argumentsTypes.size());
Assertions.assertEquals(VarcharType.SYSTEM_DEFAULT, signatures.get(0).returnType);
Assertions.assertEquals(3, signatures.get(2).argumentsTypes.size());
Assertions.assertEquals(BigIntType.INSTANCE, signatures.get(2).getArgType(2));
}

@Test
public void testWithChildren() {
Expression str = new VarcharLiteral("abc");
Expression pattern = new VarcharLiteral("(b)");
Expression index = new BigIntLiteral(2);

RegexpExtractAll func = new RegexpExtractAll(str, pattern);
RegexpExtractAll twoChildren = func.withChildren(ImmutableList.of(str, pattern));
Assertions.assertNotSame(func, twoChildren);
Assertions.assertEquals(2, twoChildren.arity());

RegexpExtractAll threeChildren = func.withChildren(ImmutableList.of(str, pattern, index));
Assertions.assertEquals(3, threeChildren.arity());

Assertions.assertThrows(IllegalArgumentException.class,
() -> func.withChildren(ImmutableList.of(str)));
}

@Test
public void testArrayVariant() {
Expression str = new VarcharLiteral("abc");
Expression pattern = new VarcharLiteral("(b)");
Expression index = new BigIntLiteral(0);

RegexpExtractAllArray func = new RegexpExtractAllArray(str, pattern);
Assertions.assertEquals("regexp_extract_all_array", func.getName());
Assertions.assertEquals(2, func.arity());
Assertions.assertEquals(4, func.getSignatures().size());

RegexpExtractAllArray threeChildren = func.withChildren(ImmutableList.of(str, pattern, index));
Assertions.assertEquals(3, threeChildren.arity());
Assertions.assertThrows(IllegalArgumentException.class,
() -> func.withChildren(ImmutableList.of(str, pattern, index, index)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,57 @@ Ben
-- !sql_field5 --
2

-- !sql_regexp_extract_all_group0 --
['x=18abc','x=17bcd']

-- !sql_regexp_extract_all_group2 --
['abc','bcd']

-- !sql_regexp_extract_all_empty_group --
['','b']

-- !sql_regexp_extract_all_array_group0 --
["x=18abc", "x=17bcd"]

-- !sql_regexp_extract_all_array_group2 --
["abc", "bcd"]

-- !sql_regexp_extract_all_array_group3 --
["e"]

-- !sql_regexp_extract_all_col_group0 --
\N

['lli']


['lli','lli']
['lli','lli']

-- !sql_regexp_extract_all_col_group1 --
\N

['ll']


['ll','ll']
['ll','ll']

-- !sql_regexp_extract_all_col_group2 --
\N

['i']


['i','i']
['i','i']

-- !sql_regexp_extract_all_null_str --
\N

-- !sql_regexp_extract_all_null_pattern --
\N

-- !sql_regexp_extract_all_null_idx --
\N

Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,58 @@ suite("test_string_function_regexp") {

qt_sql_field4 "SELECT FIELD('21','2130', '2131', '21');"
qt_sql_field5 "SELECT FIELD(21, 2130, 21, 2131);"

qt_sql_regexp_extract_all_group0 "select regexp_extract_all('x=a3&x=18abc&x=2&y=3&x=4&x=17bcd', 'x=([0-9]+)([a-z]+)', 0);"
qt_sql_regexp_extract_all_group2 "select regexp_extract_all('x=a3&x=18abc&x=2&y=3&x=4&x=17bcd', 'x=([0-9]+)([a-z]+)', 2);"
qt_sql_regexp_extract_all_empty_group "select regexp_extract_all('a b', '(a)|(b)', 2);"
qt_sql_regexp_extract_all_array_group0 "select regexp_extract_all_array('x=a3&x=18abc&x=2&y=3&x=4&x=17bcd', 'x=([0-9]+)([a-z]+)', 0);"
qt_sql_regexp_extract_all_array_group2 "select regexp_extract_all_array('x=a3&x=18abc&x=2&y=3&x=4&x=17bcd', 'x=([0-9]+)([a-z]+)', 2);"
qt_sql_regexp_extract_all_array_group3 "select regexp_extract_all_array('hitdecisiondlist', '(i)(.*?)(e)', 3);"

// column input
// column input: the table was dropped earlier in this suite, recreate it first
sql "DROP TABLE IF EXISTS test_string_function_regexp"
sql """
CREATE TABLE IF NOT EXISTS test_string_function_regexp (
k varchar(32),
v int,
)
DISTRIBUTED BY HASH(k) BUCKETS 1 properties("replication_num" = "1");
"""
sql """
INSERT INTO test_string_function_regexp VALUES
("billie eillish",1),
("It's ok",2),
("Emmy eillish",3),
("It's true",4),
(null,5),
("",6),
("billie eillish",null)
"""
qt_sql_regexp_extract_all_col_group0 "SELECT regexp_extract_all(k, '(ll)(i)', 0) from test_string_function_regexp ORDER BY k;"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These column cases run after test_string_function_regexp is dropped at line 193 and after the suite switches to test_query_db; the table is never recreated. The first query therefore fails with a missing-table error instead of producing the added .out rows or exercising the three-argument path. Please move these cases before the drop (or recreate/populate the table here) and regenerate the expected output with the regression runner.

qt_sql_regexp_extract_all_col_group1 "SELECT regexp_extract_all(k, '(ll)(i)', 1) from test_string_function_regexp ORDER BY k;"
qt_sql_regexp_extract_all_col_group2 "SELECT regexp_extract_all(k, '(ll)(i)', 2) from test_string_function_regexp ORDER BY k;"

// null literal input
qt_sql_regexp_extract_all_null_str "SELECT regexp_extract_all(null, '(b)', 1);"
qt_sql_regexp_extract_all_null_pattern "SELECT regexp_extract_all('abc', null, 1);"
qt_sql_regexp_extract_all_null_idx "SELECT regexp_extract_all('abc', '(b)', null);"

// illegal group index: negative or beyond the number of capturing groups
test {
sql "SELECT regexp_extract_all('abc', '(b)', -1);"
exception "invalid"
}
test {
sql "SELECT regexp_extract_all('abc', '(b)', 2);"
exception "invalid"
}
test {
sql "SELECT regexp_extract_all_array('abc', '(b)', -1);"
exception "invalid"
}
test {
sql "SELECT regexp_extract_all_array('abc', '(b)', 2);"
exception "invalid"
}
}