Skip to content

Commit d02077e

Browse files
committed
feat(cubesql): Support SET ROLE name
1 parent 7e12e4e commit d02077e

File tree

6 files changed

+70
-9
lines changed

6 files changed

+70
-9
lines changed

rust/cubesql/Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubesql/cubesql/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ documentation = "https://cube.dev/docs"
99
homepage = "https://cube.dev"
1010

1111
[dependencies]
12-
datafusion = { git = 'https://github.com/cube-js/arrow-datafusion.git', rev = "00ed6a6d469a69f57dd1f08c1fda3f7c2cf12d80", default-features = false, features = ["regex_expressions", "unicode_expressions"] }
12+
datafusion = { git = 'https://github.com/cube-js/arrow-datafusion.git', rev = "be967a63967f23bdd946a45925d07af0a11dcc39", default-features = false, features = ["regex_expressions", "unicode_expressions"] }
1313
anyhow = "1.0"
1414
thiserror = "1.0"
1515
cubeclient = { path = "../cubeclient" }
1616
pg-srv = { path = "../pg-srv" }
17-
sqlparser = { git = 'https://github.com/cube-js/sqlparser-rs.git', rev = "ac5fc7cbf4368cd4c9e6c2af0e0e35d2eff7cc16" }
17+
sqlparser = { git = 'https://github.com/cube-js/sqlparser-rs.git', rev = "037562f7975fcc92b725efbfbaaf050272668593" }
1818
lazy_static = "1.4.0"
1919
base64 = "0.13.0"
2020
tokio = { version = "1.0", features = ["full", "rt", "tracing"] }

rust/cubesql/cubesql/e2e/tests/postgres.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,16 @@ impl AsyncTestSuite for PostgresIntegrationTestSuite {
10831083
)
10841084
.await?;
10851085

1086+
self.test_simple_query(r#"SET ROLE "cube""#.to_string(), |messages| {
1087+
assert_eq!(messages.len(), 1);
1088+
1089+
// SET
1090+
if let SimpleQueryMessage::Row(_) = messages[0] {
1091+
panic!("Must be CommandComplete command, (SET is used)")
1092+
}
1093+
})
1094+
.await?;
1095+
10861096
// Tableau Desktop
10871097
self.test_simple_query(
10881098
r#"SET DateStyle = 'ISO';SET extra_float_digits = 2;show transaction_isolation"#

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ impl QueryPlanner {
401401
StatusFlags::empty(),
402402
CommandCompletion::Select(0),
403403
)),
404+
(ast::Statement::SetRole { role_name, .. }, _) => self.set_role_to_plan(role_name),
404405
(ast::Statement::SetVariable { key_values }, _) => {
405406
self.set_variable_to_plan(&key_values)
406407
}
@@ -938,6 +939,28 @@ WHERE `TABLE_SCHEMA` = '{}'",
938939
))
939940
}
940941

942+
fn set_role_to_plan(
943+
&self,
944+
role_name: &Option<ast::Ident>,
945+
) -> Result<QueryPlan, CompilationError> {
946+
let flags = StatusFlags::SERVER_STATE_CHANGED;
947+
let role_name = role_name
948+
.as_ref()
949+
.map(|role_name| role_name.value.clone())
950+
.unwrap_or("none".to_string());
951+
let variable =
952+
DatabaseVariable::system("role".to_string(), ScalarValue::Utf8(Some(role_name)), None);
953+
self.state.set_variables(vec![variable]);
954+
match self.state.protocol {
955+
DatabaseProtocol::PostgreSQL => Ok(QueryPlan::MetaOk(flags, CommandCompletion::Set)),
956+
// TODO: Verify that it's possible to use MetaOk too...
957+
DatabaseProtocol::MySQL => Ok(QueryPlan::MetaTabular(
958+
flags,
959+
Box::new(dataframe::DataFrame::new(vec![], vec![])),
960+
)),
961+
}
962+
}
963+
941964
fn set_variable_to_plan(
942965
&self,
943966
key_values: &Vec<ast::SetVariableKeyValue>,
@@ -7085,6 +7108,16 @@ ORDER BY \"COUNT(count)\" DESC"
70857108
.0
70867109
);
70877110

7111+
insta::assert_snapshot!(
7112+
"pg_set_role_show",
7113+
execute_queries_with_flags(
7114+
vec!["SET ROLE NONE".to_string(), "SHOW ROLE".to_string()],
7115+
DatabaseProtocol::PostgreSQL
7116+
)
7117+
.await?
7118+
.0
7119+
);
7120+
70887121
Ok(())
70897122
}
70907123

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: cubesql/src/compile/mod.rs
3+
expression: "execute_queries_with_flags(vec![\"SET ROLE NONE\".to_string(),\n \"SHOW ROLE\".to_string()],\n DatabaseProtocol::PostgreSQL).await?.0"
4+
---
5+
+---------+
6+
| setting |
7+
+---------+
8+
| none |
9+
+---------+

rust/cubesql/cubesql/src/sql/database_variables/postgres/session_vars.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,14 @@ pub fn defaults() -> DatabaseVariables {
9696
),
9797
);
9898

99+
variables.insert(
100+
"role".to_string(),
101+
DatabaseVariable::system(
102+
"role".to_string(),
103+
ScalarValue::Utf8(Some("none".to_string())),
104+
None,
105+
),
106+
);
107+
99108
variables
100109
}

0 commit comments

Comments
 (0)