Skip to content

[Bug]: Temporary table alias map key collides when database or table name contains a dot #30595

Closed as not planned

Description

@iamlinjunhong

Branch Name

main (worktree checked out at commit 57fb953085)

Commit ID

57fb953

Other Environment Information

  • Hardware parameters: Apple Silicon (arm64)
  • OS type: macOS 15.5 (darwin)
  • Others: single-node mo-service built from this worktree with make build. Launch configuration: etc/launch copied to .loom-test/launch, every port shifted by LOOM_TEST_PORT_OFFSET=20200 (CN port-base 38200, TN 39200, frontend 26201, status 27201, logservice 52200/52201/52202), data directories under .loom-test/mo-data, [hakeeper-client] service-addresses=["127.0.0.1:52201"] added to the cn/tn/log toml files; started with ./mo-service -launch .loom-test/launch/launch.toml. Connections use user dump / password 111 on frontend port 26201. MySQL 8.0 comparison: Homebrew [email protected] 8.0.46, mysqld --initialize-insecure temporary instance in .loom-test/mysql-data on port 23506 (3306 + 20200).

Actual Behavior

Dotted identifiers are legal: create database \x.y`andcreate temporary table x.`y.t`both succeed, and in a session with no temporary tablesselect ... from `x.y`.t returns the permanent rows (99 permanent_in_x.y` - control run).

But the session temporary-table alias map is keyed by the plain concatenation dbName + "." + alias, so the pair (x, y.t) and the pair (x.y, t) collide on the same key x.y.t. In one session:

Sequence A (temporary table x.\y.t`` created first):

  1. select id, v from \x.y`.t(unrelated permanent table) ->ERROR 1146 (42S02) SQL parser error: table "t" does not exist` - even though the control run in a fresh session reads the same table fine.
  2. select id, v from x.\y.t`-> returns its own row1 temp_in_x_dotted` (this alias works).
  3. create temporary table \x.y`.t (id int primary key, v varchar(20));->ERROR 1050 (42S01) table t already exists- wrongly: no temporary table exists inx.y`, and MySQL accepts this statement.

So after creating one dotted temporary alias, every reference to x.y.t in that session is broken: reads report the table does not exist and creating a temporary table there is rejected as duplicate.

Sequence B (temporary table x.y.t created first):

  1. create temporary table x.\y.t`->ERROR 1050 (42S01) table y.t already exists` (wrongly rejected, symmetric collision).
  2. select id, v from x.\y.t`->ERROR 1146 table "y.t" does not exist` (it was never created).

Sequence A reproduced 3 times, sequence B once, all identical.

MySQL 8.0 comparison - the same two sequences on a temporary [email protected] 8.0.46 instance (see evidence): every statement succeeds and each namespace keeps its own rows - x.y.t (permanent) returns 99, x.\y.t`returns1, create temporary table `x.y`.tsucceeds and returns100, and in sequence B both temporary tables coexist (100and1`). No interference.

Expected Behavior

(database, alias) pairs are distinct namespaces even when the identifiers contain dots: creating session temporary table x.\y.t`must have no effect onx.y.t- reading the permanentx.y.tkeeps returning its own rows, andcreate temporary table `x.y`.t` must succeed with both tables readable afterwards (MySQL 8.0 behavior, verified locally).

Steps to Reproduce

  1. Build and start the single-node service from the worktree:
cp -r etc/launch .loom-test/launch
# In .loom-test/launch/*.toml: add LOOM_TEST_PORT_OFFSET=20200 to every port
# (cn port-base 38200, tn 39200, frontend 26201, status 27201,
#  logservice 52200/52201/52202), point all data dirs to .loom-test/mo-data,
#  and add to cn/tn/log toml: [hakeeper-client] service-addresses=["127.0.0.1:52201"]
make build
./mo-service -launch .loom-test/launch/launch.toml
  1. Control - a fresh connection, permanent table only (reads fine):
mysql -h 127.0.0.1 -P 26201 -udump -p111 --default-character-set=utf8mb4 --force <<'EOF'
drop database if exists `x.y`;
drop database if exists x;
create database `x.y`;
create database x;
use `x.y`;
create table t (id int primary key, v varchar(20));
insert into t values (99,'permanent_in_x.y');
select id, v from t order by id;
EOF
  1. Sequence A - one connection, whole script:
mysql -h 127.0.0.1 -P 26201 -udump -p111 --default-character-set=utf8mb4 --force <<'EOF'
use `x.y`;
select id, v from t order by id;                    -- control inside same connection: 99
use x;
create temporary table `y.t` (id int primary key, v varchar(20));
insert into `y.t` values (1,'temp_in_x_dotted');
select id, v from `x.y`.t order by id;              -- ACTUAL: ERROR 1146 table "t" does not exist
create temporary table `x.y`.t (id int primary key, v varchar(20));   -- ACTUAL: ERROR 1050 table t already exists
select id, v from x.`y.t` order by id;              -- only this alias works: 1 temp_in_x_dotted
EOF
  1. Sequence B - another fresh connection:
mysql -h 127.0.0.1 -P 26201 -udump -p111 --default-character-set=utf8mb4 --force <<'EOF'
use `x.y`;
create temporary table t (id int primary key, v varchar(20));
insert into t values (100,'temp_in_x.y');
use x;
create temporary table `y.t` (id int primary key, v varchar(20));     -- ACTUAL: ERROR 1050 table y.t already exists
select id, v from x.`y.t` order by id;              -- ACTUAL: ERROR 1146 table "y.t" does not exist
EOF
  1. MySQL 8.0 comparison - run the identical sequences against a temporary [email protected] instance (same SQL, port 23506, user root): all statements succeed and each table returns its own rows (full output in evidence).

Additional information

Root cause: the session alias map uses an ambiguous string key:

  • pkg/frontend/session.go:871 GetTempTable reads ses.tempTables[dbName+"."+alias].
  • pkg/frontend/temporary_ddl.go:59 PublishTemporaryTable writes the same key := db + "." + alias (and tempTablesRev/tempTableIdentities share it).

(x, y.t) -> x.y.t and (x.y, t) -> x.y.t are the same key, so the second pair's lookup returns the first pair's physical relation. At read time the aliased physical name is then resolved in the query's database context (x.y), where it does not exist, so the permanent x.y.t surfaces as 1146; at create time the existence check (pkg/sql/compile/temporary_ddl.go:99, which trusts GetTempTable) reports a false 1050. tempTableIdentityLocked (pkg/frontend/session.go:819) has the same ambiguity - its fallback strings.Cut(key, ".") splits at the first dot, so it cannot round-trip dotted identifiers either.

Recommended fix: store the mapping under an unambiguous key - e.g. keep a struct/{db, alias} pair as the map key, or encode the two components with length prefixing/escaping before joining. tempTableIdentities already holds the exact (dbName, alias) pair, so GetTempTable can look up by the structured pair without changing any on-disk format; identifiers without dots keep byte-identical behavior, so existing sessions and tests are unaffected.

Dedup: gh is not authenticated in this environment (gh auth login / GH_TOKEN unavailable), so gh issue list / gh pr list could not be run; duplicate check skipped.

Full reproduction, exactly as Loom ran it
# Run against a running single-node mo-service (frontend port 26201, user dump/111).
#!/bin/zsh
set -u
DIR=$(mktemp -d)
cat > "$DIR/seq_a.sql" <<'SQL'
drop database if exists `x.y`;
drop database if exists x;
create database `x.y`;
create database x;
use `x.y`;
create table t (id int primary key, v varchar(20));
insert into t values (99,'permanent_in_x.y');
use `x.y`;
select id, v from t order by id;
use x;
create temporary table `y.t` (id int primary key, v varchar(20));
insert into `y.t` values (1,'temp_in_x_dotted');
select id, v from `x.y`.t order by id;
create temporary table `x.y`.t (id int primary key, v varchar(20));
select id, v from x.`y.t` order by id;
SQL
cat > "$DIR/seq_b.sql" <<'SQL'
use `x.y`;
create temporary table t (id int primary key, v varchar(20));
insert into t values (100,'temp_in_x.y');
use x;
create temporary table `y.t` (id int primary key, v varchar(20));
select id, v from x.`y.t` order by id;
SQL
echo "=== control + sequence A (one connection) ==="
mysql -h 127.0.0.1 -P 26201 -udump -p111 --default-character-set=utf8mb4 --force < "$DIR/seq_a.sql"
echo "=== sequence B (fresh connection) ==="
mysql -h 127.0.0.1 -P 26201 -udump -p111 --default-character-set=utf8mb4 --force < "$DIR/seq_b.sql"
echo "=== cleanup ==="
mysql -h 127.0.0.1 -P 26201 -udump -p111 --default-character-set=utf8mb4 -e 'drop database if exists `x.y`; drop database if exists x'
rm -rf "$DIR"

Found by Loom bug hunt (module: custom-5ec72c0c38; tests: unit, white-box, end-to-end)

Activity

  1. added
    kind/bug
    on Oct 19, 2026
  2. XuPeng-SH commented on Oct 22, 2026

    @XuPeng-SH
    Contributor

    This report is now tracked together with #30583. The canonical issue retains explicit acceptance cases for the behavior described here. This is an administrative duplicate/consolidation closure only; the underlying defect is not claimed fixed. The original description and discussion remain available on this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions