blob: ebd58e222c9045f0f83e81e07e6eff379645b6e2 [file] [view]
<!--
SPDX-License-Identifier: Apache-2.0
SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
-->
<!---
This file was generated by the dev/update_function_docs.sh script.
Do not edit it manually as changes will be overwritten.
Instead, edit the #[user_doc] attributes on the functions and run the script.
-->
# String Functions (scalar functions)
Part of the [datafusion-spark function reference](../README.md).
- [ascii](#ascii)
- [char](#char)
- [char_length](#char_length)
- [character_length](#character_length)
- [concat](#concat)
- [concat_ws](#concat_ws)
- [contains](#contains)
- [elt](#elt)
- [find_in_set](#find_in_set)
- [format_string](#format_string)
- [ilike](#ilike)
- [is_valid_utf8](#is_valid_utf8)
- [len](#len)
- [length](#length)
- [like](#like)
- [luhn_check](#luhn_check)
- [make_valid_utf8](#make_valid_utf8)
- [printf](#printf)
- [quote](#quote)
- [soundex](#soundex)
- [space](#space)
- [substr](#substr)
- [substring](#substring)
## `ascii`
Returns the numeric code point of the first character of `str`. Despite the name, this is the full Unicode code point, not just ASCII (`ascii('😀')` is 128512). An empty string returns 0, and NULL returns NULL. Unlike DataFusion's `ascii`, numeric input is accepted and cast to a string first, so `ascii(2)` is the code point of `'2'`.
```sql
ascii(str)
```
### Arguments
- **str**: String expression. Numeric values are cast to a string. NULL returns NULL.
### Example
```sql
> SELECT ascii('Spark');
83
> SELECT ascii('');
0
> SELECT ascii(2);
50
```
## `char`
Returns the character whose code point is `n` modulo 256, so `char(321)` is `A`. A negative `n` returns an empty string, and NULL returns NULL. DataFusion's `chr` instead uses `n` as a full Unicode code point and errors on negative or invalid values.
```sql
char(n)
```
### Arguments
- **n**: Integer expression, cast to BIGINT. NULL returns NULL.
### Example
```sql
> SELECT char(65);
A
> SELECT char(321);
A
> SELECT char(-1);
-- empty string
```
## `char_length`
_Alias of [length](#length)._
## `character_length`
_Alias of [length](#length)._
## `concat`
Concatenates the arguments into a single string, or binary if all arguments are binary. Returns NULL if any argument is NULL, whereas DataFusion's `concat` skips NULL arguments. Unlike DataFusion's `concat`, it also accepts zero arguments and then returns an empty string.
```sql
concat([expr1[, ..., exprN]])
```
### Arguments
- **expr**: String or binary expressions to concatenate. Any NULL makes the result NULL.
### Example
```sql
> SELECT concat('Spark', 'SQL');
SparkSQL
> SELECT concat('Spark', 'SQL', NULL);
NULL
> SELECT concat();
-- empty string
```
## `concat_ws`
Concatenates the arguments after the first, separated by `sep`. NULL arguments and NULL array elements are skipped, so if every argument is NULL the result is an empty string. A NULL separator returns NULL. With only a separator, the result is an empty string. Unlike DataFusion's `concat_ws`, array arguments are expanded into their elements, non-string arguments (numbers, booleans, dates, binary, array elements) are cast to strings, and the result is always `Utf8`.
```sql
concat_ws(sep[, expr1[, ..., exprN]])
```
### Arguments
- **sep**: Separator, cast to a string. NULL returns NULL.
- **expr**: Values or arrays to join. Arrays contribute each element. NULL values and elements are skipped. Structs are not supported.
### Example
```sql
> SELECT concat_ws(' ', 'Spark', 'SQL');
Spark SQL
> SELECT concat_ws('/', 'foo', NULL, 'bar');
foo/bar
> SELECT concat_ws(',', 'x', array('a', 'b'), 'y');
x,a,b,y
> SELECT concat_ws(NULL, 'a', 'b');
NULL
```
## `contains`
Returns true if `right` is found inside `left`. DataFusion's own `contains` takes only strings; this one also accepts binary. When both arguments are binary the match is on bytes. When one argument is a string and the other is binary, both are compared as bytes, which is the same as Spark casting the binary to a string. Integer, boolean and date arguments are cast to a string first. Other types are rejected at planning, including floating-point, decimal and timestamp arguments, which Spark casts to a string (see https://github.com/apache/datafusion-spark/issues/347). The match is case-sensitive, and an empty `right` is found in every value, including an empty `left`. Returns NULL if either input is NULL.
```sql
contains(left, right)
```
### Arguments
- **left**: String or binary expression to search in. NULL returns NULL.
- **right**: String or binary expression to search for. NULL returns NULL.
### Example
```sql
> SELECT contains('Spark SQL', 'Spark');
true
> SELECT contains('Spark SQL', 'SPARK');
false
> SELECT contains(x'537061726b2053514c', x'537061726b');
true
```
## `elt`
Returns the `n`-th of the following arguments as a string (1-based). DataFusion has no built-in `elt`. A NULL `n` returns NULL, as does picking a NULL argument. If `n` is less than 1 or greater than the number of arguments, the result is NULL, or an error when `datafusion.execution.enable_ansi_mode` is true.
```sql
elt(n, expr1[, ..., exprN])
```
### Arguments
- **n**: Integer index, 1-based, cast to BIGINT. NULL returns NULL.
- **expr**: At least one value to choose from. Each is cast to a string.
### Example
```sql
> SELECT elt(1, 'scala', 'java');
scala
> SELECT elt(2, 'a', 1);
1
> SELECT elt(3, 'scala', 'java');
NULL
```
## `find_in_set`
Returns the 1-based index of the first matching element as an INT. Uses the UTF8_BINARY collation: the search is case-sensitive and elements are split on commas without trimming. Empty elements count, including the single empty element in an empty list. Returns 0 when the string contains a comma or has no match and NULL if either argument is NULL.
```sql
find_in_set(str, strlist)
```
### Arguments
- **str**: String expression to match.
- **strlist**: Comma-separated list of strings.
### Example
```sql
> SELECT find_in_set('b', 'a,b,c');
2
> SELECT find_in_set('', 'a,,b');
2
```
## `format_string`
Returns a string formatted from a printf-style format string, following Java's `java.util.Formatter` as Spark does. DataFusion has no built-in equivalent. Supports the `%s`, `%b`, `%c`, `%d`, `%o`, `%x`, `%e`, `%f`, `%g`, `%a` and `%t` (date/time) conversions and their uppercase forms, the `-`, `+`, ` `, `0`, `#`, `,` and `(` flags, width and precision, and positional (`%2$s`) and relative (`%<s`) argument references. A NULL format string returns NULL, while a NULL argument is formatted as `null`. It is an error if there are too few arguments or an argument does not fit its conversion. `%h` and `%H` (hash code) are not supported and return an error.
```sql
format_string(format[, arg1[, ..., argN]])
```
### Arguments
- **format**: String format specification. NULL returns NULL.
- **arg**: Values to format. Each must suit the conversion that refers to it. A NULL value is formatted as `null`.
### Example
```sql
> SELECT format_string('Hello World %d %s', 100, 'days');
Hello World 100 days
> SELECT format_string('%2$s %1$d', 42, 'test');
test 42
> SELECT format_string('Precision: %.2f', 3.14159);
Precision: 3.14
> SELECT format_string('Value: %s', NULL);
Value: null
```
### Alternative Syntax
```sql
printf(format[, arg1[, ..., argN]])
```
### Aliases
- printf
## `ilike`
Returns true if `str` matches `pattern`, ignoring case. In the pattern, `_` matches any one character and `%` matches any sequence of characters. Returns NULL if either argument is NULL. This is the function form of the `ILIKE` operator, which DataFusion supports only as an operator. Spark's optional third `escape` argument is not supported.
```sql
ilike(str, pattern)
```
### Arguments
- **str**: String expression to match. NULL returns NULL.
- **pattern**: String pattern, using `_` and `%` as wildcards. NULL returns NULL.
### Example
```sql
> SELECT ilike('Spark', '_PARK');
true
> SELECT ilike('Spark', 'xyz');
false
> SELECT ilike('Spark', NULL);
NULL
```
## `is_valid_utf8`
Returns true if the input is valid UTF-8. DataFusion has no built-in `is_valid_utf8`. String input is always valid. Binary input is invalid if it has truncated sequences, stray continuation bytes, overlong encodings, encoded surrogates (U+D800 to U+DFFF) or code points above U+10FFFF. Empty input is valid. Returns NULL if the input is NULL.
```sql
is_valid_utf8(str)
```
### Arguments
- **str**: String or binary expression. NULL returns NULL.
### Example
```sql
> SELECT is_valid_utf8('Spark');
true
> SELECT is_valid_utf8(X'C2A9');
true
> SELECT is_valid_utf8(X'61C262');
false
```
## `len`
_Alias of [length](#length)._
## `length`
Returns the number of characters in a string, or the number of bytes in a binary value. Trailing spaces and binary zeros are counted. NULL returns NULL. Unlike DataFusion's `length`/`character_length`, binary input is accepted, and the result is always `Int32`, including for `LargeUtf8` input.
```sql
length(expr)
```
### Arguments
- **expr**: String or binary expression. NULL returns NULL.
### Example
```sql
> SELECT length('Spark SQL ');
10
> SELECT length(x'537061726b2053514c');
9
```
### Alternative Syntax
```sql
char_length(expr)
```
```sql
character_length(expr)
```
```sql
len(expr)
```
### Aliases
- character\_length
- char\_length
- len
## `like`
Returns true if `str` matches `pattern`, case-sensitively. In the pattern, `_` matches any one character and `%` matches any sequence of characters. Returns NULL if either argument is NULL. This is the function form of the `LIKE` operator, which DataFusion supports only as an operator. Spark's optional third `escape` argument is not supported.
```sql
like(str, pattern)
```
### Arguments
- **str**: String expression to match. NULL returns NULL.
- **pattern**: String pattern, using `_` and `%` as wildcards. NULL returns NULL.
### Example
```sql
> SELECT like('Spark', '_park');
true
> SELECT like('Spark', 'SP%');
false
> SELECT like(NULL, '_park');
NULL
```
## `luhn_check`
Returns true if the string is a number that passes the Luhn checksum, as used for credit card numbers. DataFusion has no built-in `luhn_check`. The string must contain only the digits 0-9: spaces, letters or any other character make the result false, and so does an empty string. Numeric input is cast to a string first. Returns NULL if the input is NULL.
```sql
luhn_check(str)
```
### Arguments
- **str**: String of digits to check. Numeric input is cast to a string. NULL returns NULL.
### Example
```sql
> SELECT luhn_check('79927398713');
true
> SELECT luhn_check('79927398714');
false
> SELECT luhn_check('4111111 111111111');
false
```
## `make_valid_utf8`
Returns the input as a valid UTF-8 string, replacing each invalid byte sequence with the replacement character U+FFFD. DataFusion has no built-in `make_valid_utf8`. String input is already valid and is returned unchanged. Binary input is decoded as UTF-8; overlong encodings and encoded surrogates count as invalid. Returns NULL if the input is NULL.
```sql
make_valid_utf8(str)
```
### Arguments
- **str**: String or binary expression. NULL returns NULL.
### Example
```sql
> SELECT make_valid_utf8('Spark');
Spark
> SELECT make_valid_utf8(X'C3A9');
é
> SELECT make_valid_utf8(X'61C262');
a�b
```
## `printf`
_Alias of [format_string](#format_string)._
## `quote`
Returns `str` wrapped in single quotes, with each single quote inside it escaped with a backslash. No other characters are escaped. DataFusion has no built-in equivalent. Values of other types are cast to a string first. NULL returns NULL.
```sql
quote(str)
```
### Arguments
- **str**: Expression to quote. Non-string values are cast to a string. NULL returns NULL.
### Example
```sql
> SELECT quote('Spark');
'Spark'
> SELECT quote('it''s');
'it\'s'
> SELECT quote(NULL);
NULL
```
## `soundex`
Returns the four-character Soundex code of `str`: its first letter in upper case followed by three digits, padded with `0`. Letters are matched case-insensitively. A string that does not start with an ASCII letter (for example a digit, space, symbol or `Ñ`) is returned unchanged, as is the empty string. NULL returns NULL. DataFusion has no built-in equivalent.
```sql
soundex(str)
```
### Arguments
- **str**: String expression. NULL returns NULL.
### Example
```sql
> SELECT soundex('Miller');
M460
> SELECT soundex('Robert');
R163
> SELECT soundex('123');
123
```
## `space`
Returns a string of `n` spaces. A value of 0 or less returns an empty string, and NULL returns NULL. DataFusion has no built-in equivalent. `n` must be an INT, and integer literals are BIGINT in DataFusion, so cast them (`space(2::INT)`; see [#97](https://github.com/apache/datafusion-spark/issues/97)).
```sql
space(n)
```
### Arguments
- **n**: INT number of spaces. NULL returns NULL.
### Example
```sql
> SELECT concat(space(2::INT), 'Spark');
Spark
> SELECT space(-1::INT);
-- empty string
```
## `substr`
_Alias of [substring](#substring)._
## `substring`
Returns the substring of `str` that starts at `pos` and is `length` characters long. Positions are 1-based and count characters (bytes for binary input). A position of 0 is treated as 1. A negative position counts from the end of the string (-1 is the last character). If it reaches past the start, the part before the start comes off `length` (so `substring('abc', -10, 3)` is empty). A position past the end, or a length of 0 or less, returns an empty string, and a length that runs past the end is clamped. Returns NULL if any input is NULL. DataFusion's `substr` also takes the part before the start off `length`, but counts positions below 1 from the start instead of the end (0 is one before the first character, -1 two before), so `substring('abc', -3, 6)` returns `abc`, where DataFusion's `substr('abc', -3, 6)` returns `ab`.
```sql
substring(str, pos[, length])
```
### Arguments
- **str**: String or binary expression. For binary input, positions count bytes.
- **pos**: Integer start position, 1-based. 0 is treated as 1. A negative value counts from the end of the string.
- **length**: Optional integer maximum length. If omitted, the rest of the string is returned. A value of 0 or less returns an empty string.
### Example
```sql
> SELECT substring('Spark SQL', 5);
k SQL
> SELECT substring('Spark SQL', -3);
SQL
> SELECT substring('Spark SQL', 0);
Spark SQL
> SELECT substring('Spark SQL', 3, 0);
-- empty string
> SELECT substring('Spark SQL', NULL);
NULL
```
### Alternative Syntax
```sql
substr(str, pos[, length])
```
### Aliases
- substr