Skip to content

Commit 319552e

Browse files
committed
Change how extraction of IP from BytesRef is performed
1 parent 07b6c8f commit 319552e

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

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

Lines changed: 22 additions & 22 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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package org.elasticsearch.xpack.esql.expression.function.scalar.ip;
99

10+
import org.apache.lucene.document.InetAddressPoint;
1011
import org.apache.lucene.util.BytesRef;
1112
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1213
import org.elasticsearch.common.io.stream.StreamInput;
@@ -26,6 +27,7 @@
2627
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
2728
import org.elasticsearch.xpack.esql.expression.function.Param;
2829
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
30+
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.In;
2931
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
3032

3133
import java.io.IOException;
@@ -118,7 +120,7 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvalua
118120
var internalNetworksEvaluatorSupplier = toEvaluator.apply(internalNetworks);
119121
return new NetworkDirectionEvaluator.Factory(
120122
source(),
121-
context -> new BytesRef(),
123+
context -> new BytesRef(16),
122124
sourceIpEvaluatorSupplier,
123125
destinationIpEvaluatorSupplier,
124126
internalNetworksEvaluatorSupplier
@@ -127,9 +129,11 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvalua
127129

128130
@Evaluator
129131
static BytesRef process(@Fixed(includeInToString=false, scope=THREAD_LOCAL) BytesRef scratch, BytesRef sourceIp, BytesRef destinationIp, @Position int position, BytesRefBlock networks) {
130-
// Pulling the bytes out directly using InetAddress.getByAddress() requires error handling TODO
131-
InetAddress sourceIpAddress = InetAddresses.forString(sourceIp.utf8ToString());
132-
InetAddress destinationIpAddress = InetAddresses.forString(destinationIp.utf8ToString());
132+
System.arraycopy(sourceIp.bytes, sourceIp.offset, scratch.bytes, 0, sourceIp.length);
133+
InetAddress sourceIpAddress = InetAddressPoint.decode(scratch.bytes);
134+
System.arraycopy(destinationIp.bytes, destinationIp.offset, scratch.bytes, 0, destinationIp.length);
135+
InetAddress destinationIpAddress = InetAddressPoint.decode(scratch.bytes);
136+
133137
boolean sourceInternal = false;
134138
boolean destinationInternal = false;
135139

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,34 @@
2424
import java.util.List;
2525
import java.util.function.Supplier;
2626

27+
import static org.hamcrest.core.IsEqual.equalTo;
28+
2729
public class NetworkDirectionTests extends AbstractScalarFunctionTestCase {
2830
public NetworkDirectionTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
2931
this.testCase = testCaseSupplier.get();
3032
}
3133

34+
@ParametersFactory
35+
public static Iterable<Object[]> parameters() {
36+
var suppliers = List.of(
37+
new TestCaseSupplier(
38+
List.of(DataType.IP, DataType.IP, DataType.KEYWORD),
39+
() -> new TestCaseSupplier.TestCase(
40+
List.of(
41+
new TestCaseSupplier.TypedData(EsqlDataTypeConverter.stringToIP("10.0.1.1"), DataType.IP, "source_ip"),
42+
new TestCaseSupplier.TypedData(EsqlDataTypeConverter.stringToIP("192.168.1.2"), DataType.IP, "destination_ip"),
43+
new TestCaseSupplier.TypedData(List.of("10.0.0.0/8"), DataType.KEYWORD, "internal_networks")
44+
),
45+
"NetworkDirectionEvaluator[sourceIp=Attribute[channel=0], destinationIp=Attribute[channel=1], networks=Attribute[channel=2]]",
46+
DataType.KEYWORD,
47+
equalTo(new BytesRef(NetworkDirectionUtils.DIRECTION_INBOUND))
48+
)
49+
)
50+
);
51+
52+
return parameterSuppliersFromTypedDataWithDefaultChecksNoErrors(true, suppliers);
53+
}
54+
3255
@Override
3356
protected Expression build(Source source, List<Expression> args) {
3457
return new NetworkDirection(source, args.get(0), args.get(1), args.get(2));

0 commit comments

Comments
 (0)