-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce 'pg_catalog.pg_type' (#4332)
* WIP: pg_catalog * refactor: move memory_table to crate public level to reuse it in pgcatalog * refactor: new system_schema mod to manage implementation of information_schema and pg_catalog * feat: pg_catalog.pg_type * fix: remove unused code to avoid warning * test: add pg_catalog sqlness test * feat: pg_catalog_cache in system_catalog * fix: integration test * test: rollback unit test * refactor: mix pg_catalog table_id with old ones * fix: add todo information * tests: rerun sqlness --------- Co-authored-by: johnsonlee <[email protected]>
- Loading branch information
1 parent
7900367
commit 072d7c2
Showing
36 changed files
with
782 additions
and
297 deletions.
There are no files selected for viewing
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,164 @@ | ||
// 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 information_schema; | ||
mod memory_table; | ||
pub mod pg_catalog; | ||
|
||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
use common_error::ext::BoxedError; | ||
use common_recordbatch::{RecordBatchStreamWrapper, SendableRecordBatchStream}; | ||
use datatypes::schema::SchemaRef; | ||
use futures_util::StreamExt; | ||
use snafu::ResultExt; | ||
use store_api::data_source::DataSource; | ||
use store_api::storage::ScanRequest; | ||
use table::error::{SchemaConversionSnafu, TablesRecordBatchSnafu}; | ||
use table::metadata::{ | ||
FilterPushDownType, TableId, TableInfoBuilder, TableInfoRef, TableMetaBuilder, TableType, | ||
}; | ||
use table::{Table, TableRef}; | ||
|
||
use crate::error::Result; | ||
|
||
pub trait SystemSchemaProvider { | ||
/// Returns a map of [TableRef] in information schema. | ||
fn tables(&self) -> &HashMap<String, TableRef>; | ||
|
||
/// Returns the [TableRef] by table name. | ||
fn table(&self, name: &str) -> Option<TableRef> { | ||
self.tables().get(name).cloned() | ||
} | ||
|
||
/// Returns table names in the order of table id. | ||
fn table_names(&self) -> Vec<String> { | ||
let mut tables = self.tables().values().clone().collect::<Vec<_>>(); | ||
|
||
tables.sort_by(|t1, t2| { | ||
t1.table_info() | ||
.table_id() | ||
.partial_cmp(&t2.table_info().table_id()) | ||
.unwrap() | ||
}); | ||
tables | ||
.into_iter() | ||
.map(|t| t.table_info().name.clone()) | ||
.collect() | ||
} | ||
} | ||
|
||
trait SystemSchemaProviderInner { | ||
fn catalog_name(&self) -> &str; | ||
fn schema_name() -> &'static str; | ||
fn build_table(&self, name: &str) -> Option<TableRef> { | ||
self.system_table(name).map(|table| { | ||
let table_info = Self::table_info(self.catalog_name().to_string(), &table); | ||
let filter_pushdown = FilterPushDownType::Inexact; | ||
let data_source = Arc::new(SystemTableDataSource::new(table)); | ||
let table = Table::new(table_info, filter_pushdown, data_source); | ||
Arc::new(table) | ||
}) | ||
} | ||
fn system_table(&self, name: &str) -> Option<SystemTableRef>; | ||
|
||
fn table_info(catalog_name: String, table: &SystemTableRef) -> TableInfoRef { | ||
let table_meta = TableMetaBuilder::default() | ||
.schema(table.schema()) | ||
.primary_key_indices(vec![]) | ||
.next_column_id(0) | ||
.build() | ||
.unwrap(); | ||
let table_info = TableInfoBuilder::default() | ||
.table_id(table.table_id()) | ||
.name(table.table_name().to_string()) | ||
.catalog_name(catalog_name) | ||
.schema_name(Self::schema_name().to_string()) | ||
.meta(table_meta) | ||
.table_type(table.table_type()) | ||
.build() | ||
.unwrap(); | ||
Arc::new(table_info) | ||
} | ||
} | ||
|
||
pub(crate) trait SystemTable { | ||
fn table_id(&self) -> TableId; | ||
|
||
fn table_name(&self) -> &'static str; | ||
|
||
fn schema(&self) -> SchemaRef; | ||
|
||
fn to_stream(&self, request: ScanRequest) -> Result<SendableRecordBatchStream>; | ||
|
||
fn table_type(&self) -> TableType { | ||
TableType::Temporary | ||
} | ||
} | ||
|
||
pub(crate) type SystemTableRef = Arc<dyn SystemTable + Send + Sync>; | ||
|
||
struct SystemTableDataSource { | ||
table: SystemTableRef, | ||
} | ||
|
||
impl SystemTableDataSource { | ||
fn new(table: SystemTableRef) -> Self { | ||
Self { table } | ||
} | ||
|
||
fn try_project(&self, projection: &[usize]) -> std::result::Result<SchemaRef, BoxedError> { | ||
let schema = self | ||
.table | ||
.schema() | ||
.try_project(projection) | ||
.context(SchemaConversionSnafu) | ||
.map_err(BoxedError::new)?; | ||
Ok(Arc::new(schema)) | ||
} | ||
} | ||
|
||
impl DataSource for SystemTableDataSource { | ||
fn get_stream( | ||
&self, | ||
request: ScanRequest, | ||
) -> std::result::Result<SendableRecordBatchStream, BoxedError> { | ||
let projection = request.projection.clone(); | ||
let projected_schema = match &projection { | ||
Some(projection) => self.try_project(projection)?, | ||
None => self.table.schema(), | ||
}; | ||
|
||
let stream = self | ||
.table | ||
.to_stream(request) | ||
.map_err(BoxedError::new) | ||
.context(TablesRecordBatchSnafu) | ||
.map_err(BoxedError::new)? | ||
.map(move |batch| match &projection { | ||
Some(p) => batch.and_then(|b| b.try_project(p)), | ||
None => batch, | ||
}); | ||
|
||
let stream = RecordBatchStreamWrapper { | ||
schema: projected_schema, | ||
stream: Box::pin(stream), | ||
output_ordering: None, | ||
metrics: Default::default(), | ||
}; | ||
|
||
Ok(Box::pin(stream)) | ||
} | ||
} |
Oops, something went wrong.