Skip to content

Commit e6d4adc

Browse files
authored
fix(query): fix bind internal column (#17463)
* fix(query): fix bind internal column * fix * fix merge into
1 parent 506cfd9 commit e6d4adc

File tree

5 files changed

+96
-41
lines changed

5 files changed

+96
-41
lines changed

src/query/sql/src/planner/binder/bind_context.rs

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use std::collections::btree_map;
1616
use std::collections::BTreeMap;
17+
use std::collections::BTreeSet;
1718
use std::collections::HashMap;
1819
use std::collections::HashSet;
1920
use std::hash::Hash;
@@ -147,8 +148,8 @@ pub struct BindContext {
147148

148149
pub columns: Vec<ColumnBinding>,
149150

150-
// map internal column id to (table_index, column_index)
151-
pub bound_internal_columns: BTreeMap<ColumnId, (IndexType, IndexType)>,
151+
// map internal column: (table_index, column_id) -> column_index
152+
pub bound_internal_columns: BTreeMap<(IndexType, ColumnId), IndexType>,
152153

153154
pub aggregate_info: AggregateInfo,
154155

@@ -578,35 +579,49 @@ impl BindContext {
578579
}
579580

580581
fn get_internal_column_table_index(
582+
&self,
581583
column_binding: &InternalColumnBinding,
582-
metadata: MetadataRef,
583584
) -> Result<IndexType> {
584-
let metadata = metadata.read();
585-
586585
if let Some(table_name) = &column_binding.table_name {
587-
metadata
588-
.get_table_index(column_binding.database_name.as_deref(), table_name)
589-
.ok_or_else(|| {
590-
ErrorCode::TableInfoError(format!(
591-
"Table `{table_name}` is not found in the metadata"
592-
))
593-
})
586+
for column in &self.columns {
587+
if column_binding.database_name.is_some()
588+
&& column.database_name != column_binding.database_name
589+
{
590+
continue;
591+
}
592+
if column.table_name != column_binding.table_name {
593+
continue;
594+
}
595+
if let Some(table_index) = column.table_index {
596+
return Ok(table_index);
597+
}
598+
}
599+
Err(ErrorCode::SemanticError(format!(
600+
"Table `{table_name}` is not found in the bind context"
601+
)))
594602
} else {
595-
let tables = metadata
596-
.tables()
597-
.iter()
598-
.filter(|t| !t.is_source_of_index())
599-
.collect::<Vec<_>>();
600-
debug_assert!(!tables.is_empty());
601-
602-
if tables.len() > 1 {
603+
let mut table_indices = BTreeSet::new();
604+
for column in &self.columns {
605+
if column.table_name.is_none() {
606+
continue;
607+
}
608+
if let Some(table_index) = column.table_index {
609+
table_indices.insert(table_index);
610+
}
611+
}
612+
if table_indices.is_empty() {
613+
return Err(ErrorCode::SemanticError(format!(
614+
"The table of the internal column `{}` is not found",
615+
column_binding.internal_column.column_name()
616+
)));
617+
}
618+
if table_indices.len() > 1 {
603619
return Err(ErrorCode::SemanticError(format!(
604620
"The table of the internal column `{}` is ambiguous",
605621
column_binding.internal_column.column_name()
606622
)));
607623
}
608-
609-
Ok(tables[0].index())
624+
Ok(*table_indices.first().unwrap())
610625
}
611626
}
612627

@@ -615,25 +630,31 @@ impl BindContext {
615630
&mut self,
616631
column_binding: &InternalColumnBinding,
617632
metadata: MetadataRef,
633+
table_index: Option<IndexType>,
618634
visible: bool,
619635
) -> Result<ColumnBinding> {
620636
let column_id = column_binding.internal_column.column_id();
621-
let (table_index, column_index, new) = match self.bound_internal_columns.entry(column_id) {
622-
btree_map::Entry::Vacant(e) => {
623-
let table_index =
624-
BindContext::get_internal_column_table_index(column_binding, metadata.clone())?;
625-
let mut metadata = metadata.write();
626-
let column_index = metadata
627-
.add_internal_column(table_index, column_binding.internal_column.clone());
628-
e.insert((table_index, column_index));
629-
(table_index, column_index, true)
630-
}
631-
btree_map::Entry::Occupied(e) => {
632-
let (table_index, column_index) = e.get();
633-
(*table_index, *column_index, false)
634-
}
637+
let table_index = if let Some(table_index) = table_index {
638+
table_index
639+
} else {
640+
self.get_internal_column_table_index(column_binding)?
635641
};
636642

643+
let (column_index, is_new) =
644+
match self.bound_internal_columns.entry((table_index, column_id)) {
645+
btree_map::Entry::Vacant(e) => {
646+
let mut metadata = metadata.write();
647+
let column_index = metadata
648+
.add_internal_column(table_index, column_binding.internal_column.clone());
649+
e.insert(column_index);
650+
(column_index, true)
651+
}
652+
btree_map::Entry::Occupied(e) => {
653+
let column_index = e.get();
654+
(*column_index, false)
655+
}
656+
};
657+
637658
let metadata = metadata.read();
638659
let table = metadata.table(table_index);
639660
if !table.table().supported_internal_column(column_id) {
@@ -660,7 +681,7 @@ impl BindContext {
660681
.table_index(Some(table_index))
661682
.build();
662683

663-
if new {
684+
if is_new {
664685
debug_assert!(!self.columns.iter().any(|c| c == &column_binding));
665686
self.columns.push(column_binding.clone());
666687
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ impl Binder {
417417
let column_binding = match bind_context.add_internal_column_binding(
418418
&row_id_column_binding,
419419
self.metadata.clone(),
420+
Some(table_index),
420421
true,
421422
) {
422423
Ok(column_binding) => column_binding,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ impl<'a> Binder {
975975

976976
let mut has_score = false;
977977
let mut has_matched = false;
978-
for column_id in bound_internal_columns.keys() {
978+
for (_, column_id) in bound_internal_columns.keys() {
979979
if *column_id == SEARCH_SCORE_COLUMN_ID {
980980
has_score = true;
981981
} else if *column_id == SEARCH_MATCHED_COLUMN_ID {
@@ -988,7 +988,7 @@ impl<'a> Binder {
988988
));
989989
}
990990

991-
for (table_index, column_index) in bound_internal_columns.values() {
991+
for ((table_index, _), column_index) in bound_internal_columns.iter() {
992992
let inverted_index = inverted_index_map.shift_remove(table_index).map(|mut i| {
993993
i.has_score = has_score;
994994
i

src/query/sql/src/planner/semantic/type_check.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ impl<'a> TypeChecker<'a> {
295295
let column = self.bind_context.add_internal_column_binding(
296296
&column,
297297
self.metadata.clone(),
298+
None,
298299
true,
299300
)?;
300301
let data_type = *column.data_type.clone();
@@ -2199,6 +2200,7 @@ impl<'a> TypeChecker<'a> {
21992200
let column = self.bind_context.add_internal_column_binding(
22002201
&internal_column_binding,
22012202
self.metadata.clone(),
2203+
None,
22022204
false,
22032205
)?;
22042206

@@ -2630,6 +2632,7 @@ impl<'a> TypeChecker<'a> {
26302632
let column = self.bind_context.add_internal_column_binding(
26312633
&internal_column_binding,
26322634
self.metadata.clone(),
2635+
None,
26332636
false,
26342637
)?;
26352638

tests/sqllogictests/suites/base/05_ddl/05_0031_internal_column.test

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,21 @@ SELECT a,`05_0031_t_1`._row_id FROM `05_0031_t` NATURAL JOIN `05_0031_t_1` order
5757
3 18446739675663040512
5858
3 18446739675663040513
5959

60+
query III
61+
SELECT a, `05_0031_t`._row_id, `05_0031_t_1`._row_id FROM `05_0031_t`, `05_0031_t_1` order by a, `05_0031_t`._row_id;
62+
----
63+
1 18446735277616529408 18446739675663040512
64+
1 18446735277616529408 18446739675663040513
65+
2 18446735277616529409 18446739675663040512
66+
2 18446735277616529409 18446739675663040513
67+
3 18446739675663040512 18446739675663040512
68+
3 18446739675663040512 18446739675663040513
69+
6070
# issue #11126
6171
# ===============
6272

6373
statement ok
64-
create table t_11126(a Int64)
74+
create or replace table t_11126(a Int64)
6575

6676
statement ok
6777
insert into t_11126 select * from numbers(100);
@@ -75,6 +85,26 @@ select a, _row_id from t_11126 order by a desc limit 5;
7585
96 18446739675663040608
7686
95 18446739675663040607
7787

88+
# issue 17327
89+
90+
query IF
91+
with tt as (select sum(a) as a from t_11126) select t_11126._row_id, t_11126.a/tt.a from t_11126, tt limit 5;
92+
----
93+
18446739675663040512 0.0
94+
18446739675663040513 0.00020202020202020202
95+
18446739675663040514 0.00040404040404040404
96+
18446739675663040515 0.0006060606060606061
97+
18446739675663040516 0.0008080808080808081
98+
99+
query IF
100+
with tt as (select sum(a) as a from t_11126) select t_11126._row_id, t_11126.a/tt.a from tt, t_11126 limit 5;
101+
----
102+
18446739675663040512 0.0
103+
18446739675663040513 0.00020202020202020202
104+
18446739675663040514 0.00040404040404040404
105+
18446739675663040515 0.0006060606060606061
106+
18446739675663040516 0.0008080808080808081
107+
78108
statement ok
79109
drop table t_11126
80110

@@ -104,4 +134,4 @@ statement ok
104134
drop table t_11772_1;
105135

106136
statement ok
107-
drop table t_11772_2;
137+
drop table t_11772_2;

0 commit comments

Comments
 (0)