View a markdown version of this page

Path and URL functions - Amazon Athena

Path and URL functions

Use these functions to split a file path into its parts, join path components, build a Hive-style partition segment, and percent-encode or decode text for a URL.

hive_partition_component function

Builds one Hive-style path segment, name=value, from the partition column name and its value. The function converts value to text, and percent-encodes characters in both parts, such as spaces, slashes, and equal signs, the same way that url_encode does. The result is safe to use as one segment of a path.

If value is NULL, the value part is __HIVE_DEFAULT_PARTITION__, the directory name that Hive uses for a NULL partition value.

Syntax

hive_partition_component(name, value)

Arguments

The hive_partition_component function takes the following arguments:

name

The partition column name, as a VARCHAR value.

value

The partition value, of any type. The function converts a value that is not text to text, and then encodes it. For example, the TIMESTAMP value 2026-10-01 12:30:00 becomes 2026-10-01%2012%3A30%3A00.

Return type

VARCHAR

Examples

The following SQL query returns the directory name for the value 2024 of the column year.

SELECT hive_partition_component('year', 2024) AS result

The query returns the following result.

result

year=2024

The following SQL query shows that a space in the value is percent-encoded.

SELECT hive_partition_component( 'city', 'New York' ) AS result

The query returns the following result.

result

city=New%20York

The following SQL query passes a NULL value.

SELECT hive_partition_component('year', NULL) AS result

The query returns the following result.

result

year=__HIVE_DEFAULT_PARTITION__

parse_dirname function

Splits path at its separators and returns the first component, which is usually the top-level directory. If path starts with a separator, the result is that separator. If path has no separator, the result is an empty string. If path is NULL, the result is NULL.

For an Amazon S3 URI, the first component is the scheme, s3:, not the bucket name. To get everything before the last component of a path, use the parse_dirpath function.

Syntax

parse_dirname(path [, separator ])

Arguments

The parse_dirname function takes the following arguments:

path

The path to parse, as a VARCHAR value.

separator

The option that chooses the separator, as a VARCHAR value. The default is 'both_slash', which splits on both forward slashes and backslashes. The value 'forward_slash' splits on forward slashes only, and 'backslash' splits on backslashes only. The value 'system' splits on forward slashes only, the same as 'forward_slash'. This argument is optional.

Return type

VARCHAR

Examples

The following SQL query returns the top-level directory of a relative path.

SELECT parse_dirname( 'path/to/file.csv', 'system' ) AS result

The query returns the following result.

result

path

The following SQL query shows that, for an Amazon S3 URI, the first component is the scheme.

SELECT parse_dirname( 's3://amzn-s3-demo-bucket/data/orders.csv' ) AS result

The query returns the following result.

result

s3:

The following SQL query passes a file name with no directory, so the result is an empty string.

SELECT parse_dirname('orders.csv') = '' AS is_empty

The query returns the following result.

is_empty

true

parse_dirpath function

Returns the part of path before its last separator, which is the directory that holds the file. If path has no separator, the result is an empty string. If path is NULL, the result is NULL.

To get the last component of a path instead, use the parse_filename function.

Syntax

parse_dirpath(path [, separator ])

Arguments

The parse_dirpath function takes the following arguments:

path

The path to parse, as a VARCHAR value.

separator

The option that chooses the separator, as a VARCHAR value. The default is 'both_slash', which splits on both forward slashes and backslashes. The value 'forward_slash' splits on forward slashes only, and 'backslash' splits on backslashes only. The value 'system' splits on forward slashes only, the same as 'forward_slash'. This argument is optional.

Return type

VARCHAR

Examples

The following SQL query returns the directory part of a relative path.

SELECT parse_dirpath( 'path/to/file.csv', 'forward_slash' ) AS result

The query returns the following result.

result

path/to

The following SQL query returns the directory part of an Amazon S3 URI.

SELECT parse_dirpath( 's3://amzn-s3-demo-bucket/data/orders.csv' ) AS result

The query returns the following result.

result

s3://amzn-s3-demo-bucket/data

The following SQL query passes a file name with no directory, so the result is an empty string.

SELECT parse_dirpath('orders.csv') = '' AS is_empty

The query returns the following result.

is_empty

true

parse_filename function

Returns the part of path after its last separator, which is usually the file name. If trim_extension is true, the function also removes the file extension: the last dot and the characters after it. If path ends with a separator, the result is an empty string. If path is NULL, the result is NULL.

To get the directory part of a path instead, use the parse_dirpath function.

Syntax

parse_filename(path [, trim_extension ] [, separator ])

Arguments

The parse_filename function takes the following arguments:

path

The path to parse, as a VARCHAR value.

trim_extension

A flag that controls whether the function removes the file extension from the result, as a BOOLEAN value. If true, the function removes it. The default is false. This argument is optional.

separator

The option that chooses the separator, as a VARCHAR value. The default is 'both_slash', which splits on both forward slashes and backslashes. The value 'forward_slash' splits on forward slashes only, and 'backslash' splits on backslashes only. The value 'system' splits on forward slashes only, the same as 'forward_slash'. This argument is optional.

Return type

VARCHAR

Examples

The following SQL query returns the file name without its extension.

SELECT parse_filename( 'path/to/file.csv', true, 'forward_slash' ) AS result

The query returns the following result.

result

file

The following SQL query returns the file name from an Amazon S3 URI.

SELECT parse_filename( 's3://amzn-s3-demo-bucket/data/orders.csv' ) AS result

The query returns the following result.

result

orders.csv

The following SQL query shows that trim_extension removes only the last extension.

SELECT parse_filename('archive.tar.gz', true) AS result

The query returns the following result.

result

archive.tar

The following SQL query passes a path that ends with a separator, so the result is an empty string.

SELECT parse_filename('path/to/') = '' AS is_empty

The query returns the following result.

is_empty

true

Considerations

trim_extension removes only the last extension, so archive.tar.gz becomes archive.tar. A name that starts with a dot and has no other dot, such as .bashrc, becomes an empty string.

parse_path function

Returns a list of the components of path: each directory in order, then the last component. If path starts with a separator, the first element is that separator. Repeated separators do not produce empty elements. If path is NULL, the result is NULL.

Syntax

parse_path(path [, separator ])

Arguments

The parse_path function takes the following arguments:

path

The path to split, as a VARCHAR value.

separator

The option that chooses the separator, as a VARCHAR value. The default is 'both_slash', which splits on both forward slashes and backslashes. The value 'forward_slash' splits on forward slashes only, and 'backslash' splits on backslashes only. The value 'system' splits on forward slashes only, the same as 'forward_slash'. This argument is optional.

Return type

VARCHAR[], a list of VARCHAR values.

Examples

The following SQL query splits a relative path into its components.

SELECT parse_path('path/to/file.csv', 'system') AS result

The query returns the following result.

result

[path, to, file.csv]

The following SQL query splits an absolute path. The leading separator is the first element of the list.

SELECT parse_path('/path/to/file.csv') AS result

The query returns the following result.

result

[/, path, to, file.csv]

The following SQL query splits one path twice, first with the default separator setting and then with 'forward_slash'. With 'forward_slash', the backslash stays inside the first component, dir\sub. The result shows that component in single quotes with the backslash doubled, because the string form of a list escapes backslashes. The component itself holds a single backslash.

SELECT parse_path('dir\sub/file.csv') AS both_slashes, parse_path( 'dir\sub/file.csv', 'forward_slash' ) AS forward_only

The query returns the following result.

both_slashes forward_only

[dir, sub, file.csv]

['dir\\sub', file.csv]

path_join function

Joins the path arguments into one path, with a forward slash between components. The result has no repeated separators, and the function resolves . and .. segments. An Amazon S3 URI keeps its s3:// prefix. If any argument is NULL, the result is NULL.

Syntax

path_join(path [, ...])

Arguments

The path_join function takes the following argument:

path

A path component to join, as a VARCHAR value. You can pass one or more components.

Return type

VARCHAR

Examples

The following SQL query joins three components.

SELECT path_join('dir', 'subdir', 'file') AS result

The query returns the following result.

result

dir/subdir/file

The following SQL query removes a repeated separator and resolves the . and .. segments.

SELECT path_join('a//b', './c', '../d') AS result

The query returns the following result.

result

a/b/d

The following SQL query adds a file name to an Amazon S3 URI.

SELECT path_join( 's3://amzn-s3-demo-bucket/data', 'part-0.parquet' ) AS result

The query returns the following result.

result

s3://amzn-s3-demo-bucket/data/part-0.parquet

Considerations

A component can be an absolute path, such as one that starts with / or s3://. Such a component can follow an earlier component only if it starts with the path built so far. Otherwise, the query fails. For example, path_join('dir', '/data') fails.

url_decode function

Returns string with each percent-encoded sequence, such as %20 or %2F, replaced by the character that it encodes. Sequences that encode the UTF-8 bytes of a non-ASCII character are decoded to that character.

A plus sign (+) stays a plus sign and is not decoded as a space. A percent sign that is not followed by two hexadecimal digits is also kept as is. If the decoded bytes are not valid UTF-8, the query fails. For example, url_decode('caf%E9') fails, because %E9 is the Latin-1 encoding of an accented e and is not valid UTF-8 on its own. If string is NULL, the result is NULL.

Syntax

url_decode(string)

Arguments

The url_decode function takes the following argument:

string

The percent-encoded string to decode, as a VARCHAR value.

Return type

VARCHAR

Examples

The following SQL query decodes a percent-encoded URL.

SELECT url_decode( 'https%3A%2F%2Fexample.com%2Fsearch%3Fq%3Dred%20shoes' ) AS result

The query returns the following result.

result

https://example.com/search?q=red shoes

The following SQL query shows that a plus sign is not decoded as a space.

SELECT url_decode('red+shoes') AS result

The query returns the following result.

result

red+shoes

Differences from Athena engine version 3

In Athena engine version 3, url_decode decodes a plus sign (+) as a space. DuckDB keeps the plus sign. To get the Athena engine version 3 result, replace plus signs before you decode, as in url_decode(replace(string, '+', ' ')).

url_encode function

Returns string with each character that is not safe in a URL replaced by a percent sign and two uppercase hexadecimal digits for each of its UTF-8 bytes. ASCII letters, digits, and the characters -, _, ., and ~ are not changed. A space becomes %20, and a slash becomes %2F.

Because url_encode also encodes /, ?, =, and &, use it on one part of a URL, such as a single query parameter value, rather than on a whole URL. To reverse the encoding, use the url_decode function. If string is NULL, the result is NULL.

Syntax

url_encode(string)

Arguments

The url_encode function takes the following argument:

string

The string to encode, as a VARCHAR value.

Return type

VARCHAR

Examples

The following SQL query encodes the spaces, the slash, the plus sign, and the greater-than sign in a string.

SELECT url_encode( 'this string has/ special+ characters>' ) AS result

The query returns the following result.

result

this%20string%20has%2F%20special%2B%20characters%3E

The following SQL query encodes an accented e, chr(233), as its two UTF-8 bytes.

SELECT url_encode('caf' || chr(233)) AS result

The query returns the following result.

result

caf%C3%A9

Differences from Athena engine version 3

In Athena engine version 3, url_encode encodes a space as a plus sign (+). DuckDB encodes a space as %20.