Skip to content

Commit 841f59a

Browse files
authored
feat(cubesql): Support information_schema.sql_implementation_info meta table
1 parent 2109849 commit 841f59a

File tree

7 files changed

+170
-5
lines changed

7 files changed

+170
-5
lines changed

rust/cubesql/cubesql/src/compile/engine/context_postgresql.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use super::information_schema::postgres::{
1414
views::InfoSchemaViewsProvider as PostgresSchemaViewsProvider,
1515
InfoSchemaRoleColumnGrantsProvider as PostgresInfoSchemaRoleColumnGrantsProvider,
1616
InfoSchemaRoleTableGrantsProvider as PostgresInfoSchemaRoleTableGrantsProvider,
17+
InfoSchemaSqlImplementationInfoProvider as PostgresInfoSchemaSqlImplementationInfoProvider,
1718
InfoSchemaTestingBlockingProvider, InfoSchemaTestingDatasetProvider, PgCatalogAmProvider,
1819
PgCatalogAttrdefProvider, PgCatalogAttributeProvider, PgCatalogClassProvider,
1920
PgCatalogConstraintProvider, PgCatalogDatabaseProvider, PgCatalogDependProvider,
@@ -70,6 +71,10 @@ impl DatabaseProtocol {
7071
"information_schema.role_column_grants".to_string()
7172
} else if let Some(_) = any.downcast_ref::<PostgresSchemaSchemataProvider>() {
7273
"information_schema.schemata".to_string()
74+
} else if let Some(_) =
75+
any.downcast_ref::<PostgresInfoSchemaSqlImplementationInfoProvider>()
76+
{
77+
"information_schema.sql_implementation_info".to_string()
7378
} else if let Some(_) = any.downcast_ref::<PgCatalogTableProvider>() {
7479
"pg_catalog.pg_tables".to_string()
7580
} else if let Some(_) = any.downcast_ref::<PgCatalogTypeProvider>() {
@@ -289,6 +294,11 @@ impl DatabaseProtocol {
289294
&context.session_state.database().unwrap_or("db".to_string()),
290295
)))
291296
}
297+
"sql_implementation_info" => {
298+
return Some(Arc::new(
299+
PostgresInfoSchemaSqlImplementationInfoProvider::new(),
300+
))
301+
}
292302
#[cfg(debug_assertions)]
293303
"testing_dataset" => {
294304
return Some(Arc::new(InfoSchemaTestingDatasetProvider::new(5, 1000)))

rust/cubesql/cubesql/src/compile/engine/information_schema/postgres/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub mod constraint_column_usage;
77
pub mod key_column_usage;
88
pub mod referential_constraints;
99
pub mod schemata;
10+
pub mod sql_implementation_info;
1011
pub mod table_constraints;
1112
pub mod tables;
1213
pub mod views;
@@ -77,5 +78,6 @@ pub use pg_user::*;
7778
pub use pg_views::*;
7879
pub use role_column_grants::*;
7980
pub use role_table_grants::*;
81+
pub use sql_implementation_info::*;
8082
pub use testing_blocking::*;
8183
pub use testing_dataset::*;
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
use std::{any::Any, sync::Arc};
2+
3+
use async_trait::async_trait;
4+
use datafusion::{
5+
arrow::{
6+
array::{Array, ArrayRef, StringBuilder, UInt32Builder},
7+
datatypes::{DataType, Field, Schema, SchemaRef},
8+
record_batch::RecordBatch,
9+
},
10+
datasource::{datasource::TableProviderFilterPushDown, TableProvider, TableType},
11+
error::DataFusionError,
12+
logical_plan::Expr,
13+
physical_plan::{memory::MemoryExec, ExecutionPlan},
14+
};
15+
16+
struct InfoSchemaSqlImplementationInfoBuilder {
17+
implementation_info_id: StringBuilder,
18+
implementation_info_name: StringBuilder,
19+
integer_value: UInt32Builder,
20+
character_value: StringBuilder,
21+
comments: StringBuilder,
22+
}
23+
24+
impl InfoSchemaSqlImplementationInfoBuilder {
25+
fn new(capacity: usize) -> Self {
26+
Self {
27+
implementation_info_id: StringBuilder::new(capacity),
28+
implementation_info_name: StringBuilder::new(capacity),
29+
integer_value: UInt32Builder::new(capacity),
30+
character_value: StringBuilder::new(capacity),
31+
comments: StringBuilder::new(capacity),
32+
}
33+
}
34+
35+
fn add_info(
36+
&mut self,
37+
implementation_info_id: impl AsRef<str>,
38+
implementation_info_name: impl AsRef<str>,
39+
integer_value: Option<u32>,
40+
character_value: Option<&str>,
41+
comments: Option<&str>,
42+
) {
43+
self.implementation_info_id
44+
.append_value(&implementation_info_id)
45+
.unwrap();
46+
self.implementation_info_name
47+
.append_value(&implementation_info_name)
48+
.unwrap();
49+
self.integer_value.append_option(integer_value).unwrap();
50+
self.character_value.append_option(character_value).unwrap();
51+
self.comments.append_option(comments).unwrap();
52+
}
53+
54+
fn finish(mut self) -> Vec<Arc<dyn Array>> {
55+
let columns: Vec<Arc<dyn Array>> = vec![
56+
Arc::new(self.implementation_info_id.finish()),
57+
Arc::new(self.implementation_info_name.finish()),
58+
Arc::new(self.integer_value.finish()),
59+
Arc::new(self.character_value.finish()),
60+
Arc::new(self.comments.finish()),
61+
];
62+
63+
columns
64+
}
65+
}
66+
67+
pub struct InfoSchemaSqlImplementationInfoProvider {
68+
data: Arc<Vec<ArrayRef>>,
69+
}
70+
71+
impl InfoSchemaSqlImplementationInfoProvider {
72+
pub fn new() -> Self {
73+
let mut builder = InfoSchemaSqlImplementationInfoBuilder::new(2);
74+
75+
builder.add_info("17", "DBMS NAME", None, Some("PostgreSQL"), None);
76+
builder.add_info("18", "DBMS VERSION", None, Some("14.02.0000)"), None);
77+
78+
Self {
79+
data: Arc::new(builder.finish()),
80+
}
81+
}
82+
}
83+
84+
#[async_trait]
85+
impl TableProvider for InfoSchemaSqlImplementationInfoProvider {
86+
fn as_any(&self) -> &dyn Any {
87+
self
88+
}
89+
90+
fn table_type(&self) -> TableType {
91+
TableType::View
92+
}
93+
94+
fn schema(&self) -> SchemaRef {
95+
Arc::new(Schema::new(vec![
96+
Field::new("implementation_info_id", DataType::Utf8, false),
97+
Field::new("implementation_info_name", DataType::Utf8, false),
98+
Field::new("integer_value", DataType::UInt32, true),
99+
Field::new("character_value", DataType::Utf8, true),
100+
Field::new("comments", DataType::Utf8, true),
101+
]))
102+
}
103+
104+
async fn scan(
105+
&self,
106+
projection: &Option<Vec<usize>>,
107+
_filters: &[Expr],
108+
_limit: Option<usize>,
109+
) -> Result<Arc<dyn ExecutionPlan>, DataFusionError> {
110+
let batch = RecordBatch::try_new(self.schema(), self.data.to_vec())?;
111+
112+
Ok(Arc::new(MemoryExec::try_new(
113+
&[vec![batch]],
114+
self.schema(),
115+
projection.clone(),
116+
)?))
117+
}
118+
119+
fn supports_filter_pushdown(
120+
&self,
121+
_filter: &Expr,
122+
) -> Result<TableProviderFilterPushDown, DataFusionError> {
123+
Ok(TableProviderFilterPushDown::Unsupported)
124+
}
125+
}

rust/cubesql/cubesql/src/compile/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18421,4 +18421,23 @@ LIMIT {{ limit }}{% endif %}"#.to_string(),
1842118421
}
1842218422
)
1842318423
}
18424+
18425+
#[tokio::test]
18426+
async fn test_quicksight_sql_implementation_info() -> Result<(), CubeError> {
18427+
insta::assert_snapshot!(
18428+
"quicksight_sql_implementation_info",
18429+
execute_query(
18430+
r#"
18431+
SELECT character_value, version()
18432+
FROM INFORMATION_SCHEMA.SQL_IMPLEMENTATION_INFO
18433+
WHERE implementation_info_id IN ('17','18')
18434+
"#
18435+
.to_string(),
18436+
DatabaseProtocol::PostgreSQL
18437+
)
18438+
.await?
18439+
);
18440+
18441+
Ok(())
18442+
}
1842418443
}

rust/cubesql/cubesql/src/compile/query_engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl QueryEngine for SqlQueryEngine {
366366
ctx.register_udf(create_user_udf(state.clone()));
367367
} else if state.protocol == DatabaseProtocol::PostgreSQL {
368368
ctx.register_udf(create_version_udf(
369-
"PostgreSQL 14.1 on x86_64-cubesql".to_string(),
369+
"PostgreSQL 14.2 on x86_64-cubesql".to_string(),
370370
));
371371
ctx.register_udf(create_db_udf("current_database".to_string(), state.clone()));
372372
ctx.register_udf(create_db_udf("current_schema".to_string(), state.clone()));
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
---
22
source: cubesql/src/compile/mod.rs
3-
assertion_line: 7401
4-
expression: "execute_query(\"SELECT version() UNION ALL SELECT pg_catalog.version();\".to_string(),\n DatabaseProtocol::PostgreSQL).await?"
3+
expression: "execute_query(\"SELECT version() UNION ALL SELECT pg_catalog.version();\".to_string(),\nDatabaseProtocol::PostgreSQL).await?"
54
---
65
+-----------------------------------+
76
| version() |
87
+-----------------------------------+
9-
| PostgreSQL 14.1 on x86_64-cubesql |
10-
| PostgreSQL 14.1 on x86_64-cubesql |
8+
| PostgreSQL 14.2 on x86_64-cubesql |
9+
| PostgreSQL 14.2 on x86_64-cubesql |
1110
+-----------------------------------+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: cubesql/src/compile/mod.rs
3+
expression: "execute_query(r#\"\n SELECT character_value, version() \n FROM INFORMATION_SCHEMA.SQL_IMPLEMENTATION_INFO\n WHERE implementation_info_id IN ('17','18')\n \"#.to_string(),\nDatabaseProtocol::PostgreSQL).await?"
4+
---
5+
+-----------------+-----------------------------------+
6+
| character_value | version() |
7+
+-----------------+-----------------------------------+
8+
| PostgreSQL | PostgreSQL 14.2 on x86_64-cubesql |
9+
| 14.02.0000) | PostgreSQL 14.2 on x86_64-cubesql |
10+
+-----------------+-----------------------------------+

0 commit comments

Comments
 (0)