Skip to content

Commit fd4e011

Browse files
fix: use adapter quoting in autopopulate progress()
Replace hardcoded backticks with adapter.quote_identifier() in the progress() method to support both MySQL and PostgreSQL backends. - Use adapter.quote_identifier() for all column and alias names - CONCAT_WS is supported by both MySQL and PostgreSQL Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 86bd95e commit fd4e011

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/datajoint/autopopulate.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -703,17 +703,26 @@ def progress(self, *restrictions: Any, display: bool = False) -> tuple[int, int]
703703
todo_sql = todo.make_sql()
704704
target_sql = self.make_sql()
705705

706+
# Get adapter for backend-specific quoting
707+
adapter = self.connection.adapter
708+
q = adapter.quote_identifier
709+
710+
# Alias names for subqueries
711+
ks_alias = q("$ks")
712+
tgt_alias = q("$tgt")
713+
706714
# Build join condition on common attributes
707-
join_cond = " AND ".join(f"`$ks`.`{attr}` = `$tgt`.`{attr}`" for attr in common_attrs)
715+
join_cond = " AND ".join(f"{ks_alias}.{q(attr)} = {tgt_alias}.{q(attr)}" for attr in common_attrs)
708716

709717
# Build DISTINCT key expression for counting unique jobs
710-
# Use CONCAT for composite keys to create a single distinct value
718+
# Use CONCAT_WS for composite keys (supported by both MySQL and PostgreSQL)
711719
if len(pk_attrs) == 1:
712-
distinct_key = f"`$ks`.`{pk_attrs[0]}`"
713-
null_check = f"`$tgt`.`{common_attrs[0]}`"
720+
distinct_key = f"{ks_alias}.{q(pk_attrs[0])}"
721+
null_check = f"{tgt_alias}.{q(common_attrs[0])}"
714722
else:
715-
distinct_key = "CONCAT_WS('|', {})".format(", ".join(f"`$ks`.`{attr}`" for attr in pk_attrs))
716-
null_check = f"`$tgt`.`{common_attrs[0]}`"
723+
key_cols = ", ".join(f"{ks_alias}.{q(attr)}" for attr in pk_attrs)
724+
distinct_key = f"CONCAT_WS('|', {key_cols})"
725+
null_check = f"{tgt_alias}.{q(common_attrs[0])}"
717726

718727
# Single aggregation query:
719728
# - COUNT(DISTINCT key) gives total unique jobs in key_source
@@ -722,8 +731,8 @@ def progress(self, *restrictions: Any, display: bool = False) -> tuple[int, int]
722731
SELECT
723732
COUNT(DISTINCT {distinct_key}) AS total,
724733
COUNT(DISTINCT CASE WHEN {null_check} IS NULL THEN {distinct_key} END) AS remaining
725-
FROM ({todo_sql}) AS `$ks`
726-
LEFT JOIN ({target_sql}) AS `$tgt` ON {join_cond}
734+
FROM ({todo_sql}) AS {ks_alias}
735+
LEFT JOIN ({target_sql}) AS {tgt_alias} ON {join_cond}
727736
"""
728737

729738
result = self.connection.query(sql).fetchone()

0 commit comments

Comments
 (0)