forked from databendlabs/databend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_common.rs
More file actions
134 lines (126 loc) · 5.75 KB
/
Copy pathsql_common.rs
File metadata and controls
134 lines (126 loc) · 5.75 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
// 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::HashSet;
use common_datavalues::prelude::*;
use common_exception::ErrorCode;
use common_exception::Result;
use sqlparser::ast::DataType as SQLDataType;
pub struct SQLCommon;
impl SQLCommon {
/// Maps the SQL type to the corresponding Arrow `DataType`
pub fn make_data_type(sql_type: &SQLDataType) -> Result<DataTypeImpl> {
match sql_type {
SQLDataType::TinyInt(_) => Ok(i8::to_data_type()),
SQLDataType::UnsignedTinyInt(_) => Ok(u8::to_data_type()),
SQLDataType::SmallInt(_) => Ok(i16::to_data_type()),
SQLDataType::UnsignedSmallInt(_) => Ok(u16::to_data_type()),
SQLDataType::Int(_) => Ok(i32::to_data_type()),
SQLDataType::UnsignedInt(_) => Ok(u32::to_data_type()),
SQLDataType::BigInt(_) => Ok(i64::to_data_type()),
SQLDataType::UnsignedBigInt(_) => Ok(u64::to_data_type()),
SQLDataType::Char(_)
| SQLDataType::Varchar(_)
| SQLDataType::String
| SQLDataType::Text => Ok(Vu8::to_data_type()),
SQLDataType::Float(_) => Ok(f32::to_data_type()),
SQLDataType::Decimal(_, _) => Ok(f64::to_data_type()),
SQLDataType::Real | SQLDataType::Double => Ok(f64::to_data_type()),
SQLDataType::Boolean => Ok(bool::to_data_type()),
SQLDataType::Date => Ok(DateType::new_impl()),
// default precision is 6, microseconds
SQLDataType::Timestamp(None) | SQLDataType::DateTime(None) => {
Ok(TimestampType::new_impl(6))
}
SQLDataType::Timestamp(Some(precision)) => {
if *precision <= 6 {
Ok(TimestampType::new_impl(*precision as usize))
} else {
Err(ErrorCode::IllegalDataType(format!(
"The SQL data type TIMESTAMP(n), n only ranges from 0~6, {} is invalid",
precision
)))
}
}
SQLDataType::DateTime(Some(precision)) => {
if *precision <= 6 {
Ok(TimestampType::new_impl(*precision as usize))
} else {
Err(ErrorCode::IllegalDataType(format!(
"The SQL data type DATETIME(n), n only ranges from 0~6, {} is invalid",
precision
)))
}
}
SQLDataType::Array(sql_type, nullable) => {
let inner_data_type = Self::make_data_type(sql_type)?;
if *nullable {
if inner_data_type.is_null() {
return Result::Err(ErrorCode::IllegalDataType(
"The SQL data type ARRAY(NULL, NULL) is invalid",
));
}
Ok(ArrayType::new_impl(NullableType::new_impl(inner_data_type)))
} else {
Ok(ArrayType::new_impl(inner_data_type))
}
}
SQLDataType::Tuple(names, sql_types) => {
let mut inner_data_types = Vec::with_capacity(sql_types.len());
for sql_type in sql_types {
let inner_data_type = Self::make_data_type(sql_type)?;
inner_data_types.push(inner_data_type);
}
match names {
Some(names) => {
let mut names_set = HashSet::with_capacity(names.len());
for name in names.iter() {
if !names_set.insert(name.value.clone()) {
return Result::Err(ErrorCode::IllegalDataType(
"The names of tuple elements must be unique",
));
}
}
let inner_names = names.iter().map(|v| v.value.clone()).collect::<Vec<_>>();
Ok(StructType::new_impl(Some(inner_names), inner_data_types))
}
None => Ok(StructType::new_impl(None, inner_data_types)),
}
}
// Custom types for databend:
// Custom(ObjectName([Ident { value: "uint8", quote_style: None }])
SQLDataType::Custom(obj) if !obj.0.is_empty() => {
match obj.0[0].value.to_uppercase().as_str() {
"SIGNED" => Ok(i64::to_data_type()),
"UNSIGNED" => Ok(u64::to_data_type()),
name => {
let factory = TypeFactory::instance();
let data_type = factory.get(name)?;
Ok(data_type)
}
}
}
_ => Result::Err(ErrorCode::IllegalDataType(format!(
"The SQL data type {sql_type:?} is not implemented",
))),
}
}
pub fn short_sql(query: &str) -> String {
let query = query.trim_start();
if query.len() >= 64 && query[..6].eq_ignore_ascii_case("INSERT") {
format!("{}...", &query[..64])
} else {
query.to_string()
}
}
}