Skip to content

Commit 0af3d80

Browse files
committed
feat: add pg_settings view
1 parent 678c746 commit 0af3d80

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

datafusion-postgres/src/pg_catalog.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod pg_attribute;
2424
mod pg_class;
2525
mod pg_database;
2626
mod pg_namespace;
27+
mod pg_settings;
2728

2829
const PG_CATALOG_TABLE_PG_AGGREGATE: &str = "pg_aggregate";
2930
const PG_CATALOG_TABLE_PG_AM: &str = "pg_am";
@@ -86,6 +87,7 @@ const PG_CATALOG_TABLE_PG_SUBSCRIPTION_REL: &str = "pg_subscription_rel";
8687
const PG_CATALOG_TABLE_PG_TABLESPACE: &str = "pg_tablespace";
8788
const PG_CATALOG_TABLE_PG_TRIGGER: &str = "pg_trigger";
8889
const PG_CATALOG_TABLE_PG_USER_MAPPING: &str = "pg_user_mapping";
90+
const PG_CATALOG_VIEW_PG_SETTINGS: &str = "pg_settings";
8991

9092
/// Determine PostgreSQL table type (relkind) from DataFusion TableProvider
9193
fn get_table_type(table: &Arc<dyn TableProvider>) -> &'static str {
@@ -180,6 +182,7 @@ pub const PG_CATALOG_TABLES: &[&str] = &[
180182
PG_CATALOG_TABLE_PG_TABLESPACE,
181183
PG_CATALOG_TABLE_PG_TRIGGER,
182184
PG_CATALOG_TABLE_PG_USER_MAPPING,
185+
PG_CATALOG_VIEW_PG_SETTINGS,
183186
];
184187

185188
#[derive(Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
@@ -345,6 +348,10 @@ impl SchemaProvider for PgCatalogSchemaProvider {
345348
StreamingTable::try_new(Arc::clone(table.schema()), vec![table]).unwrap(),
346349
)))
347350
}
351+
PG_CATALOG_VIEW_PG_SETTINGS => {
352+
let table = pg_settings::PgSettingsView::try_new()?;
353+
Ok(Some(Arc::new(table.try_into_memtable()?)))
354+
}
348355

349356
_ => Ok(None),
350357
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use std::sync::Arc;
2+
3+
use datafusion::arrow::array::{ArrayRef, BooleanArray, Int32Array, RecordBatch, StringArray};
4+
use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};
5+
use datafusion::catalog::MemTable;
6+
use datafusion::error::Result;
7+
8+
#[derive(Debug, Clone)]
9+
pub(crate) struct PgSettingsView {
10+
schema: SchemaRef,
11+
data: Vec<RecordBatch>,
12+
}
13+
14+
impl PgSettingsView {
15+
pub(crate) fn try_new() -> Result<PgSettingsView> {
16+
let schema = Arc::new(Schema::new(vec![
17+
// name | setting | unit | category | short_
18+
//desc | extra_desc
19+
//| context | vartype | source | min_val | max_val | enumvals |
20+
//boot_val | reset_val | sourcefile | sourceline | pending_restart
21+
Field::new("name", DataType::Utf8, true),
22+
Field::new("setting", DataType::Utf8, true),
23+
Field::new("unit", DataType::Utf8, true),
24+
Field::new("category", DataType::Utf8, true),
25+
Field::new("short_desc", DataType::Utf8, true),
26+
Field::new("extra_desc", DataType::Utf8, true),
27+
Field::new("context", DataType::Utf8, true),
28+
Field::new("vartype", DataType::Utf8, true),
29+
Field::new("source", DataType::Utf8, true),
30+
Field::new("min_val", DataType::Utf8, true),
31+
Field::new("max_val", DataType::Utf8, true),
32+
Field::new("enumvals", DataType::Utf8, true),
33+
Field::new("bool_val", DataType::Utf8, true),
34+
Field::new("reset_val", DataType::Utf8, true),
35+
Field::new("sourcefile", DataType::Utf8, true),
36+
Field::new("sourceline", DataType::Int32, true),
37+
Field::new("pending_restart", DataType::Boolean, true),
38+
]));
39+
40+
let data = Self::create_data(schema.clone())?;
41+
42+
Ok(Self { schema, data })
43+
}
44+
45+
fn create_data(schema: Arc<Schema>) -> Result<Vec<RecordBatch>> {
46+
let mut name: Vec<Option<&str>> = Vec::new();
47+
let mut setting: Vec<Option<&str>> = Vec::new();
48+
let mut unit: Vec<Option<&str>> = Vec::new();
49+
let mut category: Vec<Option<&str>> = Vec::new();
50+
let mut short_desc: Vec<Option<&str>> = Vec::new();
51+
let mut extra_desc: Vec<Option<&str>> = Vec::new();
52+
let mut context: Vec<Option<&str>> = Vec::new();
53+
let mut vartype: Vec<Option<&str>> = Vec::new();
54+
let mut source: Vec<Option<&str>> = Vec::new();
55+
let mut min_val: Vec<Option<&str>> = Vec::new();
56+
let mut max_val: Vec<Option<&str>> = Vec::new();
57+
let mut enumvals: Vec<Option<&str>> = Vec::new();
58+
let mut bool_val: Vec<Option<&str>> = Vec::new();
59+
let mut reset_val: Vec<Option<&str>> = Vec::new();
60+
let mut sourcefile: Vec<Option<&str>> = Vec::new();
61+
let mut sourceline: Vec<Option<i32>> = Vec::new();
62+
let mut pending_restart: Vec<Option<bool>> = Vec::new();
63+
64+
let data = vec![("standard_conforming_strings", "on")];
65+
66+
for (setting_name, setting_val) in data {
67+
name.push(Some(setting_name));
68+
setting.push(Some(setting_val));
69+
70+
unit.push(None);
71+
category.push(None);
72+
short_desc.push(None);
73+
extra_desc.push(None);
74+
context.push(None);
75+
vartype.push(None);
76+
source.push(None);
77+
min_val.push(None);
78+
max_val.push(None);
79+
enumvals.push(None);
80+
bool_val.push(None);
81+
reset_val.push(None);
82+
sourcefile.push(None);
83+
sourceline.push(None);
84+
pending_restart.push(None);
85+
}
86+
87+
let arrays: Vec<ArrayRef> = vec![
88+
Arc::new(StringArray::from(name)),
89+
Arc::new(StringArray::from(setting)),
90+
Arc::new(StringArray::from(unit)),
91+
Arc::new(StringArray::from(category)),
92+
Arc::new(StringArray::from(short_desc)),
93+
Arc::new(StringArray::from(extra_desc)),
94+
Arc::new(StringArray::from(context)),
95+
Arc::new(StringArray::from(vartype)),
96+
Arc::new(StringArray::from(source)),
97+
Arc::new(StringArray::from(min_val)),
98+
Arc::new(StringArray::from(max_val)),
99+
Arc::new(StringArray::from(enumvals)),
100+
Arc::new(StringArray::from(bool_val)),
101+
Arc::new(StringArray::from(reset_val)),
102+
Arc::new(StringArray::from(sourcefile)),
103+
Arc::new(Int32Array::from(sourceline)),
104+
Arc::new(BooleanArray::from(pending_restart)),
105+
];
106+
107+
let batch = RecordBatch::try_new(schema.clone(), arrays)?;
108+
109+
Ok(vec![batch])
110+
}
111+
112+
pub(crate) fn try_into_memtable(self) -> Result<MemTable> {
113+
MemTable::try_new(self.schema, vec![self.data])
114+
}
115+
}

0 commit comments

Comments
 (0)