ClickHouse / ClickHouse/ClickHouse
A default database literally named `NONE` is lost after an access-entity serialization round trip: `ASTDatabaseOrNone` formats it unquoted and the parser reads it back as the `NONE` sentinel
- Dominant language
- C++
- Stars
- 49.9k
- Forks
- 9k
- Avg merge
- 21h 32m
- Merged PRs (30d)
- 515
Description
**Describe the unexpected behaviour**
A user's default database is silently lost after an access-entity serialization round trip when the database is literally named `NONE`:
```sql
CREATE DATABASE `NONE`;
CREATE USER test_user DEFAULT DATABASE `NONE`;
```
After the entity goes through `serializeAccessEntity` and is read back (which happens on every update in `ReplicatedAccessStorage`, and across a restart with `DiskAccessStorage`), the user has no default database at all.
**Root cause**
`ASTDatabaseOrNone::formatImpl` (`src/Parsers/ASTDatabaseOrNone.cpp`) prints the database name with `backQuoteIfNeed`. Since `NONE` is lexically a valid identifier, it is emitted unquoted, so the entity is serialized as `DEFAULT DATABASE NONE`. On read-back, `ParserDatabaseOrNone` (`src/Parsers/ParserDatabaseOrNone.cpp`) matches the keyword `NONE` first and interprets it as the sentinel that clears the default database.
The formatter should back-quote a database name that collides with the `NONE` keyword (or unconditionally back-quote), so that `DEFAULT DATABASE `NONE`` round-trips as the database name and bare `DEFAULT DATABASE NONE` remains the sentinel.
**Additional context**
Found while enabling `ReplicatedAccessStorage` in the `DatabaseReplicated` stateless-test runs. The affected test (`01939_user_with_default_database`) is tagged `no-replicated-database` there until this is fixed.
Related: https://github.com/ClickHouse/ClickHouse/pull/67954
Contributor guide
Assessment
This issue has not been assessed yet.