Skip to content

Commit 39ed93d

Browse files
committed
Don't expose pub fields
Signed-off-by: Xuanwo <github@xuanwo.io>
1 parent f6e3283 commit 39ed93d

File tree

9 files changed

+80
-49
lines changed

9 files changed

+80
-49
lines changed

src/query/planner/src/metadata.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,14 @@ impl TableEntry {
238238

239239
#[derive(Clone, Debug)]
240240
pub struct ColumnEntry {
241-
pub column_index: IndexType,
242-
pub name: String,
243-
pub data_type: DataTypeImpl,
241+
column_index: IndexType,
242+
name: String,
243+
data_type: DataTypeImpl,
244244

245245
/// Table index of column entry. None if column is derived from a subquery.
246-
pub table_index: Option<IndexType>,
246+
table_index: Option<IndexType>,
247247
/// Path indices for inner column of struct data type.
248-
pub path_indices: Option<Vec<IndexType>>,
248+
path_indices: Option<Vec<IndexType>>,
249249
}
250250

251251
impl ColumnEntry {
@@ -264,6 +264,36 @@ impl ColumnEntry {
264264
path_indices,
265265
}
266266
}
267+
268+
/// Get the name of this column entry.
269+
pub fn name(&self) -> &str {
270+
&self.name
271+
}
272+
273+
/// Get the index of this column entry.
274+
pub fn index(&self) -> IndexType {
275+
self.column_index
276+
}
277+
278+
/// Get the data type of this column entry.
279+
pub fn data_type(&self) -> &DataTypeImpl {
280+
&self.data_type
281+
}
282+
283+
/// Get the table index of this column entry.
284+
pub fn table_index(&self) -> Option<IndexType> {
285+
self.table_index
286+
}
287+
288+
/// Get the path indices of this column entry.
289+
pub fn path_indices(&self) -> Option<&[IndexType]> {
290+
self.path_indices.as_deref()
291+
}
292+
293+
/// Check if this column entry contains path_indices
294+
pub fn has_path_indices(&self) -> bool {
295+
self.path_indices.is_some()
296+
}
267297
}
268298

269299
/// TODO(xuanwo): migrate this as a function of metadata.

src/query/service/src/sql/executor/expression_builder.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ where ExpressionBuilder<T>: FiledNameFormat
6363

6464
pub fn build_and_rename(&self, scalar: &Scalar, index: IndexType) -> Result<Expression> {
6565
let expr = self.build(scalar)?;
66-
let name = self.metadata.read().column(index).name.clone();
67-
Ok(Expression::Alias(
68-
Self::format(name.as_str(), index),
69-
Box::new(expr),
70-
))
66+
let metadata = self.metadata.read();
67+
let name = metadata.column(index).name();
68+
Ok(Expression::Alias(Self::format(name, index), Box::new(expr)))
7169
}
7270

7371
pub fn build(&self, scalar: &Scalar) -> Result<Expression> {
@@ -137,8 +135,9 @@ where ExpressionBuilder<T>: FiledNameFormat
137135
}
138136

139137
pub fn build_column_ref(&self, index: IndexType) -> Result<Expression> {
140-
let name = self.metadata.read().column(index).name.clone();
141-
Ok(Expression::Column(Self::format(name.as_str(), index)))
138+
let metadata = self.metadata.read();
139+
let name = metadata.column(index).name();
140+
Ok(Expression::Column(Self::format(name, index)))
142141
}
143142

144143
pub fn build_literal(

src/query/service/src/sql/executor/format.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn project_to_format_tree(
135135
.columns
136136
.iter()
137137
.sorted()
138-
.map(|column| format!("{} (#{})", metadata.read().column(*column).name, column))
138+
.map(|column| format!("{} (#{})", metadata.read().column(*column).name(), column))
139139
.collect::<Vec<_>>()
140140
.join(", ");
141141
Ok(FormatTreeNode::with_children("Project".to_string(), vec![
@@ -173,7 +173,7 @@ fn aggregate_partial_to_format_tree(
173173
.map(|column| {
174174
let index = column.parse::<IndexType>()?;
175175
let column = metadata.read().column(index).clone();
176-
Ok(column.name)
176+
Ok(column.name().to_string())
177177
})
178178
.collect::<Result<Vec<_>>>()?
179179
.join(", ");
@@ -204,7 +204,7 @@ fn aggregate_final_to_format_tree(
204204
.map(|column| {
205205
let index = column.parse::<IndexType>()?;
206206
let column = metadata.read().column(index).clone();
207-
Ok(column.name)
207+
Ok(column.name().to_string())
208208
})
209209
.collect::<Result<Vec<_>>>()?
210210
.join(", ");
@@ -234,7 +234,7 @@ fn sort_to_format_tree(plan: &Sort, metadata: &MetadataRef) -> Result<FormatTree
234234
let column = metadata.read().column(index).clone();
235235
Ok(format!(
236236
"{} {} {}",
237-
column.name,
237+
column.name(),
238238
if sort_key.asc { "ASC" } else { "DESC" },
239239
if sort_key.nulls_first {
240240
"NULLS FIRST"

src/query/service/src/sql/executor/physical_plan_builder.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use common_legacy_planners::Extras;
2626
use common_legacy_planners::PrewhereInfo;
2727
use common_legacy_planners::Projection;
2828
use common_legacy_planners::StageKind;
29+
use common_planner::IndexType;
2930
use common_planner::Metadata;
3031
use common_planner::MetadataRef;
3132
use common_planner::DUMMY_TABLE_INDEX;
@@ -83,7 +84,7 @@ impl PhysicalPlanBuilder {
8384
let col_indices = columns
8485
.iter()
8586
.map(|index| {
86-
let name = metadata.column(*index).name.as_str();
87+
let name = metadata.column(*index).name();
8788
schema.index_of(name).unwrap()
8889
})
8990
.sorted()
@@ -94,17 +95,17 @@ impl PhysicalPlanBuilder {
9495
.iter()
9596
.map(|index| {
9697
let column = metadata.column(*index);
97-
match &column.path_indices {
98-
Some(path_indices) => (column.column_index, path_indices.clone()),
98+
match &column.path_indices() {
99+
Some(path_indices) => (column.index(), path_indices.to_vec()),
99100
None => {
100-
let name = metadata.column(*index).name.as_str();
101+
let name = metadata.column(*index).name();
101102
let idx = schema.index_of(name).unwrap();
102-
(column.column_index, vec![idx])
103+
(column.index(), vec![idx])
103104
}
104105
}
105106
})
106107
.sorted()
107-
.collect::<BTreeMap<_, _>>();
108+
.collect::<BTreeMap<_, Vec<IndexType>>>();
108109
Projection::InnerColumns(col_indices)
109110
}
110111
}
@@ -120,17 +121,17 @@ impl PhysicalPlanBuilder {
120121
let metadata = self.metadata.read().clone();
121122
for index in scan.columns.iter() {
122123
let column = metadata.column(*index);
123-
if column.path_indices.is_some() {
124+
if column.has_path_indices() {
124125
has_inner_column = true;
125126
}
126127
if let Some(prewhere) = &scan.prewhere {
127128
// if there is a prewhere optimization,
128129
// we can prune `PhysicalScan`'s ouput schema.
129130
if prewhere.output_columns.contains(index) {
130-
name_mapping.insert(column.name.clone(), index.to_string());
131+
name_mapping.insert(column.name().to_string(), index.to_string());
131132
}
132133
} else {
133-
let name = column.name.clone();
134+
let name = column.name().to_string();
134135
name_mapping.insert(name, index.to_string());
135136
}
136137
}

src/query/service/src/sql/executor/physical_scalar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ impl PhysicalScalar {
7070

7171
let index = column_id.parse::<IndexType>()?;
7272
let column = metadata.read().column(index).clone();
73-
let table_name = match column.table_index {
73+
let table_name = match column.table_index() {
7474
Some(table_index) => {
7575
format!("{}.", metadata.read().table(table_index).name())
7676
}
7777
None => "".to_string(),
7878
};
79-
Ok(format!("{}{} (#{})", table_name, column.name, index))
79+
Ok(format!("{}{} (#{})", table_name, column.name(), index))
8080
}
8181
PhysicalScalar::Constant { value, .. } => Ok(value.to_string()),
8282
PhysicalScalar::Function { name, args, .. } => {
@@ -114,7 +114,7 @@ impl AggregateFunctionDesc {
114114
.map(|arg| {
115115
let index = arg.parse::<IndexType>()?;
116116
let column = metadata.read().column(index).clone();
117-
Ok(column.name)
117+
Ok(column.name().to_string())
118118
})
119119
.collect::<Result<Vec<_>>>()?
120120
.join(", ")

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,10 @@ impl<'a> Binder {
295295
let column_binding = ColumnBinding {
296296
database_name: Some(database_name.to_string()),
297297
table_name: Some(table.name().to_string()),
298-
column_name: column.name.clone(),
299-
index: column.column_index,
300-
data_type: Box::new(column.data_type.clone()),
301-
visibility: if column.path_indices.is_some() {
298+
column_name: column.name().to_string(),
299+
index: column.index(),
300+
data_type: Box::new(column.data_type().clone()),
301+
visibility: if column.has_path_indices() {
302302
Visibility::InVisible
303303
} else {
304304
Visibility::Visible
@@ -311,7 +311,7 @@ impl<'a> Binder {
311311
SExpr::create_leaf(
312312
LogicalGet {
313313
table_index,
314-
columns: columns.into_iter().map(|col| col.column_index).collect(),
314+
columns: columns.into_iter().map(|col| col.index()).collect(),
315315
push_down_predicates: None,
316316
limit: None,
317317
order_by: None,

src/query/service/src/sql/planner/format/display_rel_operator.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ fn physical_scan_to_format_tree(
270270
.iter()
271271
.map(|item| format!(
272272
"{} (#{}) {}",
273-
metadata.read().column(item.index).name.clone(),
273+
metadata.read().column(item.index).name(),
274274
item.index,
275275
if item.asc { "ASC" } else { "DESC" }
276276
))
@@ -328,7 +328,7 @@ fn logical_get_to_format_tree(
328328
.iter()
329329
.map(|item| format!(
330330
"{} (#{}) {}",
331-
metadata.read().column(item.index).name.clone(),
331+
metadata.read().column(item.index).name(),
332332
item.index,
333333
if item.asc { "ASC" } else { "DESC" }
334334
))
@@ -560,7 +560,7 @@ fn project_to_format_tree(
560560
.read()
561561
.columns()
562562
.iter()
563-
.map(|entry| format!("{} (#{})", entry.name.clone(), entry.column_index))
563+
.map(|entry| format!("{} (#{})", entry.name(), entry.index()))
564564
.collect::<Vec<String>>();
565565
// Sorted by column index to make display of Project stable
566566
let project_columns = op
@@ -595,7 +595,8 @@ fn sort_to_format_tree(
595595
.items
596596
.iter()
597597
.map(|item| {
598-
let name = metadata.read().column(item.index).name.clone();
598+
let metadata = metadata.read();
599+
let name = metadata.column(item.index).name();
599600
format!(
600601
"{} (#{}) {}",
601602
name,

src/query/service/src/sql/planner/metadata.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ pub fn find_smallest_column(entries: &[ColumnEntry]) -> usize {
3030
debug_assert!(!entries.is_empty());
3131
let mut column_indexes = entries
3232
.iter()
33-
.map(|entry| entry.column_index)
33+
.map(|entry| entry.index())
3434
.collect::<Vec<IndexType>>();
3535
column_indexes.sort();
3636
let mut smallest_index = column_indexes[0];
3737
let mut smallest_size = usize::MAX;
3838
for (idx, column_entry) in entries.iter().enumerate() {
39-
if let Ok(bytes) = column_entry.data_type.data_type_id().numeric_byte_size() {
39+
if let Ok(bytes) = column_entry.data_type().data_type_id().numeric_byte_size() {
4040
if smallest_size > bytes {
4141
smallest_size = bytes;
42-
smallest_index = entries[idx].column_index;
42+
smallest_index = entries[idx].index();
4343
}
4444
}
4545
}

src/query/service/src/sql/planner/optimizer/heuristic/decorrelate.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,12 @@ impl SubqueryRewriter {
443443
self.derived_columns.insert(
444444
*correlated_column,
445445
metadata.add_column(
446-
column_entry.name.clone(),
447-
if let DataTypeImpl::Nullable(_) = column_entry.data_type {
448-
column_entry.data_type.clone()
446+
column_entry.name().to_string(),
447+
if let DataTypeImpl::Nullable(_) = column_entry.data_type() {
448+
column_entry.data_type().clone()
449449
} else {
450450
DataTypeImpl::Nullable(NullableType::create(
451-
column_entry.data_type.clone(),
451+
column_entry.data_type().clone(),
452452
))
453453
},
454454
None,
@@ -495,8 +495,8 @@ impl SubqueryRewriter {
495495
database_name: None,
496496
table_name: None,
497497
column_name: "".to_string(),
498-
index: column_entry.column_index,
499-
data_type: Box::from(column_entry.data_type.clone()),
498+
index: column_entry.index(),
499+
data_type: Box::from(column_entry.data_type().clone()),
500500
visibility: Visibility::Visible,
501501
},
502502
})
@@ -529,7 +529,7 @@ impl SubqueryRewriter {
529529
table_name: None,
530530
column_name: format!("subquery_{}", derived_column),
531531
index: *derived_column,
532-
data_type: Box::from(column_entry.data_type.clone()),
532+
data_type: Box::from(column_entry.data_type().clone()),
533533
visibility: Visibility::Visible,
534534
};
535535
items.push(ScalarItem {
@@ -598,7 +598,7 @@ impl SubqueryRewriter {
598598
table_name: None,
599599
column_name: format!("subquery_{}", derived_column),
600600
index: *derived_column,
601-
data_type: Box::from(column_entry.data_type.clone()),
601+
data_type: Box::from(column_entry.data_type().clone()),
602602
visibility: Visibility::Visible,
603603
}
604604
};
@@ -767,7 +767,7 @@ impl SubqueryRewriter {
767767
let data_type = {
768768
let metadata = self.metadata.read();
769769
let column_entry = metadata.column(*correlated_column);
770-
column_entry.data_type.clone()
770+
column_entry.data_type().clone()
771771
};
772772
let right_column = Scalar::BoundColumnRef(BoundColumnRef {
773773
column: ColumnBinding {

0 commit comments

Comments
 (0)