Skip to content

Add compare_ip operator udfs #3821

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

ishaoxy
Copy link
Contributor

@ishaoxy ishaoxy commented Jun 25, 2025

Description

added six udfs related to IP comparison.
modify ip.sqlTypeName from OTHER to NULL in type checker.

Related Issues

Resolves #3776

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@qianheng-aws
Copy link
Collaborator

I think we should implement implicit conversion before implementing comparison for non-primitive types.

After that, for Ip comparison, we can convert string to ExprIP. And then we can implement some generic comparison functions for types of comparable, via calling their compareTo method.

But current approach is still acceptable if we won't have other UDT in the future.

Copy link
Collaborator

@qianheng-aws qianheng-aws left a comment

Choose a reason for hiding this comment

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

Please add IT for ip comparison.

// UserDefinedFunctionUtils.convertRelDataTypeToSqlTypeName
return UDFOperandMetadata.wrap(OperandTypes.STRING_STRING);
return UDFOperandMetadata.wrap(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should use new PPLTypeChecker here and override its checkOperandTypes method to only allow accept string or ip.

@@ -400,6 +400,10 @@ private static List<ExprType> getExprTypes(SqlTypeFamily family) {
OpenSearchTypeFactory.TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER));
case ANY, IGNORE -> List.of(
OpenSearchTypeFactory.TYPE_FACTORY.createSqlType(SqlTypeName.ANY));
// We borrow SqlTypeFamily.NULL to represent EXPR_IP. This is a workaround
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we stop use CompositeOperandTypeChecker and create a new TypeChecker ourself, maybe we can avoid using SqlTypeFamily.NULL to represent EXPR_IP and check operands on RelDataType level.

@@ -614,6 +614,14 @@ public PPLTypeChecker getTypeChecker() {
}

void populate() {
// register operators for IP comparing
registerOperator(NOTEQUAL, PPLBuiltinOperators.NOT_EQUALS_IP);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Questions, Do we need to support IP as UDT in PPL engine?

In OpenSearch PPL, IP handling depends on index field type. CIDR-based filtering should use ip_range queries for ip fields, script pushdown / in-memory processing for keyword, text, and runtime string fields.

Field Type Use Case Expectation
IP Field search index=log ip="192.168.0.0/16" Rewrite as term query
  search index | where cidrmatch("192.168.0.0/16", ip) Rewrite as term query
Keyword Field search index=log ip="192.168.0.0/16" Rewrite as term query, extactally keyword match
  search index | where cidrmatch("192.168.0.0/16", ip) Script pushdown — ip field is a string, not rewrite as term query.
Text Field search index=log ip="192.168.0.0/16" Rewrite as query_string query, full text search
  search index | where cidrmatch("192.168.0.0/16", ip) Script pushdown — ip field is a string, not rewrite as term query.
Runtime Field search index=log | parse ip=regex(...)| where ip="192.168.0.0/16" Script pushdown — ip field is a string, it is a string comparsion query
  search index=log | parse ip=regex(...)| where cidrmatch("192.168.0.0/16", ip) Script pushdown — ip field is a string, not rewrite as term query.

@Swiddis Swiddis added the enhancement New feature or request label Jun 26, 2025
@ishaoxy
Copy link
Contributor Author

ishaoxy commented Jun 27, 2025

Please add IT for ip comparison.

Handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Support comparing IP values with Calcite
4 participants