-
Notifications
You must be signed in to change notification settings - Fork 321
Open
Description
🚀 Feature Request: Automatic Alias Deduplication for User-Selected Fields
Problem
When users select fields with identical aliases, the generated SQL can contain duplicate column names, causing:
- Ambiguous or conflicting result sets
- Debugging difficulties in analytics and dashboards
- Increased maintenance overhead when alias collisions go unnoticed
Manually resolving these conflicts in every query introduces repetitive boilerplate and risk of inconsistency across teams and projects.
Proposed Solution
Introduce automatic alias deduplication for user-selected fields, with an opt-out toggle for advanced users.
Default Behavior:
- When duplicate aliases occur, append numeric suffixes beginning with
_1:
user_name,user_name_1,user_name_2, etc. - Prevent cascading collisions by treating already-suffixed names (e.g.,
alias_1) as unique bases, resulting inalias_1_1. - Preserve the original
field.aliasvalues; renaming applies only to the emitted SQL.
Opt-Out:
Query.from_(table, auto_deduplicate_aliases=False)Disables automatic renaming for simple select statements, maintaining exact user-defined aliases.
Example
Input:
q = Query.from_(users).select(users.name.as_('user_name'), users.nickname.as_('user_name'))Generated SQL:
SELECT "users"."name" AS "user_name", "users"."nickname" AS "user_name_1" FROM "users"Key Requirements
- Deterministic alias renaming with stable numeric suffixes
- No mutation of original Field alias attributes
- Support across all query structures:
- Joins
- Subqueries
- Aggregates, Unions, and Functions
- GROUP BY, ORDER BY, HAVING
- DISTINCT, LIMIT, OFFSET
- Handle high-volume duplicate alias scenarios gracefully
- Maintain dialect-specific quoting and formatting (e.g., PostgreSQL
", MySQL`)
Benefits
✅ Eliminates ambiguous result sets
✅ Improves SQL readability and debugging consistency
✅ Preserves backward compatibility with opt-out to
Metadata
Metadata
Assignees
Labels
No labels