search_ip function
Applies to: Databricks SQL
Databricks Runtime 19
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 moreSTRINGconstant 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 withAND.
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 |
|---|---|
| Always. |
| Only when it uses the default |
| Its child expression is string-searchable. |
| Its value expression is string-searchable. |
| 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:
NULLif asearch_patternisNULLand every othersearch_patternis valid.trueif at least one string-searchable value matches every term.NULLif no string-searchable value matches every term and at least one string-searchable value isNULL.falseotherwise.
Common error conditions
- DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE
- SEARCH_ARGUMENT_NOT_CONSTANT
- SEARCH_INVALID_IP_PATTERN
SEARCH_MULTIPLE_TARGETS_NOT_SUPPORTED- SEARCH_STAR_ARGUMENT_NOT_ALLOWED
SEARCH_TOO_MANY_NEEDLES- UNRECOGNIZED_PARAMETER_NAME
- WRONG_NUM_ARGS.WITHOUT_SUGGESTION
Examples
-- 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