import,userfiles: IMPORT from relative userfile path returns SQL errors
Describe the problem
When issuing an IMPORT SQL command targeting a userfile with a relative path, there is a SQL syntax error.
To Reproduce
- Upload a file using
cockroach userfile uploadwith a relative path. In my case, it wasimport-test/employees.csv. That succeeded. - Attempt to import using SQL:
[email protected]:26257/defaultdb> IMPORT TABLE employees (
emp_no INT PRIMARY KEY,
birth_date DATE NOT NULL,
first_name STRING NOT NULL,
last_name STRING NOT NULL,
gender STRING NOT NULL,
hire_date DATE NOT NULL
) CSV DATA ('userfile://import-test/employees.csv');
ERROR: at or near "-": syntax error
SQLSTATE: 42601
DETAIL: source SQL:
ALTER TABLE import-test RENAME TO x
It seems like there is an intermediate table being created, based on the file path. I suspect the hyphen is not the real problem.
Indeed, after removing the hyphen, I get this:
[email protected]:26257/defaultdb> IMPORT TABLE employees (
emp_no INT PRIMARY KEY,
birth_date DATE NOT NULL,
first_name STRING NOT NULL,
last_name STRING NOT NULL,
gender STRING NOT NULL,
hire_date DATE NOT NULL
) CSV DATA ('userfile://importtest/employees.csv’);
ERROR: tables-exist: at or near "]": syntax error
SQLSTATE: 42601
DETAIL: source SQL:
SELECT table_name FROM [SHOW TABLES FROM ] WHERE table_name=$1 OR table_name=$2
Expected behavior A successful import, using a relative userfile path.
If relative userfile paths are unsupported:
- The error should say that, prior to running invalid SQL
- Advise the user what a fully-qualified path looks like
Better, can relative userfile paths be supported? I think that’s better.
Environment: Build Tag: v20.2.0-alpha.1-2575-g178dc48e49 Build Time: 2020/08/21 17:32:45 Distribution: CCL Platform: darwin amd64 (x86_64-apple-darwin19.6.0) Go Version: go1.13.15 C Compiler: Apple LLVM 12.0.0 (clang-1200.0.26.2) Build Commit ID: 178dc48e499d9b0ab7b44a6d62274a8519b532ac Build Type: development
Nice catch, yes currently if the user leaves the host part of the URI empty we error out with host component of userfile URI must be a qualified table name. In the example posted above, we interpret import-test as the host and use that to try and resolve the userfile tables. This name resolution fails with the SQL error.
We can support relative paths by just resolving an empty host to defaultdb.public.userfile_username and checking if the specified file is there. The user will, however, have to use userfile:///import-test/employees.csv (with three forward slashes) to ensure we don't interpret import-test as the host. I'll write up a fix.