-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Feature](func) Support the third argument for regexp_extract_all and regexp_extract_all_array #66050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[Feature](func) Support the third argument for regexp_extract_all and regexp_extract_all_array #66050
Changes from all commits
16cef44
aa701aa
d387667
1ca8dcf
cf47ba1
4737a9d
2817282
1bcc272
ec1c238
8e9b6b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| 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 |
|---|---|---|
|
|
@@ -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;" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These column cases run after |
||
| 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" | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.