6
6
7
7
package org .elasticsearch .xpack .eql .expression .function .scalar .string ;
8
8
9
+ import org .elasticsearch .common .logging .LoggerMessageFormat ;
9
10
import org .elasticsearch .xpack .ql .expression .Expression ;
10
11
import org .elasticsearch .xpack .ql .expression .Expressions ;
11
12
import org .elasticsearch .xpack .ql .expression .Expressions .ParamOrdinal ;
12
- import org .elasticsearch .xpack .ql .expression .function . scalar . BaseSurrogateFunction ;
13
+ import org .elasticsearch .xpack .ql .expression .FieldAttribute ;
13
14
import org .elasticsearch .xpack .ql .expression .function .scalar .ScalarFunction ;
14
- import org .elasticsearch .xpack .ql .expression .predicate .logical .Or ;
15
- import org .elasticsearch .xpack .ql .expression .predicate .operator .comparison .Equals ;
15
+ import org .elasticsearch .xpack .ql .expression .gen .pipeline .Pipe ;
16
+ import org .elasticsearch .xpack .ql .expression .gen .script .ScriptTemplate ;
17
+ import org .elasticsearch .xpack .ql .expression .gen .script .Scripts ;
16
18
import org .elasticsearch .xpack .ql .tree .NodeInfo ;
17
19
import org .elasticsearch .xpack .ql .tree .Source ;
18
20
import org .elasticsearch .xpack .ql .type .DataType ;
19
21
import org .elasticsearch .xpack .ql .type .DataTypes ;
20
22
import org .elasticsearch .xpack .ql .util .CollectionUtils ;
21
23
24
+ import java .util .ArrayList ;
25
+ import java .util .LinkedHashSet ;
22
26
import java .util .List ;
23
27
28
+ import static java .util .Collections .emptyList ;
24
29
import static java .util .Collections .singletonList ;
30
+ import static org .elasticsearch .xpack .eql .expression .function .scalar .string .CIDRMatchFunctionProcessor .doProcess ;
25
31
import static org .elasticsearch .xpack .ql .expression .TypeResolutions .isFoldable ;
26
32
import static org .elasticsearch .xpack .ql .expression .TypeResolutions .isIPAndExact ;
27
33
import static org .elasticsearch .xpack .ql .expression .TypeResolutions .isStringAndExact ;
34
+ import static org .elasticsearch .xpack .ql .expression .gen .script .ParamsBuilder .paramsBuilder ;
28
35
29
36
/**
30
37
* EQL specific cidrMatch function
31
38
* Returns true if the source address matches any of the provided CIDR blocks.
32
39
* Refer to: https://eql.readthedocs.io/en/latest/query-guide/functions.html#cidrMatch
33
40
*/
34
- public class CIDRMatch extends BaseSurrogateFunction {
41
+ public class CIDRMatch extends ScalarFunction {
35
42
36
43
private final Expression field ;
37
44
private final List <Expression > addresses ;
38
45
39
46
public CIDRMatch (Source source , Expression field , List <Expression > addresses ) {
40
- super (source , CollectionUtils .combine (singletonList (field ), addresses ));
47
+ super (source , CollectionUtils .combine (singletonList (field ), addresses == null ? emptyList () : addresses ));
41
48
this .field = field ;
42
- this .addresses = addresses ;
49
+ this .addresses = addresses == null ? emptyList () : addresses ;
43
50
}
44
51
45
- @ Override
46
- protected NodeInfo <? extends Expression > info () {
47
- return NodeInfo .create (this , CIDRMatch ::new , field , addresses );
52
+ public Expression field () {
53
+ return field ;
48
54
}
49
55
50
- @ Override
51
- public Expression replaceChildren (List <Expression > newChildren ) {
52
- if (newChildren .size () < 2 ) {
53
- throw new IllegalArgumentException ("expected at least [2] children but received [" + newChildren .size () + "]" );
54
- }
55
- return new CIDRMatch (source (), newChildren .get (0 ), newChildren .subList (1 , newChildren .size ()));
56
- }
57
-
58
- @ Override
59
- public DataType dataType () {
60
- return DataTypes .BOOLEAN ;
56
+ public List <Expression > addresses () {
57
+ return addresses ;
61
58
}
62
59
63
60
@ Override
@@ -71,15 +68,6 @@ protected TypeResolution resolveType() {
71
68
return resolution ;
72
69
}
73
70
74
- for (Expression addr : addresses ) {
75
- // Currently we have limited enum for ordinal numbers
76
- // So just using default here for error messaging
77
- resolution = isStringAndExact (addr , sourceText (), ParamOrdinal .DEFAULT );
78
- if (resolution .unresolved ()) {
79
- return resolution ;
80
- }
81
- }
82
-
83
71
int index = 1 ;
84
72
85
73
for (Expression addr : addresses ) {
@@ -101,14 +89,60 @@ protected TypeResolution resolveType() {
101
89
}
102
90
103
91
@ Override
104
- public ScalarFunction makeSubstitute () {
105
- ScalarFunction func = null ;
106
-
92
+ protected Pipe makePipe () {
93
+ ArrayList <Pipe > arr = new ArrayList <>(addresses .size ());
107
94
for (Expression address : addresses ) {
108
- final Equals eq = new Equals (source (), field , address );
109
- func = (func == null ) ? eq : new Or (source (), func , eq );
95
+ arr .add (Expressions .pipe (address ));
110
96
}
97
+ return new CIDRMatchFunctionPipe (source (), this , Expressions .pipe (field ), arr );
98
+ }
99
+
100
+ @ Override
101
+ public boolean foldable () {
102
+ return field .foldable () && Expressions .foldable (addresses );
103
+ }
111
104
112
- return func ;
105
+ @ Override
106
+ public Object fold () {
107
+ return doProcess (field .fold (), Expressions .fold (addresses ));
108
+ }
109
+
110
+ @ Override
111
+ protected NodeInfo <? extends Expression > info () {
112
+ return NodeInfo .create (this , CIDRMatch ::new , field , addresses );
113
+ }
114
+
115
+ @ Override
116
+ public ScriptTemplate asScript () {
117
+ ScriptTemplate leftScript = asScript (field );
118
+
119
+ List <Object > values = new ArrayList <>(new LinkedHashSet <>(Expressions .fold (addresses )));
120
+ return new ScriptTemplate (
121
+ formatTemplate (LoggerMessageFormat .format ("{eql}." ,"cidrMatch({}, {})" , leftScript .template ())),
122
+ paramsBuilder ()
123
+ .script (leftScript .params ())
124
+ .variable (values )
125
+ .build (),
126
+ dataType ());
127
+ }
128
+
129
+ @ Override
130
+ public ScriptTemplate scriptWithField (FieldAttribute field ) {
131
+ return new ScriptTemplate (processScript (Scripts .DOC_VALUE ),
132
+ paramsBuilder ().variable (field .exactAttribute ().name ()).build (),
133
+ dataType ());
134
+ }
135
+
136
+ @ Override
137
+ public DataType dataType () {
138
+ return DataTypes .BOOLEAN ;
139
+ }
140
+
141
+ @ Override
142
+ public Expression replaceChildren (List <Expression > newChildren ) {
143
+ if (newChildren .size () < 2 ) {
144
+ throw new IllegalArgumentException ("expected at least [2] children but received [" + newChildren .size () + "]" );
145
+ }
146
+ return new CIDRMatch (source (), newChildren .get (0 ), newChildren .subList (1 , newChildren .size ()));
113
147
}
114
148
}
0 commit comments