-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
282 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// 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. | ||
|
||
pub mod column; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// 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 common_telemetry::debug; | ||
use datatypes::data_type::DataType; | ||
use snafu::{ensure, ResultExt}; | ||
use sqlx::database::HasArguments; | ||
use sqlx::{ColumnIndex, Database, Decode, Encode, Executor, IntoArguments, Type}; | ||
|
||
use crate::error::{self, Result}; | ||
use crate::ir::create_expr::ColumnOption; | ||
use crate::ir::{Column, Ident}; | ||
|
||
#[derive(Debug, sqlx::FromRow)] | ||
pub struct ColumnEntry { | ||
pub table_schema: String, | ||
pub table_name: String, | ||
pub column_name: String, | ||
pub data_type: String, | ||
pub semantic_type: String, | ||
pub column_default: Option<String>, | ||
pub is_nullable: String, | ||
} | ||
|
||
fn is_nullable(str: &str) -> bool { | ||
str.to_uppercase() == "YES" | ||
} | ||
|
||
impl PartialEq<Column> for ColumnEntry { | ||
fn eq(&self, other: &Column) -> bool { | ||
// Checks `table_name` | ||
if other.name.value != self.column_name { | ||
debug!( | ||
"expected name: {}, got: {}", | ||
other.name.value, self.column_name | ||
); | ||
return false; | ||
} | ||
// Checks `data_type` | ||
if other.column_type.name() != self.data_type { | ||
debug!( | ||
"expected column_type: {}, got: {}", | ||
other.column_type.name(), | ||
self.data_type | ||
); | ||
return false; | ||
} | ||
// Checks `column_default` | ||
match &self.column_default { | ||
Some(value) => { | ||
let default_value_opt = other.options.iter().find(|opt| { | ||
matches!( | ||
opt, | ||
ColumnOption::DefaultFn(_) | ColumnOption::DefaultValue(_) | ||
) | ||
}); | ||
if default_value_opt.is_none() { | ||
debug!("default value options is not found"); | ||
return false; | ||
} | ||
let default_value = match default_value_opt.unwrap() { | ||
ColumnOption::DefaultValue(v) => v.to_string(), | ||
ColumnOption::DefaultFn(f) => f.to_string(), | ||
_ => unreachable!(), | ||
}; | ||
if &default_value != value { | ||
debug!("expected default value: {default_value}, got: {value}"); | ||
return false; | ||
} | ||
} | ||
None => { | ||
if other.options.iter().any(|opt| { | ||
matches!( | ||
opt, | ||
ColumnOption::DefaultFn(_) | ColumnOption::DefaultValue(_) | ||
) | ||
}) { | ||
return false; | ||
} | ||
} | ||
}; | ||
// Checks `is_nullable` | ||
if is_nullable(&self.is_nullable) { | ||
// Null is the default value. Therefore, we only ensure there is no `ColumnOption::NotNull` option. | ||
if other | ||
.options | ||
.iter() | ||
.any(|opt| matches!(opt, ColumnOption::NotNull)) | ||
{ | ||
debug!("ColumnOption::NotNull is not found"); | ||
return false; | ||
} | ||
} else { | ||
// `ColumnOption::TimeIndex` imply means the field is not nullable. | ||
if !other | ||
.options | ||
.iter() | ||
.any(|opt| matches!(opt, ColumnOption::NotNull | ColumnOption::TimeIndex)) | ||
{ | ||
debug!("unexpected ColumnOption::NotNull or ColumnOption::TimeIndex"); | ||
return false; | ||
} | ||
} | ||
//TODO: Checks `semantic_type` | ||
|
||
true | ||
} | ||
} | ||
|
||
/// Asserts [&[ColumnEntry]] is equal to [&[Column]] | ||
pub fn assert_eq(fetched_columns: &[ColumnEntry], columns: &[Column]) -> Result<()> { | ||
ensure!( | ||
columns.len() == fetched_columns.len(), | ||
error::AssertSnafu { | ||
reason: format!( | ||
"Expected columns length: {}, got: {}", | ||
columns.len(), | ||
fetched_columns.len(), | ||
) | ||
} | ||
); | ||
|
||
for (idx, fetched) in fetched_columns.iter().enumerate() { | ||
ensure!( | ||
fetched == &columns[idx], | ||
error::AssertSnafu { | ||
reason: format!( | ||
"ColumnEntry {fetched:?} is not equal to Column {:?}", | ||
columns[idx] | ||
) | ||
} | ||
); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Returns all [ColumnEntry] of the `table_name` from `information_schema`. | ||
pub async fn fetch_columns<'a, DB, E>( | ||
e: E, | ||
schema_name: Ident, | ||
table_name: Ident, | ||
) -> Result<Vec<ColumnEntry>> | ||
where | ||
DB: Database, | ||
<DB as HasArguments<'a>>::Arguments: IntoArguments<'a, DB>, | ||
for<'c> E: 'a + Executor<'c, Database = DB>, | ||
for<'c> String: Decode<'c, DB> + Type<DB>, | ||
for<'c> String: Encode<'c, DB> + Type<DB>, | ||
for<'c> &'c str: ColumnIndex<<DB as Database>::Row>, | ||
{ | ||
let sql = "SELECT * FROM information_schema.columns WHERE table_schema = ? AND table_name = ?"; | ||
sqlx::query_as::<_, ColumnEntry>(sql) | ||
.bind(schema_name.value.to_string()) | ||
.bind(table_name.value.to_string()) | ||
.fetch_all(e) | ||
.await | ||
.context(error::ExecuteQuerySnafu { sql }) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use datatypes::data_type::ConcreteDataType; | ||
use datatypes::value::Value; | ||
|
||
use super::ColumnEntry; | ||
use crate::ir::create_expr::ColumnOption; | ||
use crate::ir::{Column, Ident}; | ||
|
||
#[test] | ||
fn test_column_eq() { | ||
let column_entry = ColumnEntry { | ||
table_schema: String::new(), | ||
table_name: "test".to_string(), | ||
column_name: String::new(), | ||
data_type: ConcreteDataType::int8_datatype().to_string(), | ||
semantic_type: String::new(), | ||
column_default: None, | ||
is_nullable: String::new(), | ||
}; | ||
// Naive | ||
let column = Column { | ||
name: Ident::new("test"), | ||
column_type: ConcreteDataType::int8_datatype(), | ||
options: vec![], | ||
}; | ||
assert!(column_entry == column); | ||
// With quote | ||
let column = Column { | ||
name: Ident::with_quote('\'', "test"), | ||
column_type: ConcreteDataType::int8_datatype(), | ||
options: vec![], | ||
}; | ||
assert!(column_entry == column); | ||
// With default value | ||
let column_entry = ColumnEntry { | ||
table_schema: String::new(), | ||
table_name: "test".to_string(), | ||
column_name: String::new(), | ||
data_type: ConcreteDataType::int8_datatype().to_string(), | ||
semantic_type: String::new(), | ||
column_default: Some("1".to_string()), | ||
is_nullable: String::new(), | ||
}; | ||
let column = Column { | ||
name: Ident::with_quote('\'', "test"), | ||
column_type: ConcreteDataType::int8_datatype(), | ||
options: vec![ColumnOption::DefaultValue(Value::from(1))], | ||
}; | ||
assert!(column_entry == column); | ||
// With default function | ||
let column_entry = ColumnEntry { | ||
table_schema: String::new(), | ||
table_name: "test".to_string(), | ||
column_name: String::new(), | ||
data_type: ConcreteDataType::int8_datatype().to_string(), | ||
semantic_type: String::new(), | ||
column_default: Some("Hello()".to_string()), | ||
is_nullable: String::new(), | ||
}; | ||
let column = Column { | ||
name: Ident::with_quote('\'', "test"), | ||
column_type: ConcreteDataType::int8_datatype(), | ||
options: vec![ColumnOption::DefaultFn("Hello()".to_string())], | ||
}; | ||
assert!(column_entry == column); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters