Skip to main content

uqa_sql/semantics/table_functions/
columns.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Empty table-function descriptors produce final names without copying overwritten defaults or aliases.
8
9use crate::SQLError;
10use uqa_core::memory::{Produced, ProductionControl, ProductionVec};
11
12pub fn table_function_empty_schema_with_control(
13    name: &str,
14    output_name: &str,
15    alias: Option<&str>,
16    aliases: &[String],
17    output_width: usize,
18    ordinality: bool,
19    control: &ProductionControl<'_>,
20) -> Result<Produced<Vec<String>>, SQLError> {
21    let lower = crate::semantics::builtin_function_dispatch_name_with_control(name, control)?;
22    let defaults: &[&str] = match lower.as_str() {
23        "json_each" | "jsonb_each" | "json_each_text" | "jsonb_each_text" => &["key", "value"],
24        "pg_get_sequence_data" => &["last_value", "is_called"],
25        "aclexplode" => &["grantor", "grantee", "privilege_type", "is_grantable"],
26        "pg_sequence_parameters" => &[
27            "start_value",
28            "minimum_value",
29            "maximum_value",
30            "increment",
31            "cycle_option",
32            "cache_size",
33            "data_type",
34        ],
35        "pagerank" | "graph_pagerank" | "hits" | "graph_hits" | "betweenness"
36        | "graph_betweenness" => &["_doc_id", "_score"],
37        "rpq" => &["vertex_id"],
38        "list_analyzers" => &["analyzer_name"],
39        "analyze_text" => &["analysis"],
40        "fts_index_stats" => &[
41            "table_name",
42            "field",
43            "analyzer",
44            "posting_count",
45            "doc_length_count",
46            "indexed_doc_count",
47            "term_count",
48            "total_field_length",
49        ],
50        "text_similarity_join"
51        | "vector_similarity_join"
52        | "graph_join"
53        | "hybrid_join"
54        | "cross_paradigm_join" => &["left_doc_id", "right_doc_id", "_score"],
55        _ => &[],
56    };
57    let width = if !defaults.is_empty() {
58        defaults.len()
59    } else if lower.as_str() == "unnest" {
60        output_width.max(1)
61    } else if matches!(
62        lower.as_str(),
63        "generate_series"
64            | "pg_listening_channels"
65            | "regexp_matches"
66            | "regexp_split_to_table"
67            | "string_to_table"
68            | "json_array_elements"
69            | "jsonb_array_elements"
70            | "json_array_elements_text"
71            | "jsonb_array_elements_text"
72            | "json_object_keys"
73            | "jsonb_object_keys"
74    ) {
75        1
76    } else {
77        if ordinality && aliases.len() > 1 {
78            aliases.len() - 1
79        } else {
80            aliases.len()
81        }
82        .max(1)
83    };
84    let default = if super::is_json_array_table_function(&lower) {
85        "value"
86    } else if lower.as_str() == "unnest" && width > 1 {
87        output_name
88    } else {
89        alias.unwrap_or(output_name)
90    };
91    let mut columns = ProductionVec::new(*control);
92    columns.reserve(width + usize::from(ordinality))?;
93    for position in 0..width + usize::from(ordinality) {
94        let name = aliases.get(position).map_or_else(
95            || {
96                if position == width {
97                    "ordinality"
98                } else {
99                    defaults.get(position).copied().unwrap_or(default)
100                }
101            },
102            String::as_str,
103        );
104        columns.push_produced(control.copy_text(name)?)?;
105    }
106    Ok(columns.finish()?)
107}