Skip to content

Commit

Permalink
ESQL: Fix IpPrefix function not handling correctly ByteRefs (elastic#…
Browse files Browse the repository at this point in the history
…109205)

- Fix IpPrefix function not using ByteRef offset on calculations, leading to wrong bytes being written to the prefix.
- Fixed the CSVTest case, which was also wrong, leading to a "_wrong * wrong = correct_" situation.
-  ~~Added` a BytesRef randomizer and applied it to the IpPrefixTests suppliers~~
    - ~~This highlighted another bug, which was fixed too~~
    - (Moved to elastic#109207)

Fixes elastic#109198
  • Loading branch information
ivancea authored May 30, 2024
1 parent 0ce54d7 commit 489aac9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/109205.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 109205
summary: "ESQL: Fix `IpPrefix` function not handling correctly `ByteRefs`"
area: ES|QL
type: bug
issues:
- 109198
3 changes: 0 additions & 3 deletions muted-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ tests:
- class: "org.elasticsearch.upgrades.SearchStatesIT"
issue: "https://github.com/elastic/elasticsearch/issues/109190"
method: "testBWCSearchStates"
- class: "org.elasticsearch.xpack.esql.CsvTests"
issue: "https://github.com/elastic/elasticsearch/issues/109198"
method: "test {ip.IpPrefixLengthFromColumn}"
- class: "org.elasticsearch.xpack.test.rest.XPackRestIT"
issue: "https://github.com/elastic/elasticsearch/issues/109200"
method: "test {p0=esql/70_locale/Date format with Italian locale}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ required_capability: fn_ip_prefix
from hosts
| where host == "alpha"
| sort card
| eval prefix = ip_prefix(ip0, 24, 128)
| eval prefix = ip_prefix(ip0, 24, 120)
| keep card, ip0, prefix;

card:keyword | ip0:ip | prefix:ip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,14 @@ static BytesRef process(
throw new IllegalArgumentException("Prefix length v6 must be in range [0, 128], found " + prefixLengthV6);
}

boolean isIpv4 = Arrays.compareUnsigned(ip.bytes, 0, IPV4_PREFIX.length, IPV4_PREFIX, 0, IPV4_PREFIX.length) == 0;
boolean isIpv4 = Arrays.compareUnsigned(
ip.bytes,
ip.offset,
ip.offset + IPV4_PREFIX.length,
IPV4_PREFIX,
0,
IPV4_PREFIX.length
) == 0;

if (isIpv4) {
makePrefix(ip, scratch, 12 + prefixLengthV4 / 8, prefixLengthV4 % 8);
Expand All @@ -154,7 +161,7 @@ private static void makePrefix(BytesRef ip, BytesRef scratch, int fullBytes, int
// Copy the last byte ignoring the trailing bits
if (remainingBits > 0) {
byte lastByteMask = (byte) (0xFF << (8 - remainingBits));
scratch.bytes[fullBytes] = (byte) (ip.bytes[fullBytes] & lastByteMask);
scratch.bytes[fullBytes] = (byte) (ip.bytes[ip.offset + fullBytes] & lastByteMask);
}

// Copy the last empty bytes
Expand Down

0 comments on commit 489aac9

Please sign in to comment.