XiaoHongbo-Hope commented on code in PR #293:
URL: https://github.com/apache/paimon-rust/pull/293#discussion_r3168264162
##########
crates/integrations/datafusion/src/sql_context.rs:
##########
@@ -64,54 +64,101 @@ use paimon::spec::{
use crate::error::to_datafusion_error;
use crate::DynamicOptions;
-/// Wraps a [`SessionContext`] and a Paimon [`Catalog`] to handle DDL
statements
-/// that DataFusion does not natively support (e.g. ALTER TABLE).
-///
-/// For all other SQL, it delegates to the inner `SessionContext`.
+/// A SQL context that supports registering multiple Paimon catalogs and
executing SQL.
///
/// # Example
/// ```ignore
-/// let ctx = SessionContext::new();
-/// let handler = PaimonSqlHandler::new(ctx, catalog, "paimon")?;
-/// let df = handler.sql("ALTER TABLE paimon.db.t ADD COLUMN age INT").await?;
+/// let mut ctx = SQLContext::new();
+/// ctx.register_catalog("paimon", catalog)?;
+/// ctx.set_current_catalog("paimon").await?;
+/// let df = ctx.sql("ALTER TABLE paimon.db.t ADD COLUMN age INT").await?;
/// ```
-pub struct PaimonSqlHandler {
+pub struct SQLContext {
ctx: SessionContext,
- catalog: Arc<dyn Catalog>,
- /// The catalog name registered in the SessionContext (used to strip the
catalog prefix).
- catalog_name: String,
+ catalogs: HashMap<String, Arc<dyn Catalog>>,
+ current_catalog: String,
/// Session-scoped dynamic options set via `SET 'paimon.key' = 'value'`.
dynamic_options: DynamicOptions,
}
-impl PaimonSqlHandler {
- /// Creates a new handler that registers the Paimon catalog and relation
planner
- /// on the given [`SessionContext`].
- ///
- /// Dynamic options for `SET`/`RESET` are managed internally.
- pub fn new(
- ctx: SessionContext,
- catalog: Arc<dyn Catalog>,
+impl Default for SQLContext {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl SQLContext {
+ /// Creates a new empty SQL context.
+ pub fn new() -> Self {
+ let ctx = SessionContext::new();
+ ctx.register_relation_planner(Arc::new(
+ crate::relation_planner::PaimonRelationPlanner::new(),
+ ))
+ .expect("failed to register relation planner");
+ Self {
+ ctx,
+ catalogs: HashMap::new(),
+ current_catalog: String::new(),
+ dynamic_options: Default::default(),
+ }
+ }
+
+ /// Registers a Paimon catalog under the given name.
+ pub fn register_catalog(
+ &mut self,
catalog_name: impl Into<String>,
- ) -> DFResult<Self> {
+ catalog: Arc<dyn Catalog>,
+ ) -> DFResult<()> {
let catalog_name = catalog_name.into();
- let dynamic_options: DynamicOptions = Default::default();
- ctx.register_catalog(
+ self.ctx.register_catalog(
&catalog_name,
Arc::new(crate::catalog::PaimonCatalogProvider::with_dynamic_options(
catalog.clone(),
- dynamic_options.clone(),
+ self.dynamic_options.clone(),
)),
);
- ctx.register_relation_planner(Arc::new(
- crate::relation_planner::PaimonRelationPlanner::new(),
- ))?;
- Ok(Self {
- ctx,
- catalog,
- catalog_name,
- dynamic_options,
- })
+ if self.catalogs.is_empty() {
+ self.current_catalog = catalog_name.clone();
+ }
Review Comment:
register_catalog sets the first catalog as current_catalog but doesn't sync
DataFusion's default_catalog, so custom-handled SQL and delegated SQL resolve
unqualified names differently.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]