Skip to main content

postrust_core/
embed.rs

1//! Relationship embedding: fetching related rows for a set of parent rows.
2//!
3//! Both the REST and GraphQL surfaces embed related resources, and both do it
4//! the same way: take the parent rows already fetched, collect the values of
5//! the join column, and issue **one** query for all of them rather than one
6//! query per parent row.
7//!
8//! ```text
9//! SELECT row_to_json(t) FROM (
10//!     SELECT * FROM "public"."posts" WHERE "user_id" = ANY($1::int4[])
11//! ) t
12//! ```
13//!
14//! The children are then grouped by that column and attached to the parent
15//! rows, so a request embedding two relationships across a page of 25 parents
16//! costs three queries, not fifty-one.
17
18use crate::error::{Error, Result};
19use crate::schema_cache::{Relationship, SchemaCache, Table};
20use std::collections::HashMap;
21
22/// How an embedded row or array is rendered.
23///
24/// **`jsonb` reorders keys.** It sorts them by length and then bytewise,
25/// inside PostgreSQL -- `SELECT row_to_json(t)::jsonb FROM (SELECT 1 AS aaa,
26/// 2 AS zz) t` gives `{"zz": 2, "aaa": 1}` -- so the order the columns were
27/// selected in is gone before the row reaches this process, and nothing on
28/// the Rust side can put it back. It also regularises the spacing that `json`
29/// keeps verbatim.
30///
31/// Both behaviours are wanted, in different places, which is why this is a
32/// choice made per query rather than a constant.
33#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub enum EmbedRendering {
35    /// `json`. The row is written exactly as `row_to_json` produced it, so the
36    /// columns stay in the order they were selected. This is what an embedded
37    /// resource in a JSON body wants, and what PostgREST itself emits there.
38    Verbatim,
39    /// `jsonb`. PostgreSQL normalises the text.
40    ///
41    /// Only needed where the embedded value is rendered *as text* rather than
42    /// as part of a JSON body: a schema-declared media handler that
43    /// stringifies a whole row renders each column's text, and a `json` column
44    /// renders verbatim where PostgREST's `jsonb` one renders normalised --
45    /// `{"id":1}` against `{"id": 1}`. Measured: it is the only difference
46    /// between the two in PostgREST's whole corpus.
47    Normalised,
48}
49
50impl EmbedRendering {
51    /// The SQL cast this rendering needs.
52    fn cast(self) -> &'static str {
53        match self {
54            Self::Verbatim => "::json",
55            Self::Normalised => "::jsonb",
56        }
57    }
58}
59
60/// Everything needed to fetch one relationship's rows for a set of parents.
61#[derive(Clone, Debug)]
62pub struct EmbedPlan {
63    /// Column on the parent row whose value identifies the parent.
64    pub local_column: String,
65    /// Column on the related table that points back at the parent.
66    pub foreign_column: String,
67    /// PostgreSQL type of the foreign column, used to cast the bound array.
68    pub foreign_column_type: String,
69    /// Schema of the related table.
70    pub foreign_schema: String,
71    /// Name of the related table.
72    pub foreign_table: String,
73    /// Whether the relationship yields many rows per parent.
74    pub is_list: bool,
75    /// Every column pair the join is on, parent side first.
76    ///
77    /// Usually one. A foreign key over several columns joins on all of them,
78    /// and the pairs are ordered so each parent column sits with the child
79    /// column it actually references.
80    pub columns: Vec<(String, String)>,
81    /// The junction of a many-to-many relationship, if that is what this is.
82    ///
83    /// The parent and the child share no key; each points at a table that
84    /// exists to join them, so reaching the child means going through it.
85    pub junction: Option<EmbedJunction>,
86    /// The function behind a computed relationship, if that is what this is.
87    ///
88    /// A computed relationship is a function taking the parent row and
89    /// returning rows of the related table. There is no key to join on -- the
90    /// parent row is the argument -- so the columns above are empty and the
91    /// relationship can only be expressed as a call correlated to the parent.
92    pub function: Option<crate::api_request::QualifiedIdentifier>,
93    /// The name of the parameter that takes the parent's row, where the
94    /// function has more than one and the call has to be written by name.
95    pub row_argument: Option<String>,
96}
97
98/// The table a many-to-many relationship is joined through.
99#[derive(Clone, Debug)]
100pub struct EmbedJunction {
101    /// Schema holding the junction table.
102    pub schema: String,
103    /// The junction table's name.
104    pub table: String,
105    /// `(parent column, junction column)` for each column the parent joins on.
106    ///
107    /// Usually one. A junction may be keyed over several columns on each side
108    /// -- `touched_files` joins `files(project_id, filename)` to
109    /// `users_tasks(user_id, task_id)` -- and joining on the first of them
110    /// alone would relate rows that are not related.
111    pub parent_columns: Vec<(String, String)>,
112    /// `(junction column, child column)` for each column the child joins on.
113    pub child_columns: Vec<(String, String)>,
114}
115
116impl EmbedJunction {
117    /// The junction's join to the child, one equality per column pair.
118    fn child_join(&self, junction_alias: &str, child_alias: &str) -> String {
119        Self::equalities(&self.child_columns, junction_alias, child_alias, true)
120    }
121
122    /// The junction's correlation to one parent row, one equality per pair.
123    fn parent_correlation(&self, junction_alias: &str, parent_alias: &str) -> String {
124        Self::equalities(&self.parent_columns, junction_alias, parent_alias, false)
125    }
126
127    /// `junction.column = other.column` for each pair, joined with `AND`.
128    ///
129    /// `junction_first` says which half of the pair is the junction's, the two
130    /// lists running in opposite directions: the parent's own column comes
131    /// first on the way in, and the child's second on the way out.
132    fn equalities(
133        pairs: &[(String, String)],
134        junction_alias: &str,
135        other_alias: &str,
136        junction_first: bool,
137    ) -> String {
138        pairs
139            .iter()
140            .map(|(left, right)| {
141                let (junction_column, other_column) = match junction_first {
142                    true => (left, right),
143                    false => (right, left),
144                };
145                format!(
146                    "{}.{} = {}.{}",
147                    postrust_sql::escape_ident(junction_alias),
148                    postrust_sql::escape_ident(junction_column),
149                    postrust_sql::escape_ident(other_alias),
150                    postrust_sql::escape_ident(other_column),
151                )
152            })
153            .collect::<Vec<_>>()
154            .join(" AND ")
155    }
156}
157
158impl EmbedPlan {
159    /// Resolve a relationship into an embed plan.
160    ///
161    /// Returns an error for relationships this cannot express yet, rather than
162    /// silently omitting the embedded data.
163    pub fn resolve(relationship: &Relationship, schema_cache: &SchemaCache) -> Result<Self> {
164        let foreign_table_qi = relationship.foreign_table().clone();
165
166        // A computed relationship takes the parent row as its argument, so
167        // there is nothing to look up and nothing to join on.
168        if let Relationship::Computed {
169            function,
170            foreign_table,
171            to_one,
172            row_argument,
173            ..
174        } = relationship
175        {
176            return Ok(Self {
177                local_column: String::new(),
178                foreign_column: String::new(),
179                foreign_column_type: String::new(),
180                foreign_schema: foreign_table.schema.clone(),
181                foreign_table: foreign_table.name.clone(),
182                is_list: !to_one,
183                columns: Vec::new(),
184                junction: None,
185                function: Some(function.clone()),
186                row_argument: row_argument.clone(),
187            });
188        }
189
190        if let Relationship::ForeignKey {
191            cardinality: crate::schema_cache::Cardinality::M2M(junction),
192            ..
193        } = relationship
194        {
195            // The join itself is written from every column pair below. These
196            // two are the parent's and the child's first key column, which is
197            // what the batched path groups by -- and it refuses a junction
198            // keyed on more than one, rather than grouping by half a key.
199            let (local_column, _) =
200                junction.source_columns.first().cloned().ok_or_else(|| {
201                    Error::EmbeddingError("junction has no source columns".into())
202                })?;
203            let (_, foreign_column) =
204                junction.target_columns.first().cloned().ok_or_else(|| {
205                    Error::EmbeddingError("junction has no target columns".into())
206                })?;
207
208            return Ok(Self {
209                local_column,
210                foreign_column,
211                foreign_column_type: String::new(),
212                foreign_schema: foreign_table_qi.schema.clone(),
213                foreign_table: foreign_table_qi.name.clone(),
214                // A junction always yields a set: that is what it is for.
215                is_list: true,
216                columns: Vec::new(),
217                row_argument: None,
218                junction: Some(EmbedJunction {
219                    schema: junction.table.schema.clone(),
220                    table: junction.table.name.clone(),
221                    parent_columns: junction.source_columns.clone(),
222                    child_columns: junction.target_columns.clone(),
223                }),
224                function: None,
225            });
226        }
227
228        let columns = match relationship {
229            Relationship::ForeignKey { cardinality, .. } => cardinality.columns(),
230            Relationship::Computed { .. } => unreachable!("handled above"),
231        };
232
233        let (local_column, foreign_column) = columns
234            .first()
235            .cloned()
236            .ok_or_else(|| Error::EmbeddingError("relationship joins on no columns".into()))?;
237
238        let foreign_table: &Table = schema_cache.get_table(&foreign_table_qi).ok_or_else(|| {
239            Error::EmbeddingError(format!(
240                "cannot embed \"{}\": it is not in an exposed schema",
241                foreign_table_qi
242            ))
243        })?;
244
245        let foreign_column_type = foreign_table
246            .get_column(&foreign_column)
247            .map(|c| c.nominal_type.clone())
248            .ok_or_else(|| {
249                Error::EmbeddingError(format!(
250                    "cannot embed \"{}\": join column \"{}\" not found",
251                    foreign_table_qi, foreign_column
252                ))
253            })?;
254
255        Ok(Self {
256            local_column,
257            foreign_column,
258            foreign_column_type,
259            foreign_schema: foreign_table_qi.schema.clone(),
260            foreign_table: foreign_table_qi.name.clone(),
261            is_list: !relationship.is_to_one(),
262            columns,
263            junction: None,
264            function: None,
265            row_argument: None,
266        })
267    }
268
269    /// The predicate correlating child rows to one parent row.
270    ///
271    /// A foreign key over several columns joins on all of them, so this is one
272    /// equality per pair rather than a single one.
273    fn correlation(&self, parent_alias: &str, child_alias: &str) -> String {
274        let pairs = if self.columns.is_empty() {
275            vec![(self.local_column.clone(), self.foreign_column.clone())]
276        } else {
277            self.columns.clone()
278        };
279
280        pairs
281            .iter()
282            .map(|(local, foreign)| {
283                format!(
284                    "{}.{} = {}.{}",
285                    postrust_sql::escape_ident(child_alias),
286                    postrust_sql::escape_ident(foreign),
287                    postrust_sql::escape_ident(parent_alias),
288                    postrust_sql::escape_ident(local),
289                )
290            })
291            .collect::<Vec<_>>()
292            .join(" AND ")
293    }
294
295    /// SQL that fetches every related row for the given parent key values.
296    ///
297    /// The keys are bound as a single text array and cast to the foreign
298    /// column's type, so the column itself is never wrapped in a cast and an
299    /// index on it remains usable. `limit` bounds the rows per query, not per
300    /// parent.
301    ///
302    /// `columns` is the set of columns the client asked for. An empty set means
303    /// every column. Projecting here rather than discarding columns after the
304    /// fact matters: an unprojected column is read from the heap, serialised to
305    /// JSON by PostgreSQL, sent over the socket and parsed, before being thrown
306    /// away. The join column is always included even when it was not requested,
307    /// because grouping needs it; the caller strips it afterwards.
308    pub fn children_sql(&self, limit: Option<i64>, columns: &[String]) -> Result<String> {
309        let type_name = castable_type_name(&self.foreign_column_type).ok_or_else(|| {
310            Error::EmbeddingError(format!(
311                "cannot embed \"{}\": join column type \"{}\" is not a plain type name",
312                self.foreign_table, self.foreign_column_type
313            ))
314        })?;
315
316        let projection = self.projection(columns);
317
318        let mut inner = format!(
319            "SELECT {} FROM {}.{} WHERE {} = ANY($1::{}[])",
320            projection,
321            postrust_sql::escape_ident(&self.foreign_schema),
322            postrust_sql::escape_ident(&self.foreign_table),
323            postrust_sql::escape_ident(&self.foreign_column),
324            type_name
325        );
326
327        if let Some(limit) = limit {
328            inner.push_str(&format!(" LIMIT {}", limit));
329        }
330
331        Ok(format!("SELECT row_to_json(t) FROM ({}) t", inner))
332    }
333
334    /// SQL that fetches related rows already grouped by their join key.
335    ///
336    /// One row comes back per distinct key: the key itself and a JSON array of
337    /// that key's children. Grouping in PostgreSQL rather than in this process
338    /// removes a per-child-row JSON parse, the per-row hash insert, and the
339    /// clone of each group onto its parent.
340    ///
341    /// The key is returned as JSON rather than cast to text, so it is rendered
342    /// by the same code that renders the parents' keys. Casting to text in SQL
343    /// would agree for integers and uuids and disagree for a NUMERIC join
344    /// column, where PostgreSQL and serde_json format differently.
345    ///
346    /// `limit` still bounds the rows scanned, not the rows per parent, so it is
347    /// applied to the inner select exactly as the ungrouped form does.
348    pub fn children_grouped_sql(&self, limit: Option<i64>, columns: &[String]) -> Result<String> {
349        if self
350            .junction
351            .as_ref()
352            .is_some_and(|j| j.parent_columns.len() > 1)
353        {
354            return Err(Error::EmbeddingError(format!(
355                "embedding \"{}\" this way is not supported: it joins through a junction \
356                 keyed on several columns, and grouping children by key needs a single one",
357                self.foreign_table,
358            )));
359        }
360
361        if self.columns.len() > 1 {
362            return Err(Error::EmbeddingError(format!(
363                "embedding \"{}\" this way is not supported: it joins on {} columns, and \
364                 grouping children by key needs a single one",
365                self.foreign_table,
366                self.columns.len()
367            )));
368        }
369
370        let type_name = castable_type_name(&self.foreign_column_type).ok_or_else(|| {
371            Error::EmbeddingError(format!(
372                "cannot embed \"{}\": join column type \"{}\" is not a plain type name",
373                self.foreign_table, self.foreign_column_type
374            ))
375        })?;
376
377        let key = postrust_sql::escape_ident(&self.foreign_column);
378
379        let mut inner = format!(
380            "SELECT {} FROM {}.{} WHERE {} = ANY($1::{}[])",
381            self.projection(columns),
382            postrust_sql::escape_ident(&self.foreign_schema),
383            postrust_sql::escape_ident(&self.foreign_table),
384            key,
385            type_name
386        );
387
388        if let Some(limit) = limit {
389            inner.push_str(&format!(" LIMIT {}", limit));
390        }
391
392        Ok(format!(
393            "SELECT to_jsonb(c.{key}) AS k, json_agg(row_to_json(c)) AS v \
394             FROM ({inner}) c GROUP BY c.{key}",
395            key = key,
396            inner = inner
397        ))
398    }
399
400    /// The child rows belonging to one parent row, as a SELECT correlated
401    /// against the parent's alias.
402    ///
403    /// This is the single-query form of embedding: instead of fetching parents,
404    /// collecting their keys and issuing a second query, the relationship is
405    /// attached to the parent query as an expression, so PostgreSQL builds the
406    /// array while it already has the parent row.
407    ///
408    /// `inner_select` is the child's SELECT list, which the caller assembles --
409    /// its columns, plus any deeper relationship expressions built by calling
410    /// this again. Only the caller knows the shape of its own selection tree, so
411    /// the recursion lives there and the SQL assembly lives here.
412    ///
413    /// What is done with those rows is the caller's: [`embed_expression`]
414    /// renders them as JSON, [`aggregate_expression`] summarises them.
415    #[allow(clippy::too_many_arguments)] // one parameter per SQL clause
416    pub fn correlated_rows(
417        &self,
418        parent_alias: &str,
419        parent_row: &str,
420        child_alias: &str,
421        inner_select: &str,
422        limit: Option<i64>,
423        // `clients.offset=1` skips rows inside the embed. A to-one embed that
424        // is skipped past yields no row at all, which is null -- the same
425        // answer as no match, and the one PostgREST gives.
426        offset: i64,
427        child_where: Option<&str>,
428        // `clients.order=name.desc` orders the rows inside the embed, which
429        // belongs to the child's own subselect -- and has to be applied before
430        // the child's own limit, or the limit takes an unsorted window.
431        order_by: Option<&str>,
432        // The whole argument list of a computed relationship's function, where
433        // the parent row is not the only thing it takes.
434        function_arguments: Option<&str>,
435    ) -> Result<String> {
436        // The child table is aliased rather than referred to by name. A
437        // self-referential relationship would otherwise make the correlation
438        // ambiguous, since the parent and the child are the same table.
439        //
440        // A computed relationship is correlated by argument rather than by a
441        // key: the function takes the parent row, so the parent alias is
442        // passed to it and there is no predicate to write.
443        let mut inner = match &self.function {
444            Some(function) => format!(
445                "SELECT {} FROM {}.{}({}) AS {} WHERE true",
446                inner_select,
447                postrust_sql::escape_ident(&function.schema),
448                postrust_sql::escape_ident(&function.name),
449                // The parent row alone, unless the caller wrote the whole
450                // argument list: a function with more than the row -- a search
451                // term, the caller's session -- is called by name, and only the
452                // caller knows what to put in those.
453                function_arguments.unwrap_or(parent_row),
454                postrust_sql::escape_ident(child_alias),
455            ),
456            None => match &self.junction {
457                // Two hops: the child is reached through the table that joins
458                // it to the parent, so the correlation lands on the junction.
459                Some(junction) => format!(
460                    "SELECT {} FROM {}",
461                    inner_select,
462                    self.junction_source(junction, parent_alias, child_alias),
463                ),
464                None => format!(
465                    "SELECT {} FROM {}.{} AS {} WHERE {}",
466                    inner_select,
467                    postrust_sql::escape_ident(&self.foreign_schema),
468                    postrust_sql::escape_ident(&self.foreign_table),
469                    postrust_sql::escape_ident(child_alias),
470                    self.correlation(parent_alias, child_alias),
471                ),
472            },
473        };
474
475        // Filters written against the embedded resource (`clients.id=eq.1`)
476        // narrow the children, exactly as they would if the child had been
477        // requested on its own.
478        if let Some(child_where) = child_where {
479            inner.push_str(" AND ");
480            inner.push_str(child_where);
481        }
482
483        if let Some(order_by) = order_by {
484            inner.push_str(" ORDER BY ");
485            inner.push_str(order_by);
486        }
487
488        // A to-one relationship takes the first row; a to-many takes them all.
489        // The limit bounds rows per parent here, which is what a client asking
490        // for a page of children means, and is stricter than the row cap the
491        // two-query form could apply.
492        if let Some(limit) = limit {
493            inner.push_str(&format!(" LIMIT {}", limit));
494        } else if !self.is_list {
495            inner.push_str(" LIMIT 1");
496        }
497
498        if offset > 0 {
499            inner.push_str(&format!(" OFFSET {}", offset));
500        }
501
502        Ok(inner)
503    }
504
505    /// The `FROM` source of a many-to-many hop, up to and including `WHERE true`.
506    ///
507    /// PostgREST joins child to junction, which makes a column both tables
508    /// have (`name`, `user_id`) ambiguous for the bare identifiers the select
509    /// list, filters and order write. Only the child's columns may be in
510    /// scope, so the junction sits in a lateral subselect that exposes none
511    /// of them. It is a join rather than `EXISTS`/`IN`: a child reached
512    /// through two junction rows appears twice, as it does in PostgREST.
513    ///
514    /// The aliases differ from the `{child}_j` that `embed_expression` gives
515    /// the row-to-json wrapper, so the two can never clash.
516    fn junction_source(
517        &self,
518        junction: &EmbedJunction,
519        parent_alias: &str,
520        child_alias: &str,
521    ) -> String {
522        let junction_alias = format!("{}_jx", child_alias);
523        let lateral_alias = format!("{}_jl", child_alias);
524        format!(
525            "{}.{} AS {} CROSS JOIN LATERAL (SELECT 1 FROM {}.{} AS {} WHERE {} AND {}) AS {} WHERE true",
526            postrust_sql::escape_ident(&self.foreign_schema),
527            postrust_sql::escape_ident(&self.foreign_table),
528            postrust_sql::escape_ident(child_alias),
529            postrust_sql::escape_ident(&junction.schema),
530            postrust_sql::escape_ident(&junction.table),
531            postrust_sql::escape_ident(&junction_alias),
532            junction.child_join(&junction_alias, child_alias),
533            junction.parent_correlation(&junction_alias, parent_alias),
534            postrust_sql::escape_ident(&lateral_alias),
535        )
536    }
537
538    /// A correlated subselect that yields this relationship as one JSON column.
539    ///
540    /// The rows come from [`correlated_rows`]; this decides how they are
541    /// rendered -- an array for a to-many relationship, one object or null for
542    /// a to-one.
543    #[allow(clippy::too_many_arguments)] // one parameter per SQL clause
544    pub fn embed_expression(
545        &self,
546        parent_alias: &str,
547        parent_row: &str,
548        child_alias: &str,
549        inner_select: &str,
550        limit: Option<i64>,
551        offset: i64,
552        child_where: Option<&str>,
553        order_by: Option<&str>,
554        function_arguments: Option<&str>,
555        rendering: EmbedRendering,
556    ) -> Result<String> {
557        let inner = self.correlated_rows(
558            parent_alias,
559            parent_row,
560            child_alias,
561            inner_select,
562            limit,
563            offset,
564            child_where,
565            order_by,
566            function_arguments,
567        )?;
568
569        let alias = postrust_sql::escape_ident(&format!("{}_j", child_alias));
570
571        Ok(if self.is_list {
572            // An empty array rather than null, so the shape does not depend on
573            // whether the parent happens to have children.
574            format!(
575                "COALESCE((SELECT json_agg(row_to_json({alias})) FROM ({inner}) {alias}), '[]'::json){cast}",
576                alias = alias,
577                inner = inner,
578                cast = rendering.cast()
579            )
580        } else {
581            format!(
582                "(SELECT row_to_json({alias}) FROM ({inner}) {alias}){cast}",
583                alias = alias,
584                inner = inner,
585                cast = rendering.cast()
586            )
587        })
588    }
589
590    /// A correlated subselect that summarises this relationship as one JSON
591    /// column.
592    ///
593    /// `author { articles_aggregate { aggregate { count } } }`. The rows are
594    /// the same correlated set an embed would render, but a `limit` here bounds
595    /// what is *counted* rather than what is returned -- so the aggregate reads
596    /// from the rows as a subquery instead of selecting over them directly.
597    /// The subquery carries the child's own alias, so a predicate or an
598    /// ordering written against it goes on reading the same name.
599    ///
600    /// Parent columns are deliberately left alone: they stay ordinary typed
601    /// columns and are converted to JSON by the same code as an unembedded
602    /// request, so embedding does not change how a NUMERIC or a timestamp is
603    /// rendered.
604    #[allow(clippy::too_many_arguments)] // one parameter per SQL clause
605    pub fn aggregate_expression(
606        &self,
607        parent_alias: &str,
608        parent_row: &str,
609        child_alias: &str,
610        aggregate_select: &str,
611        limit: Option<i64>,
612        offset: i64,
613        child_where: Option<&str>,
614        order_by: Option<&str>,
615        function_arguments: Option<&str>,
616        // A `DISTINCT ON (...) ` prefix, or empty. It belongs to the rows
617        // being summarised rather than to the summary: counting the distinct
618        // titles means counting the rows that survive it.
619        distinct_on: &str,
620        rendering: EmbedRendering,
621    ) -> Result<String> {
622        let child = postrust_sql::escape_ident(child_alias);
623        let rows = self.correlated_rows(
624            parent_alias,
625            parent_row,
626            child_alias,
627            &format!("{}{}.*", distinct_on, child),
628            limit,
629            offset,
630            child_where,
631            order_by,
632            function_arguments,
633        )?;
634        let alias = postrust_sql::escape_ident(&format!("{}_a", child_alias));
635        Ok(format!(
636            "(SELECT row_to_json({alias}) FROM \
637             (SELECT {select} FROM ({rows}) AS {child}) {alias}){cast}",
638            alias = alias,
639            select = aggregate_select,
640            rows = rows,
641            child = child,
642            cast = rendering.cast()
643        ))
644    }
645
646    /// A scalar subselect yielding one column of the related row.
647    ///
648    /// This is what `order=clients(name)` orders by: the parent has no such
649    /// column, so the value is fetched per parent row exactly as the embed
650    /// itself is. A to-many relationship has no single value to order by, so
651    /// the first row wins -- which is what `LIMIT 1` says and what PostgREST
652    /// does.
653    pub fn order_expression(
654        &self,
655        parent_alias: &str,
656        parent_row: &str,
657        child_alias: &str,
658        column_sql: &str,
659    ) -> String {
660        let source = match (&self.function, &self.junction) {
661            (Some(function), _) => format!(
662                "{}.{}({}) AS {} WHERE true",
663                postrust_sql::escape_ident(&function.schema),
664                postrust_sql::escape_ident(&function.name),
665                parent_row,
666                postrust_sql::escape_ident(child_alias),
667            ),
668            (None, Some(junction)) => self.junction_source(junction, parent_alias, child_alias),
669            (None, None) => format!(
670                "{}.{} AS {} WHERE {}",
671                postrust_sql::escape_ident(&self.foreign_schema),
672                postrust_sql::escape_ident(&self.foreign_table),
673                postrust_sql::escape_ident(child_alias),
674                self.correlation(parent_alias, child_alias),
675            ),
676        };
677
678        format!("(SELECT {} FROM {} LIMIT 1)", column_sql, source)
679    }
680
681    /// An `EXISTS` predicate restricting parents to those with a matching child.
682    ///
683    /// This is what `!inner` means. The embed expression on its own only
684    /// decides what the relationship column contains; without this the parent
685    /// row survives even when the relationship matched nothing, which is a
686    /// left join. `child_where` should be the same predicate given to
687    /// [`Self::embed_expression`] -- placeholders may be referenced from both
688    /// places, so no parameter needs binding twice.
689    pub fn inner_join_predicate(
690        &self,
691        parent_alias: &str,
692        parent_row: &str,
693        child_alias: &str,
694        child_where: Option<&str>,
695    ) -> String {
696        let mut predicate = match &self.function {
697            Some(function) => format!(
698                "EXISTS (SELECT 1 FROM {}.{}({}) AS {} WHERE true",
699                postrust_sql::escape_ident(&function.schema),
700                postrust_sql::escape_ident(&function.name),
701                parent_row,
702                postrust_sql::escape_ident(child_alias),
703            ),
704            None => match &self.junction {
705                Some(junction) => format!(
706                    "EXISTS (SELECT 1 FROM {}",
707                    self.junction_source(junction, parent_alias, child_alias),
708                ),
709                None => format!(
710                    "EXISTS (SELECT 1 FROM {}.{} AS {} WHERE {}",
711                    postrust_sql::escape_ident(&self.foreign_schema),
712                    postrust_sql::escape_ident(&self.foreign_table),
713                    postrust_sql::escape_ident(child_alias),
714                    self.correlation(parent_alias, child_alias),
715                ),
716            },
717        };
718
719        if let Some(child_where) = child_where {
720            predicate.push_str(" AND ");
721            predicate.push_str(child_where);
722        }
723
724        predicate.push(')');
725        predicate
726    }
727
728    /// The child projection list: the requested columns plus the join column.
729    ///
730    /// Column names come from the client, so each is escaped rather than
731    /// interpolated bare. Anything that is not a plain column reference -- a
732    /// nested relation, say -- is not a column and is skipped.
733    fn projection(&self, columns: &[String]) -> String {
734        if columns.is_empty() {
735            return "*".to_string();
736        }
737
738        let mut wanted: Vec<&str> = Vec::with_capacity(columns.len() + 1);
739        for column in columns {
740            if !wanted.contains(&column.as_str()) {
741                wanted.push(column);
742            }
743        }
744        if !wanted.contains(&self.foreign_column.as_str()) {
745            wanted.push(&self.foreign_column);
746        }
747
748        wanted
749            .into_iter()
750            .map(postrust_sql::escape_ident)
751            .collect::<Vec<_>>()
752            .join(", ")
753    }
754}
755
756/// Reject anything that is not a bare type name, since it is interpolated.
757fn castable_type_name(pg_type: &str) -> Option<&str> {
758    if pg_type.is_empty() {
759        return None;
760    }
761    if pg_type
762        .chars()
763        .all(|c| c.is_ascii_alphanumeric() || c == '_')
764    {
765        Some(pg_type)
766    } else {
767        None
768    }
769}
770
771/// Render a JSON value as the text form used to match join keys.
772///
773/// Keys are compared as text and cast by PostgreSQL, so a numeric key must not
774/// arrive quoted the way `to_string` would render a JSON string.
775pub fn key_to_text(value: &serde_json::Value) -> Option<String> {
776    match value {
777        serde_json::Value::Null => None,
778        serde_json::Value::String(s) => Some(s.clone()),
779        other => Some(other.to_string()),
780    }
781}
782
783/// Group related rows by the value of their join column.
784/// Build the grouping from rows PostgreSQL has already grouped.
785///
786/// Each row is the join key as JSON and a JSON array of that key's children.
787/// The key goes through `key_to_text`, the same rendering the parent side uses,
788/// so the two agree for every column type.
789pub fn group_from_aggregated(
790    rows: Vec<(serde_json::Value, serde_json::Value)>,
791) -> HashMap<String, Vec<serde_json::Value>> {
792    let mut grouped: HashMap<String, Vec<serde_json::Value>> = HashMap::with_capacity(rows.len());
793
794    for (key, children) in rows {
795        let key = key_to_text(&key).unwrap_or_default();
796        let children = match children {
797            serde_json::Value::Array(items) => items,
798            // json_agg only ever yields an array or null.
799            _ => Vec::new(),
800        };
801        grouped.entry(key).or_default().extend(children);
802    }
803
804    grouped
805}
806
807/// Group child rows by the value of one of their columns.
808///
809/// The join is done in SQL and the rows come back flat; this puts each child
810/// under the parent it belongs to. Keyed by the rendered value, so a parent
811/// with no children gets no entry rather than an empty one.
812pub fn group_by_key(
813    children: Vec<serde_json::Value>,
814    foreign_column: &str,
815) -> HashMap<String, Vec<serde_json::Value>> {
816    let mut grouped: HashMap<String, Vec<serde_json::Value>> = HashMap::new();
817
818    for child in children {
819        let key = child
820            .get(foreign_column)
821            .and_then(key_to_text)
822            .unwrap_or_default();
823        grouped.entry(key).or_default().push(child);
824    }
825
826    grouped
827}
828
829/// Attach grouped children onto a parent row under `field_name`.
830///
831/// A to-one relationship yields the first match or `null`; a to-many yields an
832/// array, empty when there are no matches, so the shape of the response does
833/// not depend on whether data happens to exist.
834pub fn attach_to_parent(
835    parent: &mut serde_json::Value,
836    field_name: &str,
837    plan: &EmbedPlan,
838    grouped: &HashMap<String, Vec<serde_json::Value>>,
839) {
840    let key = parent.get(&plan.local_column).and_then(key_to_text);
841
842    let matches = key
843        .as_ref()
844        .and_then(|k| grouped.get(k))
845        .cloned()
846        .unwrap_or_default();
847
848    let value = if plan.is_list {
849        serde_json::Value::Array(matches)
850    } else {
851        matches
852            .into_iter()
853            .next()
854            .unwrap_or(serde_json::Value::Null)
855    };
856
857    if let Some(object) = parent.as_object_mut() {
858        object.insert(field_name.to_string(), value);
859    }
860}
861
862/// Merge a related resource's columns into the parent row itself.
863///
864/// This is what the `...` of `select=title,...directors(name)` means. Where
865/// [`attach_to_parent`] puts the related rows under a key of their own, a
866/// spread lifts their columns into the parent object, so the result stays flat.
867///
868/// Spreading a to-many relationship gives each column an array of that
869/// column's value across the matched rows, rather than an array of objects.
870///
871/// `columns` pairs each requested column with the key it should appear under,
872/// which differ when the client aliased it: `...clients(client_name:name)`
873/// reads `name` and writes `client_name`. Listing them explicitly is also what
874/// lets a parent with no matching child still carry the right keys with null
875/// values, since there is no child row to read the names off.
876///
877/// An empty list spreads nothing, which is what `...clients()` means. Falling
878/// back to every column the child happens to have would be actively wrong: the
879/// related table's own `id` would land on top of the parent's.
880pub fn spread_into_parent(
881    parent: &mut serde_json::Value,
882    plan: &EmbedPlan,
883    grouped: &HashMap<String, Vec<serde_json::Value>>,
884    columns: &[(String, String)],
885) {
886    let key = parent.get(&plan.local_column).and_then(key_to_text);
887    let matches = key
888        .as_ref()
889        .and_then(|k| grouped.get(k))
890        .cloned()
891        .unwrap_or_default();
892
893    let Some(object) = parent.as_object_mut() else {
894        return;
895    };
896
897    for (source, output) in columns {
898        let value = if plan.is_list {
899            serde_json::Value::Array(
900                matches
901                    .iter()
902                    .map(|child| {
903                        child
904                            .get(source)
905                            .cloned()
906                            .unwrap_or(serde_json::Value::Null)
907                    })
908                    .collect(),
909            )
910        } else {
911            matches
912                .first()
913                .and_then(|child| child.get(source).cloned())
914                .unwrap_or(serde_json::Value::Null)
915        };
916        object.insert(output.clone(), value);
917    }
918}
919
920/// Collect the distinct, non-null join keys of a set of parent rows.
921pub fn parent_keys(parents: &[serde_json::Value], local_column: &str) -> Vec<String> {
922    let mut seen = std::collections::HashSet::new();
923    let mut keys = Vec::new();
924
925    for parent in parents {
926        if let Some(key) = parent.get(local_column).and_then(key_to_text) {
927            if seen.insert(key.clone()) {
928                keys.push(key);
929            }
930        }
931    }
932
933    keys
934}
935
936#[cfg(test)]
937mod tests {
938    use super::*;
939
940    fn plan(is_list: bool) -> EmbedPlan {
941        EmbedPlan {
942            row_argument: None,
943            local_column: "id".into(),
944            foreign_column: "user_id".into(),
945            foreign_column_type: "int4".into(),
946            foreign_schema: "public".into(),
947            foreign_table: "posts".into(),
948            is_list,
949            columns: vec![("id".into(), "user_id".into())],
950            junction: None,
951            function: None,
952        }
953    }
954
955    #[test]
956    fn a_junction_hop_keeps_only_the_childs_columns_in_scope() {
957        let mut p = plan(true);
958        p.foreign_table = "jobs".into();
959        p.junction = Some(EmbedJunction {
960            schema: "public".into(),
961            table: "big_projects".into(),
962            parent_columns: vec![("id".into(), "project_id".into())],
963            child_columns: vec![("job_id".into(), "id".into())],
964        });
965        let rows = p
966            .correlated_rows("p", "\"p\"", "jobs", "jobs.*", None, 0, None, None, None)
967            .unwrap();
968        // The junction hides in a lateral subselect, so `name` (on both
969        // tables) is not ambiguous; it is a join, so duplicates stay.
970        assert!(
971            rows.contains("CROSS JOIN LATERAL (SELECT 1 FROM"),
972            "{}",
973            rows
974        );
975        assert!(
976            !rows.contains(" JOIN \"public\".\"big_projects\" AS"),
977            "{}",
978            rows
979        );
980        assert!(
981            !rows.contains("EXISTS") && !rows.contains(" IN ("),
982            "{}",
983            rows
984        );
985        assert!(
986            rows.contains(r#""jobs_jx"."project_id" = "p"."id""#),
987            "{}",
988            rows
989        );
990        assert!(rows.ends_with("WHERE true"), "{}", rows);
991        let order = p.order_expression("p", "\"p\"", "jobs", "\"name\"");
992        assert!(order.contains("CROSS JOIN LATERAL"), "{}", order);
993        let inner = p.inner_join_predicate("p", "\"p\"", "jobs", None);
994        assert!(
995            inner.starts_with(
996                "EXISTS (SELECT 1 FROM \"public\".\"jobs\" AS \"jobs\" CROSS JOIN LATERAL"
997            ),
998            "{}",
999            inner
1000        );
1001    }
1002
1003    #[test]
1004    fn children_sql_binds_keys_as_a_cast_array() {
1005        let sql = plan(true).children_sql(None, &[]).unwrap();
1006        assert_eq!(
1007            sql,
1008            "SELECT row_to_json(t) FROM (SELECT * FROM \"public\".\"posts\" \
1009             WHERE \"user_id\" = ANY($1::int4[])) t"
1010        );
1011    }
1012
1013    #[test]
1014    fn embed_expression_aggregates_a_to_many_relation() {
1015        let sql = plan(true)
1016            .embed_expression(
1017                "p",
1018                "\"p\"",
1019                "posts",
1020                r#""id", "title""#,
1021                None,
1022                0,
1023                None,
1024                None,
1025                None,
1026                EmbedRendering::Verbatim,
1027            )
1028            .unwrap();
1029
1030        assert!(sql.starts_with("COALESCE((SELECT json_agg("), "{}", sql);
1031        // Correlated on the parent, so there is no second query and no bound
1032        // array of keys.
1033        assert!(sql.contains(r#""posts"."user_id" = "p"."id""#), "{}", sql);
1034        assert!(
1035            sql.contains(r#"AS "posts""#),
1036            "the child table is aliased: {}",
1037            sql
1038        );
1039        assert!(!sql.contains("ANY("), "{}", sql);
1040        // An absent relation is an empty array, not null.
1041        assert!(sql.contains("'[]'::json"), "{}", sql);
1042    }
1043
1044    #[test]
1045    fn embed_expression_takes_one_row_for_a_to_one_relation() {
1046        let sql = plan(false)
1047            .embed_expression(
1048                "p",
1049                "\"p\"",
1050                "author",
1051                r#""id""#,
1052                None,
1053                0,
1054                None,
1055                None,
1056                None,
1057                EmbedRendering::Verbatim,
1058            )
1059            .unwrap();
1060
1061        assert!(sql.contains("row_to_json"), "{}", sql);
1062        assert!(!sql.contains("json_agg"), "{}", sql);
1063        assert!(
1064            sql.contains("LIMIT 1"),
1065            "a to-one relation yields one row: {}",
1066            sql
1067        );
1068    }
1069
1070    #[test]
1071    fn embed_expression_limits_rows_per_parent() {
1072        let sql = plan(true)
1073            .embed_expression(
1074                "p",
1075                "\"p\"",
1076                "posts",
1077                r#""id""#,
1078                Some(25),
1079                0,
1080                None,
1081                None,
1082                None,
1083                EmbedRendering::Verbatim,
1084            )
1085            .unwrap();
1086        assert!(sql.contains("LIMIT 25"), "{}", sql);
1087    }
1088
1089    #[test]
1090    fn children_sql_projects_only_the_requested_columns() {
1091        let sql = plan(true)
1092            .children_sql(None, &["title".to_string(), "body".to_string()])
1093            .unwrap();
1094
1095        assert!(
1096            sql.contains(r#"SELECT "title", "body", "user_id" FROM"#),
1097            "{}",
1098            sql
1099        );
1100        assert!(
1101            !sql.contains("SELECT *"),
1102            "an unrequested column should not be read at all: {}",
1103            sql
1104        );
1105    }
1106
1107    #[test]
1108    fn children_sql_always_includes_the_join_column() {
1109        // The grouping keys off the join column, so it has to come back even
1110        // when the client did not ask for it.
1111        let sql = plan(true)
1112            .children_sql(None, &["title".to_string()])
1113            .unwrap();
1114        assert!(sql.contains(r#""user_id""#), "{}", sql);
1115    }
1116
1117    #[test]
1118    fn children_sql_does_not_repeat_the_join_column() {
1119        let sql = plan(true)
1120            .children_sql(None, &["user_id".to_string(), "title".to_string()])
1121            .unwrap();
1122        assert_eq!(
1123            sql.matches(r#""user_id""#).count(),
1124            2,
1125            "expected the column once in the projection and once in the WHERE: {}",
1126            sql
1127        );
1128    }
1129
1130    #[test]
1131    fn children_sql_escapes_column_names() {
1132        // Column names reach here from the client.
1133        let sql = plan(true)
1134            .children_sql(None, &[r#"ev"il"#.to_string()])
1135            .unwrap();
1136        assert!(sql.contains(r#""ev""il""#), "{}", sql);
1137    }
1138
1139    #[test]
1140    fn children_sql_falls_back_to_every_column() {
1141        let sql = plan(true).children_sql(None, &[]).unwrap();
1142        assert!(sql.contains("SELECT * FROM"), "{}", sql);
1143    }
1144
1145    #[test]
1146    fn children_sql_applies_a_limit() {
1147        let sql = plan(true).children_sql(Some(25), &[]).unwrap();
1148        assert!(sql.contains("LIMIT 25"), "{}", sql);
1149    }
1150
1151    #[test]
1152    fn children_sql_rejects_a_non_plain_type_name() {
1153        let mut p = plan(true);
1154        p.foreign_column_type = "int4; DROP TABLE users".into();
1155        assert!(p.children_sql(None, &[]).is_err());
1156    }
1157
1158    #[test]
1159    fn keys_are_rendered_without_json_quoting() {
1160        assert_eq!(key_to_text(&serde_json::json!(7)), Some("7".to_string()));
1161        assert_eq!(
1162            key_to_text(&serde_json::json!("abc")),
1163            Some("abc".to_string())
1164        );
1165        assert_eq!(key_to_text(&serde_json::Value::Null), None);
1166    }
1167
1168    #[test]
1169    fn parent_keys_are_distinct_and_skip_nulls() {
1170        let parents = vec![
1171            serde_json::json!({"id": 1}),
1172            serde_json::json!({"id": 2}),
1173            serde_json::json!({"id": 1}),
1174            serde_json::json!({"id": null}),
1175        ];
1176        assert_eq!(parent_keys(&parents, "id"), vec!["1", "2"]);
1177    }
1178
1179    #[test]
1180    fn to_many_attaches_an_array_and_empty_when_absent() {
1181        let grouped = group_by_key(vec![serde_json::json!({"id": 10, "user_id": 1})], "user_id");
1182
1183        let mut matched = serde_json::json!({"id": 1});
1184        attach_to_parent(&mut matched, "posts", &plan(true), &grouped);
1185        assert_eq!(matched["posts"].as_array().map(|a| a.len()), Some(1));
1186
1187        let mut unmatched = serde_json::json!({"id": 2});
1188        attach_to_parent(&mut unmatched, "posts", &plan(true), &grouped);
1189        assert_eq!(
1190            unmatched["posts"],
1191            serde_json::json!([]),
1192            "a to-many with no matches must still be an array"
1193        );
1194    }
1195
1196    #[test]
1197    fn to_one_attaches_an_object_or_null() {
1198        let grouped = group_by_key(vec![serde_json::json!({"id": 10, "user_id": 1})], "user_id");
1199
1200        let mut matched = serde_json::json!({"id": 1});
1201        attach_to_parent(&mut matched, "author", &plan(false), &grouped);
1202        assert_eq!(matched["author"]["id"], serde_json::json!(10));
1203
1204        let mut unmatched = serde_json::json!({"id": 2});
1205        attach_to_parent(&mut unmatched, "author", &plan(false), &grouped);
1206        assert_eq!(unmatched["author"], serde_json::Value::Null);
1207    }
1208
1209    #[test]
1210    fn spread_lifts_a_to_one_relation_into_the_parent() {
1211        let mut grouped = HashMap::new();
1212        grouped.insert(
1213            "1".to_string(),
1214            vec![serde_json::json!({"first_name": "Ada", "last_name": "L"})],
1215        );
1216
1217        let mut parent = serde_json::json!({"id": 1, "title": "Notes"});
1218        spread_into_parent(
1219            &mut parent,
1220            &plan(false),
1221            &grouped,
1222            &[
1223                ("first_name".into(), "first_name".into()),
1224                ("last_name".into(), "last_name".into()),
1225            ],
1226        );
1227
1228        // Flat, not nested under a key of its own.
1229        assert_eq!(parent["first_name"], "Ada");
1230        assert_eq!(parent["last_name"], "L");
1231        assert_eq!(parent["title"], "Notes");
1232        assert!(parent.get("posts").is_none());
1233    }
1234
1235    #[test]
1236    fn spread_without_a_match_still_carries_the_requested_keys() {
1237        let mut parent = serde_json::json!({"id": 9});
1238        spread_into_parent(
1239            &mut parent,
1240            &plan(false),
1241            &HashMap::new(),
1242            &[("first_name".into(), "first_name".into())],
1243        );
1244        assert_eq!(parent["first_name"], serde_json::Value::Null);
1245    }
1246
1247    #[test]
1248    fn spread_of_a_to_many_relation_gives_a_column_per_array() {
1249        let mut grouped = HashMap::new();
1250        grouped.insert(
1251            "1".to_string(),
1252            vec![
1253                serde_json::json!({"title": "a"}),
1254                serde_json::json!({"title": "b"}),
1255            ],
1256        );
1257
1258        let mut parent = serde_json::json!({"id": 1});
1259        spread_into_parent(
1260            &mut parent,
1261            &plan(true),
1262            &grouped,
1263            &[("title".into(), "title".into())],
1264        );
1265
1266        assert_eq!(parent["title"], serde_json::json!(["a", "b"]));
1267    }
1268
1269    #[test]
1270    fn spread_without_a_column_list_adds_nothing() {
1271        // `...clients()` spreads no columns. Taking every column the child has
1272        // would drop the related table's `id` on top of the parent's.
1273        let mut grouped = HashMap::new();
1274        grouped.insert("1".to_string(), vec![serde_json::json!({"id": 7, "b": 2})]);
1275
1276        let mut parent = serde_json::json!({"id": 1});
1277        spread_into_parent(&mut parent, &plan(false), &grouped, &[]);
1278
1279        assert_eq!(parent, serde_json::json!({"id": 1}));
1280    }
1281}