forked from databendlabs/databend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataframe.rs
More file actions
516 lines (460 loc) · 16 KB
/
Copy pathdataframe.rs
File metadata and controls
516 lines (460 loc) · 16 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// 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::sync::Arc;
use databend_common_ast::ast::ColumnID;
use databend_common_ast::ast::ColumnRef;
use databend_common_ast::ast::Expr;
use databend_common_ast::ast::FunctionCall;
use databend_common_ast::ast::GroupBy;
use databend_common_ast::ast::Identifier;
use databend_common_ast::ast::Join;
use databend_common_ast::ast::JoinCondition;
use databend_common_ast::ast::JoinOperator;
use databend_common_ast::ast::OrderByExpr;
use databend_common_ast::ast::SelectTarget;
use databend_common_ast::ast::TableRef;
use databend_common_ast::ast::TableReference;
use databend_common_catalog::catalog::CATALOG_DEFAULT;
use databend_common_catalog::catalog::CatalogManager;
use databend_common_catalog::table::Table;
use databend_common_catalog::table_context::TableContext;
use databend_common_exception::ErrorCode;
use databend_common_exception::Result;
use databend_common_expression::DataSchemaRef;
use parking_lot::RwLock;
use crate::BindContext;
use crate::Binder;
use crate::Metadata;
use crate::NameResolutionContext;
use crate::optimizer::ir::SExpr;
use crate::planner::binder::SelectInfo;
use crate::plans::Limit;
pub struct Dataframe {
query_ctx: Arc<dyn TableContext>,
binder: Binder,
bind_context: BindContext,
s_expr: SExpr,
}
impl Dataframe {
fn apply_select_output(self, select_info: SelectInfo) -> Result<Self> {
let Dataframe {
query_ctx,
mut binder,
mut bind_context,
s_expr,
} = self;
let s_expr = binder.bind_projection(&mut bind_context, select_info, s_expr)?;
let s_expr = binder.add_bound_columns_into_expr(&mut bind_context, s_expr)?;
Ok(Self {
query_ctx,
binder,
bind_context,
s_expr,
})
}
pub async fn scan(
query_ctx: Arc<dyn TableContext>,
db: Option<&str>,
table_name: &str,
) -> Result<Self> {
let table = TableReference::Table {
table: TableRef {
catalog: None,
database: db.map(|db| Identifier::from_name(None, db)),
table: Identifier::from_name(None, table_name),
branch: None,
},
span: None,
alias: None,
temporal: None,
with_options: None,
pivot: None,
unpivot: None,
sample: None,
};
let settings = query_ctx.get_settings();
let metadata = Arc::new(RwLock::new(Metadata::default()));
let name_resolution_ctx = NameResolutionContext::try_from(settings.as_ref())?;
let mut binder = Binder::new(
query_ctx.clone(),
CatalogManager::instance(),
name_resolution_ctx,
metadata.clone(),
);
let mut bind_context = BindContext::new();
let (s_expr, bind_context) = if db == Some("system") && table_name == "one" {
let catalog = CATALOG_DEFAULT;
let database = "system";
let table_meta: Arc<dyn Table> = binder
.resolve_data_source(&query_ctx, catalog, database, "one", None, None, None)?;
let table_index = metadata.write().add_table(
CATALOG_DEFAULT.to_owned(),
database.to_string(),
table_meta,
None,
None,
false,
false,
false,
None,
);
binder.bind_base_table(&bind_context, database, table_index, None, &None, true)
} else {
binder.bind_table_reference(&mut bind_context, &table)
}?;
Ok(Dataframe {
query_ctx,
binder,
bind_context,
s_expr,
})
}
pub async fn scan_one(query_ctx: Arc<dyn TableContext>) -> Result<Self> {
Self::scan(query_ctx, Some("system"), "one").await
}
pub fn select_columns(self, columns: &[&str]) -> Result<Self> {
let schema = self.bind_context.output_schema();
let select_list = parse_cols(schema, columns)?;
self.select_targets(&select_list)
}
pub fn select(self, expr_list: Vec<Expr>) -> Result<Self> {
let select_list: Vec<SelectTarget> = expr_list
.into_iter()
.map(|expr| SelectTarget::AliasedExpr {
expr: Box::new(expr),
alias: None,
})
.collect();
self.select_targets(&select_list)
}
fn select_targets(mut self, select_list: &[SelectTarget]) -> Result<Self> {
let bind_context = &mut self.bind_context;
let select_list = self
.binder
.normalize_select_list(bind_context, select_list)?;
let select_info = self.binder.analyze_projection(bind_context, &select_list)?;
self.apply_select_output(select_info)
}
pub async fn filter(mut self, expr: Expr) -> Result<Self> {
let (s_expr, _) =
self.binder
.bind_where(&mut self.bind_context, &[], &expr, self.s_expr)?;
self.s_expr = s_expr;
Ok(self)
}
pub fn count(mut self) -> Result<Self> {
let select_list = [SelectTarget::AliasedExpr {
expr: Box::new(Expr::FunctionCall {
span: None,
func: FunctionCall {
distinct: false,
name: Identifier::from_name(None, "count"),
args: vec![],
params: vec![],
order_by: vec![],
filter: None,
window: None,
lambda: None,
},
}),
alias: None,
}];
let mut select_list = self
.binder
.normalize_select_list(&mut self.bind_context, &select_list)?;
self.binder
.analyze_aggregate_select(&mut self.bind_context, &mut select_list)?;
let select_info = self
.binder
.analyze_projection(&self.bind_context, &select_list)?;
self.s_expr = self
.binder
.bind_aggregate(&mut self.bind_context, self.s_expr)?;
self.apply_select_output(select_info)
}
pub async fn aggregate(
mut self,
groupby: GroupBy,
aggr_expr: Vec<Expr>,
having: Option<Expr>,
) -> Result<Self> {
let select_list: Vec<SelectTarget> = aggr_expr
.into_iter()
.map(|expr| SelectTarget::AliasedExpr {
expr: Box::new(expr),
alias: None,
})
.collect();
let select_list = self
.binder
.normalize_select_list(&mut self.bind_context, &select_list)?;
let alias_catalog = select_list.alias_catalog();
let aliases = alias_catalog.all_aliases();
let group_by_aliases = alias_catalog.group_by_bindings(
self.query_ctx
.get_settings()
.get_enable_group_by_column_first()?,
);
self.binder.analyze_group_items(
&mut self.bind_context,
&select_list,
&group_by_aliases,
&groupby,
)?;
if self.bind_context.aggregate_info.has_aggregate_calls()
|| self.bind_context.aggregate_info.has_group_items()
{
self.s_expr = self
.binder
.bind_aggregate(&mut self.bind_context, self.s_expr)?;
}
if let Some(having) = &having {
let having =
self.binder
.analyze_aggregate_having(&mut self.bind_context, aliases, having)?;
self.s_expr = self
.binder
.bind_having(&mut self.bind_context, having, self.s_expr)?;
}
let select_info = self
.binder
.analyze_projection(&self.bind_context, &select_list)?;
self.apply_select_output(select_info)
}
pub fn distinct_col(self, columns: &[&str]) -> Result<Self> {
let select_list = parse_cols(self.bind_context.output_schema(), columns)?;
self.distinct_target(select_list)
}
pub fn distinct(self, select_list: Vec<Expr>) -> Result<Self> {
let select_list: Vec<SelectTarget> = select_list
.into_iter()
.map(|expr| SelectTarget::AliasedExpr {
expr: Box::new(expr),
alias: None,
})
.collect();
self.distinct_target(select_list)
}
pub fn distinct_target(mut self, select_list: Vec<SelectTarget>) -> Result<Self> {
let mut select_list = self
.binder
.normalize_select_list(&mut self.bind_context, select_list.as_slice())?;
self.binder
.analyze_aggregate_select(&mut self.bind_context, &mut select_list)?;
let mut select_info = self
.binder
.analyze_projection(&self.bind_context, &select_list)?;
self.s_expr = self
.binder
.bind_distinct(None, &mut select_info, self.s_expr)?;
self.apply_select_output(select_info)
}
pub async fn limit(mut self, limit: Option<usize>, offset: usize) -> Result<Self> {
let limit_plan = Limit {
before_exchange: false,
limit,
offset,
lazy_columns: Default::default(),
};
self.s_expr = SExpr::create_unary(Arc::new(limit_plan.into()), Arc::new(self.s_expr));
Ok(self)
}
pub async fn sort_column(
self,
columns: &[&str],
order_by: Vec<(Expr, Option<bool>, Option<bool>)>,
distinct: bool,
) -> Result<Self> {
let select_list = parse_cols(self.bind_context.output_schema(), columns)?;
self.sort_target(select_list, order_by, distinct).await
}
pub async fn sort(
self,
select_list: Vec<Expr>,
order_by: Vec<(Expr, Option<bool>, Option<bool>)>,
distinct: bool,
) -> Result<Self> {
let select_list: Vec<SelectTarget> = select_list
.into_iter()
.map(|expr| SelectTarget::AliasedExpr {
expr: Box::new(expr),
alias: None,
})
.collect();
self.sort_target(select_list, order_by, distinct).await
}
pub async fn sort_target(
mut self,
select_list: Vec<SelectTarget>,
order_by: Vec<(Expr, Option<bool>, Option<bool>)>,
distinct: bool,
) -> Result<Self> {
let mut order = vec![];
for (expr, asc, nulls_first) in order_by {
order.push(OrderByExpr {
expr,
asc,
nulls_first,
});
}
let select_list = self
.binder
.normalize_select_list(&mut self.bind_context, select_list.as_slice())?;
let aliases = select_list
.items
.iter()
.map(|item| (item.alias.clone(), item.scalar.clone()))
.collect::<Vec<_>>();
let mut select_info = self
.binder
.analyze_projection(&self.bind_context, &select_list)?;
let order_items = self.binder.analyze_order_items(
&mut self.bind_context,
&mut select_info,
&aliases,
None,
&order,
distinct,
)?;
self.binder
.refresh_select_output(&self.bind_context, &mut select_info)?;
self.s_expr = self.binder.bind_order_by(order_items, self.s_expr)?;
self.apply_select_output(select_info)
}
pub async fn except(mut self, dataframe: Dataframe) -> Result<Self> {
let (s_expr, bind_context) = self.binder.bind_except(
(None, self.s_expr, self.bind_context),
(None, dataframe.s_expr, dataframe.bind_context),
)?;
self.s_expr = s_expr;
self.bind_context = bind_context;
Ok(self)
}
pub async fn intersect(mut self, dataframe: Dataframe) -> Result<Self> {
let (s_expr, bind_context) = self.binder.bind_intersect(
(None, self.s_expr, self.bind_context),
(None, dataframe.s_expr, dataframe.bind_context),
)?;
self.s_expr = s_expr;
self.bind_context = bind_context;
Ok(self)
}
pub async fn join(
mut self,
from: Vec<(Option<&str>, &str)>,
op: JoinOperator,
condition: JoinCondition,
) -> Result<Self> {
let mut table_ref = vec![];
for (db, table_name) in from {
let table = TableReference::Table {
table: TableRef {
catalog: None,
database: db.map(|db| Identifier::from_name(None, db)),
table: Identifier::from_name(None, table_name),
branch: None,
},
span: None,
alias: None,
temporal: None,
with_options: None,
pivot: None,
unpivot: None,
sample: None,
};
table_ref.push(table);
}
let cross_joins = table_ref
.iter()
.cloned()
.reduce(|left, right| TableReference::Join {
span: None,
join: Join {
op: op.clone(),
condition: condition.clone(),
left: Box::new(left),
right: Box::new(right),
},
})
.unwrap();
let (join_expr, ctx) = self
.binder
.bind_table_reference(&mut self.bind_context, &cross_joins)?;
self.s_expr = join_expr;
self.bind_context = ctx;
Ok(self)
}
pub async fn union(mut self, dataframe: Dataframe) -> Result<Self> {
let (s_expr, bind_context) = self.binder.bind_union(
None,
None,
None,
&self.bind_context,
&dataframe.bind_context,
self.s_expr,
dataframe.s_expr,
false,
None,
)?;
self.s_expr = s_expr;
self.bind_context = bind_context;
Ok(self)
}
pub async fn union_distinct(mut self, dataframe: Dataframe) -> Result<Self> {
let (s_expr, bind_context) = self.binder.bind_union(
None,
None,
None,
&self.bind_context,
&dataframe.bind_context,
self.s_expr,
dataframe.s_expr,
true,
None,
)?;
self.s_expr = s_expr;
self.bind_context = bind_context;
Ok(self)
}
pub fn get_query_ctx(self) -> Arc<dyn TableContext> {
self.query_ctx.clone()
}
pub fn get_expr(&self) -> &SExpr {
&self.s_expr
}
}
fn parse_cols(schema: DataSchemaRef, columns: &[&str]) -> Result<Vec<SelectTarget>> {
for column in columns {
if schema.field_with_name(column).is_err() {
return Err(ErrorCode::UnknownColumn(format!(
"Unknown column: '{}'",
column
)));
}
}
Ok(columns
.iter()
.map(|c| SelectTarget::AliasedExpr {
expr: Box::new(Expr::ColumnRef {
span: None,
column: ColumnRef {
database: None,
table: None,
column: ColumnID::Name(Identifier::from_name_with_quoted(None, *c, Some('`'))),
},
}),
alias: None,
})
.collect())
}