Skip to main content

search_ip function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 19

Beta

This feature is in Beta.

Searches expr for IP addresses or Classless Inter-Domain Routing (CIDR) blocks that match search_pattern.

Syntax​

search_ip(expr, search_pattern [, search_pattern ...])

Arguments​

  • expr: The expression to search. Only one target expression is supported.
  • search_pattern: One or more STRING constant expressions, each containing an IPv4 address, IPv6 address, or CIDR block. You can pass up to 5 search patterns per call. To search for more, combine several calls with AND.

Returns​

A BOOLEAN value.

An address found in expr matches a search term when it belongs to that term's CIDR block. A CIDR block found in expr matches when it is a subnet of the searched CIDR block. IPv6 hexadecimal digits match regardless of letter case. Invalid IP-shaped text in expr is ignored.

When you pass multiple search_pattern arguments, every term must match within the same string-searchable value in expr. Matches from separate string-searchable values are not combined.

Notes​

Each search_pattern must be a valid IPv4 address, IPv6 address, or CIDR block. search_ip treats a plain IPv4 address as a /32 CIDR block and a plain IPv6 address as a /128 CIDR block.

Examples of supported address forms include:

  • IPv4: 192.0.2.1.
  • IPv4-mapped IPv6 in dotted-decimal form: ::ffff:192.0.2.1.
  • IPv4-mapped IPv6 in hexadecimal form: ::ffff:c000:0201.

The following types are string-searchable:

Type

String-searchable when

VOID, VARIANT

Always.

STRING

Only when it uses the default UTF8_BINARY collation. The function ignores STRING values with any other collation. See Collation.

ARRAY

Its child expression is string-searchable.

MAP

Its value expression is string-searchable.

STRUCT

At least one of its field expressions is string-searchable.

Type

String-searchable when

VOID, VARIANT

Always.

STRING

Only when it uses the default UTF8_BINARY collation. The function ignores STRING values with any other collation. See Collation.

ARRAY

Its child expression is string-searchable.

MAP

Its value expression is string-searchable.

STRUCT

At least one of its field expressions is string-searchable.

For STRUCT expressions, search_ip ignores fields that are not string-searchable. For VARIANT expressions, it searches only leaf nodes of type VOID or STRING.

A STRING column declared without a COLLATE clause uses its table's default collation, which the table can inherit from its schema. If that collation is not UTF8_BINARY, search_ip ignores the column, so a search over only that column returns false. To search the column, cast it to STRING COLLATE UTF8_BINARY.

The function does not implicitly cast non-STRING expressions to STRING expressions.

The return value is determined as follows:

  • NULL if a search_pattern is NULL and every other search_pattern is valid.
  • true if at least one string-searchable value matches every term.
  • NULL if no string-searchable value matches every term and at least one string-searchable value is NULL.
  • false otherwise.

Common error conditions​

Examples​

SQL
-- Match a plain IPv4 address.
> SELECT search_ip('src=192.0.2.1', '192.0.2.1');
true

-- Match an IPv4 address in a CIDR block.
> SELECT search_ip('src=10.0.0.1', '10.0.0.0/8');
true

-- Match every CIDR block within one value.
> SELECT search_ip('src=10.0.0.1 dst=192.168.1.50', '10.0.0.0/8', '192.168.0.0/16');
true

> SELECT search_ip(
array('src=10.0.0.1', 'dst=192.168.1.50'),
'10.0.0.0/8', '192.168.0.0/16');
false

-- Match an IPv6 address.
> SELECT search_ip('client=2001:db8::1', '2001:db8::/32');
true

-- Match equivalent dotted-decimal and hexadecimal IPv4-mapped IPv6 forms.
> SELECT search_ip('client=::ffff:192.0.2.1', '::ffff:c000:0201');
true

-- A CIDR block in the target matches a broader searched block.
> SELECT search_ip('router=10.0.0.0/24', '10.0.0.0/16');
true

-- An address outside the searched block does not match.
> SELECT search_ip('src=192.168.1.100', '192.168.0.0/24');
false

-- A column with a default collation other than UTF8_BINARY is ignored.
> CREATE TABLE connections (msg STRING) DEFAULT COLLATION UTF8_LCASE;
> INSERT INTO connections VALUES ('connected from 192.0.2.1');
> SELECT search_ip(msg, '192.0.2.0/24') FROM connections;
false

-- Cast the column to UTF8_BINARY to search it.
> SELECT search_ip(CAST(msg AS STRING COLLATE UTF8_BINARY), '192.0.2.0/24') FROM connections;
true

-- NULL behavior.
> SELECT search_ip(CAST(NULL AS STRING), '10.0.0.1');
NULL