forked from databendlabs/databend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind.rs
More file actions
410 lines (372 loc) · 14.4 KB
/
Copy pathbind.rs
File metadata and controls
410 lines (372 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
use std::sync::Arc;
use databend_common_ast::Span;
use databend_common_ast::ast::CTE;
use databend_common_ast::ast::ColumnDefinition;
use databend_common_ast::ast::CreateOption;
use databend_common_ast::ast::CreateTableSource;
use databend_common_ast::ast::CreateTableStmt;
use databend_common_ast::ast::Engine;
use databend_common_ast::ast::Expr;
use databend_common_ast::ast::Identifier;
use databend_common_ast::ast::Query;
use databend_common_ast::ast::SetExpr;
use databend_common_ast::ast::TableReference;
use databend_common_ast::ast::TableType;
use databend_common_ast::ast::With;
use databend_common_ast::visit::VisitControl;
use databend_common_ast::visit::Visitor;
use databend_common_ast::visit::VisitorMut;
use databend_common_ast::visit::Walk;
use databend_common_ast::visit::WalkMut;
use databend_common_catalog::catalog::CATALOG_DEFAULT;
use databend_common_exception::ErrorCode;
use databend_common_exception::Result;
use databend_common_expression::types::convert_to_type_name;
use crate::NameResolutionContext;
use crate::normalize_identifier;
use crate::optimizer::ir::SExpr;
use crate::planner::binder::BindContext;
use crate::planner::binder::Binder;
use crate::planner::binder::scalar::ScalarBinder;
use crate::plans::BoundColumnRef;
use crate::plans::ScalarExpr;
use crate::plans::Sort;
use crate::plans::SortItem;
#[derive(Debug, Default)]
struct CTERefCounter {
cte_ref_count: HashMap<String, usize>,
name_resolution_ctx: NameResolutionContext,
}
impl Visitor for CTERefCounter {
fn visit_table_reference(
&mut self,
table_ref: &TableReference,
) -> std::result::Result<VisitControl, !> {
if let TableReference::Table { table, .. } = table_ref {
let table_name = normalize_identifier(&table.table, &self.name_resolution_ctx).name;
if let Some(count) = self.cte_ref_count.get_mut(&table_name) {
*count += 1;
}
}
Ok(VisitControl::Continue)
}
}
impl Binder {
#[recursive::recursive]
pub(crate) fn bind_query(
&mut self,
bind_context: &mut BindContext,
query: &Query,
) -> Result<(SExpr, BindContext)> {
let mut with = query.with.clone();
bind_context.allow_virtual_column = self.is_virtual_column_rewrite_enabled();
if self.ctx.get_settings().get_enable_auto_materialize_cte()? {
if let Some(with) = &mut with {
if !with.recursive {
self.auto_materialize_cte(with, query)?;
}
}
}
bind_context.cte_context.virtual_column_outputs.clear();
let rewritten_body = if bind_context.allow_virtual_column {
if let Some(with) = &mut with {
let mut body = query.body.clone();
let virtual_column_outputs =
self.rewrite_materialized_cte_virtual_columns(bind_context, with, &mut body);
bind_context.cte_context.virtual_column_outputs = virtual_column_outputs;
Some(body)
} else {
None
}
} else {
None
};
let body = rewritten_body.as_ref().unwrap_or(&query.body);
self.init_cte(bind_context, &with)?;
// Extract limit and offset from query.
let (limit, offset) = self.extract_limit_and_offset(query)?;
// Bind query body.
let (mut s_expr, mut bind_context) =
self.bind_set_expr(bind_context, body, &query.order_by, limit, None)?;
// Bind order by for `SetOperation` and `Values`.
s_expr = self.bind_query_order_by(&mut bind_context, query, s_expr)?;
// Bind limit.
s_expr = self.bind_query_limit(query, s_expr, limit, offset);
if let Some(with) = &with {
s_expr = self.bind_materialized_cte(with, s_expr, bind_context.cte_context.clone())?;
}
bind_context.reset_result_column_positions();
Ok((s_expr, bind_context))
}
pub(crate) fn is_virtual_column_rewrite_enabled(&self) -> bool {
self.ctx
.get_settings()
.get_enable_experimental_virtual_column()
.unwrap_or_default()
}
fn auto_materialize_cte(&mut self, with: &mut With, query: &Query) -> Result<()> {
let cte_ref_count = self.compute_cte_ref_count(with, query)?;
// Update materialization based on reference count
for cte in with.ctes.iter_mut() {
let table_name = self.normalize_identifier(&cte.alias.name).name;
if let Some(count) = cte_ref_count.get(&table_name) {
log::info!("[CTE]cte_ref_count: {table_name} {count}");
// Materialize if referenced more than once
cte.materialized = !cte.user_specified_materialized && *count > 1;
}
}
Ok(())
}
#[recursive::recursive]
pub fn compute_cte_ref_count(
&self,
with: &With,
query: &Query,
) -> Result<HashMap<String, usize>> {
// Initialize the count of each CTE to 0
let mut cte_ref_count: HashMap<String, usize> = HashMap::new();
for cte in with.ctes.iter() {
let table_name = self.normalize_identifier(&cte.alias.name).name;
cte_ref_count.insert(table_name, 0);
}
// Count the number of times each CTE is referenced in the query
let mut visitor = CTERefCounter {
cte_ref_count,
name_resolution_ctx: self.name_resolution_ctx.clone(),
};
query.walk(&mut visitor)?;
Ok(visitor.cte_ref_count)
}
pub(crate) fn bind_query_order_by(
&mut self,
bind_context: &mut BindContext,
query: &Query,
child: SExpr,
) -> Result<SExpr> {
if !matches!(
query.body,
SetExpr::SetOperation(_) | SetExpr::Values { .. }
) || query.order_by.is_empty()
{
return Ok(child);
}
let mut scalar_binder = ScalarBinder::new(
bind_context,
self.ctx.clone(),
&self.name_resolution_ctx,
self.metadata.clone(),
&[],
);
let mut order_by_items = Vec::with_capacity(query.order_by.len());
let settings = self.ctx.get_settings();
let default_nulls_first = settings.get_nulls_first();
for order in query.order_by.iter() {
match order.expr {
Expr::ColumnRef { .. } => {
let scalar = scalar_binder.bind(&order.expr)?.0;
match scalar {
ScalarExpr::BoundColumnRef(BoundColumnRef { column, .. }) => {
let asc = order.asc.unwrap_or(true);
let order_by_item = SortItem {
index: column.index,
asc,
nulls_first: order
.nulls_first
.unwrap_or_else(|| default_nulls_first(asc)),
};
order_by_items.push(order_by_item);
}
_ => {
return Err(ErrorCode::Internal("scalar should be BoundColumnRef")
.set_span(order.expr.span()));
}
}
}
_ => {
return Err(
ErrorCode::SemanticError("can only order by column".to_string())
.set_span(order.expr.span()),
);
}
}
}
let sort_plan = Sort {
items: order_by_items,
limit: None,
after_exchange: None,
pre_projection: None,
window_partition: None,
};
Ok(SExpr::create_unary(
Arc::new(sort_plan.into()),
Arc::new(child),
))
}
#[recursive::recursive]
pub fn m_cte_to_temp_table(
&mut self,
cte: &CTE,
cte_index: usize,
mut with: With,
) -> Result<()> {
let engine = if self.ctx.get_settings().get_persist_materialized_cte()? {
Engine::Fuse
} else {
Engine::Memory
};
let query_id = self.ctx.get_id();
let database = self.ctx.get_current_database();
let mut table_identifier = cte.alias.name.clone();
table_identifier.name = format!("{}${}", table_identifier.name, query_id.replace("-", ""));
let table_name = normalize_identifier(&table_identifier, &self.name_resolution_ctx).name;
self.m_cte_table_name.insert(
normalize_identifier(&cte.alias.name, &self.name_resolution_ctx).name,
table_name.clone(),
);
if self
.ctx
.is_temp_table(CATALOG_DEFAULT, &database, &table_name)
{
return Err(ErrorCode::Internal(format!(
"Temporary table {:?} already exists in current session, please change the materialized CTE name",
table_name
)));
}
let mut expr_replacer = TableNameReplacer::new(
database.clone(),
self.m_cte_table_name.clone(),
self.name_resolution_ctx.clone(),
);
let mut as_query = cte.query.clone();
with.ctes.truncate(cte_index);
with.ctes.retain(|cte| !cte.user_specified_materialized);
as_query.with = if !with.ctes.is_empty() {
Some(with)
} else {
None
};
as_query.walk_mut(&mut expr_replacer)?;
let source = if cte.alias.columns.is_empty() {
None
} else {
let mut bind_context = BindContext::new();
let (_, bind_context) = self.bind_query(&mut bind_context, &as_query)?;
let columns = &bind_context.columns;
if columns.len() != cte.alias.columns.len() {
return Err(ErrorCode::Internal("Number of columns does not match"));
}
Some(CreateTableSource::Columns {
columns: columns
.iter()
.zip(cte.alias.columns.iter())
.map(|(column, ident)| {
let data_type = convert_to_type_name(&column.data_type);
ColumnDefinition {
name: ident.clone(),
data_type,
expr: None,
check: None,
comment: None,
stats_truncate_len: None,
}
})
.collect(),
opt_table_indexes: None,
opt_column_constraints: None,
opt_table_constraints: None,
})
};
let catalog = self.ctx.get_current_catalog();
let create_table_stmt = CreateTableStmt {
create_option: CreateOption::Create,
catalog: Some(Identifier::from_name(Span::None, catalog.clone())),
database: Some(Identifier::from_name(Span::None, database.clone())),
table: table_identifier,
source,
engine: Some(engine),
uri_location: None,
cluster_by: None,
table_options: Default::default(),
iceberg_table_partition: None,
table_properties: Default::default(),
as_query: Some(as_query),
table_type: TableType::Temporary,
};
let create_table_sql = create_table_stmt.to_string();
log::info!("[CTE]create_table_sql: {create_table_sql}");
if let Some(subquery_executor) = &self.subquery_executor {
let _ = databend_common_base::runtime::block_on(async move {
subquery_executor
.execute_query_with_sql_string(&create_table_sql)
.await
})?;
} else {
return Err(ErrorCode::Internal("Binder's Subquery executor is not set"));
};
self.ctx.add_m_cte_temp_table(&database, &table_name);
self.ctx
.evict_table_from_cache(&catalog, &database, &table_name)
}
}
pub struct TableNameReplacer {
database: String,
new_name: HashMap<String, String>,
name_resolution_ctx: NameResolutionContext,
}
impl VisitorMut for TableNameReplacer {
fn visit_table_reference(
&mut self,
table_reference: &mut TableReference,
) -> std::result::Result<VisitControl, !> {
if let TableReference::Table { table, .. } = table_reference {
if table.database.is_none() || table.database.as_ref().unwrap().name == self.database {
self.replace_identifier(&mut table.table);
}
}
Ok(VisitControl::Continue)
}
fn visit_expr(&mut self, expr: &mut Expr) -> std::result::Result<VisitControl, !> {
if let Expr::ColumnRef { column, .. } = expr {
if column.database.is_none() || column.database.as_ref().unwrap().name == self.database
{
if let Some(table_identifier) = &mut column.table {
self.replace_identifier(table_identifier);
}
}
}
Ok(VisitControl::Continue)
}
}
impl TableNameReplacer {
pub fn new(
database: String,
new_name: HashMap<String, String>,
name_resolution_ctx: NameResolutionContext,
) -> Self {
Self {
database,
new_name,
name_resolution_ctx,
}
}
fn replace_identifier(&mut self, identifier: &mut Identifier) {
let name = normalize_identifier(identifier, &self.name_resolution_ctx).name;
if let Some(new_name) = self.new_name.get(&name) {
identifier.name = new_name.clone();
}
}
}