Skip to content

Commit cdb8115

Browse files
committed
Add 2 arg test
1 parent cebe806 commit cdb8115

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

cpp/src/gandiva/tests/projector_test.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3024,6 +3024,47 @@ TEST_F(TestProjector, TestRegexpExtract) {
30243024
EXPECT_ARROW_ARRAY_EQUALS(exp_extract, outputs.at(0));
30253025
}
30263026

3027+
TEST_F(TestProjector, TestRegexpExtractTwoArg) {
3028+
// schema for input fields
3029+
auto field0 = field("f0", arrow::utf8());
3030+
auto schema = arrow::schema({field0});
3031+
3032+
// output fields
3033+
auto field_extract = field("extract", arrow::utf8());
3034+
3035+
// The two-arg overload defaults to extracting the first capture group (index 1).
3036+
std::string pattern(R"((\w+) (\w+))");
3037+
auto literal = TreeExprBuilder::MakeStringLiteral(pattern);
3038+
auto node0 = TreeExprBuilder::MakeField(field0);
3039+
3040+
// Build expression with the two-arg overload: regexp_extract(string, pattern)
3041+
auto regexp_extract_func =
3042+
TreeExprBuilder::MakeFunction("regexp_extract", {node0, literal}, arrow::utf8());
3043+
auto extract_expr = TreeExprBuilder::MakeExpression(regexp_extract_func, field_extract);
3044+
3045+
std::shared_ptr<Projector> projector;
3046+
auto status = Projector::Make(schema, {extract_expr}, TestConfiguration(), &projector);
3047+
EXPECT_TRUE(status.ok()) << status.message();
3048+
3049+
// Create a row-batch with some sample data
3050+
int num_records = 3;
3051+
auto array0 = MakeArrowArrayUtf8({"John Doe", "Ringo Beast", "stringthatdonotmatch"},
3052+
{true, true, true});
3053+
// expected output: first capture group, empty string when the pattern does not match
3054+
auto exp_extract = MakeArrowArrayUtf8({"John", "Ringo", ""}, {true, true, true});
3055+
3056+
// prepare input record batch
3057+
auto in = arrow::RecordBatch::Make(schema, num_records, {array0});
3058+
3059+
// Evaluate expression
3060+
arrow::ArrayVector outputs;
3061+
status = projector->Evaluate(*in, pool_, &outputs);
3062+
EXPECT_TRUE(status.ok()) << status.message();
3063+
3064+
// Validate results
3065+
EXPECT_ARROW_ARRAY_EQUALS(exp_extract, outputs.at(0));
3066+
}
3067+
30273068
TEST_F(TestProjector, TestCastVarbinary) {
30283069
auto field0 = field("f0", arrow::utf8());
30293070
auto field1 = field("f1", arrow::int64());

0 commit comments

Comments
 (0)