pingcap / pingcap/tidb

IMPORT INTO statement fails to read local files with spaces in path

Open
#60,156 0 comments 0 reactions 0 assignees View on GitHub
component/ddl type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

When using the `IMPORT INTO` statement to import data from a file, the operation fails if the file path contains spaces.

### 1. Steps to Reproduce

1. Create a file in a directory with spaces in the path:

```sh
mkdir -p "/tmp/dir with space"
echo "1,2,3" > "/tmp/dir with space/test_string_import.csv"
```

2. Attempt to import the file using the `IMPORT INTO` statement:

```sql
CREATE TABLE test.test_string (col1 INT, col2 INT, col3 INT);

IMPORT INTO test.test_string
FROM '/tmp/dir with space/test_string_import.csv'
FORMAT 'csv'
WITH
FIELDS_TERMINATED_BY = ',',
FIELDS_ENCLOSED_BY = '"';
```

3. Observe the error message:

```text
ERROR 8160 (HY000): Failed to read source files. Reason: open /tmp/dir%20with%20space/test_string_import.csv: no such file or directory. Please check the file location is correct
```

> This may happen because the file path is URL-encoded internally, converting spaces to `%20`, which results in the file not being found.

4. Verify that the file exists:

```sh
ls "/tmp/dir with space/"
```

5. Verify that directories without space work:

```sh
mkdir -p /tmp/dir_without_space
cp "/tmp/dir with space/test_string_import.csv" /tmp/dir_without_space/
```

```sql
IMPORT INTO test.test_string
FROM '/tmp/dir_without_space/test_string_import.csv'
FORMAT 'csv'
WITH
FIELDS_TERMINATED_BY = ',',
FIELDS_ENCLOSED_BY = '"';
```

### 2. Expected Behavior

The `IMPORT INTO` statement should correctly handle file paths with spaces and import the data from the specified file without errors.

### 3. Current Behavior

```text
ERROR 8160 (HY000): Failed to read source files. Reason: open /tmp/dir%20with%20space/test_string_import.csv: no such file or directory. Please check the file location is correct
```

File path is URL-encoded internally, converting spaces to `%20`, which results in the file not being found.

### 4. TiDB version

8.5.1

### References

- [TiDB Documentation: IMPORT INTO](https://docs.pingcap.com/tidb/stable/sql-statement-import-into/#filelocation)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.