shyjsarah commented on code in PR #444:
URL: https://github.com/apache/paimon-rust/pull/444#discussion_r3518759270
##########
crates/integrations/datafusion/src/table/mod.rs:
##########
@@ -89,6 +97,119 @@ impl PaimonTableProvider {
}
}
+/// Build a `CREATE TABLE` DDL string for a Paimon table.
+///
+/// Mirrors the syntax accepted by `SQLContext::handle_create_table`:
+/// `CREATE TABLE <db>.<table> (<col> <type>, ..., PRIMARY KEY (...))
[PARTITIONED BY (...)] [WITH ('k'='v', ...)]`.
+fn build_table_definition(table: &Table) -> String {
+ let identifier = table.identifier();
+ let schema = table.schema();
+ let mut ddl = String::new();
+ let _ = write!(
+ ddl,
+ "CREATE TABLE {}.{} (",
+ identifier.database(),
+ identifier.object()
+ );
+
+ for (i, field) in schema.fields().iter().enumerate() {
+ if i > 0 {
+ ddl.push_str(", ");
+ }
+ let _ = write!(
+ ddl,
+ "{} {}",
+ field.name(),
+ data_type_to_sql(field.data_type())
+ );
+ }
+
+ let pks = schema.primary_keys();
+ if !pks.is_empty() {
+ ddl.push_str(", PRIMARY KEY (");
+ for (i, pk) in pks.iter().enumerate() {
+ if i > 0 {
+ ddl.push_str(", ");
+ }
+ let _ = write!(ddl, "{}", pk);
+ }
+ ddl.push(')');
+ }
+ ddl.push(')');
+
+ let partition_keys = schema.partition_keys();
+ if !partition_keys.is_empty() {
+ ddl.push_str(" PARTITIONED BY (");
+ for (i, pk) in partition_keys.iter().enumerate() {
+ if i > 0 {
+ ddl.push_str(", ");
+ }
+ let _ = write!(ddl, "{}", pk);
+ }
+ ddl.push(')');
+ }
+
+ let options = schema.options();
+ if !options.is_empty() {
+ ddl.push_str(" WITH (");
+ for (i, (k, v)) in options.iter().enumerate() {
+ if i > 0 {
+ ddl.push_str(", ");
+ }
+ let _ = write!(ddl, "'{}' = '{}'", k, v);
+ }
+ ddl.push(')');
+ }
+
+ ddl
+}
+
+/// Render a Paimon [`DataType`] as a SQL type string matching the syntax
+/// accepted by paimon-rust's `CREATE TABLE` parser.
+fn data_type_to_sql(data_type: &DataType) -> String {
+ match data_type {
+ DataType::Boolean(_) => "BOOLEAN".to_string(),
+ DataType::TinyInt(_) => "TINYINT".to_string(),
+ DataType::SmallInt(_) => "SMALLINT".to_string(),
+ DataType::Int(_) => "INT".to_string(),
+ DataType::BigInt(_) => "BIGINT".to_string(),
+ DataType::Decimal(t) => format!("DECIMAL({}, {})", t.precision(),
t.scale()),
+ DataType::Double(_) => "DOUBLE".to_string(),
+ DataType::Float(_) => "FLOAT".to_string(),
+ DataType::Binary(t) => format!("BINARY({})", t.length()),
+ DataType::VarBinary(t) => format!("VARBINARY({})", t.length()),
+ DataType::Blob(_) => "BLOB".to_string(),
+ DataType::Char(t) => format!("CHAR({})", t.length()),
+ DataType::VarChar(t) => format!("VARCHAR({})", t.length()),
+ DataType::Date(_) => "DATE".to_string(),
+ DataType::Time(t) => format!("TIME({})", t.precision()),
+ DataType::Timestamp(t) => format!("TIMESTAMP({})", t.precision()),
+ DataType::LocalZonedTimestamp(t) => {
+ format!("TIMESTAMP_LTZ({})", t.precision())
+ }
+ DataType::Array(t) => format!("ARRAY<{}>",
data_type_to_sql(t.element_type())),
+ DataType::Map(t) => format!(
Review Comment:
Fixed in 8dfcb93 — `MAP<...>` changed to `MAP(key_type, value_type)` to
match what `SQLContext` parses.
##########
crates/integrations/datafusion/src/table/mod.rs:
##########
@@ -89,6 +97,119 @@ impl PaimonTableProvider {
}
}
+/// Build a `CREATE TABLE` DDL string for a Paimon table.
+///
+/// Mirrors the syntax accepted by `SQLContext::handle_create_table`:
+/// `CREATE TABLE <db>.<table> (<col> <type>, ..., PRIMARY KEY (...))
[PARTITIONED BY (...)] [WITH ('k'='v', ...)]`.
+fn build_table_definition(table: &Table) -> String {
+ let identifier = table.identifier();
+ let schema = table.schema();
+ let mut ddl = String::new();
+ let _ = write!(
+ ddl,
+ "CREATE TABLE {}.{} (",
+ identifier.database(),
+ identifier.object()
+ );
+
+ for (i, field) in schema.fields().iter().enumerate() {
+ if i > 0 {
+ ddl.push_str(", ");
+ }
+ let _ = write!(
+ ddl,
+ "{} {}",
+ field.name(),
+ data_type_to_sql(field.data_type())
+ );
+ }
+
+ let pks = schema.primary_keys();
+ if !pks.is_empty() {
+ ddl.push_str(", PRIMARY KEY (");
+ for (i, pk) in pks.iter().enumerate() {
+ if i > 0 {
+ ddl.push_str(", ");
+ }
+ let _ = write!(ddl, "{}", pk);
+ }
+ ddl.push(')');
+ }
+ ddl.push(')');
+
+ let partition_keys = schema.partition_keys();
+ if !partition_keys.is_empty() {
+ ddl.push_str(" PARTITIONED BY (");
+ for (i, pk) in partition_keys.iter().enumerate() {
+ if i > 0 {
+ ddl.push_str(", ");
+ }
+ let _ = write!(ddl, "{}", pk);
+ }
+ ddl.push(')');
+ }
+
+ let options = schema.options();
+ if !options.is_empty() {
+ ddl.push_str(" WITH (");
+ for (i, (k, v)) in options.iter().enumerate() {
+ if i > 0 {
+ ddl.push_str(", ");
+ }
+ let _ = write!(ddl, "'{}' = '{}'", k, v);
+ }
+ ddl.push(')');
+ }
+
+ ddl
+}
+
+/// Render a Paimon [`DataType`] as a SQL type string matching the syntax
+/// accepted by paimon-rust's `CREATE TABLE` parser.
+fn data_type_to_sql(data_type: &DataType) -> String {
+ match data_type {
+ DataType::Boolean(_) => "BOOLEAN".to_string(),
+ DataType::TinyInt(_) => "TINYINT".to_string(),
+ DataType::SmallInt(_) => "SMALLINT".to_string(),
+ DataType::Int(_) => "INT".to_string(),
+ DataType::BigInt(_) => "BIGINT".to_string(),
+ DataType::Decimal(t) => format!("DECIMAL({}, {})", t.precision(),
t.scale()),
+ DataType::Double(_) => "DOUBLE".to_string(),
+ DataType::Float(_) => "FLOAT".to_string(),
+ DataType::Binary(t) => format!("BINARY({})", t.length()),
+ DataType::VarBinary(t) => format!("VARBINARY({})", t.length()),
+ DataType::Blob(_) => "BLOB".to_string(),
+ DataType::Char(t) => format!("CHAR({})", t.length()),
+ DataType::VarChar(t) => format!("VARCHAR({})", t.length()),
+ DataType::Date(_) => "DATE".to_string(),
+ DataType::Time(t) => format!("TIME({})", t.precision()),
+ DataType::Timestamp(t) => format!("TIMESTAMP({})", t.precision()),
+ DataType::LocalZonedTimestamp(t) => {
+ format!("TIMESTAMP_LTZ({})", t.precision())
+ }
+ DataType::Array(t) => format!("ARRAY<{}>",
data_type_to_sql(t.element_type())),
+ DataType::Map(t) => format!(
Review Comment:
Fixed in 8dfcb93 — `MAP<...>` changed to `MAP(key_type, value_type)` to
match what `SQLContext` parses.
--
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]