Skip to content

Commit

Permalink
fix(cubesql): Normalize column names for joins and aliased columns
Browse files Browse the repository at this point in the history
  • Loading branch information
MazterQyou authored and ovr committed May 23, 2022
1 parent 0f8a522 commit 7faadc9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
14 changes: 7 additions & 7 deletions rust/cubesql/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/cubesql/cubesql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://cube.dev/docs"
homepage = "https://cube.dev"

[dependencies]
datafusion = { git = 'https://github.com/cube-js/arrow-datafusion.git', rev = "ee0d5c3ac681ab32b24433c3ba0efa7875bb8039", default-features = false, features = ["unicode_expressions"] }
datafusion = { git = 'https://github.com/cube-js/arrow-datafusion.git', rev = "6e51e1716bf48992802c1f16f362d8392737c705", default-features = false, features = ["unicode_expressions"] }
anyhow = "1.0"
thiserror = "1.0"
cubeclient = { path = "../cubeclient" }
Expand Down
68 changes: 68 additions & 0 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7249,6 +7249,74 @@ ORDER BY \"COUNT(count)\" DESC"
.await?
);

insta::assert_snapshot!(
"powerbi_from_subquery",
execute_query(
"
select
pkcol.COLUMN_NAME as PK_COLUMN_NAME,
fkcol.TABLE_SCHEMA AS FK_TABLE_SCHEMA,
fkcol.TABLE_NAME AS FK_TABLE_NAME,
fkcol.COLUMN_NAME as FK_COLUMN_NAME,
fkcol.ORDINAL_POSITION as ORDINAL,
fkcon.CONSTRAINT_SCHEMA || '_' || fkcol.TABLE_NAME || '_' || 'users' || '_' || fkcon.CONSTRAINT_NAME as FK_NAME
from
(select distinct constraint_catalog, constraint_schema, unique_constraint_schema, constraint_name, unique_constraint_name
from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS) fkcon
inner join
INFORMATION_SCHEMA.KEY_COLUMN_USAGE fkcol
on fkcon.CONSTRAINT_SCHEMA = fkcol.CONSTRAINT_SCHEMA
and fkcon.CONSTRAINT_NAME = fkcol.CONSTRAINT_NAME
inner join
INFORMATION_SCHEMA.KEY_COLUMN_USAGE pkcol
on fkcon.UNIQUE_CONSTRAINT_SCHEMA = pkcol.CONSTRAINT_SCHEMA
and fkcon.UNIQUE_CONSTRAINT_NAME = pkcol.CONSTRAINT_NAME
where pkcol.TABLE_SCHEMA = 'public' and pkcol.TABLE_NAME = 'users'
and pkcol.ORDINAL_POSITION = fkcol.ORDINAL_POSITION
order by FK_NAME, fkcol.ORDINAL_POSITION
;
"
.to_string(),
DatabaseProtocol::PostgreSQL
)
.await?
);

insta::assert_snapshot!(
"powerbi_uppercase_alias",
execute_query(
"
select
i.CONSTRAINT_SCHEMA || '_' || i.CONSTRAINT_NAME as INDEX_NAME,
ii.COLUMN_NAME,
ii.ORDINAL_POSITION,
case
when i.CONSTRAINT_TYPE = 'PRIMARY KEY' then 'Y'
else 'N'
end as PRIMARY_KEY
from INFORMATION_SCHEMA.table_constraints i
inner join INFORMATION_SCHEMA.key_column_usage ii on
i.CONSTRAINT_SCHEMA = ii.CONSTRAINT_SCHEMA and
i.CONSTRAINT_NAME = ii.CONSTRAINT_NAME and
i.TABLE_SCHEMA = ii.TABLE_SCHEMA and
i.TABLE_NAME = ii.TABLE_NAME
where
i.TABLE_SCHEMA = 'public' and
i.TABLE_NAME = 'KibanaSampleDataEcommerce' and
i.CONSTRAINT_TYPE in ('PRIMARY KEY', 'UNIQUE')
order by
i.CONSTRAINT_SCHEMA || '_' || i.CONSTRAINT_NAME,
ii.TABLE_SCHEMA,
ii.TABLE_NAME,
ii.ORDINAL_POSITION
;
"
.to_string(),
DatabaseProtocol::PostgreSQL
)
.await?
);

// TODO: 'Boolean = Utf8' can't be evaluated because there isn't a common type to coerce the types to
// insta::assert_snapshot!(
// "powerbi_introspection",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: cubesql/src/compile/mod.rs
expression: "execute_query(\"\n select\n pkcol.COLUMN_NAME as PK_COLUMN_NAME,\n fkcol.TABLE_SCHEMA AS FK_TABLE_SCHEMA,\n fkcol.TABLE_NAME AS FK_TABLE_NAME,\n fkcol.COLUMN_NAME as FK_COLUMN_NAME,\n fkcol.ORDINAL_POSITION as ORDINAL,\n fkcon.CONSTRAINT_SCHEMA || '_' || fkcol.TABLE_NAME || '_' || 'users' || '_' || fkcon.CONSTRAINT_NAME as FK_NAME\n from\n (select distinct constraint_catalog, constraint_schema, unique_constraint_schema, constraint_name, unique_constraint_name\n from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS) fkcon\n inner join\n INFORMATION_SCHEMA.KEY_COLUMN_USAGE fkcol\n on fkcon.CONSTRAINT_SCHEMA = fkcol.CONSTRAINT_SCHEMA\n and fkcon.CONSTRAINT_NAME = fkcol.CONSTRAINT_NAME\n inner join\n INFORMATION_SCHEMA.KEY_COLUMN_USAGE pkcol\n on fkcon.UNIQUE_CONSTRAINT_SCHEMA = pkcol.CONSTRAINT_SCHEMA\n and fkcon.UNIQUE_CONSTRAINT_NAME = pkcol.CONSTRAINT_NAME\n where pkcol.TABLE_SCHEMA = 'public' and pkcol.TABLE_NAME = 'users'\n and pkcol.ORDINAL_POSITION = fkcol.ORDINAL_POSITION\n order by FK_NAME, fkcol.ORDINAL_POSITION\n ;\n \".to_string(),\n DatabaseProtocol::PostgreSQL).await?"
---
++
++
++
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: cubesql/src/compile/mod.rs
expression: "execute_query(\"\n select\n i.CONSTRAINT_SCHEMA || '_' || i.CONSTRAINT_NAME as INDEX_NAME,\n ii.COLUMN_NAME,\n ii.ORDINAL_POSITION,\n case\n when i.CONSTRAINT_TYPE = 'PRIMARY KEY' then 'Y'\n else 'N'\n end as PRIMARY_KEY\n from INFORMATION_SCHEMA.table_constraints i\n inner join INFORMATION_SCHEMA.key_column_usage ii on\n i.CONSTRAINT_SCHEMA = ii.CONSTRAINT_SCHEMA and\n i.CONSTRAINT_NAME = ii.CONSTRAINT_NAME and\n i.TABLE_SCHEMA = ii.TABLE_SCHEMA and\n i.TABLE_NAME = ii.TABLE_NAME\n where\n i.TABLE_SCHEMA = 'public' and\n i.TABLE_NAME = 'KibanaSampleDataEcommerce' and\n i.CONSTRAINT_TYPE in ('PRIMARY KEY', 'UNIQUE')\n order by\n i.CONSTRAINT_SCHEMA || '_' || i.CONSTRAINT_NAME,\n ii.TABLE_SCHEMA,\n ii.TABLE_NAME,\n ii.ORDINAL_POSITION\n ;\n \".to_string(),\n DatabaseProtocol::PostgreSQL).await?"
---
++
++
++

0 comments on commit 7faadc9

Please sign in to comment.