trino: IpAddressFunctions.contains should return False instead of raising an exception on IP version mismatch

I have an IP field that contains a mix of IPv4 and IPv6 addresses. I want to get all rows where the IP is in a CIDR. But I get the following exception:

TrinoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message="IP address version should be the same", query_id=...)

The expected behaviour would be to return False as the IP is not in the CIDR

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

@Sartan4455, here’s a more comprehensive implementation for this: https://github.com/trinodb/trino/pull/18997

This would be a good improvement to make. We tried to be conservative in the initial implementation (https://github.com/trinodb/trino/pull/110) because we didn’t have a good understanding of how this should work in the context of mixed IPv4/6 addresses and CIDRs. It’s always easy to relax and expand behaviors that are currently not supported, but the opposite can be problematic.

Our usecase: string column of IP4 and IP6 values. Our queries are being build by our own builder tool, so we just check if the CIDR value contains a dot or colon and add a LIKE filter to the query. "ip_field LIKE '%.%' AND contains('10.0.0.0/8', CAST("ip_field" as IPADDRESS))

Python code using the pika library:

def IpInCidr(term: Term, cidr, alias: str = None):
    # Need to ensure we compare IPv4 to IPv4 CIDR and IPv6 to IPv6 CIDR
    # https://github.com/trinodb/trino/issues/18497
    char = '.' if '.' in cidr else ':'

    return term.like(f'%{char}%') & Contains(cidr, Cast(term, 'IPADDRESS'), alias=alias)