-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathcreate_view.rs
More file actions
97 lines (90 loc) · 3.45 KB
/
create_view.rs
File metadata and controls
97 lines (90 loc) · 3.45 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
// Copyright 2024 KipData/KiteSQL
//
// 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 crate::binder::{lower_case_name, lower_ident, Binder};
use crate::catalog::view::View;
use crate::catalog::{ColumnCatalog, ColumnRef};
use crate::errors::DatabaseError;
use crate::expression::{AliasType, ScalarExpression};
use crate::planner::operator::create_view::CreateViewOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
use itertools::Itertools;
use sqlparser::ast::{ObjectName, Query, ViewColumnDef};
use std::sync::Arc;
use ulid::Ulid;
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_create_view(
&mut self,
or_replace: &bool,
name: &ObjectName,
columns: &[ViewColumnDef],
query: &Query,
) -> Result<LogicalPlan, DatabaseError> {
fn projection_exprs(
view_name: &Arc<str>,
mapping_schema: &[ColumnRef],
column_names: impl Iterator<Item = String>,
) -> Vec<ScalarExpression> {
column_names
.enumerate()
.map(|(i, column_name)| {
let mapping_column = &mapping_schema[i];
let mut column = ColumnCatalog::new(
column_name,
mapping_column.nullable(),
mapping_column.desc().clone(),
);
column.set_ref_table(view_name.clone(), Ulid::new(), true);
ScalarExpression::Alias {
expr: Box::new(ScalarExpression::column_expr(mapping_column.clone())),
alias: AliasType::Expr(Box::new(ScalarExpression::column_expr(
ColumnRef::from(column),
))),
}
})
.collect_vec()
}
let view_name: Arc<str> = lower_case_name(name)?.into();
let mut plan = self.bind_query(query)?;
let mapping_schema = plan.output_schema();
let exprs: Vec<ScalarExpression> = if columns.is_empty() {
projection_exprs(
&view_name,
mapping_schema,
mapping_schema
.iter()
.map(|column| column.name().to_string()),
)
} else {
projection_exprs(
&view_name,
mapping_schema,
columns.iter().map(|column| lower_ident(&column.name)),
)
};
plan = self.bind_project(plan, exprs)?;
Ok(LogicalPlan::new(
Operator::CreateView(CreateViewOperator {
view: View {
name: view_name,
plan: Box::new(plan),
},
or_replace: *or_replace,
}),
Childrens::None,
))
}
}