Skip to content

Commit

Permalink
Fix default value handling in REGEXP_EXTRACT transform function (#14489)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmayya authored Nov 21, 2024
1 parent c5e00bb commit 87ea7c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public void init(List<TransformFunction> arguments, Map<String, ColumnContext> c
}

if (arguments.size() == 4) {
TransformFunction positionFunction = arguments.get(3);
Preconditions.checkState(positionFunction instanceof LiteralTransformFunction,
TransformFunction defaultValueTransformFunction = arguments.get(3);
Preconditions.checkState(defaultValueTransformFunction instanceof LiteralTransformFunction,
"`default_value` must be a literal expression.");
_defaultValue = ((LiteralTransformFunction) regexpFunction).getStringLiteral();
_defaultValue = ((LiteralTransformFunction) defaultValueTransformFunction).getStringLiteral();
} else {
_defaultValue = "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,20 @@ public void testRegexpExtractLegalArguments(String expressionStr, int group, Str
for (int i = 0; i < NUM_ROWS; i++) {
Matcher matcher = PATTERN.matcher(_stringSVValues[i]);
Assert.assertEquals(
matcher.find() && matcher.groupCount() >= group ? matcher.group(group) : defaultValue,
actualValues[i]);
actualValues[i],
matcher.find() && matcher.groupCount() >= group ? matcher.group(group) : defaultValue
);
}
}

@Test
public void testDefaultValue() {
String expressionStr = String.format("REGEXP_EXTRACT(%s, '%s', 1, 'null')", STRING_SV_COLUMN, "nonMatchingRegex");
ExpressionContext expression = RequestContextUtils.getExpression(expressionStr);
TransformFunction transformFunction = TransformFunctionFactory.get(expression, _dataSourceMap);
String[] actualValues = transformFunction.transformToStringValuesSV(_projectionBlock);
for (int i = 0; i < NUM_ROWS; i++) {
Assert.assertEquals(actualValues[i], "null");
}
}

Expand Down

0 comments on commit 87ea7c8

Please sign in to comment.