Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Dictionary::GLUE_OBJECTS to serve OBJECT_TYPE, CREATED metadata #924

Merged
merged 15 commits into from
Oct 27, 2022
Merged
10 changes: 7 additions & 3 deletions cli/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ mod tests {
crate::command::{SetOption, ShowOption},
gluesql_core::{
ast::{BinaryOperator, Expr},
chrono::Utc,
data::{SchemaIndex, SchemaIndexOrd},
},
};
Expand Down Expand Up @@ -431,12 +432,14 @@ mod tests {
SchemaIndex {
name: "id_ndx".to_owned(),
order: SchemaIndexOrd::Asc,
expr: Expr::Identifier("id".to_owned())
expr: Expr::Identifier("id".to_owned()),
created: Utc::now().naive_utc(),
},
SchemaIndex {
name: "name_ndx".to_owned(),
order: SchemaIndexOrd::Desc,
expr: Expr::Identifier("name".to_owned())
expr: Expr::Identifier("name".to_owned()),
created: Utc::now().naive_utc(),
},
SchemaIndex {
name: "expr_ndx".to_owned(),
Expand All @@ -445,7 +448,8 @@ mod tests {
left: Box::new(Expr::Identifier("expr1".to_owned())),
op: BinaryOperator::Minus,
right: Box::new(Expr::Identifier("expr2".to_owned()))
}
},
created: Utc::now().naive_utc(),
}
],),
"
Expand Down
1 change: 1 addition & 0 deletions core/src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub enum Dictionary {
GlueTables,
GlueTableColumns,
GlueIndexes,
GlueObjects,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down
3 changes: 3 additions & 0 deletions core/src/data/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
crate::ast::{ColumnDef, ColumnOption, ColumnOptionDef, Expr},
chrono::NaiveDateTime,
serde::{Deserialize, Serialize},
std::fmt::Debug,
strum_macros::Display,
Expand All @@ -18,13 +19,15 @@ pub struct SchemaIndex {
pub name: String,
pub expr: Expr,
pub order: SchemaIndexOrd,
pub created: NaiveDateTime,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Schema {
pub table_name: String,
pub column_defs: Vec<ColumnDef>,
pub indexes: Vec<SchemaIndex>,
pub created: NaiveDateTime,
}

pub trait ColumnDefExt {
Expand Down
2 changes: 2 additions & 0 deletions core/src/executor/alter/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use {
result::{Error, IntoControlFlow, MutResult, Result, TrySelf},
store::{GStore, GStoreMut},
},
chrono::Utc,
futures::stream::{self, TryStreamExt},
std::{
iter,
Expand Down Expand Up @@ -110,6 +111,7 @@ pub async fn create_table<T: GStore + GStoreMut>(
table_name: target_table_name.to_owned(),
column_defs: target_columns_defs,
indexes: vec![],
created: Utc::now().naive_utc(),
};

for column_def in &schema.column_defs {
Expand Down
29 changes: 28 additions & 1 deletion core/src/executor/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,34 @@ pub async fn fetch_relation_rows<'a>(
TableFactor::Dictionary { dict, .. } => {
let rows = {
#[derive(Iterator)]
enum Rows<I1, I2, I3> {
enum Rows<I1, I2, I3, I4> {
Tables(I1),
TableColumns(I2),
Indexes(I3),
Objects(I4),
}
match dict {
Dictionary::GlueObjects => {
let schemas = storage.fetch_all_schemas().await?;
let rows = schemas.into_iter().flat_map(|schema| {
let table_rows = vec![Ok(Row(vec![
Value::Str(schema.table_name),
Value::Str("TABLE".to_owned()),
Value::Timestamp(schema.created),
]))];
let index_rows = schema.indexes.into_iter().map(|index| {
Ok(Row(vec![
Value::Str(index.name.clone()),
Value::Str("INDEX".to_owned()),
Value::Timestamp(index.created),
]))
});

table_rows.into_iter().chain(index_rows)
});

Rows::Objects(rows)
}
Dictionary::GlueTables => {
let schemas = storage.fetch_all_schemas().await?;
let rows = schemas
Expand Down Expand Up @@ -263,6 +285,11 @@ pub async fn fetch_relation_columns(
TableFactor::Table { name, .. } => fetch_columns(storage, name).await,
TableFactor::Series { .. } => Ok(vec!["N".to_owned()]),
TableFactor::Dictionary { dict, .. } => match dict {
Dictionary::GlueObjects => Ok(vec![
"OBJECT_NAME".to_owned(),
"OBJECT_TYPE".to_owned(),
"CREATED".to_owned(),
]),
Dictionary::GlueTables => Ok(vec!["TABLE_NAME".to_owned()]),
Dictionary::GlueTableColumns => Ok(vec![
"TABLE_NAME".to_owned(),
Expand Down
4 changes: 4 additions & 0 deletions core/src/translate/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ fn translate_table_factor(sql_table_factor: &SqlTableFactor) -> Result<TableFact
alias: alias_or_name,
size: translate_table_args(args)?,
}),
"GLUE_OBJECTS" => Ok(TableFactor::Dictionary {
dict: Dictionary::GlueObjects,
alias: alias_or_name,
}),
"GLUE_TABLES" => Ok(TableFactor::Dictionary {
dict: Dictionary::GlueTables,
alias: alias_or_name,
Expand Down
10 changes: 10 additions & 0 deletions storages/sled-storage/src/alter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl AlterTable for SledStorage {
let Schema {
column_defs,
indexes,
created,
..
} = old_schema
.ok_or_else(|| AlterTableError::TableNotFound(table_name.to_owned()).into())
Expand All @@ -62,6 +63,7 @@ impl AlterTable for SledStorage {
table_name: new_table_name.to_owned(),
column_defs,
indexes,
created,
};

bincode::serialize(&old_snapshot)
Expand Down Expand Up @@ -158,6 +160,7 @@ impl AlterTable for SledStorage {
let Schema {
column_defs,
indexes,
created,
..
} = snapshot
.get(txid, None)
Expand Down Expand Up @@ -185,6 +188,7 @@ impl AlterTable for SledStorage {
table_name: table_name.to_owned(),
column_defs,
indexes,
created,
};
let (snapshot, _) = snapshot.update(txid, schema);
let value = bincode::serialize(&snapshot)
Expand Down Expand Up @@ -235,6 +239,8 @@ impl AlterTable for SledStorage {
table_name,
column_defs,
indexes,
created,
..
} = schema_snapshot
.get(txid, None)
.ok_or_else(|| AlterTableError::TableNotFound(table_name.to_owned()).into())
Expand Down Expand Up @@ -308,6 +314,7 @@ impl AlterTable for SledStorage {
table_name,
column_defs,
indexes,
created,
};
let (schema_snapshot, _) = schema_snapshot.update(txid, schema);
let schema_value = bincode::serialize(&schema_snapshot)
Expand Down Expand Up @@ -362,6 +369,8 @@ impl AlterTable for SledStorage {
table_name,
column_defs,
indexes,
created,
..
} = schema_snapshot
.get(txid, None)
.ok_or_else(|| AlterTableError::TableNotFound(table_name.to_owned()).into())
Expand Down Expand Up @@ -428,6 +437,7 @@ impl AlterTable for SledStorage {
table_name,
column_defs,
indexes,
created,
};
let (schema_snapshot, _) = schema_snapshot.update(txid, schema);
let schema_value = bincode::serialize(&schema_snapshot)
Expand Down
6 changes: 6 additions & 0 deletions storages/sled-storage/src/index_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use {
async_trait::async_trait,
gluesql_core::{
ast::OrderByExpr,
chrono::Utc,
data::{Schema, SchemaIndex, SchemaIndexOrd},
result::{Error, MutResult, Result, TrySelf},
store::{IndexError, IndexMut, Store},
Expand Down Expand Up @@ -67,6 +68,7 @@ impl IndexMut for SledStorage {
let Schema {
column_defs,
indexes,
created,
..
} = schema
.ok_or_else(|| IndexError::ConflictTableNotFound(table_name.to_owned()).into())
Expand All @@ -81,6 +83,7 @@ impl IndexMut for SledStorage {
name: index_name.to_owned(),
expr: index_expr.clone(),
order: SchemaIndexOrd::Both,
created: Utc::now().naive_utc(),
};

let indexes = indexes
Expand All @@ -92,6 +95,7 @@ impl IndexMut for SledStorage {
table_name: table_name.to_owned(),
column_defs,
indexes,
created,
};

let index_sync = IndexSync::from_schema(tree, txid, &schema);
Expand Down Expand Up @@ -144,6 +148,7 @@ impl IndexMut for SledStorage {
let Schema {
column_defs,
indexes,
created,
..
} = schema
.ok_or_else(|| IndexError::ConflictTableNotFound(table_name.to_owned()).into())
Expand All @@ -165,6 +170,7 @@ impl IndexMut for SledStorage {
table_name: table_name.to_owned(),
column_defs,
indexes,
created,
};

let index_sync = IndexSync::from_schema(tree, txid, &schema);
Expand Down
12 changes: 12 additions & 0 deletions test-suite/src/dictionary_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ test_case!(ditionary_index, async move {
"CREATE INDEX Primary ON Foo (id)",
Err(TranslateError::ReservedIndexName("Primary".to_owned()).into()),
),
(
"SELECT OBJECT_NAME, OBJECT_TYPE FROM GLUE_OBJECTS WHERE CREATED > NOW() - INTERVAL 1 MINUTE",
Ok(select!(
OBJECT_NAME | OBJECT_TYPE ;
Str | Str ;
"Bar".to_owned() "TABLE".to_owned();
"Bar_name_concat".to_owned() "INDEX".to_owned();
"Foo".to_owned() "TABLE".to_owned();
"Foo_id".to_owned() "INDEX".to_owned();
"Foo_id_2".to_owned() "INDEX".to_owned()
))
),
];
for (sql, expected) in test_cases {
test!(sql, expected);
Expand Down
14 changes: 14 additions & 0 deletions test-suite/src/index/basic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
crate::*,
chrono::Utc,
gluesql_core::{
ast::IndexOperator::*,
executor::AlterError,
Expand Down Expand Up @@ -320,4 +321,17 @@ CREATE TABLE Test (
"DROP INDEX Test.idx_aaa",
Err(IndexError::IndexNameDoesNotExist("idx_aaa".to_owned()).into())
);

let today = Utc::now().naive_utc().format("%Y-%m-%d");

test!(
"SELECT OBJECT_NAME, OBJECT_TYPE, FORMAT(CREATED, \"%Y-%m-%d\") CREATED_DATE FROM GLUE_OBJECTS",
Ok(select!(
OBJECT_NAME | OBJECT_TYPE | CREATED_DATE;
Str | Str | Str;
"Test".to_owned() "TABLE".to_owned() today.to_string();
"idx_id".to_owned() "INDEX".to_owned() today.to_string();
"idx_name".to_owned() "INDEX".to_owned() today.to_string()
))
);
});
33 changes: 1 addition & 32 deletions test-suite/src/index/showindexes.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use {
crate::*,
gluesql_core::{
ast::{BinaryOperator, Expr},
data::{SchemaIndex, SchemaIndexOrd},
executor::ExecuteError,
prelude::Payload,
},
gluesql_core::{executor::ExecuteError, prelude::Payload},
};

test_case!(showindexes, async move {
Expand Down Expand Up @@ -39,32 +34,6 @@ CREATE TABLE Test (
"CREATE INDEX idx_id2 ON Test (id + num)",
Ok(Payload::CreateIndex)
);

test!(
"show indexes from Test",
Ok(Payload::ShowIndexes(vec![
SchemaIndex {
name: "idx_id".to_owned(),
order: SchemaIndexOrd::Both,
expr: Expr::Identifier("id".to_owned())
},
SchemaIndex {
name: "idx_name".to_owned(),
order: SchemaIndexOrd::Both,
expr: Expr::Identifier("name".to_owned())
},
SchemaIndex {
name: "idx_id2".to_owned(),
order: SchemaIndexOrd::Both,
expr: Expr::BinaryOp {
left: Box::new(Expr::Identifier("id".to_owned())),
op: BinaryOperator::Plus,
right: Box::new(Expr::Identifier("num".to_owned()))
}
}
]))
);
panarch marked this conversation as resolved.
Show resolved Hide resolved

test!(
"show indexes from NoTable",
Err(ExecuteError::TableNotFound("NoTable".to_owned()).into())
Expand Down