|
10 | 10 | import org.elasticsearch.xpack.ql.expression.Expressions;
|
11 | 11 | import org.elasticsearch.xpack.ql.expression.Nullability;
|
12 | 12 | import org.elasticsearch.xpack.ql.expression.function.scalar.UnaryScalarFunction;
|
| 13 | +import org.elasticsearch.xpack.ql.expression.gen.processor.Processor; |
| 14 | +import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate; |
13 | 15 | import org.elasticsearch.xpack.ql.tree.Source;
|
14 | 16 | import org.elasticsearch.xpack.ql.type.DataType;
|
15 | 17 | import org.elasticsearch.xpack.ql.type.DataTypes;
|
16 | 18 |
|
17 | 19 | import java.util.Objects;
|
18 | 20 |
|
| 21 | +import static org.elasticsearch.common.logging.LoggerMessageFormat.format; |
19 | 22 | import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isStringAndExact;
|
| 23 | +import static org.elasticsearch.xpack.ql.expression.gen.script.ParamsBuilder.paramsBuilder; |
20 | 24 |
|
21 |
| -public abstract class RegexMatch<T> extends UnaryScalarFunction { |
| 25 | +public abstract class RegexMatch<T extends StringPattern> extends UnaryScalarFunction { |
22 | 26 |
|
23 | 27 | private final T pattern;
|
24 | 28 |
|
@@ -54,8 +58,30 @@ public boolean foldable() {
|
54 | 58 | // right() is not directly foldable in any context but Like can fold it.
|
55 | 59 | return field().foldable();
|
56 | 60 | }
|
57 |
| - |
| 61 | + |
| 62 | + @Override |
| 63 | + public Boolean fold() { |
| 64 | + Object val = field().fold(); |
| 65 | + return RegexProcessor.RegexOperation.match(val, pattern().asJavaRegex()); |
| 66 | + } |
| 67 | + |
58 | 68 | @Override
|
| 69 | + protected Processor makeProcessor() { |
| 70 | + return new RegexProcessor(pattern().asJavaRegex()); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public ScriptTemplate asScript() { |
| 75 | + ScriptTemplate fieldAsScript = asScript(field()); |
| 76 | + return new ScriptTemplate( |
| 77 | + formatTemplate(format("{sql}.", "regex({},{})", fieldAsScript.template())), |
| 78 | + paramsBuilder() |
| 79 | + .script(fieldAsScript.params()) |
| 80 | + .variable(pattern.asJavaRegex()) |
| 81 | + .build(), |
| 82 | + dataType()); |
| 83 | + } |
| 84 | + |
59 | 85 | public boolean equals(Object obj) {
|
60 | 86 | return super.equals(obj) && Objects.equals(((RegexMatch<?>) obj).pattern(), pattern());
|
61 | 87 | }
|
|
0 commit comments