From 30e6524c4eae61e5897b12d459d934f71970134d Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Wed, 1 Oct 2025 08:37:12 +0300 Subject: [PATCH] Fix: JOIN USING should pick columns from left table, not right Closes #3468 Closes #3479 --- core/translate/expr.rs | 15 ++++++++------- testing/join.test | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/core/translate/expr.rs b/core/translate/expr.rs index c7558562d..4f7093a70 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -3370,14 +3370,15 @@ pub fn bind_and_rewrite_expr<'a>( id.as_str() ); } + } else { + let col = + joined_table.table.columns().get(col_idx.unwrap()).unwrap(); + match_result = Some(( + joined_table.internal_id, + col_idx.unwrap(), + col.is_rowid_alias, + )); } - let col = - joined_table.table.columns().get(col_idx.unwrap()).unwrap(); - match_result = Some(( - joined_table.internal_id, - col_idx.unwrap(), - col.is_rowid_alias, - )); // only if we haven't found a match, check for explicit rowid reference } else if let Some(row_id_expr) = parse_row_id( &normalized_id, diff --git a/testing/join.test b/testing/join.test index a52973421..c14b96ab4 100755 --- a/testing/join.test +++ b/testing/join.test @@ -362,3 +362,25 @@ do_execsql_test_on_specific_db {:memory:} left-join-where-clause-regression { select t.a, s.a from t left join s on t.a=s.a where s.a = 2; } {2|2 2|2} + +# Regression test for: https://github.com/tursodatabase/turso/issues/3468 +do_execsql_test_on_specific_db {:memory:} left-join-using-star-vs-explicit { + create table t(a, tb); + create table s(a, sb); + insert into t values (1, 't1'), (2, 't2'); + insert into s values (1, 's1'), (3, 's3'); + select * from t left join s using(a); + select a, tb, sb from t left join s using(a); +} {1|t1|s1 +2|t2| +1|t1|s1 +2|t2|} + +do_execsql_test_on_specific_db {:memory:} left-join-using-null { + create table t(a, b); + create table s(a, b); + insert into t values (1, null), (2, null); + insert into s values (1, null), (2, null); + select a, b from t left join s using (a, b); +} {1| +2|}