Skip to content

Commit

Permalink
Move default catalog and schema onto ConfigOptions (#3887) (#4805)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold authored Jan 3, 2023
1 parent 68dc644 commit 5b70e35
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 59 deletions.
6 changes: 6 additions & 0 deletions datafusion/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ config_namespace! {
/// concurrency. Defaults to the number of cpu cores on the system.
pub create_default_catalog_and_schema: bool, default = true

/// The default catalog name - this impacts what SQL queries use if not specified
pub default_catalog: String, default = "datafusion".to_string()

/// The default schema name - this impacts what SQL queries use if not specified
pub default_schema: String, default = "public".to_string()

/// Should DataFusion provide access to `information_schema`
/// virtual tables for displaying schema information
pub information_schema: bool, default = false
Expand Down
61 changes: 32 additions & 29 deletions datafusion/core/src/execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ use super::options::{
AvroReadOptions, CsvReadOptions, NdJsonReadOptions, ParquetReadOptions,
};

/// The default catalog name - this impacts what SQL queries use if not specified
const DEFAULT_CATALOG: &str = "datafusion";
/// The default schema name - this impacts what SQL queries use if not specified
const DEFAULT_SCHEMA: &str = "public";

/// SessionContext is the main interface for executing queries with DataFusion. It stands for
/// the connection between user and DataFusion/Ballista cluster.
/// The context provides the following functionality
Expand Down Expand Up @@ -381,18 +376,32 @@ impl SessionContext {
// so for now, we default to default catalog
let tokens: Vec<&str> = schema_name.split('.').collect();
let (catalog, schema_name) = match tokens.len() {
1 => Ok((DEFAULT_CATALOG, schema_name.as_str())),
2 => Ok((tokens[0], tokens[1])),
_ => Err(DataFusionError::Execution(format!(
"Unable to parse catalog from {schema_name}"
))),
}?;
let catalog = self.catalog(catalog).ok_or_else(|| {
DataFusionError::Execution(format!(
"Missing '{DEFAULT_CATALOG}' catalog"
))
})?;

1 => {
let state = self.state.read();
let name = &state.config.options.catalog.default_catalog;
let catalog =
state.catalog_list.catalog(name).ok_or_else(|| {
DataFusionError::Execution(format!(
"Missing default catalog '{name}'"
))
})?;
(catalog, tokens[0])
}
2 => {
let name = &tokens[0];
let catalog = self.catalog(name).ok_or_else(|| {
DataFusionError::Execution(format!(
"Missing catalog '{name}'"
))
})?;
(catalog, tokens[1])
}
_ => {
return Err(DataFusionError::Execution(format!(
"Unable to parse catalog from {schema_name}"
)))
}
};
let schema = catalog.schema(schema_name);

match (if_not_exists, schema) {
Expand Down Expand Up @@ -1098,11 +1107,6 @@ impl Hasher for IdHasher {
/// Configuration options for session context
#[derive(Clone)]
pub struct SessionConfig {
/// Default catalog name for table resolution
default_catalog: String,
/// Default schema name for table resolution (not in ConfigOptions
/// due to `resolve_table_ref` which passes back references)
default_schema: String,
/// Configuration options
options: ConfigOptions,
/// Opaque extensions.
Expand All @@ -1112,8 +1116,6 @@ pub struct SessionConfig {
impl Default for SessionConfig {
fn default() -> Self {
Self {
default_catalog: DEFAULT_CATALOG.to_owned(),
default_schema: DEFAULT_SCHEMA.to_owned(),
options: ConfigOptions::new(),
// Assume no extensions by default.
extensions: HashMap::with_capacity_and_hasher(
Expand Down Expand Up @@ -1219,8 +1221,8 @@ impl SessionConfig {
catalog: impl Into<String>,
schema: impl Into<String>,
) -> Self {
self.default_catalog = catalog.into();
self.default_schema = schema.into();
self.options.catalog.default_catalog = catalog.into();
self.options.catalog.default_schema = schema.into();
self
}

Expand Down Expand Up @@ -1435,15 +1437,15 @@ impl SessionState {

default_catalog
.register_schema(
&config.default_schema,
&config.config_options().catalog.default_schema,
Arc::new(MemorySchemaProvider::new()),
)
.expect("memory catalog provider can register schema");

Self::register_default_schema(&config, &runtime, &default_catalog);

catalog_list.register_catalog(
config.default_catalog.clone(),
config.config_options().catalog.default_catalog.clone(),
Arc::new(default_catalog),
);
}
Expand Down Expand Up @@ -1560,9 +1562,10 @@ impl SessionState {
&'a self,
table_ref: impl Into<TableReference<'a>>,
) -> ResolvedTableReference<'a> {
let catalog = &self.config_options().catalog;
table_ref
.into()
.resolve(&self.config.default_catalog, &self.config.default_schema)
.resolve(&catalog.default_catalog, &catalog.default_schema)
}

fn schema_for_ref<'a>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ query R
SHOW ALL
----
datafusion.catalog.create_default_catalog_and_schema true
datafusion.catalog.default_catalog datafusion
datafusion.catalog.default_schema public
datafusion.catalog.format NULL
datafusion.catalog.has_header false
datafusion.catalog.information_schema true
Expand Down
Loading

0 comments on commit 5b70e35

Please sign in to comment.