

# 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
<a name="athena-duckdb-functions-string-hive-partition-component"></a>

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
<a name="athena-duckdb-functions-string-hive-partition-component-syntax"></a>

```sql
hive_partition_component({{name}}, {{value}})
```

### Arguments
<a name="athena-duckdb-functions-string-hive-partition-component-arguments"></a>

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
<a name="athena-duckdb-functions-string-hive-partition-component-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-hive-partition-component-examples"></a>

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

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

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

```sql
SELECT hive_partition_component('year', NULL) AS result
```

The query returns the following result.


| result | 
| --- | 
| `year=__HIVE_DEFAULT_PARTITION__` | 

## `parse_dirname` function
<a name="athena-duckdb-functions-string-parse-dirname"></a>

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](#athena-duckdb-functions-string-parse-dirpath).

### Syntax
<a name="athena-duckdb-functions-string-parse-dirname-syntax"></a>

```sql
parse_dirname({{path}} [, {{separator}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-parse-dirname-arguments"></a>

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
<a name="athena-duckdb-functions-string-parse-dirname-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-parse-dirname-examples"></a>

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

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

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

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

The query returns the following result.


| is\_empty | 
| --- | 
| `true` | 

## `parse_dirpath` function
<a name="athena-duckdb-functions-string-parse-dirpath"></a>

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](#athena-duckdb-functions-string-parse-filename).

### Syntax
<a name="athena-duckdb-functions-string-parse-dirpath-syntax"></a>

```sql
parse_dirpath({{path}} [, {{separator}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-parse-dirpath-arguments"></a>

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
<a name="athena-duckdb-functions-string-parse-dirpath-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-parse-dirpath-examples"></a>

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

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

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

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

The query returns the following result.


| is\_empty | 
| --- | 
| `true` | 

## `parse_filename` function
<a name="athena-duckdb-functions-string-parse-filename"></a>

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](#athena-duckdb-functions-string-parse-dirpath).

### Syntax
<a name="athena-duckdb-functions-string-parse-filename-syntax"></a>

```sql
parse_filename({{path}} [, {{trim_extension}} ] [, {{separator}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-parse-filename-arguments"></a>

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
<a name="athena-duckdb-functions-string-parse-filename-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-parse-filename-examples"></a>

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

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

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

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

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

The query returns the following result.


| is\_empty | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-string-parse-filename-considerations"></a>

`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
<a name="athena-duckdb-functions-string-parse-path"></a>

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
<a name="athena-duckdb-functions-string-parse-path-syntax"></a>

```sql
parse_path({{path}} [, {{separator}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-parse-path-arguments"></a>

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
<a name="athena-duckdb-functions-string-parse-path-return-type"></a>

`VARCHAR[]`, a list of `VARCHAR` values.

### Examples
<a name="athena-duckdb-functions-string-parse-path-examples"></a>

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

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

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

```sql
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
<a name="athena-duckdb-functions-string-path-join"></a>

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
<a name="athena-duckdb-functions-string-path-join-syntax"></a>

```sql
path_join({{path}} [, ...])
```

### Arguments
<a name="athena-duckdb-functions-string-path-join-arguments"></a>

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
<a name="athena-duckdb-functions-string-path-join-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-path-join-examples"></a>

The following SQL query joins three components.

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

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

```sql
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 name="athena-duckdb-functions-string-path-join-considerations"></a>

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
<a name="athena-duckdb-functions-string-url-decode"></a>

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
<a name="athena-duckdb-functions-string-url-decode-syntax"></a>

```sql
url_decode({{string}})
```

### Arguments
<a name="athena-duckdb-functions-string-url-decode-arguments"></a>

The `url_decode` function takes the following argument:

**{{string}}**  
The percent-encoded string to decode, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-url-decode-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-url-decode-examples"></a>

The following SQL query decodes a percent-encoded URL.

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

```sql
SELECT url_decode('red+shoes') AS result
```

The query returns the following result.


| result | 
| --- | 
| `red+shoes` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-url-decode-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-string-url-encode"></a>

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](#athena-duckdb-functions-string-url-decode). If `string` is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-url-encode-syntax"></a>

```sql
url_encode({{string}})
```

### Arguments
<a name="athena-duckdb-functions-string-url-encode-arguments"></a>

The `url_encode` function takes the following argument:

**{{string}}**  
The string to encode, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-url-encode-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-url-encode-examples"></a>

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

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

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

The query returns the following result.


| result | 
| --- | 
| `caf%C3%A9` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-url-encode-differences-from-engine-version-3"></a>

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