Skip to content

Commit

Permalink
fix: properly alias with column overlapping name (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
eakmanrq authored May 22, 2024
1 parent 4024bc4 commit d1bf8a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sqlframe/base/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ def withColumn(self, colName: str, col: Column) -> Self:
)
if existing_col_index:
expression = self.expression.copy()
expression.expressions[existing_col_index] = col.expression
expression.expressions[existing_col_index] = col.alias(colName).expression
return self.copy(expression=expression)
return self.copy().select(col.alias(colName), append=True)

Expand Down
11 changes: 11 additions & 0 deletions tests/unit/standalone/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from sqlglot import expressions as exp

from sqlframe.standalone import functions as F
from sqlframe.standalone.dataframe import StandaloneDataFrame

pytest_plugins = ["tests.common_fixtures", "tests.unit.standalone.fixtures"]
Expand Down Expand Up @@ -44,3 +45,13 @@ def test_persist_storagelevel(standalone_employee: StandaloneDataFrame, compare_
"SELECT `t31563`.`fname` AS `fname` FROM `t31563` AS `t31563`",
]
compare_sql(df, expected_statements)


def test_with_column_duplicate_alias(standalone_employee: StandaloneDataFrame):
df = standalone_employee.withColumn("fname", F.col("age").cast("string"))
assert df.columns == ["employee_id", "fname", "lname", "age", "store_id"]
# Make sure that the new columns is added with an alias to `fname`
assert (
df.sql(pretty=False)
== "SELECT `a1`.`employee_id` AS `employee_id`, CAST(`a1`.`age` AS STRING) AS `fname`, CAST(`a1`.`lname` AS STRING) AS `lname`, `a1`.`age` AS `age`, `a1`.`store_id` AS `store_id` FROM VALUES (1, 'Jack', 'Shephard', 37, 1), (2, 'John', 'Locke', 65, 1), (3, 'Kate', 'Austen', 37, 2), (4, 'Claire', 'Littleton', 27, 2), (5, 'Hugo', 'Reyes', 29, 100) AS `a1`(`employee_id`, `fname`, `lname`, `age`, `store_id`)"
)

0 comments on commit d1bf8a1

Please sign in to comment.