Skip to content

Commit d87bd19

Browse files
committed
fix(cubesql): Support Sigma Computing table schema sync
1 parent 0ea12c4 commit d87bd19

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10831,6 +10831,50 @@ ORDER BY \"COUNT(count)\" DESC"
1083110831
Ok(())
1083210832
}
1083310833

10834+
#[tokio::test]
10835+
async fn test_sigma_computing_attnames() -> Result<(), CubeError> {
10836+
insta::assert_snapshot!(
10837+
"sigma_computing_attnames",
10838+
execute_query(
10839+
"
10840+
with
10841+
nsp as (
10842+
select oid as relnamespace
10843+
from pg_catalog.pg_namespace
10844+
where nspname = 'public'
10845+
),
10846+
tbl as (
10847+
select
10848+
nsp.relnamespace as connamespace,
10849+
tbl.oid as conrelid
10850+
from pg_catalog.pg_class tbl
10851+
inner join nsp using (relnamespace)
10852+
where relname = 'emptytbl'
10853+
),
10854+
con as (
10855+
select
10856+
conrelid,
10857+
conkey
10858+
from pg_catalog.pg_constraint
10859+
inner join tbl using (connamespace, conrelid)
10860+
where contype = 'p'
10861+
)
10862+
select attname
10863+
from pg_catalog.pg_attribute att
10864+
inner join con on
10865+
conrelid = attrelid
10866+
and attnum = any(con.conkey)
10867+
order by attnum
10868+
"
10869+
.to_string(),
10870+
DatabaseProtocol::PostgreSQL
10871+
)
10872+
.await?
10873+
);
10874+
10875+
Ok(())
10876+
}
10877+
1083410878
#[tokio::test]
1083510879
async fn test_google_sheets_pg_database_query() -> Result<(), CubeError> {
1083610880
insta::assert_snapshot!(

rust/cubesql/cubesql/src/compile/parser.rs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22

3+
use regex::Regex;
34
use sqlparser::{
45
ast::Statement,
56
dialect::{Dialect, PostgreSqlDialect},
@@ -39,7 +40,7 @@ impl Dialect for MySqlDialectWithBackTicks {
3940
}
4041

4142
lazy_static! {
42-
static ref SIGMA_WORKAROUND: regex::Regex = regex::Regex::new(r#"(?s)^\s*with\s+nsp\sas\s\(.*nspname\s=\s(?P<nspname>'[^']+'|\$\d+).*\),\s+tbl\sas\s\(.*relname\s=\s(?P<relname>'[^']+'|\$\d+).*\).*$"#).unwrap();
43+
static ref SIGMA_WORKAROUND: Regex = Regex::new(r#"(?s)^\s*with\s+nsp\sas\s\(.*nspname\s=\s.*\),\s+tbl\sas\s\(.*relname\s=\s.*\).*select\s+attname.*from\spg_attribute.*$"#).unwrap();
4344
}
4445

4546
pub fn parse_sql_to_statements(
@@ -184,33 +185,17 @@ pub fn parse_sql_to_statements(
184185
);
185186

186187
// Sigma Computing WITH query workaround
187-
let query = match SIGMA_WORKAROUND.captures(&query) {
188-
Some(c) => {
189-
let nspname = c.name("nspname").unwrap().as_str();
190-
let relname = c.name("relname").unwrap().as_str();
191-
format!(
192-
"
193-
select
194-
attname,
195-
typname,
196-
description
197-
from pg_attribute a
198-
join pg_type on atttypid = pg_type.oid
199-
left join pg_description on
200-
attrelid = objoid and
201-
attnum = objsubid
202-
join pg_catalog.pg_namespace nsp ON nspname = {}
203-
join pg_catalog.pg_class tbl ON relname = {} and relnamespace = nsp.oid
204-
where
205-
attnum > 0 and
206-
attrelid = tbl.oid
207-
order by attnum
208-
;
209-
",
210-
nspname, relname
211-
)
212-
}
213-
None => query,
188+
let query = if SIGMA_WORKAROUND.is_match(&query) {
189+
let relnamespace_re = Regex::new(r#"(?s)from\spg_catalog\.pg_class\s+where\s+relname\s=\s(?P<relname>'(?:[^']|'')+'|\$\d+)\s+and\s+relnamespace\s=\s\(select\soid\sfrom\snsp\)"#).unwrap();
190+
let relnamespace_replaced = relnamespace_re.replace(
191+
&query,
192+
"from pg_catalog.pg_class join nsp on relnamespace = nsp.oid where relname = $relname",
193+
);
194+
let attrelid_re = Regex::new(r#"(?s)left\sjoin\spg_description\son\s+attrelid\s=\sobjoid\sand\s+attnum\s=\sobjsubid\s+where\s+attnum\s>\s0\s+and\s+attrelid\s=\s\(select\soid\sfrom\stbl\)"#).unwrap();
195+
let attrelid_replaced = attrelid_re.replace(&relnamespace_replaced, "left join pg_description on attrelid = objoid and attnum = objsubid join tbl on attrelid = tbl.oid where attnum > 0");
196+
attrelid_replaced.to_string()
197+
} else {
198+
query
214199
};
215200

216201
// Metabase
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
source: cubesql/src/compile/mod.rs
3+
expression: "execute_query(\"\n with\n nsp as (\n select oid as relnamespace\n from pg_catalog.pg_namespace\n where nspname = 'public'\n ),\n tbl as (\n select\n nsp.relnamespace as connamespace,\n tbl.oid as conrelid\n from pg_catalog.pg_class tbl\n inner join nsp using (relnamespace)\n where relname = 'emptytbl'\n ),\n con as (\n select\n conrelid,\n conkey\n from pg_catalog.pg_constraint\n inner join tbl using (connamespace, conrelid)\n where contype = 'p'\n )\n select attname\n from pg_catalog.pg_attribute att\n inner join con on\n conrelid = attrelid\n and attnum = any(con.conkey)\n order by attnum\n \".to_string(),\n DatabaseProtocol::PostgreSQL).await?"
4+
---
5+
+---------+
6+
| attname |
7+
+---------+
8+
+---------+

0 commit comments

Comments
 (0)