|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.expression.function.udf; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | +import org.apache.calcite.adapter.enumerable.NotNullImplementor; |
| 13 | +import org.apache.calcite.adapter.enumerable.NullPolicy; |
| 14 | +import org.apache.calcite.adapter.enumerable.RexToLixTranslator; |
| 15 | +import org.apache.calcite.linq4j.tree.Expression; |
| 16 | +import org.apache.calcite.linq4j.tree.Expressions; |
| 17 | +import org.apache.calcite.rex.RexCall; |
| 18 | +import org.apache.calcite.sql.type.SqlReturnTypeInference; |
| 19 | +import org.apache.calcite.sql.type.SqlTypeName; |
| 20 | +import org.opensearch.sql.calcite.utils.PPLOperandTypes; |
| 21 | +import org.opensearch.sql.expression.function.ImplementorUDF; |
| 22 | +import org.opensearch.sql.expression.function.UDFOperandMetadata; |
| 23 | + |
| 24 | +/** Custom REX_EXTRACT_MULTI function for extracting multiple regex matches. */ |
| 25 | +public final class RexExtractMultiFunction extends ImplementorUDF { |
| 26 | + |
| 27 | + public RexExtractMultiFunction() { |
| 28 | + super(new RexExtractMultiImplementor(), NullPolicy.ARG0); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public SqlReturnTypeInference getReturnTypeInference() { |
| 33 | + return call -> |
| 34 | + call.getTypeFactory() |
| 35 | + .createArrayType(call.getTypeFactory().createSqlType(SqlTypeName.VARCHAR, 2000), -1); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public UDFOperandMetadata getOperandMetadata() { |
| 40 | + return PPLOperandTypes.STRING_STRING_INTEGER_INTEGER; |
| 41 | + } |
| 42 | + |
| 43 | + private static class RexExtractMultiImplementor implements NotNullImplementor { |
| 44 | + |
| 45 | + @Override |
| 46 | + public Expression implement( |
| 47 | + RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { |
| 48 | + Expression field = translatedOperands.get(0); |
| 49 | + Expression pattern = translatedOperands.get(1); |
| 50 | + Expression groupIndex = translatedOperands.get(2); |
| 51 | + Expression maxMatch = translatedOperands.get(3); |
| 52 | + |
| 53 | + return Expressions.call( |
| 54 | + RexExtractMultiFunction.class, |
| 55 | + "extractMultipleGroups", |
| 56 | + field, |
| 57 | + pattern, |
| 58 | + groupIndex, |
| 59 | + maxMatch); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static List<String> extractMultipleGroups( |
| 64 | + String text, String pattern, int groupIndex, int maxMatch) { |
| 65 | + if (text == null || pattern == null) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + Pattern compiledPattern = Pattern.compile(pattern); |
| 71 | + Matcher matcher = compiledPattern.matcher(text); |
| 72 | + List<String> matches = new ArrayList<>(); |
| 73 | + |
| 74 | + int matchCount = 0; |
| 75 | + while (matcher.find() && (maxMatch == 0 || matchCount < maxMatch)) { |
| 76 | + if (groupIndex > 0 && groupIndex <= matcher.groupCount()) { |
| 77 | + String match = matcher.group(groupIndex); |
| 78 | + if (match != null) { |
| 79 | + matches.add(match); |
| 80 | + matchCount++; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return matches.isEmpty() ? null : matches; |
| 86 | + } catch (Exception e) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments