polars_io/catalog/unity/
models.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use polars_core::prelude::PlHashMap;
use polars_utils::pl_str::PlSmallStr;

#[derive(Debug, serde::Deserialize)]
pub struct CatalogInfo {
    pub name: String,

    pub comment: Option<String>,

    #[serde(default)]
    pub storage_location: Option<String>,

    #[serde(default, deserialize_with = "null_to_default")]
    pub properties: PlHashMap<PlSmallStr, String>,

    #[serde(default, deserialize_with = "null_to_default")]
    pub options: PlHashMap<PlSmallStr, String>,

    #[serde(with = "chrono::serde::ts_milliseconds_option")]
    pub created_at: Option<chrono::DateTime<chrono::Utc>>,

    pub created_by: Option<String>,

    #[serde(with = "chrono::serde::ts_milliseconds_option")]
    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,

    pub updated_by: Option<String>,
}

#[derive(Debug, serde::Deserialize)]
pub struct NamespaceInfo {
    pub name: String,
    pub comment: Option<String>,

    #[serde(default, deserialize_with = "null_to_default")]
    pub properties: PlHashMap<PlSmallStr, String>,

    #[serde(default)]
    pub storage_location: Option<String>,

    #[serde(with = "chrono::serde::ts_milliseconds_option")]
    pub created_at: Option<chrono::DateTime<chrono::Utc>>,

    pub created_by: Option<String>,

    #[serde(with = "chrono::serde::ts_milliseconds_option")]
    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,

    pub updated_by: Option<String>,
}

#[derive(Debug, serde::Deserialize)]
pub struct TableInfo {
    pub name: String,
    pub table_id: String,
    pub table_type: TableType,

    #[serde(default)]
    pub comment: Option<String>,

    #[serde(default)]
    pub storage_location: Option<String>,

    #[serde(default)]
    pub data_source_format: Option<DataSourceFormat>,

    #[serde(default)]
    pub columns: Option<Vec<ColumnInfo>>,

    #[serde(default, deserialize_with = "null_to_default")]
    pub properties: PlHashMap<PlSmallStr, String>,

    #[serde(with = "chrono::serde::ts_milliseconds_option")]
    pub created_at: Option<chrono::DateTime<chrono::Utc>>,

    pub created_by: Option<String>,

    #[serde(with = "chrono::serde::ts_milliseconds_option")]
    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,

    pub updated_by: Option<String>,
}

#[derive(
    Debug, strum_macros::Display, strum_macros::EnumString, serde::Serialize, serde::Deserialize,
)]
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum TableType {
    Managed,
    External,
    View,
    MaterializedView,
    StreamingTable,
    ManagedShallowClone,
    Foreign,
    ExternalShallowClone,
}

#[derive(
    Debug, strum_macros::Display, strum_macros::EnumString, serde::Serialize, serde::Deserialize,
)]
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum DataSourceFormat {
    Delta,
    Csv,
    Json,
    Avro,
    Parquet,
    Orc,
    Text,

    // Databricks-specific
    UnityCatalog,
    Deltasharing,
    DatabricksFormat,
    MysqlFormat,
    PostgresqlFormat,
    RedshiftFormat,
    SnowflakeFormat,
    SqldwFormat,
    SqlserverFormat,
    SalesforceFormat,
    BigqueryFormat,
    NetsuiteFormat,
    WorkdayRaasFormat,
    HiveSerde,
    HiveCustom,
    VectorIndexFormat,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct ColumnInfo {
    pub name: PlSmallStr,
    pub type_name: PlSmallStr,
    pub type_text: PlSmallStr,
    pub type_json: String,
    pub position: Option<u32>,
    pub comment: Option<String>,
    pub partition_index: Option<u32>,
}

/// Note: This struct contains all the field names for a few different possible type / field presence
/// combinations. We use serde(default) and skip_serializing_if to get the desired serialization
/// output.
///
/// E.g.:
///
/// ```text
/// {
///     "name": "List",
///     "type": {"type": "array", "elementType": "long", "containsNull": True},
///     "nullable": True,
///     "metadata": {},
/// }
/// {
///     "name": "Struct",
///     "type": {
///         "type": "struct",
///         "fields": [{"name": "x", "type": "long", "nullable": True, "metadata": {}}],
///     },
///     "nullable": True,
///     "metadata": {},
/// }
/// {
///     "name": "ListStruct",
///     "type": {
///         "type": "array",
///         "elementType": {
///             "type": "struct",
///             "fields": [{"name": "x", "type": "long", "nullable": True, "metadata": {}}],
///         },
///         "containsNull": True,
///     },
///     "nullable": True,
///     "metadata": {},
/// }
/// {
///     "name": "Map",
///     "type": {
///         "type": "map",
///         "keyType": "string",
///         "valueType": "string",
///         "valueContainsNull": True,
///     },
///     "nullable": True,
///     "metadata": {},
/// }
/// ```
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct ColumnTypeJson {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<PlSmallStr>,

    #[serde(rename = "type")]
    pub type_: ColumnTypeJsonType,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub nullable: Option<bool>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<PlHashMap<String, String>>,

    // Used for List types
    #[serde(
        default,
        rename = "elementType",
        skip_serializing_if = "Option::is_none"
    )]
    pub element_type: Option<ColumnTypeJsonType>,

    #[serde(
        default,
        rename = "containsNull",
        skip_serializing_if = "Option::is_none"
    )]
    pub contains_null: Option<bool>,

    // Used for Struct types
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fields: Option<Vec<ColumnTypeJson>>,

    // Used for Map types
    #[serde(default, rename = "keyType", skip_serializing_if = "Option::is_none")]
    pub key_type: Option<ColumnTypeJsonType>,

    #[serde(default, rename = "valueType", skip_serializing_if = "Option::is_none")]
    pub value_type: Option<ColumnTypeJsonType>,

    #[serde(
        default,
        rename = "valueContainsNull",
        skip_serializing_if = "Option::is_none"
    )]
    pub value_contains_null: Option<bool>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum ColumnTypeJsonType {
    /// * `{"type": "name", ..}``
    TypeName(PlSmallStr),
    /// * `{"type": {"type": "name", ..}}`
    TypeJson(Box<ColumnTypeJson>),
}

impl Default for ColumnTypeJsonType {
    fn default() -> Self {
        Self::TypeName(PlSmallStr::EMPTY)
    }
}

impl ColumnTypeJsonType {
    pub const fn from_static_type_name(type_name: &'static str) -> Self {
        Self::TypeName(PlSmallStr::from_static(type_name))
    }
}

#[derive(Debug, serde::Deserialize)]
pub struct TableCredentials {
    pub aws_temp_credentials: Option<TableCredentialsAws>,
    pub azure_user_delegation_sas: Option<TableCredentialsAzure>,
    pub gcp_oauth_token: Option<TableCredentialsGcp>,
    pub expiration_time: i64,
}

impl TableCredentials {
    pub fn into_enum(self) -> Option<TableCredentialsVariants> {
        if let v @ Some(_) = self.aws_temp_credentials {
            v.map(TableCredentialsVariants::Aws)
        } else if let v @ Some(_) = self.azure_user_delegation_sas {
            v.map(TableCredentialsVariants::Azure)
        } else if let v @ Some(_) = self.gcp_oauth_token {
            v.map(TableCredentialsVariants::Gcp)
        } else {
            None
        }
    }
}

pub enum TableCredentialsVariants {
    Aws(TableCredentialsAws),
    Azure(TableCredentialsAzure),
    Gcp(TableCredentialsGcp),
}

#[derive(Debug, serde::Deserialize)]
pub struct TableCredentialsAws {
    pub access_key_id: String,
    pub secret_access_key: String,
    pub session_token: Option<String>,

    #[serde(default)]
    pub access_point: Option<String>,
}

#[derive(Debug, serde::Deserialize)]
pub struct TableCredentialsAzure {
    pub sas_token: String,
}

#[derive(Debug, serde::Deserialize)]
pub struct TableCredentialsGcp {
    pub oauth_token: String,
}

fn null_to_default<'de, T, D>(d: D) -> Result<T, D::Error>
where
    T: Default + serde::de::Deserialize<'de>,
    D: serde::de::Deserializer<'de>,
{
    use serde::Deserialize;
    let opt_val = Option::<T>::deserialize(d)?;
    Ok(opt_val.unwrap_or_default())
}