XiaoHongbo-Hope commented on code in PR #92:
URL: https://github.com/apache/paimon-rust/pull/92#discussion_r2811542666
##########
crates/paimon/src/spec/schema.rs:
##########
@@ -102,6 +102,344 @@ pub fn escape_single_quotes(text: &str) -> String {
text.replace('\'', "''")
}
+// ======================= Schema (DDL) ===============================
+
+/// Option key for primary key in table options (same as
[CoreOptions.PRIMARY_KEY](https://github.com/apache/paimon/blob/release-1.3/paimon-api/src/main/java/org/apache/paimon/CoreOptions.java)).
+pub const PRIMARY_KEY_OPTION: &str = "primary-key";
+/// Option key for partition in table options (same as
[CoreOptions.PARTITION](https://github.com/apache/paimon/blob/release-1.3/paimon-api/src/main/java/org/apache/paimon/CoreOptions.java)).
+pub const PARTITION_OPTION: &str = "partition";
+
+/// Schema of a table (logical DDL schema).
+///
+/// Corresponds to
[org.apache.paimon.schema.Schema](https://github.com/apache/paimon/blob/1.3/paimon-api/src/main/java/org/apache/paimon/schema/Schema.java).
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct Schema {
+ fields: Vec<DataField>,
+ partition_keys: Vec<String>,
+ primary_keys: Vec<String>,
+ options: HashMap<String, String>,
+ comment: Option<String>,
+}
+
+impl Schema {
+ /// Build a schema with validation. Normalizes partition/primary keys from
options if present.
+ pub fn new(
+ fields: Vec<DataField>,
+ partition_keys: Vec<String>,
+ primary_keys: Vec<String>,
+ mut options: HashMap<String, String>,
+ comment: Option<String>,
+ ) -> crate::Result<Self> {
+ let primary_keys = Self::normalize_primary_keys(&primary_keys, &mut
options)?;
+ let partition_keys = Self::normalize_partition_keys(&partition_keys,
&mut options)?;
+ let fields = Self::normalize_fields(&fields, &partition_keys,
&primary_keys)?;
+
+ Ok(Self {
+ fields,
+ partition_keys,
+ primary_keys,
+ options,
+ comment,
+ })
+ }
+
+ /// Normalize primary keys: optionally take from table options
(`primary-key`), remove from options.
+ /// Corresponds to Java `normalizePrimaryKeys`.
+ fn normalize_primary_keys(
+ primary_keys: &[String],
+ options: &mut HashMap<String, String>,
+ ) -> crate::Result<Vec<String>> {
+ if let Some(pk) = options.remove(PRIMARY_KEY_OPTION) {
+ if !primary_keys.is_empty() {
Review Comment:
Will this affect the the original options? We can work on a clone of options
to avoid mutating the original one.
--
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]