|
| 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_SED function for string replacement using sed expressions. */ |
| 24 | +public final class RexSedFunction extends ImplementorUDF { |
| 25 | + |
| 26 | + public RexSedFunction() { |
| 27 | + super(new RexSedImplementor(), 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 RexSedImplementor 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 sedExpression = translatedOperands.get(1); |
| 47 | + |
| 48 | + return Expressions.call(RexSedFunction.class, "applySed", field, sedExpression); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static String applySed(String text, String sedExpression) { |
| 53 | + if (text == null || sedExpression == null) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + if (sedExpression.startsWith("s/")) { |
| 59 | + return applySubstitution(text, sedExpression); |
| 60 | + } else if (sedExpression.startsWith("y/")) { |
| 61 | + return applyTransliteration(text, sedExpression); |
| 62 | + } else { |
| 63 | + return text; |
| 64 | + } |
| 65 | + } catch (Exception e) { |
| 66 | + return text; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static String applySubstitution(String text, String sedExpression) { |
| 71 | + Pattern sedPattern = Pattern.compile("s/(.*?)/(.*?)/(.*?)$"); |
| 72 | + Matcher sedMatcher = sedPattern.matcher(sedExpression); |
| 73 | + |
| 74 | + if (!sedMatcher.matches()) { |
| 75 | + return text; |
| 76 | + } |
| 77 | + |
| 78 | + String regex = sedMatcher.group(1); |
| 79 | + String replacement = sedMatcher.group(2); |
| 80 | + String flags = sedMatcher.group(3); |
| 81 | + |
| 82 | + try { |
| 83 | + Pattern regexPattern = Pattern.compile(regex); |
| 84 | + String javaReplacement = convertSedBackreferencesToJava(replacement); |
| 85 | + |
| 86 | + if (flags.contains("g")) { |
| 87 | + return regexPattern.matcher(text).replaceAll(javaReplacement); |
| 88 | + } else if (flags.matches("\\d+")) { |
| 89 | + int nth = Integer.parseInt(flags); |
| 90 | + return replaceNthOccurrence(text, regexPattern, javaReplacement, nth); |
| 91 | + } else { |
| 92 | + return regexPattern.matcher(text).replaceFirst(javaReplacement); |
| 93 | + } |
| 94 | + } catch (Exception e) { |
| 95 | + return text; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static String applyTransliteration(String text, String sedExpression) { |
| 100 | + Pattern yPattern = Pattern.compile("y/(.*?)/(.*?)/"); |
| 101 | + Matcher yMatcher = yPattern.matcher(sedExpression); |
| 102 | + |
| 103 | + if (!yMatcher.matches()) { |
| 104 | + return text; |
| 105 | + } |
| 106 | + |
| 107 | + String from = yMatcher.group(1); |
| 108 | + String to = yMatcher.group(2); |
| 109 | + |
| 110 | + if (from.length() != to.length()) { |
| 111 | + return text; |
| 112 | + } |
| 113 | + |
| 114 | + StringBuilder result = new StringBuilder(); |
| 115 | + for (char c : text.toCharArray()) { |
| 116 | + int index = from.indexOf(c); |
| 117 | + if (index >= 0) { |
| 118 | + result.append(to.charAt(index)); |
| 119 | + } else { |
| 120 | + result.append(c); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return result.toString(); |
| 125 | + } |
| 126 | + |
| 127 | + private static String replaceNthOccurrence( |
| 128 | + String text, Pattern pattern, String replacement, int nth) { |
| 129 | + Matcher matcher = pattern.matcher(text); |
| 130 | + StringBuffer result = new StringBuffer(); |
| 131 | + int count = 0; |
| 132 | + |
| 133 | + while (matcher.find()) { |
| 134 | + count++; |
| 135 | + if (count == nth) { |
| 136 | + matcher.appendReplacement(result, replacement); |
| 137 | + } else { |
| 138 | + matcher.appendReplacement(result, matcher.group()); |
| 139 | + } |
| 140 | + } |
| 141 | + matcher.appendTail(result); |
| 142 | + |
| 143 | + return result.toString(); |
| 144 | + } |
| 145 | + |
| 146 | + private static String convertSedBackreferencesToJava(String sedReplacement) { |
| 147 | + return sedReplacement.replaceAll("\\\\(\\d+)", "\\$$1"); |
| 148 | + } |
| 149 | +} |
0 commit comments