Skip to content

Commit 737eae0

Browse files
committed
Tests work great now
1 parent 1502b71 commit 737eae0

File tree

4 files changed

+32
-49
lines changed

4 files changed

+32
-49
lines changed

x-pack/plugin/esql/src/main/generated/org/elasticsearch/xpack/esql/expression/function/scalar/ip/NetworkDirectionEvaluator.java

Lines changed: 6 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/ip/NetworkDirection.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
import java.util.List;
3535

3636
import static org.elasticsearch.compute.ann.Fixed.Scope.THREAD_LOCAL;
37+
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
38+
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
39+
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.THIRD;
40+
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.fromIndex;
41+
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isIPAndExact;
42+
import static org.elasticsearch.xpack.esql.expression.EsqlTypeResolutions.isStringAndExact;
3743

3844
/**
3945
* Returns the direction type (inbound, outbound, internal, external) given a source IP address, destination IP address, and a list of internal networks.
@@ -126,15 +132,15 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvalua
126132
);
127133
}
128134

129-
@Evaluator(warnExceptions = IllegalArgumentException.class)
130-
static BytesRef process(@Fixed(includeInToString=false, scope=THREAD_LOCAL) BytesRef scratch, @Fixed(includeInToString=false, scope=THREAD_LOCAL) BytesRef netScratch, BytesRef sourceIp, BytesRef destinationIp, @Position int position, BytesRefBlock networks) {
135+
@Evaluator()
136+
static void process(BytesRefBlock.Builder builder, @Fixed(includeInToString=false, scope=THREAD_LOCAL) BytesRef scratch, @Fixed(includeInToString=false, scope=THREAD_LOCAL) BytesRef netScratch, BytesRef sourceIp, BytesRef destinationIp, @Position int position, BytesRefBlock networks) {
131137
int valueCount = networks.getValueCount(position);
132138
if (valueCount == 0) {
133-
throw new IllegalArgumentException("List of internal networks must not be empty");
139+
builder.appendNull();
140+
return;
134141
}
135142
int first = networks.getFirstValueIndex(position);
136143

137-
138144
System.arraycopy(sourceIp.bytes, sourceIp.offset, scratch.bytes, 0, sourceIp.length);
139145
InetAddress sourceIpAddress = InetAddressPoint.decode(scratch.bytes);
140146
System.arraycopy(destinationIp.bytes, destinationIp.offset, scratch.bytes, 0, destinationIp.length);
@@ -156,14 +162,25 @@ static BytesRef process(@Fixed(includeInToString=false, scope=THREAD_LOCAL) Byte
156162
}
157163
}
158164

159-
return new BytesRef(NetworkDirectionUtils.getDirection(sourceInternal, destinationInternal));
165+
builder.appendBytesRef(new BytesRef(NetworkDirectionUtils.getDirection(sourceInternal, destinationInternal)));
160166
}
161167

162168
@Override
163169
public DataType dataType() {
164170
return DataType.KEYWORD;
165171
}
166172

173+
@Override
174+
protected TypeResolution resolveType() {
175+
if (childrenResolved() == false) {
176+
return new TypeResolution("Unresolved children");
177+
}
178+
179+
return isIPAndExact(sourceIpField, sourceText(), FIRST)
180+
.and(isIPAndExact(destinationIpField, sourceText(), SECOND))
181+
.and(isStringAndExact(internalNetworks, sourceText(), THIRD));
182+
}
183+
167184
public Expression sourceIpField() {
168185
return sourceIpField;
169186
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/ip/NetworkDirectionErrorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected Expression build(Source source, List<Expression> args) {
3434
protected Matcher<String> expectedTypeErrorMatcher(List<Set<DataType>> validPerPosition, List<DataType> signature) {
3535
return equalTo(typeErrorMessage(true, validPerPosition, signature, (v, p) -> switch (p) {
3636
case 0, 1 -> "ip";
37-
case 2 -> "keyword";
37+
case 2 -> "string";
3838
default -> "";
3939
}));
4040
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/ip/NetworkDirectionTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.function.Supplier;
2626

27+
import static org.hamcrest.Matchers.nullValue;
2728
import static org.hamcrest.core.IsEqual.equalTo;
2829

2930
public class NetworkDirectionTests extends AbstractScalarFunctionTestCase {
@@ -34,7 +35,7 @@ public NetworkDirectionTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCas
3435
@ParametersFactory
3536
public static Iterable<Object[]> parameters() {
3637
// These tests copy the data from the NetworkDirectionUtils tests
37-
var suppliers = new ArrayList<TestCaseSupplier>();
38+
List<TestCaseSupplier> suppliers = new ArrayList<>();
3839

3940
for (var stringType : DataType.stringTypes()) {
4041
suppliers.addAll(List.of(
@@ -212,6 +213,7 @@ public static Iterable<Object[]> parameters() {
212213
)
213214
));
214215
}
216+
suppliers = anyNullIsNull(true, suppliers);
215217

216218
return parameterSuppliersFromTypedData(randomizeBytesRefsOffset(suppliers));
217219
}

0 commit comments

Comments
 (0)