Skip to content

ESQL: TO_IP can handle leading zeros #126294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

nik9000
Copy link
Member

@nik9000 nik9000 commented Apr 4, 2025

Modifies TO_IP so it can handle leading 0s in ipv4s. It's not done, but the goal is to keep the current behavior:

ROW ip = TO_IP("192.168.0.1") // OK!
ROW ip = TO_IP("192.168.01.1") // Fails

But to allow

ROW ip = TO_IP("192.168.01.1", leading_zeros: octal)
ROW ip = TO_IP("192.168.01.1", leading_zeros: decimal)

I didn't manage to plug in the map arguments properly - and I'm not entirely sure this is the right thing to do with TO_IP anyway. So this is a draft. There's some change we say that we need TO_IP_LEADING_ZEROS_OCTAL and TO_IP_LEADING_ZEROS_DECIMAL instead of using named parameters. That's because TO_IP is a conversion function which is sort of defined as being unary - and adding the options to it breaks that contract.

@elasticsearchmachine
Copy link
Collaborator

Hi @nik9000, I've created a changelog YAML for you.

@@ -33,6 +33,7 @@ base {
dependencies {
compileOnly project(path: xpackModule('core'))
compileOnly project(':modules:lang-painless:spi')
compileOnly project(':libs:simdvec')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a leftover.

) },
description = "(Optional) Additional options.",
optional = true
) Expression options
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fang-xing-esql could you tell me if this is a sane idea? Or should I just make two more functions instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fang-xing-esql could you tell me if this is a sane idea? Or should I just make two more functions instead?

It looks fine to me to extend to_ip, instead of making more functions. :)

public class ParseIp {
private static final byte[] IPV4_PREFIX = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1 };

@ConvertEvaluator(extraName = "LeadingZerosRejected", warnExceptions = { IllegalArgumentException.class })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to extract these to a separate PR because the leadingZerosRejected version can be used even in older versions of Elasticsearch. I can backport that PR to 9.0 and 8.19.

Copy link
Member

@fang-xing-esql fang-xing-esql left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @nik9000! I left a comment related to the format of entries in a MapExpression.

return new TypeResolution("map keys must be strings");
}
if (e.key() instanceof Literal keyl) {
key = (BytesRef) keyl.value();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key and value of an entry in the MapExpression can be either a String or BytesRef, depending on when resolveType() is called. If it is called by Analyzer, they are in (human readable)String format, if it is called by LogicalPlanOptimizer, they might be in BytesRef.

Having some CsvTests should expose the different format, after the interface of to_ip is finalized.

@nik9000
Copy link
Member Author

nik9000 commented Apr 9, 2025

Superseded by #126532

@nik9000 nik9000 closed this Apr 9, 2025
@nik9000 nik9000 removed the v9.1.0 label Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants