refactor: drop Table trait (#3654)

* refactor: drop Table trait

Signed-off-by: tison <wander4096@gmail.com>

* finish rename

Signed-off-by: tison <wander4096@gmail.com>

* Apply suggestions from code review

Co-authored-by: Zhenchi <zhongzc_arch@outlook.com>

* Update time_range_filter_test.rs

* Update src/query/src/tests/time_range_filter_test.rs

* apply comments

Signed-off-by: tison <wander4096@gmail.com>

---------

Signed-off-by: tison <wander4096@gmail.com>
Co-authored-by: Zhenchi <zhongzc_arch@outlook.com>
This commit is contained in:
tison
2024-04-08 15:28:55 +08:00
committed by GitHub
parent c4798d1913
commit e920f95902
9 changed files with 90 additions and 156 deletions

View File

@@ -21,17 +21,16 @@ use store_api::storage::ScanRequest;
use crate::error::UnsupportedSnafu;
use crate::metadata::{FilterPushDownType, TableInfoRef};
use crate::thin_table::{ThinTable, ThinTableAdapter};
use crate::TableRef;
use crate::{Table, TableRef};
#[derive(Clone)]
pub struct DistTable;
impl DistTable {
pub fn table(table_info: TableInfoRef) -> TableRef {
let thin_table = ThinTable::new(table_info, FilterPushDownType::Inexact);
let data_source = Arc::new(DummyDataSource);
Arc::new(ThinTableAdapter::new(thin_table, data_source))
let table = Table::new(table_info, FilterPushDownType::Inexact, data_source);
Arc::new(table)
}
}

View File

@@ -23,7 +23,6 @@ pub mod stats;
pub mod table;
pub mod table_reference;
pub mod test_util;
pub mod thin_table;
pub use crate::error::{Error, Result};
pub use crate::stats::{ColumnStatistics, TableStatistics};

View File

@@ -12,49 +12,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::sync::Arc;
use common_query::logical_plan::Expr;
use common_recordbatch::SendableRecordBatchStream;
use datatypes::schema::SchemaRef;
use snafu::ResultExt;
use store_api::data_source::DataSourceRef;
use store_api::storage::ScanRequest;
use crate::error::{Result, TablesRecordBatchSnafu};
use crate::metadata::{FilterPushDownType, TableId, TableInfoRef, TableType};
pub mod adapter;
mod metrics;
pub mod numbers;
pub mod scan;
use std::any::Any;
use std::sync::Arc;
use async_trait::async_trait;
use common_query::logical_plan::Expr;
use common_recordbatch::SendableRecordBatchStream;
use datatypes::schema::SchemaRef;
use store_api::storage::ScanRequest;
use crate::error::Result;
use crate::metadata::{FilterPushDownType, TableId, TableInfoRef, TableType};
/// Table abstraction.
#[async_trait]
pub trait Table: Send + Sync {
/// Returns the table as [`Any`](std::any::Any) so that it can be
/// downcast to a specific implementation.
fn as_any(&self) -> &dyn Any;
/// Get a reference to the schema for this table
fn schema(&self) -> SchemaRef;
/// Get a reference to the table info.
fn table_info(&self) -> TableInfoRef;
/// Get the type of this table for metadata/catalog purposes.
fn table_type(&self) -> TableType;
async fn scan_to_stream(&self, request: ScanRequest) -> Result<SendableRecordBatchStream>;
/// Tests whether the table provider can make use of any or all filter expressions
/// to optimise data retrieval.
fn supports_filters_pushdown(&self, filters: &[&Expr]) -> Result<Vec<FilterPushDownType>> {
Ok(vec![FilterPushDownType::Unsupported; filters.len()])
}
}
pub type TableRef = Arc<dyn Table>;
pub type TableRef = Arc<Table>;
#[async_trait::async_trait]
pub trait TableIdProvider {
@@ -62,3 +37,55 @@ pub trait TableIdProvider {
}
pub type TableIdProviderRef = Arc<dyn TableIdProvider + Send + Sync>;
/// Table handle.
pub struct Table {
table_info: TableInfoRef,
filter_pushdown: FilterPushDownType,
data_source: DataSourceRef,
}
impl Table {
pub fn new(
table_info: TableInfoRef,
filter_pushdown: FilterPushDownType,
data_source: DataSourceRef,
) -> Self {
Self {
table_info,
filter_pushdown,
data_source,
}
}
pub fn data_source(&self) -> DataSourceRef {
self.data_source.clone()
}
/// Get a reference to the schema for this table.
pub fn schema(&self) -> SchemaRef {
self.table_info.meta.schema.clone()
}
/// Get a reference to the table info.
pub fn table_info(&self) -> TableInfoRef {
self.table_info.clone()
}
/// Get the type of this table for metadata/catalog purposes.
pub fn table_type(&self) -> TableType {
self.table_info.table_type
}
pub async fn scan_to_stream(&self, request: ScanRequest) -> Result<SendableRecordBatchStream> {
self.data_source
.get_stream(request)
.context(TablesRecordBatchSnafu)
}
/// Tests whether the table provider can make use of any or all filter expressions
/// to optimise data retrieval.
pub fn supports_filters_pushdown(&self, filters: &[&Expr]) -> Result<Vec<FilterPushDownType>> {
Ok(vec![self.filter_pushdown; filters.len()])
}
}

View File

@@ -32,8 +32,7 @@ use store_api::storage::ScanRequest;
use crate::metadata::{
FilterPushDownType, TableId, TableInfoBuilder, TableInfoRef, TableMetaBuilder, TableType,
};
use crate::thin_table::{ThinTable, ThinTableAdapter};
use crate::TableRef;
use crate::{Table, TableRef};
const NUMBER_COLUMN: &str = "number";
@@ -49,12 +48,13 @@ impl NumbersTable {
}
pub fn table_with_name(table_id: TableId, name: String) -> TableRef {
let thin_table = ThinTable::new(
let data_source = Arc::new(NumbersDataSource::new(Self::schema()));
let table = Table::new(
Self::table_info(table_id, name, "test_engine".to_string()),
FilterPushDownType::Unsupported,
data_source,
);
let data_source = Arc::new(NumbersDataSource::new(Self::schema()));
Arc::new(ThinTableAdapter::new(thin_table, data_source))
Arc::new(table)
}
pub fn schema() -> SchemaRef {

View File

@@ -21,18 +21,21 @@ use store_api::data_source::DataSource;
use store_api::storage::ScanRequest;
use crate::metadata::{FilterPushDownType, TableInfo};
use crate::thin_table::{ThinTable, ThinTableAdapter};
use crate::TableRef;
use crate::{Table, TableRef};
pub struct EmptyTable;
impl EmptyTable {
pub fn from_table_info(info: &TableInfo) -> TableRef {
let thin_table = ThinTable::new(Arc::new(info.clone()), FilterPushDownType::Unsupported);
let data_source = Arc::new(EmptyDataSource {
schema: info.meta.schema.clone(),
});
Arc::new(ThinTableAdapter::new(thin_table, data_source))
let table = Table::new(
Arc::new(info.clone()),
FilterPushDownType::Unsupported,
data_source,
);
Arc::new(table)
}
}

View File

@@ -33,8 +33,7 @@ use crate::error::{SchemaConversionSnafu, TableProjectionSnafu, TablesRecordBatc
use crate::metadata::{
FilterPushDownType, TableId, TableInfoBuilder, TableMetaBuilder, TableType, TableVersion,
};
use crate::thin_table::{ThinTable, ThinTableAdapter};
use crate::TableRef;
use crate::{Table, TableRef};
pub struct MemTable;
@@ -94,9 +93,9 @@ impl MemTable {
.unwrap(),
);
let thin_table = ThinTable::new(info, FilterPushDownType::Unsupported);
let data_source = Arc::new(MemtableDataSource { recordbatch });
Arc::new(ThinTableAdapter::new(thin_table, data_source))
let table = Table::new(info, FilterPushDownType::Unsupported, data_source);
Arc::new(table)
}
/// Creates a 1 column 100 rows table, with table name "numbers", column name "uint32s" and

View File

@@ -1,87 +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::any::Any;
use async_trait::async_trait;
use common_query::prelude::Expr;
use common_recordbatch::SendableRecordBatchStream;
use datatypes::schema::SchemaRef;
use snafu::ResultExt;
use store_api::data_source::DataSourceRef;
use store_api::storage::ScanRequest;
use crate::error::{Result, TablesRecordBatchSnafu};
use crate::metadata::{FilterPushDownType, TableInfoRef, TableType};
use crate::Table;
/// The `ThinTable` struct will replace the `Table` trait.
/// TODO(zhongzc): After completion, perform renaming and documentation work.
pub struct ThinTable {
table_info: TableInfoRef,
filter_pushdown: FilterPushDownType,
}
impl ThinTable {
pub fn new(table_info: TableInfoRef, filter_pushdown: FilterPushDownType) -> Self {
Self {
table_info,
filter_pushdown,
}
}
}
pub struct ThinTableAdapter {
table: ThinTable,
data_source: DataSourceRef,
}
impl ThinTableAdapter {
pub fn new(table: ThinTable, data_source: DataSourceRef) -> Self {
Self { table, data_source }
}
pub fn data_source(&self) -> DataSourceRef {
self.data_source.clone()
}
}
#[async_trait]
impl Table for ThinTableAdapter {
fn as_any(&self) -> &dyn Any {
self
}
fn schema(&self) -> SchemaRef {
self.table.table_info.meta.schema.clone()
}
fn table_info(&self) -> TableInfoRef {
self.table.table_info.clone()
}
fn table_type(&self) -> TableType {
self.table.table_info.table_type
}
async fn scan_to_stream(&self, request: ScanRequest) -> Result<SendableRecordBatchStream> {
self.data_source
.get_stream(request)
.context(TablesRecordBatchSnafu)
}
fn supports_filters_pushdown(&self, filters: &[&Expr]) -> Result<Vec<FilterPushDownType>> {
Ok(vec![self.table.filter_pushdown; filters.len()])
}
}