arrow_flight/sql/metadata/
catalogs.rs1use std::sync::{Arc, LazyLock};
19
20use arrow_array::{RecordBatch, StringArray};
21use arrow_schema::{DataType, Field, Schema, SchemaRef};
22
23use crate::error::Result;
24use crate::sql::CommandGetCatalogs;
25
26pub struct GetCatalogsBuilder {
32 catalogs: Vec<String>,
33}
34
35impl CommandGetCatalogs {
36 pub fn into_builder(self) -> GetCatalogsBuilder {
38 self.into()
39 }
40}
41
42impl From<CommandGetCatalogs> for GetCatalogsBuilder {
43 fn from(_: CommandGetCatalogs) -> Self {
44 Self::new()
45 }
46}
47
48impl Default for GetCatalogsBuilder {
49 fn default() -> Self {
50 Self::new()
51 }
52}
53
54impl GetCatalogsBuilder {
55 pub fn new() -> Self {
57 Self {
58 catalogs: Vec::new(),
59 }
60 }
61
62 pub fn append(&mut self, catalog_name: impl Into<String>) {
64 self.catalogs.push(catalog_name.into());
65 }
66
67 pub fn build(self) -> Result<RecordBatch> {
70 let Self { mut catalogs } = self;
71 catalogs.sort_unstable();
72
73 let batch = RecordBatch::try_new(
74 Arc::clone(&GET_CATALOG_SCHEMA),
75 vec![Arc::new(StringArray::from_iter_values(catalogs)) as _],
76 )?;
77
78 Ok(batch)
79 }
80
81 pub fn schema(&self) -> SchemaRef {
85 get_catalogs_schema()
86 }
87}
88
89fn get_catalogs_schema() -> SchemaRef {
90 Arc::clone(&GET_CATALOG_SCHEMA)
91}
92
93static GET_CATALOG_SCHEMA: LazyLock<SchemaRef> = LazyLock::new(|| {
95 Arc::new(Schema::new(vec![Field::new(
96 "catalog_name",
97 DataType::Utf8,
98 false,
99 )]))
100});
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
107 fn test_catalogs_are_sorted() {
108 let batch = ["a_catalog", "c_catalog", "b_catalog"]
109 .into_iter()
110 .fold(GetCatalogsBuilder::new(), |mut builder, catalog| {
111 builder.append(catalog);
112 builder
113 })
114 .build()
115 .unwrap();
116 let catalogs = batch
117 .column(0)
118 .as_any()
119 .downcast_ref::<StringArray>()
120 .unwrap()
121 .iter()
122 .flatten()
123 .collect::<Vec<_>>();
124 assert!(catalogs.is_sorted());
125 assert_eq!(catalogs, ["a_catalog", "b_catalog", "c_catalog"]);
126 }
127}