String Functions (scalar functions)

Part of the datafusion-spark function reference.

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'.

ascii(str)

Arguments

  • str: String expression. Numeric values are cast to a string. NULL returns NULL.

Example

> 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.

char(n)

Arguments

  • n: Integer expression, cast to BIGINT. NULL returns NULL.

Example

> SELECT char(65);
A
> SELECT char(321);
A
> SELECT char(-1);
-- empty string

char_length

Alias of length.

character_length

Alias of 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.

concat([expr1[, ..., exprN]])

Arguments

  • expr: String or binary expressions to concatenate. Any NULL makes the result NULL.

Example

> 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.

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

> 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.

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

> 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.

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

> 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.

find_in_set(str, strlist)

Arguments

  • str: String expression to match.
  • strlist: Comma-separated list of strings.

Example

> 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.

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

> 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

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.

ilike(str, pattern)

Arguments

  • str: String expression to match. NULL returns NULL.
  • pattern: String pattern, using _ and % as wildcards. NULL returns NULL.

Example

> 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.

is_valid_utf8(str)

Arguments

  • str: String or binary expression. NULL returns NULL.

Example

> 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

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.

length(expr)

Arguments

  • expr: String or binary expression. NULL returns NULL.

Example

> SELECT length('Spark SQL ');
10
> SELECT length(x'537061726b2053514c');
9

Alternative Syntax

char_length(expr)
character_length(expr)
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.

like(str, pattern)

Arguments

  • str: String expression to match. NULL returns NULL.
  • pattern: String pattern, using _ and % as wildcards. NULL returns NULL.

Example

> 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.

luhn_check(str)

Arguments

  • str: String of digits to check. Numeric input is cast to a string. NULL returns NULL.

Example

> 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.

make_valid_utf8(str)

Arguments

  • str: String or binary expression. NULL returns NULL.

Example

> 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.

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.

quote(str)

Arguments

  • str: Expression to quote. Non-string values are cast to a string. NULL returns NULL.

Example

> 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.

soundex(str)

Arguments

  • str: String expression. NULL returns NULL.

Example

> 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).

space(n)

Arguments

  • n: INT number of spaces. NULL returns NULL.

Example

> SELECT concat(space(2::INT), 'Spark');
  Spark
> SELECT space(-1::INT);
-- empty string

substr

Alias of 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.

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

> 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

substr(str, pos[, length])

Aliases

  • substr