Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't extract list of all columns with saving the order of them #468

Open
rrr2rrr opened this issue Feb 24, 2024 · 1 comment
Open

Can't extract list of all columns with saving the order of them #468

rrr2rrr opened this issue Feb 24, 2024 · 1 comment
Labels

Comments

@rrr2rrr
Copy link

rrr2rrr commented Feb 24, 2024

Query

SELECT 
    dj.field_1,
    cardinality(dj.field_1) as field_1_count,
    dj.field_2,
    cardinality(dj.field_2) as field_2_count,
    dj.field_3 as field_3
FROM dj

Code

parser = Parser(sql_query)
print(parser.columns)
print(parser.columns_aliases)

Output

['dj.field_1', 'dj.field_2', 'dj.field_3']
{'field_1_count': 'dj.field_1', 'field_2_count': 'dj.field_2', 'field_3': 'dj.field_3'}

Feature needed

[
   'field_1': {'refer':'dj.field_1'}, 
   'field_1_count': {'alias':'field_1_count', 'refer':'dj.field_1'},
   'field_2': {'refer':'dj.field_2'}, 
   'field_2_count': {'alias':'field_2_count', 'refer':'dj.field_2'},
   'field_3': {'alias':'field_3', 'refer':'dj.field_3'}
]
@rrr2rrr
Copy link
Author

rrr2rrr commented Feb 24, 2024

Quick local fix

To fix it in my case I added aliases to all columns:

def add_alias_to_sql_select_fields(sql_query):
    # Splitting the query into parts before and after the FROM clause
    select_part, from_part = sql_query.split("FROM")
    
    # Splitting the SELECT part into fields
    fields = select_part.replace("SELECT", "").strip().split(",")
    
    new_fields = []
    for field in fields:
        field = field.strip()
        # Check if field already has an alias
        if " as " not in field.lower():
            # Extracting the field name after the last dot for alias
            field_name = field.split(".")[-1].strip()
            # Adding alias
            field_with_alias = f"{field} as {field_name}"
        else:
            field_with_alias = field
        new_fields.append(field_with_alias)
    
    # Reconstructing the SELECT part with aliases
    new_select_part = "SELECT\n    " + ",\n    ".join(new_fields)
    
    # Reconstructing the full query
    new_sql_query = f"{new_select_part}\nFROM{from_part}"
    
    return new_sql_query

# Your SQL query
sql_query = """
SELECT 
    dj.field_1,
    cardinality(dj.field_1) as field_1_count,
    dj.field_2,
    cardinality(dj.field_2) as field_2_count,
    dj.field_3
FROM dj
"""

# Adding aliases
new_sql_query = add_alias_to_sql_select_fields(sql_query)
print(new_sql_query)

Output

SELECT
    dj.field_1 as field_1,
    cardinality(dj.field_1) as field_1_count,
    dj.field_2 as field_2,
    cardinality(dj.field_2) as field_2_count,
    dj.field_3 as field_3
FROM dj

@macbre macbre added the bug label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants