Skip to content

Commit

Permalink
fix: rewrite shouldn't be performed on a column name same as the tabl…
Browse files Browse the repository at this point in the history
…e name (#33)

* Rewrite shouldn't be performed on a column name same as the table name

* fix comment

* Add unit test
  • Loading branch information
Sevenannn authored Dec 27, 2024
1 parent 884b2a5 commit ce91d5a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions sources/sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ fn rewrite_column_name_in_expr(
// Calculate the absolute index of the occurrence in string as the index above is relative to start_pos
let idx = start_pos + idx;

// Table name same as column name
// Shouldn't rewrite in this case
if idx == 0 && start_pos == 0 {
return None;
}

if idx > 0 {
// Check if the previous character is alphabetic, numeric, underscore or period, in which case we
// should not rewrite as it is a part of another name.
Expand Down Expand Up @@ -1072,6 +1078,23 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn test_rewrite_same_column_table_name() -> Result<()> {
init_tracing();
let ctx = get_test_df_context();

let tests = vec![(
"SELECT app_table FROM (SELECT a app_table from app_table limit 100);",
r#"SELECT app_table FROM (SELECT remote_table.a AS app_table FROM remote_table LIMIT 100)"#,
)];

for test in tests {
test_sql(&ctx, test.0, test.1).await?;
}

Ok(())
}

async fn test_sql(
ctx: &SessionContext,
sql_query: &str,
Expand Down

0 comments on commit ce91d5a

Please sign in to comment.