Skip to main content

deltalake_core/table/
columns.rs

1//! Constraints and generated column mappings
2use serde::{Deserialize, Serialize};
3
4use crate::kernel::DataType;
5use crate::table::DataCheck;
6use std::any::Any;
7
8/// A constraint in a check constraint
9#[derive(Eq, PartialEq, Debug, Default, Clone, Serialize, Deserialize)]
10pub struct Constraint {
11    /// The full path to the field.
12    pub name: String,
13    /// The SQL string that must always evaluate to true.
14    pub expr: String,
15}
16
17impl Constraint {
18    /// Create a new invariant
19    pub fn new(field_name: &str, invariant_sql: &str) -> Self {
20        Self {
21            name: field_name.to_string(),
22            expr: invariant_sql.to_string(),
23        }
24    }
25}
26
27impl DataCheck for Constraint {
28    fn get_name(&self) -> &str {
29        &self.name
30    }
31
32    fn get_expression(&self) -> &str {
33        &self.expr
34    }
35
36    fn as_any(&self) -> &dyn Any {
37        self
38    }
39}
40
41/// A generated column
42#[derive(Eq, PartialEq, Debug, Clone)]
43pub struct GeneratedColumn {
44    /// The full path to the field.
45    pub name: String,
46    /// The SQL string that generate the column value.
47    pub generation_expr: String,
48    /// The SQL string that must always evaluate to true.
49    pub validation_expr: String,
50    /// Data Type
51    pub data_type: DataType,
52}
53
54impl GeneratedColumn {
55    /// Create a new invariant
56    pub fn new(field_name: &str, sql_generation: &str, data_type: &DataType) -> Self {
57        Self {
58            name: field_name.to_string(),
59            generation_expr: sql_generation.to_string(),
60            validation_expr: format!("{field_name} <=> {sql_generation}"), // update to
61            data_type: data_type.clone(),
62        }
63    }
64
65    /// Returns the SQL expression used to generate this column's values.
66    pub fn get_generation_expression(&self) -> &str {
67        &self.generation_expr
68    }
69}
70
71impl DataCheck for GeneratedColumn {
72    fn get_name(&self) -> &str {
73        &self.name
74    }
75
76    fn get_expression(&self) -> &str {
77        &self.validation_expr
78    }
79
80    fn as_any(&self) -> &dyn Any {
81        self
82    }
83}