|
| 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.Pattern; |
| 10 | +import java.util.regex.PatternSyntaxException; |
| 11 | +import org.apache.calcite.adapter.enumerable.NotNullImplementor; |
| 12 | +import org.apache.calcite.adapter.enumerable.NullPolicy; |
| 13 | +import org.apache.calcite.adapter.enumerable.RexImpTable; |
| 14 | +import org.apache.calcite.adapter.enumerable.RexToLixTranslator; |
| 15 | +import org.apache.calcite.linq4j.tree.Expression; |
| 16 | +import org.apache.calcite.linq4j.tree.Types; |
| 17 | +import org.apache.calcite.rex.RexCall; |
| 18 | +import org.apache.calcite.schema.impl.ScalarFunctionImpl; |
| 19 | +import org.apache.calcite.sql.type.ReturnTypes; |
| 20 | +import org.apache.calcite.sql.type.SqlReturnTypeInference; |
| 21 | +import org.opensearch.sql.expression.function.ImplementorUDF; |
| 22 | +import org.opensearch.sql.expression.function.UDFOperandMetadata; |
| 23 | + |
| 24 | +/** |
| 25 | + * REGEX_MATCH UDF implementation for Calcite engine. |
| 26 | + * This function provides Java regex matching via script query pushdown. |
| 27 | + */ |
| 28 | +public class RegexMatchFunctionImpl extends ImplementorUDF { |
| 29 | + |
| 30 | + public RegexMatchFunctionImpl() { |
| 31 | + super(new RegexMatchImplementor(), NullPolicy.ANY); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public SqlReturnTypeInference getReturnTypeInference() { |
| 36 | + return ReturnTypes.BOOLEAN_FORCE_NULLABLE; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public UDFOperandMetadata getOperandMetadata() { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + public static class RegexMatchImplementor implements NotNullImplementor { |
| 45 | + @Override |
| 46 | + public Expression implement( |
| 47 | + RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { |
| 48 | + ScalarFunctionImpl function = |
| 49 | + (ScalarFunctionImpl) |
| 50 | + ScalarFunctionImpl.create( |
| 51 | + Types.lookupMethod( |
| 52 | + RegexMatchFunctionImpl.class, "eval", String.class, String.class)); |
| 53 | + return function.getImplementor().implement(translator, call, RexImpTable.NullAs.NULL); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Evaluation method for REGEX_MATCH function. This method is called by Calcite's generated code |
| 59 | + * during execution. |
| 60 | + * |
| 61 | + * @param field The field value to match against |
| 62 | + * @param pattern The Java regex pattern |
| 63 | + * @return Boolean result of regex match |
| 64 | + */ |
| 65 | + public static Boolean eval(String field, String pattern) { |
| 66 | + if (field == null || pattern == null) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + // Use Java regex library for evaluation (same logic as RegexMatch) |
| 71 | + try { |
| 72 | + Pattern compiledPattern = Pattern.compile(pattern); |
| 73 | + java.util.regex.Matcher matcher = compiledPattern.matcher(field); |
| 74 | + return matcher.find(); // Use find() for partial match like SPL |
| 75 | + } catch (PatternSyntaxException e) { |
| 76 | + throw new IllegalArgumentException("Invalid regex pattern: " + e.getMessage()); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments