You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@paimon.apache.org by "JingsongLi (via GitHub)" <gi...@apache.org> on 2026/04/11 09:35:12 UTC
[PR] feat(datafusion): Add DDL support with PRIMARY KEY constraint syntax [paimon-rust]
JingsongLi opened a new pull request, #237:
URL: https://github.com/apache/paimon-rust/pull/237
<!--
Thank you very much for contributing to Paimon Rust - we are happy that you want to help us improve it. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.
## Contribution Checklist
- Make sure that the pull request corresponds to a [GitHub issue](https://github.com/apache/paimon-rust/issues). Exceptions are made for typos in documentation or comments, which need no issue.
- Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
- Make sure that the change passes the automated tests, i.e., `cargo test` passes.
- Each pull request should address only one issue, not mix up code from multiple issues.
**(The sections below can be removed for hotfixes or typos)**
-->
### Purpose
<!-- Linking this pull request to the issue -->
- Add PaimonDdlHandler for CREATE TABLE, ALTER TABLE (ADD/DROP/RENAME COLUMN, RENAME TABLE)
- Add PaimonTableFactory for CREATE EXTERNAL TABLE via DataFusion TableProviderFactory
- Extend PaimonCatalogProvider/SchemaProvider with CREATE/DROP SCHEMA and DROP TABLE
- Add arrow_to_paimon_type and arrow_fields_to_paimon to paimon::arrow for Arrow-to-Paimon type conversion
- Support PRIMARY KEY (col, ...) constraint syntax in CREATE TABLE DDL
<!-- What is the purpose of the change -->
### Brief change log
<!-- Please describe the changes made in this pull request and explain how they address the issue -->
### Tests
<!-- List unit tests or integration cases to verify this change -->
### API and Format
<!-- Does this change affect API or storage format -->
### Documentation
<!-- Does this change introduce a new feature or require documentation updates -->
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(datafusion): Add DDL support with PRIMARY KEY constraint syntax [paimon-rust]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #237:
URL: https://github.com/apache/paimon-rust/pull/237#discussion_r3068813316
##########
crates/integrations/datafusion/src/ddl.rs:
##########
@@ -0,0 +1,387 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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.
+
+//! DDL support for Paimon tables.
+//!
+//! DataFusion does not natively support all DDL statements needed by Paimon.
+//! This module provides [`PaimonDdlHandler`] which intercepts CREATE TABLE and
+//! ALTER TABLE SQL, translates them to Paimon catalog operations, and delegates
+//! everything else (SELECT, CREATE/DROP SCHEMA, DROP TABLE, etc.) to the
+//! underlying [`SessionContext`].
+//!
+//! Supported DDL:
+//! - `CREATE TABLE db.t (col TYPE, ..., PRIMARY KEY (col, ...)) [PARTITIONED BY (col TYPE, ...)] [WITH ('key' = 'val')]`
+//! - `ALTER TABLE db.t ADD COLUMN col TYPE`
+//! - `ALTER TABLE db.t DROP COLUMN col`
+//! - `ALTER TABLE db.t RENAME COLUMN old TO new`
+//! - `ALTER TABLE db.t RENAME TO new_name`
+
+use std::sync::Arc;
+
+use datafusion::arrow::array::StringArray;
+use datafusion::arrow::datatypes::{DataType as ArrowDataType, Field, Schema};
+use datafusion::arrow::record_batch::RecordBatch;
+use datafusion::error::{DataFusionError, Result as DFResult};
+use datafusion::prelude::{DataFrame, SessionContext};
+use datafusion::sql::sqlparser::ast::{
+ AlterTableOperation, ColumnDef, CreateTable, CreateTableOptions, HiveDistributionStyle,
+ ObjectName, RenameTableNameKind, SqlOption, Statement,
+};
+use datafusion::sql::sqlparser::dialect::GenericDialect;
+use datafusion::sql::sqlparser::parser::Parser;
+use paimon::catalog::{Catalog, Identifier};
+use paimon::spec::SchemaChange;
+
+use crate::error::to_datafusion_error;
+use paimon::arrow::arrow_to_paimon_type;
+
+/// 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`.
+///
+/// # Example
+/// ```ignore
+/// let handler = PaimonDdlHandler::new(ctx, catalog);
+/// let df = handler.sql("ALTER TABLE paimon.db.t ADD COLUMN age INT").await?;
+/// ```
+pub struct PaimonDdlHandler {
+ ctx: SessionContext,
+ catalog: Arc<dyn Catalog>,
+ /// The catalog name registered in the SessionContext (used to strip the catalog prefix).
+ catalog_name: String,
+}
+
+impl PaimonDdlHandler {
+ pub fn new(
+ ctx: SessionContext,
+ catalog: Arc<dyn Catalog>,
+ catalog_name: impl Into<String>,
+ ) -> Self {
+ Self {
+ ctx,
+ catalog,
+ catalog_name: catalog_name.into(),
+ }
+ }
+
+ /// Returns a reference to the inner [`SessionContext`].
+ pub fn ctx(&self) -> &SessionContext {
+ &self.ctx
+ }
+
+ /// Execute a SQL statement. ALTER TABLE is handled by Paimon directly;
+ /// everything else is delegated to DataFusion.
+ pub async fn sql(&self, sql: &str) -> DFResult<DataFrame> {
+ let dialect = GenericDialect {};
+ let statements = Parser::parse_sql(&dialect, sql)
+ .map_err(|e| DataFusionError::Plan(format!("SQL parse error: {e}")))?;
+
+ if statements.len() != 1 {
+ return Err(DataFusionError::Plan(
+ "Expected exactly one SQL statement".to_string(),
+ ));
+ }
+
+ match &statements[0] {
+ Statement::CreateTable(create_table) => self.handle_create_table(create_table).await,
+ Statement::AlterTable {
+ name, operations, ..
Review Comment:
Good catch.
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(datafusion): Add DDL support with PRIMARY KEY constraint syntax [paimon-rust]
Posted by "littlecoder04 (via GitHub)" <gi...@apache.org>.
littlecoder04 commented on code in PR #237:
URL: https://github.com/apache/paimon-rust/pull/237#discussion_r3068077186
##########
crates/integrations/datafusion/src/ddl.rs:
##########
@@ -0,0 +1,387 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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.
+
+//! DDL support for Paimon tables.
+//!
+//! DataFusion does not natively support all DDL statements needed by Paimon.
+//! This module provides [`PaimonDdlHandler`] which intercepts CREATE TABLE and
+//! ALTER TABLE SQL, translates them to Paimon catalog operations, and delegates
+//! everything else (SELECT, CREATE/DROP SCHEMA, DROP TABLE, etc.) to the
+//! underlying [`SessionContext`].
+//!
+//! Supported DDL:
+//! - `CREATE TABLE db.t (col TYPE, ..., PRIMARY KEY (col, ...)) [PARTITIONED BY (col TYPE, ...)] [WITH ('key' = 'val')]`
+//! - `ALTER TABLE db.t ADD COLUMN col TYPE`
+//! - `ALTER TABLE db.t DROP COLUMN col`
+//! - `ALTER TABLE db.t RENAME COLUMN old TO new`
+//! - `ALTER TABLE db.t RENAME TO new_name`
+
+use std::sync::Arc;
+
+use datafusion::arrow::array::StringArray;
+use datafusion::arrow::datatypes::{DataType as ArrowDataType, Field, Schema};
+use datafusion::arrow::record_batch::RecordBatch;
+use datafusion::error::{DataFusionError, Result as DFResult};
+use datafusion::prelude::{DataFrame, SessionContext};
+use datafusion::sql::sqlparser::ast::{
+ AlterTableOperation, ColumnDef, CreateTable, CreateTableOptions, HiveDistributionStyle,
+ ObjectName, RenameTableNameKind, SqlOption, Statement,
+};
+use datafusion::sql::sqlparser::dialect::GenericDialect;
+use datafusion::sql::sqlparser::parser::Parser;
+use paimon::catalog::{Catalog, Identifier};
+use paimon::spec::SchemaChange;
+
+use crate::error::to_datafusion_error;
+use paimon::arrow::arrow_to_paimon_type;
+
+/// 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`.
+///
+/// # Example
+/// ```ignore
+/// let handler = PaimonDdlHandler::new(ctx, catalog);
+/// let df = handler.sql("ALTER TABLE paimon.db.t ADD COLUMN age INT").await?;
+/// ```
+pub struct PaimonDdlHandler {
+ ctx: SessionContext,
+ catalog: Arc<dyn Catalog>,
+ /// The catalog name registered in the SessionContext (used to strip the catalog prefix).
+ catalog_name: String,
+}
+
+impl PaimonDdlHandler {
+ pub fn new(
+ ctx: SessionContext,
+ catalog: Arc<dyn Catalog>,
+ catalog_name: impl Into<String>,
+ ) -> Self {
+ Self {
+ ctx,
+ catalog,
+ catalog_name: catalog_name.into(),
+ }
+ }
+
+ /// Returns a reference to the inner [`SessionContext`].
+ pub fn ctx(&self) -> &SessionContext {
+ &self.ctx
+ }
+
+ /// Execute a SQL statement. ALTER TABLE is handled by Paimon directly;
+ /// everything else is delegated to DataFusion.
+ pub async fn sql(&self, sql: &str) -> DFResult<DataFrame> {
+ let dialect = GenericDialect {};
+ let statements = Parser::parse_sql(&dialect, sql)
+ .map_err(|e| DataFusionError::Plan(format!("SQL parse error: {e}")))?;
+
+ if statements.len() != 1 {
+ return Err(DataFusionError::Plan(
+ "Expected exactly one SQL statement".to_string(),
+ ));
+ }
+
+ match &statements[0] {
+ Statement::CreateTable(create_table) => self.handle_create_table(create_table).await,
+ Statement::AlterTable {
+ name, operations, ..
Review Comment:
Statement::AlterTable has an if_exists flag, but it is ignored here.
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(datafusion): Add DDL support with PRIMARY KEY constraint syntax [paimon-rust]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #237:
URL: https://github.com/apache/paimon-rust/pull/237#discussion_r3069253469
##########
crates/integrations/datafusion/src/table_factory.rs:
##########
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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.
+
+//! [`TableProviderFactory`] implementation for creating Paimon tables via
+//! `CREATE EXTERNAL TABLE`.
+
+use std::fmt::Debug;
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use datafusion::catalog::{Session, TableProvider, TableProviderFactory};
+use datafusion::common::TableReference;
+use datafusion::error::{DataFusionError, Result as DFResult};
+use datafusion::logical_expr::CreateExternalTable;
+use paimon::catalog::{Catalog, Identifier};
+use paimon::spec::Schema;
+
+use crate::error::to_datafusion_error;
+use crate::table::PaimonTableProvider;
+use paimon::arrow::arrow_to_paimon_type;
+
+/// A [`TableProviderFactory`] that creates Paimon tables.
+///
+/// Register with:
+/// ```ignore
+/// ctx.state_mut().table_factories_mut()
+/// .insert("PAIMON".to_string(), Arc::new(PaimonTableFactory::new(catalog)));
+/// ```
+///
+/// Then use:
+/// ```sql
+/// CREATE EXTERNAL TABLE paimon.my_db.my_table (
+/// id INT NOT NULL,
+/// name STRING,
+/// dt STRING,
+/// PRIMARY KEY (id, dt)
+/// ) PARTITIONED BY (dt)
+/// WITH ('bucket' = '2');
+/// ```
+pub struct PaimonTableFactory {
+ catalog: Arc<dyn Catalog>,
+}
+
+impl Debug for PaimonTableFactory {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("PaimonTableFactory").finish()
+ }
+}
+
+impl PaimonTableFactory {
+ pub fn new(catalog: Arc<dyn Catalog>) -> Self {
+ Self { catalog }
+ }
+}
+
+#[async_trait]
+impl TableProviderFactory for PaimonTableFactory {
+ async fn create(
+ &self,
+ _state: &dyn Session,
+ cmd: &CreateExternalTable,
+ ) -> DFResult<Arc<dyn TableProvider>> {
+ if !cmd.location.is_empty() {
+ return Err(DataFusionError::Plan(
+ "LOCATION is not supported for Paimon tables. Table path is determined by the catalog warehouse.".to_string(),
+ ));
+ }
+
+ let identifier = resolve_identifier(&cmd.name)?;
+
+ // Build Paimon schema from the CREATE EXTERNAL TABLE command.
Review Comment:
Good point, let' ban EXTERNAL.
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(datafusion): Add DDL support with PRIMARY KEY constraint syntax [paimon-rust]
Posted by "littlecoder04 (via GitHub)" <gi...@apache.org>.
littlecoder04 commented on PR #237:
URL: https://github.com/apache/paimon-rust/pull/237#issuecomment-4231291672
+1
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(datafusion): Add DDL support with PRIMARY KEY constraint syntax [paimon-rust]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi merged PR #237:
URL: https://github.com/apache/paimon-rust/pull/237
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(datafusion): Add DDL support with PRIMARY KEY constraint syntax [paimon-rust]
Posted by "littlecoder04 (via GitHub)" <gi...@apache.org>.
littlecoder04 commented on code in PR #237:
URL: https://github.com/apache/paimon-rust/pull/237#discussion_r3068947012
##########
crates/integrations/datafusion/src/table_factory.rs:
##########
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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.
+
+//! [`TableProviderFactory`] implementation for creating Paimon tables via
+//! `CREATE EXTERNAL TABLE`.
+
+use std::fmt::Debug;
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use datafusion::catalog::{Session, TableProvider, TableProviderFactory};
+use datafusion::common::TableReference;
+use datafusion::error::{DataFusionError, Result as DFResult};
+use datafusion::logical_expr::CreateExternalTable;
+use paimon::catalog::{Catalog, Identifier};
+use paimon::spec::Schema;
+
+use crate::error::to_datafusion_error;
+use crate::table::PaimonTableProvider;
+use paimon::arrow::arrow_to_paimon_type;
+
+/// A [`TableProviderFactory`] that creates Paimon tables.
+///
+/// Register with:
+/// ```ignore
+/// ctx.state_mut().table_factories_mut()
+/// .insert("PAIMON".to_string(), Arc::new(PaimonTableFactory::new(catalog)));
+/// ```
+///
+/// Then use:
+/// ```sql
+/// CREATE EXTERNAL TABLE paimon.my_db.my_table (
+/// id INT NOT NULL,
+/// name STRING,
+/// dt STRING,
+/// PRIMARY KEY (id, dt)
+/// ) PARTITIONED BY (dt)
+/// WITH ('bucket' = '2');
+/// ```
+pub struct PaimonTableFactory {
+ catalog: Arc<dyn Catalog>,
+}
+
+impl Debug for PaimonTableFactory {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("PaimonTableFactory").finish()
+ }
+}
+
+impl PaimonTableFactory {
+ pub fn new(catalog: Arc<dyn Catalog>) -> Self {
+ Self { catalog }
+ }
+}
+
+#[async_trait]
+impl TableProviderFactory for PaimonTableFactory {
+ async fn create(
+ &self,
+ _state: &dyn Session,
+ cmd: &CreateExternalTable,
+ ) -> DFResult<Arc<dyn TableProvider>> {
+ if !cmd.location.is_empty() {
+ return Err(DataFusionError::Plan(
+ "LOCATION is not supported for Paimon tables. Table path is determined by the catalog warehouse.".to_string(),
+ ));
+ }
+
+ let identifier = resolve_identifier(&cmd.name)?;
+
+ // Build Paimon schema from the CREATE EXTERNAL TABLE command.
Review Comment:
My understanding is that both CREATE TABLE and CREATE EXTERNAL TABLE create managed Paimon tables here, rather than true external tables. It looks like two SQL entry points for creating the managed Paimon tables. Is that right?
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org