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

fix(bridge): handle information schema #245

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions optd-datafusion-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use datafusion::arrow::datatypes::DataType;
use datafusion::catalog::information_schema::InformationSchemaProvider;
use datafusion::catalog::CatalogList;
use datafusion::error::Result;
use datafusion::execution::context::{QueryPlanner, SessionState};
Expand Down Expand Up @@ -60,8 +61,24 @@ impl DatafusionCatalog {
impl Catalog for DatafusionCatalog {
fn get(&self, name: &str) -> optd_datafusion_repr::properties::schema::Schema {
let catalog = self.catalog.catalog("datafusion").unwrap();
let schema = catalog.schema("public").unwrap();
let table = futures_lite::future::block_on(schema.table(name.as_ref())).unwrap();
let (schema, table) = if let Some((schema, table)) = name.split_once('.') {
(schema, table)
} else {
("public", name)
};
let schema = if schema == "information_schema" {
// This is `INFORMATION_SCHEMA` but datafusion didn't expose this constant.
// Also, note that we didn't check `session_state.is_information_schema_enabled()` so this schema is always
// available.
Arc::new(InformationSchemaProvider::new(Arc::clone(&self.catalog)))
} else if let Some(schema) = catalog.schema(schema) {
schema
} else {
panic!("schema not found in datafusion catalog: {}", schema)
};
let Some(table) = futures_lite::future::block_on(schema.table(table.as_ref())) else {
panic!("table not found in datafusion catalog: {}", name);
};
Comment on lines +64 to +81
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel using datafusion's resolve method makes it cleaner:

let resolved = TableReference::from(name).resolve("datafusion", "public");
let catalog = self.catalog.catalog(&resolved.catalog).unwrap();
let schema = if resolved.schema == "information_schema" {
self.information_schema.clone()
} else {
catalog.schema(&resolved.schema).unwrap()
};
let table = futures_lite::future::block_on(schema.table(&resolved.table)).unwrap();

I had it while drafting #233

let schema = table.schema();
let fields = schema.fields();
let mut optd_fields = Vec::with_capacity(fields.len());
Expand Down
Loading