forked from databendlabs/databend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_operator.rs
More file actions
87 lines (80 loc) · 3.19 KB
/
Copy pathblock_operator.rs
File metadata and controls
87 lines (80 loc) · 3.19 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
// 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::BTreeSet;
use databend_common_catalog::plan::AggIndexMeta;
use databend_common_exception::Result;
use databend_common_expression::BlockMetaInfoDowncast;
use databend_common_expression::DataBlock;
use databend_common_expression::Evaluator;
use databend_common_expression::Expr;
use databend_common_expression::FieldIndex;
use databend_common_expression::FunctionContext;
use databend_common_functions::BUILTIN_FUNCTIONS;
/// `BlockOperator` takes a `DataBlock` as input and produces a `DataBlock` as output.
#[derive(Clone, Debug)]
pub enum BlockOperator {
/// Batch mode of map which merges map operators into one.
Map {
exprs: Vec<Expr>,
/// The index of the output columns, based on the exprs.
projections: Option<BTreeSet<usize>>,
},
/// Reorganize the input [`DataBlock`] with `projection`.
Project { projection: Vec<FieldIndex> },
}
impl BlockOperator {
pub fn execute(&self, func_ctx: &FunctionContext, mut input: DataBlock) -> Result<DataBlock> {
if input.is_empty() {
return Ok(input);
}
match self {
BlockOperator::Map { exprs, projections } => {
let num_evals = input
.get_meta()
.and_then(AggIndexMeta::downcast_ref_from)
.map(|a| a.num_evals);
if let Some(num_evals) = num_evals {
// It's from aggregating index.
match projections {
Some(projections) => {
Ok(input.project_with_agg_index(projections, num_evals))
}
None => Ok(input),
}
} else {
for expr in exprs {
let evaluator = Evaluator::new(&input, func_ctx, &BUILTIN_FUNCTIONS);
let result = evaluator.run(expr)?;
input.add_value(result, expr.data_type().clone());
}
match projections {
Some(projections) => Ok(input.project(projections)),
None => Ok(input),
}
}
}
BlockOperator::Project { projection } => {
let entries = projection
.iter()
.map(|i| input.get_by_offset(*i).clone())
.collect();
Ok(DataBlock::new_with_meta(
entries,
input.num_rows(),
input.take_meta(),
))
}
}
}
}