|
| 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.List; |
| 9 | +import java.util.regex.Matcher; |
| 10 | +import java.util.regex.Pattern; |
| 11 | +import org.apache.calcite.adapter.enumerable.NotNullImplementor; |
| 12 | +import org.apache.calcite.adapter.enumerable.NullPolicy; |
| 13 | +import org.apache.calcite.adapter.enumerable.RexToLixTranslator; |
| 14 | +import org.apache.calcite.linq4j.tree.Expression; |
| 15 | +import org.apache.calcite.linq4j.tree.Expressions; |
| 16 | +import org.apache.calcite.rex.RexCall; |
| 17 | +import org.apache.calcite.sql.type.ReturnTypes; |
| 18 | +import org.apache.calcite.sql.type.SqlReturnTypeInference; |
| 19 | +import org.opensearch.sql.calcite.utils.PPLOperandTypes; |
| 20 | +import org.opensearch.sql.expression.function.ImplementorUDF; |
| 21 | +import org.opensearch.sql.expression.function.UDFOperandMetadata; |
| 22 | + |
| 23 | +/** Custom REX_OFFSET function for calculating regex match positions. */ |
| 24 | +public final class RexOffsetFunction extends ImplementorUDF { |
| 25 | + |
| 26 | + public RexOffsetFunction() { |
| 27 | + super(new RexOffsetImplementor(), NullPolicy.ARG0); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public SqlReturnTypeInference getReturnTypeInference() { |
| 32 | + return ReturnTypes.VARCHAR_2000_NULLABLE; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public UDFOperandMetadata getOperandMetadata() { |
| 37 | + return PPLOperandTypes.STRING_STRING; |
| 38 | + } |
| 39 | + |
| 40 | + private static class RexOffsetImplementor implements NotNullImplementor { |
| 41 | + |
| 42 | + @Override |
| 43 | + public Expression implement( |
| 44 | + RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { |
| 45 | + Expression field = translatedOperands.get(0); |
| 46 | + Expression pattern = translatedOperands.get(1); |
| 47 | + |
| 48 | + return Expressions.call(RexOffsetFunction.class, "calculateOffsets", field, pattern); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static String calculateOffsets(String text, String patternStr) { |
| 53 | + if (text == null || patternStr == null) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + Pattern pattern = Pattern.compile(patternStr); |
| 59 | + Matcher matcher = pattern.matcher(text); |
| 60 | + |
| 61 | + if (!matcher.find()) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + StringBuilder result = new StringBuilder(); |
| 66 | + |
| 67 | + Pattern namedGroupPattern = Pattern.compile("\\(\\?<([^>]+)>"); |
| 68 | + Matcher namedGroupMatcher = namedGroupPattern.matcher(patternStr); |
| 69 | + |
| 70 | + int groupIndex = 1; |
| 71 | + boolean firstGroup = true; |
| 72 | + |
| 73 | + while (namedGroupMatcher.find()) { |
| 74 | + String groupName = namedGroupMatcher.group(1); |
| 75 | + |
| 76 | + if (groupIndex <= matcher.groupCount()) { |
| 77 | + int start = matcher.start(groupIndex); |
| 78 | + int end = matcher.end(groupIndex); |
| 79 | + |
| 80 | + if (start >= 0 && end >= 0) { |
| 81 | + if (!firstGroup) { |
| 82 | + result.append(","); |
| 83 | + } |
| 84 | + result.append(groupName).append("=").append(start).append("-").append(end - 1); |
| 85 | + firstGroup = false; |
| 86 | + } |
| 87 | + } |
| 88 | + groupIndex++; |
| 89 | + } |
| 90 | + |
| 91 | + return result.length() > 0 ? result.toString() : null; |
| 92 | + } catch (Exception e) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments