Skip to content

Commit a4b6b0c

Browse files
authored
fix: update failed with Unable to get field named... (#18078)
* fix * add test
1 parent 3d93c17 commit a4b6b0c

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/query/sql/src/planner/binder/bind_mutation/mutation_expression.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ impl MutationExpression {
198198
let (mut s_expr, mut bind_context) =
199199
binder.bind_table_reference(bind_context, target)?;
200200

201+
// Note: We intentionally get target table index before binding source table,
202+
// because source table may contain tables with same name as target table,
203+
// which could cause us to get wrong table index if we do it later.
204+
let target_table_index = binder
205+
.metadata
206+
.read()
207+
.get_table_index(
208+
Some(target_table_identifier.database_name().as_str()),
209+
target_table_identifier.table_name().as_str(),
210+
)
211+
.ok_or_else(|| ErrorCode::Internal("Can't get target table index"))?;
212+
201213
let from_s_expr = if let Some(from) = from {
202214
let (from_s_expr, mut from_context) =
203215
binder.bind_table_reference(&mut bind_context, from)?;
@@ -216,16 +228,6 @@ impl MutationExpression {
216228
None
217229
};
218230

219-
// Get target table index.
220-
let target_table_index = binder
221-
.metadata
222-
.read()
223-
.get_table_index(
224-
Some(target_table_identifier.database_name().as_str()),
225-
target_table_identifier.table_name().as_str(),
226-
)
227-
.ok_or_else(|| ErrorCode::Internal("Can't get target table index"))?;
228-
229231
// If the filter is a simple expression, change the mutation strategy to MutationStrategy::Direct.
230232
let (mut mutation_strategy, predicates) =
231233
binder.process_filter(&mut bind_context, filter)?;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
statement ok
2+
CREATE OR REPLACE TABLE students (
3+
code VARCHAR,
4+
score INT
5+
);
6+
7+
statement ok
8+
CREATE OR REPLACE TABLE pairs (
9+
student1 VARCHAR,
10+
student2 VARCHAR,
11+
score1 INT,
12+
score2 INT
13+
);
14+
15+
statement ok
16+
UPDATE pairs AS p
17+
SET p.score1 = data.s1, p.score2 = data.s2
18+
FROM (
19+
SELECT
20+
split_part(a.code, '_', 2) AS c1, a.score AS s1,
21+
split_part(b.code, '_', 2) AS c2, b.score AS s2
22+
FROM pairs AS p
23+
JOIN students AS a, students AS b
24+
WHERE split_part(a.code, '_', 2) = p.student1
25+
AND split_part(b.code, '_', 2) = p.student2
26+
) data
27+
WHERE p.student1 = data.c1 AND p.student2 = data.c2;

0 commit comments

Comments
 (0)