CI Fix (Athena): Hive and Iceberg tables overlap in schema
Link: https://github.com/SQLMesh/sqlmesh/actions/runs/34422850718/job/102707663059
Failing Test
tests/core/engine_adapter/integration/test_integration.py::test_init_project[athena_iceberg]
tests/core/engine_adapter/integration/test_integration.py::test_init_project[athena_hive]
What's Wrong
test_init_project names every Glue database with TestContext.add_test_suffix in tests/core/engine_adapter/integration/__init__.py. That function only appends self.test_id. TestContext.__init__ sets self.test_id from random_id(short=True) in sqlmesh/utils/__init__.py, which is random.choices(..., k=8). It does not include self.mark (athena_hive vs athena_iceberg).
In this CI run both xdist workers used the same test_id values (p2esheja, saq5u3pg, rkehdnam, reuu4fly). Hive (gw0) and Iceberg (gw1) therefore created and dropped the same Glue databases: test_schema_reuu4fly and sqlmesh__test_schema_reuu4fly.
Iceberg’s assert in test_init_project (len(physical_layer_results.tables) == 3) saw 5 tables because TestContext.get_metadata_results listed Iceberg’s three tables plus Hive’s leftover ...seed_model__1441783336 and ...incremental_model__1160025361. Hive’s context.plan failed inside BuiltInPlanEvaluator.visit_backfill_stage with Database sqlmesh__test_schema_reuu4fly not found after the other worker dropped that schema. create_engine_adapter in tests/core/engine_adapter/integration/conftest.py also puts both variants under the same S3 prefix (request.node.originalname == test_init_project).
This is a schema-name collision between the two Athena params. The assertion is fine.
Possible Fix
In TestContext.__init__ (tests/core/engine_adapter/integration/__init__.py), stop using random_id(short=True).
Set self.test_id to f"{self.mark}_{uuid.uuid4().hex}" so athena_hive and athena_iceberg cannot share a Glue database when add_test_suffix builds test_schema_* / sqlmesh__test_schema_*.
Physical tables are {schema}__{model}__{fingerprint}. A 32-char uuid makes those names longer. StarRocksEngineAdapter.MAX_IDENTIFIER_LENGTH is 64; Keep this in mind. Recommend: uuid.uuid4().hex[:8] to match the length of the hard-coded ids.
Disclaimer: AI Generated
I'd like to take this one.
I think the collision is a symptom though. random_id(short=True) uses random.choices, which draws from the global Mersenne Twister, so forked xdist workers inherit the parent's RNG state and produce identical sequences — which would explain four ids matching across two workers:
random.seed(42); [random_id(short=True) for _ in range(4)]
# ['xaji0y6d', 'pbhsahxt', 'hv3a3zmf', '8mdd4v30'] <- same every time
random.seed(42); [uuid.uuid4().hex[:8] for _ in range(4)]
# distinct every time, since uuid4 reads os.urandom
So uuid.uuid4().hex[:8] fixes the cause on its own, stays 8 characters, and covers any two params rather than just the Athena pair.
That makes the self.mark prefix optional. It's handy for spotting which param left a stray Glue database, but MAX_IDENTIFIER_LENGTH is enforced at base.py:3127 (Postgres 63, MySQL/StarRocks 64) and the longest marks are clickhouse_standalone (21) and clickhouse_cluster (18), so it adds ~22 chars to every schema and physical table name. Probably fine since those engines don't set a limit, but worth measuring.
Inclined to do the uuid switch first and add the prefix only if you want it. Happy either way.
No Athena credentials here, so I can verify the naming and run local suites but not reproduce the Glue collision itself.
I'm looking into this and can submit a PR for the fix.
Proposed Approach
The collision happens because TestContext sets self.test_id using random_id(short=True), which does not incorporate the engine/table variant (self.mark) and can collide across concurrent xdist workers.
- Update
self.test_idintests/core/engine_adapter/integration/__init__.pyto includeself.markand a uniqueuuid4fragment:self.test_id = f"{self.mark}_{uuid.uuid4().hex[:8]}"
@cookieark7 -- I agree that the prefix is not required here except for metadata. Having a longer hex would also decrease the odds of collision if we don't bother with the prefix. Assigned.
Ran the options against a real Postgres before writing anything, and the numbers turned up something worth sharing.
Keeping the full mark as a prefix overflows Postgres:
SQLMeshError: Identifier name '__temp_sushi_postgres__a52ca3bd__marketing__2185185764__dev_h8gjuc1n'
(length 68) exceeds Postgres's max identifier limit of 63 characters
_get_temp_table wraps physical names with __temp_ plus its own 8-char suffix, so the budget is tighter than it looks — the longest name in the suite today is already 58 on Postgres and 62 on duckdb.
Measured on the same Postgres run:
max id len result
random_id(short=True) (today) 58 76 passed
f"{mark}_{uuid4().hex[:8]}" 67 5 failed
uuid4().hex[:12] 62 76 passed
So hex[:12] works, and if you'd rather keep the change minimal I'm happy to just do that. The one thing I'd flag is that it sits one character under the limit, so a model name a few characters longer than raw_marketing would push it over later.
On the cause — I tried to confirm the shared-RNG-state theory and couldn't. Running 4 xdist workers locally, all generated ids were distinct; xdist spawns fresh processes rather than forking, so they seed independently. random.choices is reproducible when state is shared, but I have no evidence that's what happened on CI. So I don't know why those four ids matched.
That uncertainty is partly why I'd suggest a slightly different shape. test_id currently does two jobs with one random string — separating concurrent runs, and separating params. Randomness handles the first probabilistically and the second not at all, which is how athena_hive and athena_iceberg could land on the same Glue database. Splitting them:
tag = hashlib.sha1(self.mark.encode()).hexdigest()[:3]
rnd = "".join(secrets.choice(ALPHANUMERIC) for _ in range(5))
self.test_id = f"{tag}{rnd}"
Still 8 characters, so the identifier budget doesn't move. The 3-char tag comes from the param, so two params can't produce the same schema regardless of what the underlying cause was — that felt worth having given I couldn't pin the cause down. Checked distinct across all 22 current params. The 5 random chars keep concurrent runs and rerun attempts apart, and secrets avoids the global random state entirely.
It also keeps the debuggability you mentioned — the tag still tells you which param left a stray Glue database.
tag(3) + secrets(5) postgres 58, 76 passed duckdb 62, 71 passed
Same lengths as today on both.
One related thing I noticed: the S3 prefix in conftest.py uses testrun_uid + request.node.originalname, and originalname is test_init_project for both Athena params, so it has the same param-blindness. Happy to include that or leave it out.
Let me know which you'd prefer and I'll open the PR.
@cookieark7 -- I'm okay with your suggested approach, tag + secret, - go for it