From fbd019779401e1f6350c34f026665d87cb4188be Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Wed, 17 Jan 2024 12:18:37 +0800 Subject: [PATCH] refactor: remove TableEngine (#3181) * refactor: remove TableEngine Signed-off-by: Ruihang Xia * Update src/table/src/table_reference.rs Co-authored-by: LFC <990479+MichaelScofield@users.noreply.github.com> --------- Signed-off-by: Ruihang Xia Co-authored-by: LFC <990479+MichaelScofield@users.noreply.github.com> --- src/catalog/src/kvbackend.rs | 2 - src/catalog/src/kvbackend/mock.rs | 128 ----------- src/common/grpc-expr/src/insert.rs | 2 +- src/common/grpc-expr/src/util.rs | 2 +- src/common/meta/src/ddl/alter_table.rs | 2 +- src/common/meta/src/ddl/create_table.rs | 2 +- src/common/meta/src/ddl/drop_table.rs | 2 +- src/common/meta/src/ddl/truncate_table.rs | 2 +- src/common/meta/src/key/table_info.rs | 2 +- src/common/meta/src/rpc/ddl.rs | 2 +- src/common/meta/src/table_name.rs | 2 +- src/operator/src/expr_factory.rs | 2 +- src/operator/src/insert.rs | 2 +- src/operator/src/statement.rs | 2 +- src/operator/src/statement/copy_table_from.rs | 2 +- src/operator/src/statement/copy_table_to.rs | 2 +- src/table/src/engine.rs | 204 ------------------ src/table/src/error.rs | 10 +- src/table/src/lib.rs | 2 +- src/table/src/requests.rs | 2 +- src/table/src/table_reference.rs | 73 +++++++ src/table/src/test_util.rs | 2 - src/table/src/test_util/mock_engine.rs | 149 ------------- 23 files changed, 90 insertions(+), 510 deletions(-) delete mode 100644 src/catalog/src/kvbackend/mock.rs delete mode 100644 src/table/src/engine.rs create mode 100644 src/table/src/table_reference.rs delete mode 100644 src/table/src/test_util/mock_engine.rs diff --git a/src/catalog/src/kvbackend.rs b/src/catalog/src/kvbackend.rs index f899586e7ee6..e26e06e80ed0 100644 --- a/src/catalog/src/kvbackend.rs +++ b/src/catalog/src/kvbackend.rs @@ -17,6 +17,4 @@ pub use client::{CachedMetaKvBackend, MetaKvBackend}; mod client; mod manager; -#[cfg(feature = "testing")] -pub mod mock; pub use manager::KvBackendCatalogManager; diff --git a/src/catalog/src/kvbackend/mock.rs b/src/catalog/src/kvbackend/mock.rs deleted file mode 100644 index 4e88f54ce4e2..000000000000 --- a/src/catalog/src/kvbackend/mock.rs +++ /dev/null @@ -1,128 +0,0 @@ -// 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 std::collections::HashMap; -use std::sync::{Arc, RwLock as StdRwLock}; - -use common_recordbatch::RecordBatch; -use datatypes::data_type::ConcreteDataType; -use datatypes::schema::{ColumnSchema, Schema}; -use datatypes::vectors::StringVector; -use table::engine::{CloseTableResult, EngineContext, TableEngine}; -use table::metadata::TableId; -use table::requests::{ - AlterTableRequest, CloseTableRequest, CreateTableRequest, DropTableRequest, OpenTableRequest, - TruncateTableRequest, -}; -use table::test_util::MemTable; -use table::TableRef; - -#[derive(Default)] -pub struct MockTableEngine { - tables: StdRwLock>, -} - -#[async_trait::async_trait] -impl TableEngine for MockTableEngine { - fn name(&self) -> &str { - "MockTableEngine" - } - - /// Create a table with only one column - async fn create_table( - &self, - _ctx: &EngineContext, - request: CreateTableRequest, - ) -> table::Result { - let table_id = request.id; - - let schema = Arc::new(Schema::new(vec![ColumnSchema::new( - "name", - ConcreteDataType::string_datatype(), - true, - )])); - - let data = vec![Arc::new(StringVector::from(vec!["a", "b", "c"])) as _]; - let record_batch = RecordBatch::new(schema, data).unwrap(); - let table = MemTable::new_with_catalog( - &request.table_name, - record_batch, - table_id, - request.catalog_name, - request.schema_name, - vec![0], - ); - - let mut tables = self.tables.write().unwrap(); - let _ = tables.insert(table_id, table.clone() as TableRef); - Ok(table) - } - - async fn open_table( - &self, - _ctx: &EngineContext, - request: OpenTableRequest, - ) -> table::Result> { - Ok(self.tables.read().unwrap().get(&request.table_id).cloned()) - } - - async fn alter_table( - &self, - _ctx: &EngineContext, - _request: AlterTableRequest, - ) -> table::Result { - unimplemented!() - } - - fn get_table( - &self, - _ctx: &EngineContext, - table_id: TableId, - ) -> table::Result> { - Ok(self.tables.read().unwrap().get(&table_id).cloned()) - } - - fn table_exists(&self, _ctx: &EngineContext, table_id: TableId) -> bool { - self.tables.read().unwrap().contains_key(&table_id) - } - - async fn drop_table( - &self, - _ctx: &EngineContext, - _request: DropTableRequest, - ) -> table::Result { - unimplemented!() - } - - async fn close_table( - &self, - _ctx: &EngineContext, - request: CloseTableRequest, - ) -> table::Result { - let _ = self.tables.write().unwrap().remove(&request.table_id); - Ok(CloseTableResult::Released(vec![])) - } - - async fn close(&self) -> table::Result<()> { - Ok(()) - } - - async fn truncate_table( - &self, - _ctx: &EngineContext, - _request: TruncateTableRequest, - ) -> table::Result { - Ok(true) - } -} diff --git a/src/common/grpc-expr/src/insert.rs b/src/common/grpc-expr/src/insert.rs index d504b5092be7..3bb731367005 100644 --- a/src/common/grpc-expr/src/insert.rs +++ b/src/common/grpc-expr/src/insert.rs @@ -20,8 +20,8 @@ use datatypes::data_type::{ConcreteDataType, DataType}; use datatypes::prelude::VectorRef; use datatypes::schema::SchemaRef; use snafu::{ensure, ResultExt}; -use table::engine::TableReference; use table::metadata::TableId; +use table::table_reference::TableReference; use crate::error::{CreateVectorSnafu, Result, UnexpectedValuesLengthSnafu}; use crate::util; diff --git a/src/common/grpc-expr/src/util.rs b/src/common/grpc-expr/src/util.rs index 74b9b358ae0e..3d6c65df9004 100644 --- a/src/common/grpc-expr/src/util.rs +++ b/src/common/grpc-expr/src/util.rs @@ -20,8 +20,8 @@ use api::v1::{ }; use datatypes::schema::Schema; use snafu::{ensure, OptionExt}; -use table::engine::TableReference; use table::metadata::TableId; +use table::table_reference::TableReference; use crate::error::{ DuplicatedColumnNameSnafu, DuplicatedTimestampColumnSnafu, MissingTimestampColumnSnafu, Result, diff --git a/src/common/meta/src/ddl/alter_table.rs b/src/common/meta/src/ddl/alter_table.rs index aa9e505449e8..3a110ea52315 100644 --- a/src/common/meta/src/ddl/alter_table.rs +++ b/src/common/meta/src/ddl/alter_table.rs @@ -33,9 +33,9 @@ use serde::{Deserialize, Serialize}; use snafu::{ensure, OptionExt, ResultExt}; use store_api::storage::{ColumnId, RegionId}; use strum::AsRefStr; -use table::engine::TableReference; use table::metadata::{RawTableInfo, TableId, TableInfo}; use table::requests::AlterKind; +use table::table_reference::TableReference; use crate::cache_invalidator::Context; use crate::ddl::utils::handle_operate_region_error; diff --git a/src/common/meta/src/ddl/create_table.rs b/src/common/meta/src/ddl/create_table.rs index 11d63bebd5e5..a846ee46185a 100644 --- a/src/common/meta/src/ddl/create_table.rs +++ b/src/common/meta/src/ddl/create_table.rs @@ -33,8 +33,8 @@ use snafu::{ensure, OptionExt, ResultExt}; use store_api::metric_engine_consts::LOGICAL_TABLE_METADATA_KEY; use store_api::storage::{RegionId, RegionNumber}; use strum::AsRefStr; -use table::engine::TableReference; use table::metadata::{RawTableInfo, TableId}; +use table::table_reference::TableReference; use crate::ddl::utils::{handle_operate_region_error, handle_retry_error, region_storage_path}; use crate::ddl::DdlContext; diff --git a/src/common/meta/src/ddl/drop_table.rs b/src/common/meta/src/ddl/drop_table.rs index db9974df20c8..b1045abdea2c 100644 --- a/src/common/meta/src/ddl/drop_table.rs +++ b/src/common/meta/src/ddl/drop_table.rs @@ -29,8 +29,8 @@ use serde::{Deserialize, Serialize}; use snafu::{ensure, OptionExt, ResultExt}; use store_api::storage::RegionId; use strum::AsRefStr; -use table::engine::TableReference; use table::metadata::{RawTableInfo, TableId}; +use table::table_reference::TableReference; use super::utils::handle_retry_error; use crate::cache_invalidator::Context; diff --git a/src/common/meta/src/ddl/truncate_table.rs b/src/common/meta/src/ddl/truncate_table.rs index d0782f8dea7c..973c15196577 100644 --- a/src/common/meta/src/ddl/truncate_table.rs +++ b/src/common/meta/src/ddl/truncate_table.rs @@ -27,8 +27,8 @@ use serde::{Deserialize, Serialize}; use snafu::{ensure, ResultExt}; use store_api::storage::RegionId; use strum::AsRefStr; -use table::engine::TableReference; use table::metadata::{RawTableInfo, TableId}; +use table::table_reference::TableReference; use super::utils::handle_retry_error; use crate::ddl::utils::handle_operate_region_error; diff --git a/src/common/meta/src/key/table_info.rs b/src/common/meta/src/key/table_info.rs index 5415a0f1f941..80531669bfd7 100644 --- a/src/common/meta/src/key/table_info.rs +++ b/src/common/meta/src/key/table_info.rs @@ -15,8 +15,8 @@ use std::collections::HashMap; use serde::{Deserialize, Serialize}; -use table::engine::TableReference; use table::metadata::{RawTableInfo, TableId}; +use table::table_reference::TableReference; use super::{DeserializedValueWithBytes, TableMetaValue, TABLE_INFO_KEY_PREFIX}; use crate::error::Result; diff --git a/src/common/meta/src/rpc/ddl.rs b/src/common/meta/src/rpc/ddl.rs index 935afac311e0..2cc7ff8a0c43 100644 --- a/src/common/meta/src/rpc/ddl.rs +++ b/src/common/meta/src/rpc/ddl.rs @@ -26,8 +26,8 @@ use base64::Engine as _; use prost::Message; use serde::{Deserialize, Serialize}; use snafu::{OptionExt, ResultExt}; -use table::engine::TableReference; use table::metadata::{RawTableInfo, TableId}; +use table::table_reference::TableReference; use crate::error::{self, Result}; use crate::table_name::TableName; diff --git a/src/common/meta/src/table_name.rs b/src/common/meta/src/table_name.rs index bfb5fb3cece5..62615e5c211b 100644 --- a/src/common/meta/src/table_name.rs +++ b/src/common/meta/src/table_name.rs @@ -16,7 +16,7 @@ use std::fmt::{Display, Formatter}; use api::v1::meta::TableName as PbTableName; use serde::{Deserialize, Serialize}; -use table::engine::TableReference; +use table::table_reference::TableReference; #[derive(Debug, Clone, Hash, Eq, PartialEq, Deserialize, Serialize)] pub struct TableName { diff --git a/src/operator/src/expr_factory.rs b/src/operator/src/expr_factory.rs index 0f1d5faeb72a..55f66c30076a 100644 --- a/src/operator/src/expr_factory.rs +++ b/src/operator/src/expr_factory.rs @@ -35,8 +35,8 @@ use sql::statements::alter::{AlterTable, AlterTableOperation}; use sql::statements::create::{CreateExternalTable, CreateTable, TIME_INDEX}; use sql::statements::{column_def_to_schema, sql_column_def_to_grpc_column_def}; use sql::util::to_lowercase_options_map; -use table::engine::TableReference; use table::requests::{TableOptions, FILE_TABLE_META_KEY}; +use table::table_reference::TableReference; use crate::error::{ BuildCreateExprOnInsertionSnafu, ColumnDataTypeSnafu, ConvertColumnDefaultConstraintSnafu, diff --git a/src/operator/src/insert.rs b/src/operator/src/insert.rs index a17839ed64ae..30b732a5d2d9 100644 --- a/src/operator/src/insert.rs +++ b/src/operator/src/insert.rs @@ -35,8 +35,8 @@ use partition::manager::PartitionRuleManagerRef; use session::context::QueryContextRef; use snafu::prelude::*; use sql::statements::insert::Insert; -use table::engine::TableReference; use table::requests::InsertRequest as TableInsertRequest; +use table::table_reference::TableReference; use table::TableRef; use crate::error::{ diff --git a/src/operator/src/statement.rs b/src/operator/src/statement.rs index 216942521a32..84efcd13addd 100644 --- a/src/operator/src/statement.rs +++ b/src/operator/src/statement.rs @@ -46,8 +46,8 @@ use sql::statements::statement::Statement; use sql::statements::OptionMap; use sql::util::format_raw_object_name; use sqlparser::ast::{Expr, ObjectName, Value}; -use table::engine::TableReference; use table::requests::{CopyDatabaseRequest, CopyDirection, CopyTableRequest}; +use table::table_reference::TableReference; use table::TableRef; use crate::error::{ diff --git a/src/operator/src/statement/copy_table_from.rs b/src/operator/src/statement/copy_table_from.rs index a6944e8938ee..2aedca15a4fc 100644 --- a/src/operator/src/statement/copy_table_from.rs +++ b/src/operator/src/statement/copy_table_from.rs @@ -42,8 +42,8 @@ use object_store::{Entry, EntryMode, ObjectStore}; use regex::Regex; use session::context::QueryContextRef; use snafu::ResultExt; -use table::engine::TableReference; use table::requests::{CopyTableRequest, InsertRequest}; +use table::table_reference::TableReference; use tokio::io::BufReader; use crate::error::{self, IntoVectorsSnafu, Result}; diff --git a/src/operator/src/statement/copy_table_to.rs b/src/operator/src/statement/copy_table_to.rs index 78c192ad01af..d0a015e0a765 100644 --- a/src/operator/src/statement/copy_table_to.rs +++ b/src/operator/src/statement/copy_table_to.rs @@ -32,9 +32,9 @@ use object_store::ObjectStore; use query::plan::LogicalPlan; use session::context::QueryContextRef; use snafu::{OptionExt, ResultExt}; -use table::engine::TableReference; use table::requests::CopyTableRequest; use table::table::adapter::DfTableProviderAdapter; +use table::table_reference::TableReference; use crate::error::{self, BuildDfLogicalPlanSnafu, ExecLogicalPlanSnafu, Result}; use crate::statement::StatementExecutor; diff --git a/src/table/src/engine.rs b/src/table/src/engine.rs deleted file mode 100644 index ccb56d874d28..000000000000 --- a/src/table/src/engine.rs +++ /dev/null @@ -1,204 +0,0 @@ -// 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 std::fmt::{self, Display}; -use std::sync::Arc; - -use common_procedure::BoxedProcedure; -use datafusion_common::TableReference as DfTableReference; -use store_api::storage::RegionNumber; - -use crate::error::{self, Result}; -use crate::metadata::TableId; -use crate::requests::{ - AlterTableRequest, CloseTableRequest, CreateTableRequest, DropTableRequest, OpenTableRequest, - TruncateTableRequest, -}; -use crate::TableRef; - -/// Represents a resolved path to a table of the form “catalog.schema.table” -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct TableReference<'a> { - pub catalog: &'a str, - pub schema: &'a str, - pub table: &'a str, -} - -pub type OwnedTableReference = TableReference<'static>; - -// TODO(LFC): Find a better place for `TableReference`, -// so that we can reuse the default catalog and schema consts. -// Could be done together with issue #559. -impl<'a> TableReference<'a> { - pub fn bare(table: &'a str) -> Self { - TableReference { - catalog: "greptime", - schema: "public", - table, - } - } - - pub fn full(catalog: &'a str, schema: &'a str, table: &'a str) -> Self { - TableReference { - catalog, - schema, - table, - } - } -} - -impl<'a> Display for TableReference<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}.{}.{}", self.catalog, self.schema, self.table) - } -} - -impl<'a> From> for DfTableReference<'a> { - fn from(val: TableReference<'a>) -> Self { - DfTableReference::full(val.catalog, val.schema, val.table) - } -} - -/// CloseTableResult -/// -/// Returns [`CloseTableResult::Released`] and closed region numbers if a table was removed -/// from the engine. -/// Returns [`CloseTableResult::PartialClosed`] and closed region numbers if only partial -/// regions were closed. -#[derive(Debug)] -pub enum CloseTableResult { - Released(Vec), - PartialClosed(Vec), - NotFound, -} - -/// Table engine abstraction. -#[async_trait::async_trait] -pub trait TableEngine: Send + Sync { - /// Return engine name - fn name(&self) -> &str; - - /// Create a table by given request. - /// - /// Return the created table. - async fn create_table( - &self, - ctx: &EngineContext, - request: CreateTableRequest, - ) -> Result; - - /// Open an existing table by given `request`, returns the opened table. If the table does not - /// exist, returns an `Ok(None)`. - async fn open_table( - &self, - ctx: &EngineContext, - request: OpenTableRequest, - ) -> Result>; - - /// Alter table schema, options etc. by given request, - /// - /// Returns the table after altered. - async fn alter_table( - &self, - ctx: &EngineContext, - request: AlterTableRequest, - ) -> Result; - - /// Returns the table by it's name. - fn get_table(&self, ctx: &EngineContext, table_id: TableId) -> Result>; - - /// Returns true when the given table is exists. - fn table_exists(&self, ctx: &EngineContext, table_id: TableId) -> bool; - - /// Drops the given table. Return true if the table is dropped, or false if the table doesn't exist. - async fn drop_table(&self, ctx: &EngineContext, request: DropTableRequest) -> Result; - - /// Closes the (partial) given table. - /// - /// Removes a table from the engine if all regions are closed. - async fn close_table( - &self, - _ctx: &EngineContext, - _request: CloseTableRequest, - ) -> Result { - error::UnsupportedSnafu { - operation: "close_table", - } - .fail()? - } - - /// Close the engine. - async fn close(&self) -> Result<()>; - - async fn truncate_table( - &self, - _ctx: &EngineContext, - _request: TruncateTableRequest, - ) -> Result; -} - -pub type TableEngineRef = Arc; - -/// Table engine context. -#[derive(Debug, Clone, Default)] -pub struct EngineContext {} - -/// Procedures for table engine. -pub trait TableEngineProcedure: Send + Sync { - /// Returns a procedure that creates a table by specific `request`. - fn create_table_procedure( - &self, - ctx: &EngineContext, - request: CreateTableRequest, - ) -> Result; - - /// Returns a procedure that alters a table by specific `request`. - fn alter_table_procedure( - &self, - ctx: &EngineContext, - request: AlterTableRequest, - ) -> Result; - - /// Returns a procedure that drops a table by specific `request`. - fn drop_table_procedure( - &self, - ctx: &EngineContext, - request: DropTableRequest, - ) -> Result; - - /// Returns a procedure that truncates a table by specific `request`. - fn truncate_table_procedure( - &self, - ctx: &EngineContext, - request: TruncateTableRequest, - ) -> Result; -} - -pub type TableEngineProcedureRef = Arc; - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_table_reference() { - let table_ref = TableReference { - catalog: "greptime", - schema: "public", - table: "test", - }; - - assert_eq!("greptime.public.test", table_ref.to_string()); - } -} diff --git a/src/table/src/error.rs b/src/table/src/error.rs index 5d034d09e2c3..b67dbac5e2f5 100644 --- a/src/table/src/error.rs +++ b/src/table/src/error.rs @@ -43,12 +43,6 @@ pub enum Error { location: Location, }, - #[snafu(display("Engine not found: {}", engine))] - EngineNotFound { engine: String, location: Location }, - - #[snafu(display("Engine exist: {}", engine))] - EngineExist { engine: String, location: Location }, - #[snafu(display("Table projection error"))] TableProjection { #[snafu(source)] @@ -164,9 +158,7 @@ impl ErrorExt for Error { Error::ColumnNotExists { .. } => StatusCode::TableColumnNotFound, Error::RegionSchemaMismatch { .. } => StatusCode::StorageUnavailable, Error::Unsupported { .. } => StatusCode::Unsupported, - Error::ParseTableOption { .. } - | Error::EngineNotFound { .. } - | Error::EngineExist { .. } => StatusCode::InvalidArguments, + Error::ParseTableOption { .. } => StatusCode::InvalidArguments, Error::InvalidTable { .. } | Error::MissingTimeIndexColumn { .. } => { StatusCode::Internal diff --git a/src/table/src/lib.rs b/src/table/src/lib.rs index 2b10f8f20570..8a5929fbd533 100644 --- a/src/table/src/lib.rs +++ b/src/table/src/lib.rs @@ -15,13 +15,13 @@ #![feature(assert_matches)] pub mod dist_table; -pub mod engine; pub mod error; pub mod metadata; pub mod predicate; pub mod requests; pub mod stats; pub mod table; +pub mod table_reference; pub mod test_util; pub mod thin_table; diff --git a/src/table/src/requests.rs b/src/table/src/requests.rs index 2351ae8fa069..8e6b60fa99d8 100644 --- a/src/table/src/requests.rs +++ b/src/table/src/requests.rs @@ -28,10 +28,10 @@ use serde::{Deserialize, Serialize}; use store_api::metric_engine_consts::{LOGICAL_TABLE_METADATA_KEY, PHYSICAL_TABLE_METADATA_KEY}; use store_api::storage::RegionNumber; -use crate::engine::TableReference; use crate::error; use crate::error::ParseTableOptionSnafu; use crate::metadata::{TableId, TableVersion}; +use crate::table_reference::TableReference; pub const FILE_TABLE_META_KEY: &str = "__private.file_table_meta"; pub const FILE_TABLE_LOCATION_KEY: &str = "location"; diff --git a/src/table/src/table_reference.rs b/src/table/src/table_reference.rs new file mode 100644 index 000000000000..fb1f6bc63a96 --- /dev/null +++ b/src/table/src/table_reference.rs @@ -0,0 +1,73 @@ +// 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 std::fmt::{self, Display}; + +use datafusion_common::TableReference as DfTableReference; + +/// Represents a resolved path to a table of the form “catalog.schema.table” +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct TableReference<'a> { + pub catalog: &'a str, + pub schema: &'a str, + pub table: &'a str, +} + +pub type OwnedTableReference = TableReference<'static>; + +impl<'a> TableReference<'a> { + pub fn bare(table: &'a str) -> Self { + TableReference { + catalog: common_catalog::consts::DEFAULT_CATALOG_NAME, + schema: common_catalog::consts::DEFAULT_SCHEMA_NAME, + table, + } + } + + pub fn full(catalog: &'a str, schema: &'a str, table: &'a str) -> Self { + TableReference { + catalog, + schema, + table, + } + } +} + +impl<'a> Display for TableReference<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}.{}.{}", self.catalog, self.schema, self.table) + } +} + +impl<'a> From> for DfTableReference<'a> { + fn from(val: TableReference<'a>) -> Self { + DfTableReference::full(val.catalog, val.schema, val.table) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_table_reference() { + let table_ref = TableReference { + catalog: "greptime", + schema: "public", + table: "test", + }; + + assert_eq!("greptime.public.test", table_ref.to_string()); + } +} diff --git a/src/table/src/test_util.rs b/src/table/src/test_util.rs index 884f17602901..c3f14af24f9f 100644 --- a/src/table/src/test_util.rs +++ b/src/table/src/test_util.rs @@ -14,9 +14,7 @@ mod empty_table; mod memtable; -mod mock_engine; pub mod table_info; pub use empty_table::EmptyTable; pub use memtable::MemTable; -pub use mock_engine::MockTableEngine; diff --git a/src/table/src/test_util/mock_engine.rs b/src/table/src/test_util/mock_engine.rs deleted file mode 100644 index 8d88d9366651..000000000000 --- a/src/table/src/test_util/mock_engine.rs +++ /dev/null @@ -1,149 +0,0 @@ -// 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 std::collections::HashMap; - -use async_trait::async_trait; -use common_procedure::BoxedProcedure; -use tokio::sync::Mutex; - -use crate::engine::{EngineContext, TableEngine, TableEngineProcedure}; -use crate::metadata::TableId; -use crate::requests::{ - AlterTableRequest, CreateTableRequest, DropTableRequest, OpenTableRequest, TruncateTableRequest, -}; -use crate::test_util::EmptyTable; -use crate::{Result, TableRef}; - -#[derive(Default)] -pub struct MockTableEngine { - tables: Mutex>, -} - -impl MockTableEngine { - pub fn new() -> Self { - Self::default() - } -} - -#[async_trait] -impl TableEngine for MockTableEngine { - fn name(&self) -> &str { - "MockTableEngine" - } - - async fn create_table( - &self, - _ctx: &EngineContext, - request: CreateTableRequest, - ) -> Result { - let catalog_name = request.catalog_name.clone(); - let schema_name = request.schema_name.clone(); - let table_name = request.table_name.clone(); - - let table_ref = EmptyTable::table(request); - - let _ = self - .tables - .lock() - .await - .insert((catalog_name, schema_name, table_name), table_ref.clone()); - Ok(table_ref) - } - - async fn open_table( - &self, - _ctx: &EngineContext, - request: OpenTableRequest, - ) -> Result> { - let catalog_name = request.catalog_name; - let schema_name = request.schema_name; - let table_name = request.table_name; - - let res = self - .tables - .lock() - .await - .get(&(catalog_name, schema_name, table_name)) - .cloned(); - - Ok(res) - } - - async fn alter_table( - &self, - _ctx: &EngineContext, - _request: AlterTableRequest, - ) -> Result { - unimplemented!() - } - - fn get_table(&self, _ctx: &EngineContext, _table_id: TableId) -> Result> { - unimplemented!() - } - - fn table_exists(&self, _ctx: &EngineContext, _table_id: TableId) -> bool { - unimplemented!() - } - - async fn drop_table(&self, _ctx: &EngineContext, _request: DropTableRequest) -> Result { - unimplemented!() - } - - async fn close(&self) -> Result<()> { - Ok(()) - } - - async fn truncate_table( - &self, - _ctx: &EngineContext, - _request: TruncateTableRequest, - ) -> Result { - unimplemented!() - } -} - -impl TableEngineProcedure for MockTableEngine { - fn create_table_procedure( - &self, - _ctx: &EngineContext, - _request: CreateTableRequest, - ) -> Result { - unimplemented!() - } - - fn alter_table_procedure( - &self, - _ctx: &EngineContext, - _request: AlterTableRequest, - ) -> Result { - unimplemented!() - } - - fn drop_table_procedure( - &self, - _ctx: &EngineContext, - _request: DropTableRequest, - ) -> Result { - unimplemented!() - } - - fn truncate_table_procedure( - &self, - _ctx: &EngineContext, - _request: TruncateTableRequest, - ) -> Result { - unimplemented!() - } -}