blob: c3ccf52e72774a139d7b3603d8c5e8c0af73a8ce [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.
-->
# Binary String Functions (scalar functions)
Part of the [datafusion-spark function reference](../README.md).
- [base64](#base64)
- [encode](#encode)
- [to_binary](#to_binary)
- [try_to_binary](#try_to_binary)
- [unbase64](#unbase64)
## `base64`
Encodes binary as a padded base64 string, using the standard alphabet. It is the same as DataFusion's `encode(expr, 'base64pad')`; DataFusion's `encode(expr, 'base64')` leaves out the padding. String input is encoded as its UTF-8 bytes. Returns NULL if the input is NULL.
```sql
base64(bin)
```
### Arguments
- **bin**: Binary or string expression to encode. NULL returns NULL.
### Example
```sql
> SELECT base64('Spark SQL');
U3BhcmsgU1FM
> SELECT base64('Spark S');
U3BhcmsgUw==
```
## `encode`
Encodes a string into binary using a character set. Unlike DataFusion's `encode`, which takes `base64` or `hex` and returns a string, this takes a character set and returns binary. Supported character sets are `US-ASCII`, `ISO-8859-1`, `UTF-8`, `UTF-16`, `UTF-16BE`, `UTF-16LE`, and `UTF-32`, matched case-insensitively. Any other character set raises `[INVALID_PARAMETER_VALUE.CHARSET]`. `UTF-16` writes a big-endian byte order mark first, except for an empty string, which encodes to an empty binary. `UTF-32` is big-endian with no byte order mark. Characters the character set cannot represent (non-ASCII in `US-ASCII`, code points above U+00FF in `ISO-8859-1`) raise `[MALFORMED_CHARACTER_CODING]`. Binary input is decoded as UTF-8 first, with each invalid byte replaced by U+FFFD. Returns NULL if either input is NULL. This follows Spark 4.0. Pre-Spark 4.0 accepted Java aliases (legacy mode supports a fixed set of aliases: `UTF8`, `ASCII`, `LATIN1`, `ISO88591`, `UTF16`, `UTF16BE`, `UTF16LE`, `UTF32`, `UTF32BE`, `UTF32LE`) and replaced unmappable characters with `?`; those legacy behaviors can be enabled programmatically via `with_legacy_java_charsets(true)` and `with_legacy_coding_error_action(true)`.
```sql
encode(str, charset)
```
### Arguments
- **str**: String or binary expression to encode. Binary input is decoded as UTF-8, with invalid bytes replaced by U+FFFD. NULL returns NULL.
- **charset**: String name of the character set, matched case-insensitively. May vary per row. NULL returns NULL.
### Example
```sql
> SELECT encode('Spark SQL', 'utf-8');
537061726b2053514c
> SELECT encode('AB', 'utf-16le');
41004200
> SELECT encode('Spark SQL', 'us-ascii');
537061726b2053514c
```
## `to_binary`
Converts a string to binary using the given format. DataFusion has no built-in `to_binary`. `hex` decodes hex digits, and an odd number of digits is padded with a leading zero. `utf-8` (or `utf8`) returns the string's UTF-8 bytes. `base64` decodes like Java's MIME decoder, as Spark does: padding is optional and characters outside the base64 alphabet are skipped. An empty string converts to empty binary in every format. A value that cannot be converted, or a format that is not recognised, is an error; use `try_to_binary` to get NULL instead. Returns NULL if either input is NULL.
```sql
to_binary(str[, fmt])
```
### Arguments
- **str**: String expression to convert. NULL returns NULL.
- **fmt**: Optional constant string format, matched case-insensitively: `hex` (the default), `utf-8`, `utf8` or `base64`. A NULL format returns NULL.
### Example
```sql
> SELECT to_binary('537061726b');
537061726b
> SELECT to_binary('abc', 'utf-8');
616263
> SELECT to_binary('U3Bhcms', 'base64');
537061726b
```
## `try_to_binary`
Same as `to_binary`, except that a value which cannot be converted returns NULL for that row, and a format that is not recognised returns NULL, instead of an error. DataFusion has no built-in `try_to_binary`.
```sql
try_to_binary(str[, fmt])
```
### Arguments
- **str**: String expression to convert. NULL, or a value that cannot be converted, returns NULL.
- **fmt**: Optional constant string format, matched case-insensitively: `hex` (the default), `utf-8`, `utf8` or `base64`. A NULL or unrecognised format returns NULL.
### Example
```sql
> SELECT try_to_binary('U3Bhcms=', 'base64');
537061726b
> SELECT try_to_binary('zz', 'hex');
NULL
> SELECT try_to_binary('abc', 'invalidFormat');
NULL
```
## `unbase64`
Decodes a base64 string to binary, the same way as Java's MIME decoder, which Spark uses. DataFusion has no built-in `unbase64`. Padding is optional, and characters outside the base64 alphabet (such as spaces and line breaks) are skipped. A single leftover character at the end, malformed padding, or base64 characters after the padding are errors. Returns NULL if the input is NULL.
```sql
unbase64(str)
```
### Arguments
- **str**: String or binary expression holding base64 text. NULL returns NULL.
### Example
```sql
> SELECT arrow_cast(unbase64('U3BhcmsgU1FM'), 'Utf8');
Spark SQL
> SELECT arrow_cast(unbase64('YW Jj'), 'Utf8');
abc
> SELECT hex(unbase64('123'));
D76D
```