Skip to main content

micromegas_analytics/lakehouse/
view_definition_store.rs

1//! Postgres-backed persistence for `lakehouse_view_set_definitions`.
2//!
3//! [`ViewDefinitionStore`] is reduced to the single pool-backed [`ViewDefinitionStore::list`]
4//! method [`super::view_registry::ViewRegistry::reload`] needs -- its only implementors are
5//! [`PgViewDefinitionStore`] and a test fake. The DDL executor
6//! (`rust/public/src/servers/view_ddl.rs`) instead calls the free functions below directly,
7//! against its own open `sqlx::Transaction`, so a `CREATE`/`DROP`'s existence check, upsert/delete
8//! and dependent-protection re-read all run inside one transaction, alongside `retire_partitions`
9//! on a `DROP` -- with no trait indirection for methods a test fake could never implement.
10
11use super::view_definition::{ViewDefinition, ViewOptions};
12use anyhow::{Context, Result};
13use async_trait::async_trait;
14use chrono::{DateTime, Utc};
15use sqlx::Row;
16use std::fmt::Debug;
17
18/// One row of `lakehouse_view_set_definitions`, exactly as read back. `view_options` is kept as
19/// its raw JSON text here (parsed into a [`ViewOptions`] only by [`ViewDefinitionRow::into_definition`])
20/// so a row that fails to parse can still be reported by name rather than losing its identity.
21#[derive(Debug, Clone)]
22pub struct ViewDefinitionRow {
23    pub view_set_name: String,
24    pub definition_sql: String,
25    pub extract_query: String,
26    pub count_src_query: String,
27    pub merge_partitions_query: String,
28    pub update_group: Option<i32>,
29    pub view_options: String,
30    pub created_at: DateTime<Utc>,
31    pub updated_at: DateTime<Utc>,
32    pub updated_by: Option<String>,
33}
34
35impl ViewDefinitionRow {
36    /// Parses `view_options` and assembles the normalized [`ViewDefinition`] the DDL executor and
37    /// registry loader both validate and build a `SqlBatchView` from.
38    pub fn into_definition(self) -> Result<ViewDefinition> {
39        let options: ViewOptions = serde_json::from_str(&self.view_options).with_context(|| {
40            format!(
41                "'{}': parsing view_options {:?}",
42                self.view_set_name, self.view_options
43            )
44        })?;
45        Ok(ViewDefinition {
46            view_set_name: self.view_set_name,
47            extract_query: self.extract_query,
48            count_src_query: self.count_src_query,
49            merge_partitions_query: self.merge_partitions_query,
50            update_group: self.update_group,
51            options,
52        })
53    }
54}
55
56fn row_from_sqlx(row: sqlx::postgres::PgRow) -> Result<ViewDefinitionRow> {
57    Ok(ViewDefinitionRow {
58        view_set_name: row.try_get("view_set_name")?,
59        definition_sql: row.try_get("definition_sql")?,
60        extract_query: row.try_get("extract_query")?,
61        count_src_query: row.try_get("count_src_query")?,
62        merge_partitions_query: row.try_get("merge_partitions_query")?,
63        update_group: row.try_get("update_group")?,
64        view_options: row.try_get("view_options")?,
65        created_at: row.try_get("created_at")?,
66        updated_at: row.try_get("updated_at")?,
67        updated_by: row.try_get("updated_by")?,
68    })
69}
70
71const SELECT_COLUMNS: &str = "view_set_name, definition_sql, extract_query, count_src_query, \
72     merge_partitions_query, update_group, view_options, created_at, updated_at, updated_by";
73
74/// The rows-in seam [`super::view_registry::ViewRegistry::reload`] needs: every definition
75/// currently in force, ordered by `(update_group NULLS LAST, view_set_name)` -- on-demand rows
76/// (no group) last, on purpose. `super::view_registry::build_factory` relies on the group order
77/// for daemon-updated rows regardless of which caller fed it the rows.
78#[async_trait]
79pub trait ViewDefinitionStore: Send + Sync + Debug {
80    async fn list(&self) -> Result<Vec<ViewDefinitionRow>>;
81}
82
83/// The production, pool-backed implementation.
84#[derive(Debug)]
85pub struct PgViewDefinitionStore {
86    pool: sqlx::Pool<sqlx::Postgres>,
87}
88
89impl PgViewDefinitionStore {
90    pub fn new(pool: sqlx::Pool<sqlx::Postgres>) -> Self {
91        Self { pool }
92    }
93}
94
95#[async_trait]
96impl ViewDefinitionStore for PgViewDefinitionStore {
97    async fn list(&self) -> Result<Vec<ViewDefinitionRow>> {
98        let sql = format!(
99            "SELECT {SELECT_COLUMNS} FROM lakehouse_view_set_definitions \
100             ORDER BY update_group NULLS LAST, view_set_name"
101        );
102        let rows = sqlx::query(&sql)
103            .fetch_all(&self.pool)
104            .await
105            .with_context(|| "listing lakehouse_view_set_definitions")?;
106        rows.into_iter().map(row_from_sqlx).collect()
107    }
108}
109
110/// The transaction-scoped counterpart of [`ViewDefinitionStore::list`], for a caller (the DDL
111/// executor) already holding an open `sqlx::Transaction` -- so a pre-mutation read cannot miss a
112/// concurrent, uncommitted write from a sibling transaction, and a post-mutation read sees this
113/// transaction's own write.
114pub async fn list_tx(
115    tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
116) -> Result<Vec<ViewDefinitionRow>> {
117    let sql = format!(
118        "SELECT {SELECT_COLUMNS} FROM lakehouse_view_set_definitions \
119         ORDER BY update_group NULLS LAST, view_set_name"
120    );
121    let rows = sqlx::query(&sql)
122        .fetch_all(&mut **tx)
123        .await
124        .with_context(|| "listing lakehouse_view_set_definitions (tx)")?;
125    rows.into_iter().map(row_from_sqlx).collect()
126}
127
128/// Inserts a new row, or -- on a name collision -- updates every column including `updated_at`/
129/// `updated_by` explicitly: the column defaults only fire on `INSERT`, and `reload()`'s digest is
130/// over `(view_set_name, updated_at)`, so a `REPLACE` that left it unset would go live only on the
131/// replacing node while every other replica kept serving/materializing the old definition.
132pub async fn upsert_tx(
133    tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
134    def: &ViewDefinition,
135    definition_sql: &str,
136    updated_by: &str,
137) -> Result<()> {
138    let view_options = serde_json::to_string(&def.options)
139        .with_context(|| format!("serializing view_options for '{}'", def.view_set_name))?;
140    sqlx::query(
141        "INSERT INTO lakehouse_view_set_definitions
142             (view_set_name, definition_sql, extract_query, count_src_query,
143              merge_partitions_query, update_group, view_options, updated_at, updated_by)
144         VALUES ($1, $2, $3, $4, $5, $6, $7, now(), $8)
145         ON CONFLICT (view_set_name) DO UPDATE SET
146             definition_sql = EXCLUDED.definition_sql,
147             extract_query = EXCLUDED.extract_query,
148             count_src_query = EXCLUDED.count_src_query,
149             merge_partitions_query = EXCLUDED.merge_partitions_query,
150             update_group = EXCLUDED.update_group,
151             view_options = EXCLUDED.view_options,
152             updated_at = now(),
153             updated_by = EXCLUDED.updated_by;",
154    )
155    .bind(&def.view_set_name)
156    .bind(definition_sql)
157    .bind(&def.extract_query)
158    .bind(&def.count_src_query)
159    .bind(&def.merge_partitions_query)
160    .bind(def.update_group)
161    .bind(view_options)
162    .bind(updated_by)
163    .execute(&mut **tx)
164    .await
165    .with_context(|| format!("upserting '{}'", def.view_set_name))?;
166    Ok(())
167}
168
169/// Deletes a row by name; `true` if a row was actually deleted.
170pub async fn delete_tx(
171    tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
172    view_set_name: &str,
173) -> Result<bool> {
174    let result = sqlx::query("DELETE FROM lakehouse_view_set_definitions WHERE view_set_name = $1")
175        .bind(view_set_name)
176        .execute(&mut **tx)
177        .await
178        .with_context(|| format!("deleting '{view_set_name}'"))?;
179    Ok(result.rows_affected() > 0)
180}
181
182/// The exact insert-time range currently materialized for `view_set_name`'s `'global'` instance --
183/// `None` when it has no partitions -- for `DROP`'s call to `retire_partitions`. Deliberately
184/// exact rather than an unbounded range: `retire_partitions` is hash-agnostic but still needs an
185/// explicit range to key its deletion on.
186pub async fn partition_insert_range(
187    tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
188    view_set_name: &str,
189) -> Result<Option<(DateTime<Utc>, DateTime<Utc>)>> {
190    let row = sqlx::query(
191        "SELECT min(begin_insert_time) AS begin, max(end_insert_time) AS end
192         FROM lakehouse_partitions
193         WHERE view_set_name = $1 AND view_instance_id = 'global';",
194    )
195    .bind(view_set_name)
196    .fetch_one(&mut **tx)
197    .await
198    .with_context(|| format!("reading partition insert-time range for '{view_set_name}'"))?;
199    let begin: Option<DateTime<Utc>> = row.try_get("begin")?;
200    let end: Option<DateTime<Utc>> = row.try_get("end")?;
201    Ok(begin.zip(end))
202}