feat(transwarp): add Inceptor data source with table operations
This commit is contained in:
1 parent
597b134d21
commit
6fdc338348
101 files changed
+2300
-66
No files matched your search
@@ -279,6 +279,12 @@ fn build_create_database_statement(options: &CreateDatabaseSqlOptions) -> Result
|
||||
if !supports_create_database_target(options.database_type, options.driver_profile.as_deref()) {
|
||||
return Err(format!("Creating databases is not supported for {}.", database_label(options.database_type)));
|
||||
}
|
||||
if options.database_type == Some(DatabaseType::Transwarp) {
|
||||
return Ok(format!(
|
||||
"CREATE DATABASE IF NOT EXISTS {};",
|
||||
quote_table_identifier(options.database_type, &options.name)
|
||||
));
|
||||
}
|
||||
if is_informix_family(options.database_type, options.driver_profile.as_deref()) {
|
||||
// Informix / GBase 8s accept only a bare `CREATE DATABASE <name>`. The new database
|
||||
// inherits the instance default locale, and the MySQL `CHARACTER SET`/`COLLATE`
|
||||
@@ -347,6 +353,7 @@ pub fn supports_create_database_target(database_type: Option<DatabaseType>, driv
|
||||
| DatabaseType::Highgo
|
||||
| DatabaseType::Kingbase
|
||||
| DatabaseType::Yashandb
|
||||
| DatabaseType::Transwarp
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -691,7 +698,11 @@ fn supports_truncate_table_cascade(database_type: Option<DatabaseType>) -> bool
|
||||
}
|
||||
|
||||
pub fn build_drop_database_sql(options: DatabaseNameSqlOptions) -> String {
|
||||
format!("DROP DATABASE {};", quote_table_identifier(options.database_type, &options.name))
|
||||
let name = quote_table_identifier(options.database_type, &options.name);
|
||||
if options.database_type == Some(DatabaseType::Transwarp) {
|
||||
return format!("DROP DATABASE IF EXISTS {name};");
|
||||
}
|
||||
format!("DROP DATABASE {name};")
|
||||
}
|
||||
|
||||
pub fn build_update_database_properties_sql(options: DatabasePropertyEditSqlOptions) -> Result<String, String> {
|
||||
@@ -921,6 +932,9 @@ pub fn supports_object_rename(database_type: Option<DatabaseType>, object_type:
|
||||
if database_type == DatabaseType::SqlServer {
|
||||
return true;
|
||||
}
|
||||
if database_type == DatabaseType::Transwarp {
|
||||
return object_type == DatabaseObjectType::Table;
|
||||
}
|
||||
if matches!(object_type, DatabaseObjectType::Procedure | DatabaseObjectType::Function) {
|
||||
return false;
|
||||
}
|
||||
@@ -964,6 +978,14 @@ pub fn build_rename_object_sql(options: RenameObjectSqlOptions) -> Result<String
|
||||
));
|
||||
}
|
||||
|
||||
if database_type == Some(DatabaseType::Transwarp) {
|
||||
return Ok(format!(
|
||||
"ALTER TABLE {} RENAME TO {};",
|
||||
qualified_name(database_type, options.schema.as_deref(), &options.old_name),
|
||||
qualified_name(database_type, options.schema.as_deref(), &options.new_name)
|
||||
));
|
||||
}
|
||||
|
||||
if matches!(database_type, Some(DatabaseType::Mysql | DatabaseType::Goldendb)) {
|
||||
return Ok(format!(
|
||||
"RENAME TABLE {} TO {};",
|
||||
@@ -1539,6 +1561,50 @@ mod tests {
|
||||
assert!(!supports_create_database_target(Some(DatabaseType::Gbase), Some("gbase8a")));
|
||||
assert!(!supports_create_database_target(Some(DatabaseType::Gbase), None));
|
||||
assert!(supports_create_database_target(Some(DatabaseType::Informix), None));
|
||||
assert!(supports_create_database_target(Some(DatabaseType::Transwarp), Some("transwarp-inceptor")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transwarp_database_actions_match_waterdrop_ddl() {
|
||||
assert!(!supports_create_schema_target(Some(DatabaseType::Transwarp)));
|
||||
assert_eq!(
|
||||
build_create_database_sql(CreateDatabaseSqlOptions {
|
||||
database_type: Some(DatabaseType::Transwarp),
|
||||
driver_profile: Some("transwarp-inceptor".to_string()),
|
||||
target: None,
|
||||
parent: None,
|
||||
name: "analytics db".to_string(),
|
||||
charset: None,
|
||||
collation: None,
|
||||
})
|
||||
.unwrap(),
|
||||
"CREATE DATABASE IF NOT EXISTS `analytics db`;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_drop_database_sql(DatabaseNameSqlOptions {
|
||||
database_type: Some(DatabaseType::Transwarp),
|
||||
name: "analytics db".to_string(),
|
||||
}),
|
||||
"DROP DATABASE IF EXISTS `analytics db`;"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transwarp_table_rename_matches_waterdrop_ddl() {
|
||||
assert!(supports_object_rename(Some(DatabaseType::Transwarp), DatabaseObjectType::Table));
|
||||
assert!(!supports_object_rename(Some(DatabaseType::Transwarp), DatabaseObjectType::View));
|
||||
assert!(!supports_database_rename(Some(DatabaseType::Transwarp)));
|
||||
assert_eq!(
|
||||
build_rename_object_sql(RenameObjectSqlOptions {
|
||||
database_type: Some(DatabaseType::Transwarp),
|
||||
object_type: DatabaseObjectType::Table,
|
||||
schema: Some("analytics".to_string()),
|
||||
old_name: "old table".to_string(),
|
||||
new_name: "new table".to_string(),
|
||||
})
|
||||
.unwrap(),
|
||||
"ALTER TABLE `analytics`.`old table` RENAME TO `analytics`.`new table`;"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -2938,6 +3004,7 @@ mod tests {
|
||||
table_comment: None,
|
||||
original_table_comment: None,
|
||||
mysql_engine: None,
|
||||
transwarp_create: None,
|
||||
partitioned: false,
|
||||
is_gaussdb_m_mode: false,
|
||||
table_collation: None,
|
||||
|
||||
@@ -9,6 +9,7 @@ mod indexes;
|
||||
mod mysql_engine;
|
||||
mod owner;
|
||||
mod partitions;
|
||||
mod transwarp;
|
||||
mod triggers;
|
||||
mod types;
|
||||
mod util;
|
||||
|
||||
@@ -372,6 +372,15 @@ pub(super) fn build_mysql_existing_column_clause(column: &EditableStructureColum
|
||||
format!("{operation}{position_clause}")
|
||||
}
|
||||
|
||||
pub(super) fn build_transwarp_existing_column_clause(column: &EditableStructureColumn) -> String {
|
||||
let original_name = column.original.as_ref().map(|original| original.name.as_str()).unwrap_or(&column.name);
|
||||
format!(
|
||||
"CHANGE {} {}",
|
||||
quote_ident(StructureDialect::Mysql, original_name),
|
||||
column_definition(StructureDialect::Mysql, column)
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn build_doris_existing_column_sql(
|
||||
table: &str,
|
||||
column: &EditableStructureColumn,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use super::column_alter::build_transwarp_existing_column_clause;
|
||||
use super::column_alter::{
|
||||
build_clickhouse_existing_column_sql, build_dameng_existing_column_sql, build_doris_existing_column_sql,
|
||||
build_duckdb_existing_column_sql, build_h2_existing_column_sql, build_informix_existing_column_sql,
|
||||
@@ -115,7 +116,8 @@ pub(super) fn build_column_sql(options: &TableStructureSqlOptions, warnings: &mu
|
||||
}
|
||||
|
||||
let active_index = active_columns.iter().position(|active| active.id == column.id).unwrap_or(0);
|
||||
let position_clause = if has_original_column_positions {
|
||||
let position_clause = if has_original_column_positions && options.database_type != Some(DatabaseType::Transwarp)
|
||||
{
|
||||
column_position_clause(dialect, &active_columns, active_index)
|
||||
} else {
|
||||
String::new()
|
||||
@@ -248,10 +250,12 @@ pub(super) fn build_column_sql(options: &TableStructureSqlOptions, warnings: &mu
|
||||
} else {
|
||||
column
|
||||
};
|
||||
let clause = build_mysql_existing_column_clause(
|
||||
effective_column,
|
||||
if has_position_change { &position_clause } else { "" },
|
||||
);
|
||||
let position = if has_position_change { &position_clause } else { "" };
|
||||
let clause = if options.database_type == Some(DatabaseType::Transwarp) {
|
||||
build_transwarp_existing_column_clause(effective_column)
|
||||
} else {
|
||||
build_mysql_existing_column_clause(effective_column, position)
|
||||
};
|
||||
if mysql_coalesced_primary_key_change
|
||||
.is_some_and(|change| mysql_auto_increment_touches_primary_key(column, change))
|
||||
{
|
||||
@@ -592,7 +596,9 @@ pub(super) fn build_add_column_sql(
|
||||
driver_profile: Option<&str>,
|
||||
) -> Vec<String> {
|
||||
let definition = column_definition(dialect, column);
|
||||
let mut statements = if is_oracle_like(dialect) || dialect == StructureDialect::Informix {
|
||||
let mut statements = if database_type == Some(DatabaseType::Transwarp) {
|
||||
vec![format!("ALTER TABLE {table} ADD COLUMNS ({definition});")]
|
||||
} else if is_oracle_like(dialect) || dialect == StructureDialect::Informix {
|
||||
vec![format!("ALTER TABLE {table} ADD ({definition});")]
|
||||
} else {
|
||||
let add_keyword = if dialect == StructureDialect::SqlServer
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use super::dialect::{capabilities_for, database_label, dialect_label, StructureDialect};
|
||||
use super::types::TableStructureSqlOptions;
|
||||
use super::util::{clean, qualified_table, quote_string};
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
pub(super) fn build_table_comment_sql(options: &TableStructureSqlOptions, warnings: &mut Vec<String>) -> Vec<String> {
|
||||
let capabilities = capabilities_for(options.database_type, options.driver_profile.as_deref());
|
||||
@@ -19,6 +20,9 @@ pub(super) fn build_table_comment_sql(options: &TableStructureSqlOptions, warnin
|
||||
let dialect = capabilities.dialect;
|
||||
let table = qualified_table(dialect, options.schema.as_deref(), &options.table_name);
|
||||
let quoted = quote_string(&clean(new_comment));
|
||||
if options.database_type == Some(DatabaseType::Transwarp) {
|
||||
return vec![format!("ALTER TABLE {table} SET TBLPROPERTIES ('comment' = {quoted});")];
|
||||
}
|
||||
match dialect {
|
||||
StructureDialect::Mysql | StructureDialect::GaussdbM => {
|
||||
vec![format!("ALTER TABLE {table} COMMENT = {quoted};")]
|
||||
|
||||
@@ -8,6 +8,7 @@ use super::dialect::{capabilities_for, database_label, StructureDialect};
|
||||
use super::foreign_keys::build_foreign_key_sql_for_new_table;
|
||||
use super::indexes::build_create_index_statements;
|
||||
use super::mysql_engine::{append_mysql_table_option, validate_mysql_engine};
|
||||
use super::transwarp;
|
||||
use super::triggers::build_trigger_sql_for_new_table;
|
||||
use super::types::{TableStructureSqlOptions, TableStructureSqlResult};
|
||||
use super::util::{
|
||||
@@ -63,6 +64,7 @@ pub(super) fn build_create_table_sql_with_partition_clause(
|
||||
}
|
||||
validate_columns(&active_columns, &mut warnings);
|
||||
validate_dameng_identity(&options, &active_columns, &mut warnings);
|
||||
transwarp::validate_create_options(&options, &active_columns, &mut warnings);
|
||||
if !warnings.is_empty() {
|
||||
return TableStructureSqlResult { statements: Vec::new(), warnings };
|
||||
}
|
||||
@@ -105,6 +107,9 @@ pub(super) fn build_create_table_sql_with_partition_clause(
|
||||
let mut column_definitions = Vec::new();
|
||||
|
||||
for column in &active_columns {
|
||||
if transwarp::is_partition_column(&options, column) {
|
||||
continue;
|
||||
}
|
||||
let mut data_type = column_data_type(dialect, column);
|
||||
// SQLite accepts AUTOINCREMENT only on an exact INTEGER PRIMARY KEY,
|
||||
// so integer-family aliases are normalized when auto-increment is on.
|
||||
@@ -190,7 +195,8 @@ pub(super) fn build_create_table_sql_with_partition_clause(
|
||||
column_definitions.push(format!("PRIMARY KEY ({pk_list})"));
|
||||
}
|
||||
|
||||
let create_table = format!("CREATE TABLE {table} (\n {}\n)", column_definitions.join(",\n "));
|
||||
let mut create_table = format!("CREATE TABLE {table} (\n {}\n)", column_definitions.join(",\n "));
|
||||
create_table.push_str(&transwarp::create_table_suffix(&options, &active_columns, dialect));
|
||||
let create_table = match partition_clause {
|
||||
Some(clause) => format!("{create_table} {clause}"),
|
||||
None => create_table,
|
||||
@@ -205,7 +211,7 @@ pub(super) fn build_create_table_sql_with_partition_clause(
|
||||
|
||||
if capabilities.comment {
|
||||
let table_comment = clean(options.table_comment.as_deref().unwrap_or(""));
|
||||
if !table_comment.is_empty() {
|
||||
if !table_comment.is_empty() && options.database_type != Some(DatabaseType::Transwarp) {
|
||||
if matches!(dialect, StructureDialect::Mysql | StructureDialect::GaussdbM) {
|
||||
if let Some(last) = statements.last_mut() {
|
||||
append_mysql_table_option(last, &format!("COMMENT = {}", quote_string(&table_comment)));
|
||||
|
||||
@@ -379,6 +379,15 @@ pub(super) fn capabilities_for(
|
||||
rebuild_index: true,
|
||||
..base
|
||||
},
|
||||
// Inceptor accepts ADD COLUMNS and CHANGE, but not DROP COLUMN.
|
||||
Some(DatabaseType::Transwarp) => TableStructureCapabilities {
|
||||
dialect: StructureDialect::Mysql,
|
||||
add_column: true,
|
||||
rename_column: true,
|
||||
alter_existing_column: true,
|
||||
comment: true,
|
||||
..base
|
||||
},
|
||||
_ => base,
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,124 @@
|
||||
use super::column_format::column_data_type;
|
||||
use super::dialect::StructureDialect;
|
||||
use super::types::{EditableStructureColumn, TableStructureSqlOptions, TranswarpCreateTableOptions};
|
||||
use super::util::{clean, quote_new_ident, quote_string};
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
pub(super) fn validate_create_options(
|
||||
options: &TableStructureSqlOptions,
|
||||
active_columns: &[&EditableStructureColumn],
|
||||
warnings: &mut Vec<String>,
|
||||
) {
|
||||
let Some(create) = options.transwarp_create.as_ref() else { return };
|
||||
if options.database_type != Some(DatabaseType::Transwarp) {
|
||||
warnings.push("Inceptor table options require a Transwarp connection.".to_string());
|
||||
return;
|
||||
}
|
||||
for name in create.partition_columns.iter().chain(&create.bucket_columns) {
|
||||
if !active_columns.iter().any(|column| column.name.eq_ignore_ascii_case(name.trim())) {
|
||||
warnings.push(format!("Inceptor column {name:?} does not exist in the table draft."));
|
||||
}
|
||||
}
|
||||
if create.partition_columns.len() >= active_columns.len() && !create.partition_columns.is_empty() {
|
||||
warnings.push("Inceptor partitioned tables need at least one non-partition column.".to_string());
|
||||
}
|
||||
for names in [&create.partition_columns, &create.bucket_columns] {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for name in names {
|
||||
if name.trim().is_empty() || !seen.insert(name.trim().to_ascii_lowercase()) {
|
||||
warnings.push("Inceptor partition and bucket columns must be distinct and non-empty.".to_string());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if create
|
||||
.bucket_columns
|
||||
.iter()
|
||||
.any(|bucket| create.partition_columns.iter().any(|partition| partition.eq_ignore_ascii_case(bucket)))
|
||||
{
|
||||
warnings.push("Inceptor bucket columns cannot also be partition columns.".to_string());
|
||||
}
|
||||
if create.bucket_columns.is_empty() != create.bucket_count.is_none()
|
||||
|| create.bucket_count.is_some_and(|count| count == 0 || count > 4096)
|
||||
{
|
||||
warnings.push("Inceptor bucketing requires columns and a bucket count from 1 to 4096.".to_string());
|
||||
}
|
||||
let storage = create.storage_format.as_deref().unwrap_or("").trim();
|
||||
if !storage.is_empty()
|
||||
&& !["ORC", "PARQUET", "TEXTFILE"].iter().any(|allowed| storage.eq_ignore_ascii_case(allowed))
|
||||
{
|
||||
warnings.push("Unsupported Inceptor storage format.".to_string());
|
||||
}
|
||||
if create.transactional && (!storage.eq_ignore_ascii_case("ORC") || create.bucket_columns.is_empty()) {
|
||||
warnings.push("Inceptor transactional tables require ORC storage and bucket columns.".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn is_partition_column(options: &TableStructureSqlOptions, column: &EditableStructureColumn) -> bool {
|
||||
options.database_type == Some(DatabaseType::Transwarp)
|
||||
&& options.transwarp_create.as_ref().is_some_and(|create| {
|
||||
create.partition_columns.iter().any(|name| column.name.eq_ignore_ascii_case(name.trim()))
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn create_table_suffix(
|
||||
options: &TableStructureSqlOptions,
|
||||
active_columns: &[&EditableStructureColumn],
|
||||
dialect: StructureDialect,
|
||||
) -> String {
|
||||
if options.database_type != Some(DatabaseType::Transwarp) {
|
||||
return String::new();
|
||||
}
|
||||
let mut suffix = String::new();
|
||||
let comment = clean(options.table_comment.as_deref().unwrap_or(""));
|
||||
if !comment.is_empty() {
|
||||
suffix.push_str(&format!(" COMMENT {}", quote_string(&comment)));
|
||||
}
|
||||
if let Some(create) = options.transwarp_create.as_ref() {
|
||||
suffix.push_str(&create_options_clause(create, active_columns, dialect));
|
||||
}
|
||||
suffix
|
||||
}
|
||||
|
||||
fn create_options_clause(
|
||||
create: &TranswarpCreateTableOptions,
|
||||
active_columns: &[&EditableStructureColumn],
|
||||
dialect: StructureDialect,
|
||||
) -> String {
|
||||
let mut clauses = String::new();
|
||||
if !create.partition_columns.is_empty() {
|
||||
let partition_columns = create
|
||||
.partition_columns
|
||||
.iter()
|
||||
.map(|name| {
|
||||
let column = active_columns
|
||||
.iter()
|
||||
.find(|column| column.name.eq_ignore_ascii_case(name.trim()))
|
||||
.expect("validated partition column");
|
||||
format!(
|
||||
"{} {}",
|
||||
quote_new_ident(Some(DatabaseType::Transwarp), dialect, &column.name),
|
||||
column_data_type(dialect, column)
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
clauses.push_str(&format!(" PARTITIONED BY ({partition_columns})"));
|
||||
}
|
||||
if let Some(count) = create.bucket_count {
|
||||
let columns = create
|
||||
.bucket_columns
|
||||
.iter()
|
||||
.map(|name| quote_new_ident(Some(DatabaseType::Transwarp), dialect, name.trim()))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
clauses.push_str(&format!(" CLUSTERED BY ({columns}) INTO {count} BUCKETS"));
|
||||
}
|
||||
if let Some(storage) = create.storage_format.as_deref().map(str::trim).filter(|value| !value.is_empty()) {
|
||||
clauses.push_str(&format!(" STORED AS {}", storage.to_ascii_uppercase()));
|
||||
}
|
||||
if create.transactional {
|
||||
clauses.push_str(" TBLPROPERTIES ('transactional'='true')");
|
||||
}
|
||||
clauses
|
||||
}
|
||||
@@ -237,6 +237,8 @@ pub struct TableStructureSqlOptions {
|
||||
pub original_table_comment: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub mysql_engine: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub transwarp_create: Option<TranswarpCreateTableOptions>,
|
||||
/// MySQL only: the table's current default collation
|
||||
/// (`information_schema.TABLES.TABLE_COLLATION`). A column whose collation
|
||||
/// merely matches it inherits the table default, so its `CHARACTER SET` /
|
||||
@@ -259,6 +261,21 @@ pub struct TableStructureSqlOptions {
|
||||
pub is_gaussdb_m_mode: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TranswarpCreateTableOptions {
|
||||
#[serde(default)]
|
||||
pub partition_columns: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub bucket_columns: Vec<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub bucket_count: Option<u32>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub storage_format: Option<String>,
|
||||
#[serde(default)]
|
||||
pub transactional: bool,
|
||||
}
|
||||
|
||||
/// Options for `build_table_partition_operation_sql`.
|
||||
///
|
||||
/// Partition maintenance is deliberately kept out of `TableStructureSqlOptions`:
|
||||
|
||||
Reference in new issue
Block a user