Skip to content

Commit a86dc09

Browse files
authored
chore(cubesql): ProtocolDetails - support session variables (#8587)
1 parent 9927ba2 commit a86dc09

File tree

13 files changed

+99
-84
lines changed

13 files changed

+99
-84
lines changed

rust/cubesql/cubesql/src/compile/engine/information_schema/mysql/variables.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{any::Any, sync::Arc};
22

3-
use crate::compile::engine::context::TableName;
3+
use crate::compile::{engine::context::TableName, DatabaseVariables};
44
use async_trait::async_trait;
55
use datafusion::{
66
arrow::{
@@ -14,8 +14,6 @@ use datafusion::{
1414
physical_plan::{memory::MemoryExec, ExecutionPlan},
1515
};
1616

17-
use crate::sql::database_variables::DatabaseVariables;
18-
1917
pub struct PerfSchemaVariablesProvider {
2018
table_name: String,
2119
variables: DatabaseVariables,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{any::Any, sync::Arc};
22

3+
use crate::compile::DatabaseVariables;
34
use async_trait::async_trait;
45
use datafusion::{
56
arrow::{
@@ -13,8 +14,6 @@ use datafusion::{
1314
physical_plan::{memory::MemoryExec, ExecutionPlan},
1415
};
1516

16-
use crate::sql::database_variables::DatabaseVariables;
17-
1817
pub struct PgCatalogSettingsProvider {
1918
vars: DatabaseVariables,
2019
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod query_engine;
1111
pub mod rewrite;
1212
pub mod router;
1313
pub mod service;
14+
pub mod session;
1415

1516
// Internal API
1617
pub mod test;
@@ -22,6 +23,7 @@ pub use protocol::*;
2223
pub use query_engine::*;
2324
pub use rewrite::rewriter::Rewriter;
2425
pub use router::*;
26+
pub use session::*;
2527

2628
// Re-export base deps to minimise version maintenance for crate users such as cloud
2729
pub use datafusion::{self, arrow};

rust/cubesql/cubesql/src/compile/protocol.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
use crate::{compile::CubeContext, CubeError};
1+
use crate::{
2+
compile::{CubeContext, DatabaseVariable, DatabaseVariables},
3+
CubeError,
4+
};
25
use datafusion::datasource;
6+
use log::error;
37
use std::{
48
fmt::Debug,
59
hash::{Hash, Hasher},
@@ -13,11 +17,17 @@ pub trait DatabaseProtocolDetails: Debug + Send + Sync {
1317

1418
fn support_transactions(&self) -> bool;
1519

20+
/// Get default state for session variables
21+
fn get_session_default_variables(&self) -> DatabaseVariables;
22+
23+
/// Get default value for specific session variable
24+
fn get_session_variable_default(&self, name: &str) -> Option<DatabaseVariable>;
25+
1626
fn get_provider(
1727
&self,
1828
context: &CubeContext,
1929
tr: datafusion::catalog::TableReference,
20-
) -> Option<std::sync::Arc<dyn datasource::TableProvider>>;
30+
) -> Option<Arc<dyn datasource::TableProvider>>;
2131

2232
fn table_name_by_table_provider(
2333
&self,
@@ -70,6 +80,28 @@ impl DatabaseProtocolDetails for DatabaseProtocol {
7080
}
7181
}
7282

83+
fn get_session_default_variables(&self) -> DatabaseVariables {
84+
match &self {
85+
DatabaseProtocol::MySQL => {
86+
// TODO(ovr): Should we move it from session?
87+
error!("get_session_default_variables was called on MySQL protocol");
88+
89+
DatabaseVariables::default()
90+
}
91+
DatabaseProtocol::PostgreSQL => {
92+
// TODO(ovr): Should we move it from session?
93+
error!("get_session_default_variables was called on PostgreSQL protocol");
94+
95+
DatabaseVariables::default()
96+
}
97+
DatabaseProtocol::Extension(ext) => ext.get_session_default_variables(),
98+
}
99+
}
100+
101+
fn get_session_variable_default(&self, name: &str) -> Option<DatabaseVariable> {
102+
self.get_session_default_variables().get(name).cloned()
103+
}
104+
73105
fn get_provider(
74106
&self,
75107
context: &CubeContext,

rust/cubesql/cubesql/src/compile/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use crate::{
1010
engine::{df::planner::CubeQueryPlanner, udf::*, VariablesProvider},
1111
error::{CompilationError, CompilationResult},
1212
parser::parse_sql_to_statement,
13+
DatabaseVariable, DatabaseVariablesToUpdate,
1314
},
1415
sql::{
15-
database_variables::{DatabaseVariable, DatabaseVariablesToUpdate},
1616
dataframe,
1717
statement::{
1818
ApproximateCountDistinctVisitor, CastReplacer, RedshiftDatePartReplacer,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use datafusion::{scalar::ScalarValue, variable::VarType};
2+
use std::collections::HashMap;
3+
4+
pub type DatabaseVariablesToUpdate = Vec<DatabaseVariable>;
5+
pub type DatabaseVariables = HashMap<String, DatabaseVariable>;
6+
7+
#[derive(Debug, Clone)]
8+
pub struct DatabaseVariable {
9+
pub name: String,
10+
pub value: ScalarValue,
11+
pub var_type: VarType,
12+
pub readonly: bool,
13+
// Postgres schema includes a range of additional parameters
14+
pub additional_params: Option<HashMap<String, ScalarValue>>,
15+
}
16+
17+
impl DatabaseVariable {
18+
pub fn system(
19+
name: String,
20+
value: ScalarValue,
21+
additional_params: Option<HashMap<String, ScalarValue>>,
22+
) -> Self {
23+
Self {
24+
name: name,
25+
value: value,
26+
var_type: VarType::System,
27+
readonly: false,
28+
additional_params,
29+
}
30+
}
31+
32+
pub fn user_defined(
33+
name: String,
34+
value: ScalarValue,
35+
additional_params: Option<HashMap<String, ScalarValue>>,
36+
) -> Self {
37+
Self {
38+
name: name,
39+
value: value,
40+
var_type: VarType::UserDefined,
41+
readonly: false,
42+
additional_params,
43+
}
44+
}
45+
}

rust/cubesql/cubesql/src/sql/database_variables/mod.rs

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,8 @@
1-
use std::collections::HashMap;
2-
3-
use datafusion::{scalar::ScalarValue, variable::VarType};
1+
use crate::compile::DatabaseVariables;
42

53
pub mod mysql;
64
pub mod postgres;
75

8-
pub type DatabaseVariablesToUpdate = Vec<DatabaseVariable>;
9-
pub type DatabaseVariables = HashMap<String, DatabaseVariable>;
10-
11-
#[derive(Debug, Clone)]
12-
pub struct DatabaseVariable {
13-
pub name: String,
14-
pub value: ScalarValue,
15-
pub var_type: VarType,
16-
pub readonly: bool,
17-
// Postgres schema includes a range of additional parameters
18-
pub additional_params: Option<HashMap<String, ScalarValue>>,
19-
}
20-
21-
impl DatabaseVariable {
22-
pub fn system(
23-
name: String,
24-
value: ScalarValue,
25-
additional_params: Option<HashMap<String, ScalarValue>>,
26-
) -> Self {
27-
Self {
28-
name: name,
29-
value: value,
30-
var_type: VarType::System,
31-
readonly: false,
32-
additional_params,
33-
}
34-
}
35-
36-
pub fn user_defined(
37-
name: String,
38-
value: ScalarValue,
39-
additional_params: Option<HashMap<String, ScalarValue>>,
40-
) -> Self {
41-
Self {
42-
name: name,
43-
value: value,
44-
var_type: VarType::UserDefined,
45-
readonly: false,
46-
additional_params,
47-
}
48-
}
49-
}
50-
516
pub fn mysql_default_session_variables() -> DatabaseVariables {
527
mysql::session_vars::defaults()
538
}

rust/cubesql/cubesql/src/sql/database_variables/mysql/global_vars.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::collections::HashMap;
22

3+
use crate::compile::{DatabaseVariable, DatabaseVariables};
34
use datafusion::scalar::ScalarValue;
45

5-
use crate::sql::database_variables::{DatabaseVariable, DatabaseVariables};
6-
76
pub fn defaults() -> DatabaseVariables {
87
let mut variables: DatabaseVariables = HashMap::new();
98

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::collections::HashMap;
22

3+
use crate::compile::{DatabaseVariable, DatabaseVariables};
34
use datafusion::scalar::ScalarValue;
45

5-
use crate::sql::database_variables::{DatabaseVariable, DatabaseVariables};
6-
76
pub fn defaults() -> DatabaseVariables {
87
let mut variables: DatabaseVariables = HashMap::new();
98

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashMap;
22

33
use datafusion::scalar::ScalarValue;
44

5-
use crate::sql::database_variables::{DatabaseVariable, DatabaseVariables};
5+
use crate::compile::{DatabaseVariable, DatabaseVariables};
66

77
pub fn defaults() -> DatabaseVariables {
88
let mut variables: DatabaseVariables = HashMap::new();

0 commit comments

Comments
 (0)