zealchen commented on code in PR #1620:
URL: https://github.com/apache/horaedb/pull/1620#discussion_r1898224383


##########
src/metric_engine/src/types.rs:
##########
@@ -132,18 +140,107 @@ pub struct WriteResult {
     pub size: usize,
 }
 
+/// The schema is like:
+/// ```plaintext
+/// primary_key1, primary_key2, ..., primary_keyN, value1, value2, ..., 
valueM, seq, reserved
+/// ```
+/// seq and reserved are builtin columns, and they will be appended to the end
+/// of the original schema.
 #[derive(Debug, Clone)]
 pub struct StorageSchema {
     pub arrow_schema: SchemaRef,
     pub num_primary_keys: usize,
     pub seq_idx: usize,
+    pub reserved_idx: usize,
     pub value_idxes: Vec<usize>,
     pub update_mode: UpdateMode,
 }
 
+impl StorageSchema {
+    pub fn try_new(
+        arrow_schema: SchemaRef,
+        num_primary_keys: usize,
+        update_mode: UpdateMode,
+    ) -> Result<Self> {
+        ensure!(num_primary_keys > 0, "num_primary_keys should large than 0");
+
+        let fields = arrow_schema.fields();
+        ensure!(
+            !fields.iter().any(Self::is_builtin_field),
+            "schema should not use builtin columns name"
+        );
+
+        let value_idxes = 
(num_primary_keys..arrow_schema.fields.len()).collect::<Vec<_>>();
+        ensure!(!value_idxes.is_empty(), "no value column found");
+
+        let mut new_fields = arrow_schema.fields().clone().to_vec();
+        new_fields.extend_from_slice(&[
+            Arc::new(Field::new(SEQ_COLUMN_NAME, DataType::UInt64, true)),
+            Arc::new(Field::new(RESERVED_COLUMN_NAME, DataType::UInt64, true)),
+        ]);
+        let seq_idx = new_fields.len() - 2;
+        let reserved_idx = new_fields.len() - 1;
+
+        let arrow_schema = Arc::new(Schema::new_with_metadata(
+            new_fields,
+            arrow_schema.metadata.clone(),
+        ));
+        Ok(Self {
+            arrow_schema,
+            num_primary_keys,
+            seq_idx,
+            reserved_idx,
+            value_idxes,
+            update_mode,
+        })
+    }
+
+    pub fn is_builtin_field(f: &FieldRef) -> bool {
+        f.name() == SEQ_COLUMN_NAME || f.name() == RESERVED_COLUMN_NAME
+    }
+
+    pub fn fill_builtin_projections(&self, projection: &mut 
Option<Vec<usize>>) {
+        if let Some(proj) = projection.as_mut() {
+            for i in 0..self.num_primary_keys {
+                if !proj.contains(&i) {

Review Comment:
   primary keys are not builtin columns, why put them in 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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to