mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-01-08 14:22:58 +00:00
* feat: querying from view works * feat: use MemoryCatalogProviderList instead of DummyCatalogList * refactor: revert src/query/src/dummy_catalog.rs * chore: clean code * fix: make clippy happy * fix: toml format * fix: sqlness * fix: forgot files * fix: make sqlness happy * test: table source, serializer and decoder * fix: fail to decode plan because of invalid table names * test: adds more sqlness test for view * chore: remove unused errors * fix: comments * fix: typo * fix: invalidate view info cache after creating view successfully * chore: apply suggestion Co-authored-by: Ruihang Xia <waynestxia@gmail.com> * chore: apply suggestion Co-authored-by: Ruihang Xia <waynestxia@gmail.com> * fix: compile error after rebeasing * chore: style Co-authored-by: Ruihang Xia <waynestxia@gmail.com> * fix: don't export table_name in common-meta * chore: change ViewInfo::new signature * docs: leave a TODO for optimize param --------- Co-authored-by: Ruihang Xia <waynestxia@gmail.com>
309 lines
8.6 KiB
Rust
309 lines
8.6 KiB
Rust
// 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 std::fmt::Debug;
|
|
|
|
use common_error::ext::{BoxedError, ErrorExt};
|
|
use common_error::status_code::StatusCode;
|
|
use common_macro::stack_trace_debug;
|
|
use datafusion::error::DataFusionError;
|
|
use snafu::{Location, Snafu};
|
|
|
|
#[derive(Snafu)]
|
|
#[snafu(visibility(pub))]
|
|
#[stack_trace_debug]
|
|
pub enum Error {
|
|
#[snafu(display("Failed to list catalogs"))]
|
|
ListCatalogs {
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
source: BoxedError,
|
|
},
|
|
|
|
#[snafu(display("Failed to list {}'s schemas", catalog))]
|
|
ListSchemas {
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
catalog: String,
|
|
source: BoxedError,
|
|
},
|
|
|
|
#[snafu(display("Failed to list {}.{}'s tables", catalog, schema))]
|
|
ListTables {
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
catalog: String,
|
|
schema: String,
|
|
source: BoxedError,
|
|
},
|
|
|
|
#[snafu(display("Failed to list nodes in cluster: {source}"))]
|
|
ListNodes {
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
source: BoxedError,
|
|
},
|
|
|
|
#[snafu(display("Failed to re-compile script due to internal error"))]
|
|
CompileScriptInternal {
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
source: BoxedError,
|
|
},
|
|
|
|
#[snafu(display("Failed to create table, table info: {}", table_info))]
|
|
CreateTable {
|
|
table_info: String,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
source: table::error::Error,
|
|
},
|
|
|
|
#[snafu(display("System catalog is not valid: {}", msg))]
|
|
SystemCatalog {
|
|
msg: String,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("Cannot find catalog by name: {}", catalog_name))]
|
|
CatalogNotFound {
|
|
catalog_name: String,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("Cannot find schema {} in catalog {}", schema, catalog))]
|
|
SchemaNotFound {
|
|
catalog: String,
|
|
schema: String,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("Table `{}` already exists", table))]
|
|
TableExists {
|
|
table: String,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("Table not found: {}", table))]
|
|
TableNotExist {
|
|
table: String,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("View info not found: {}", name))]
|
|
ViewInfoNotFound {
|
|
name: String,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("Failed to find table partitions"))]
|
|
FindPartitions { source: partition::error::Error },
|
|
|
|
#[snafu(display("Failed to find region routes"))]
|
|
FindRegionRoutes { source: partition::error::Error },
|
|
|
|
#[snafu(display("Failed to create recordbatch"))]
|
|
CreateRecordBatch {
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
source: common_recordbatch::error::Error,
|
|
},
|
|
|
|
#[snafu(display("Internal error"))]
|
|
Internal {
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
source: BoxedError,
|
|
},
|
|
|
|
#[snafu(display("Failed to upgrade weak catalog manager reference"))]
|
|
UpgradeWeakCatalogManagerRef {
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("Failed to decode logical plan for view: {}", name))]
|
|
DecodePlan {
|
|
name: String,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
source: common_query::error::Error,
|
|
},
|
|
|
|
#[snafu(display("Failed to perform metasrv operation"))]
|
|
Metasrv {
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
source: meta_client::error::Error,
|
|
},
|
|
|
|
#[snafu(display("Invalid table info in catalog"))]
|
|
InvalidTableInfoInCatalog {
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
source: datatypes::error::Error,
|
|
},
|
|
|
|
#[snafu(display("Illegal access to catalog: {} and schema: {}", catalog, schema))]
|
|
QueryAccessDenied { catalog: String, schema: String },
|
|
|
|
#[snafu(display("DataFusion error"))]
|
|
Datafusion {
|
|
#[snafu(source)]
|
|
error: DataFusionError,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("Table metadata manager error"))]
|
|
TableMetadataManager {
|
|
source: common_meta::error::Error,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("Failed to get table cache"))]
|
|
GetTableCache {
|
|
source: common_meta::error::Error,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("Failed to get view info from cache"))]
|
|
GetViewCache {
|
|
source: common_meta::error::Error,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("Cache not found: {name}"))]
|
|
CacheNotFound {
|
|
name: String,
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
|
|
#[snafu(display("Failed to cast the catalog manager"))]
|
|
CastManager {
|
|
#[snafu(implicit)]
|
|
location: Location,
|
|
},
|
|
}
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
impl ErrorExt for Error {
|
|
fn status_code(&self) -> StatusCode {
|
|
match self {
|
|
Error::SchemaNotFound { .. }
|
|
| Error::CatalogNotFound { .. }
|
|
| Error::FindPartitions { .. }
|
|
| Error::FindRegionRoutes { .. }
|
|
| Error::CacheNotFound { .. }
|
|
| Error::CastManager { .. } => StatusCode::Unexpected,
|
|
|
|
Error::ViewInfoNotFound { .. } => StatusCode::TableNotFound,
|
|
|
|
Error::SystemCatalog { .. } => StatusCode::StorageUnavailable,
|
|
|
|
Error::UpgradeWeakCatalogManagerRef { .. } => StatusCode::Internal,
|
|
|
|
Error::CreateRecordBatch { source, .. } => source.status_code(),
|
|
Error::TableExists { .. } => StatusCode::TableAlreadyExists,
|
|
Error::TableNotExist { .. } => StatusCode::TableNotFound,
|
|
Error::ListCatalogs { source, .. }
|
|
| Error::ListNodes { source, .. }
|
|
| Error::ListSchemas { source, .. }
|
|
| Error::ListTables { source, .. } => source.status_code(),
|
|
|
|
Error::CreateTable { source, .. } => source.status_code(),
|
|
|
|
Error::Metasrv { source, .. } => source.status_code(),
|
|
Error::DecodePlan { source, .. } => source.status_code(),
|
|
Error::InvalidTableInfoInCatalog { source, .. } => source.status_code(),
|
|
|
|
Error::CompileScriptInternal { source, .. } | Error::Internal { source, .. } => {
|
|
source.status_code()
|
|
}
|
|
|
|
Error::QueryAccessDenied { .. } => StatusCode::AccessDenied,
|
|
Error::Datafusion { .. } => StatusCode::EngineExecuteQuery,
|
|
Error::TableMetadataManager { source, .. } => source.status_code(),
|
|
Error::GetViewCache { source, .. } | Error::GetTableCache { source, .. } => {
|
|
source.status_code()
|
|
}
|
|
}
|
|
}
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl From<Error> for DataFusionError {
|
|
fn from(e: Error) -> Self {
|
|
DataFusionError::Internal(e.to_string())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use snafu::GenerateImplicitData;
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
pub fn test_error_status_code() {
|
|
assert_eq!(
|
|
StatusCode::TableAlreadyExists,
|
|
Error::TableExists {
|
|
table: "some_table".to_string(),
|
|
location: Location::generate(),
|
|
}
|
|
.status_code()
|
|
);
|
|
|
|
assert_eq!(
|
|
StatusCode::StorageUnavailable,
|
|
Error::SystemCatalog {
|
|
msg: String::default(),
|
|
location: Location::generate(),
|
|
}
|
|
.status_code()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
pub fn test_errors_to_datafusion_error() {
|
|
let e: DataFusionError = Error::TableExists {
|
|
table: "test_table".to_string(),
|
|
location: Location::generate(),
|
|
}
|
|
.into();
|
|
match e {
|
|
DataFusionError::Internal(_) => {}
|
|
_ => {
|
|
panic!("catalog error should be converted to DataFusionError::Internal")
|
|
}
|
|
}
|
|
}
|
|
}
|