-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add SQL queries support in /v1/sql endpoint (#9301)
* refactor(cubesql): Use &str instead of &String * refactor(backend-native): Extract create_session function * refactor(backend-native): Extract with_session function * refactor(cubesql): Extract QueryPlan::try_as_logical_plan * feat: Add SQL queries support in /v1/sql endpoint * Add docs * Remove mention of data_source and query_plan response fields from /v1/sql docs --------- Co-authored-by: Igor Lukanin <[email protected]>
- Loading branch information
1 parent
e19beb5
commit 7eba663
Showing
16 changed files
with
1,045 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::future::Future; | ||
use std::net::SocketAddr; | ||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
|
||
use cubesql::compile::DatabaseProtocol; | ||
use cubesql::config::ConfigObj; | ||
use cubesql::sql::{Session, SessionManager}; | ||
use cubesql::CubeError; | ||
|
||
use crate::auth::NativeAuthContext; | ||
use crate::config::NodeCubeServices; | ||
|
||
pub async fn create_session( | ||
services: &NodeCubeServices, | ||
native_auth_ctx: Arc<NativeAuthContext>, | ||
) -> Result<Arc<Session>, CubeError> { | ||
let config = services | ||
.injector() | ||
.get_service_typed::<dyn ConfigObj>() | ||
.await; | ||
|
||
let session_manager = services | ||
.injector() | ||
.get_service_typed::<SessionManager>() | ||
.await; | ||
|
||
let (host, port) = match SocketAddr::from_str( | ||
config | ||
.postgres_bind_address() | ||
.as_deref() | ||
.unwrap_or("127.0.0.1:15432"), | ||
) { | ||
Ok(addr) => (addr.ip().to_string(), addr.port()), | ||
Err(e) => { | ||
return Err(CubeError::internal(format!( | ||
"Failed to parse postgres_bind_address: {}", | ||
e | ||
))) | ||
} | ||
}; | ||
|
||
let session = session_manager | ||
.create_session(DatabaseProtocol::PostgreSQL, host, port, None) | ||
.await?; | ||
|
||
session | ||
.state | ||
.set_auth_context(Some(native_auth_ctx.clone())); | ||
|
||
Ok(session) | ||
} | ||
|
||
pub async fn with_session<T, F, Fut>( | ||
services: &NodeCubeServices, | ||
native_auth_ctx: Arc<NativeAuthContext>, | ||
f: F, | ||
) -> Result<T, CubeError> | ||
where | ||
F: FnOnce(Arc<Session>) -> Fut, | ||
Fut: Future<Output = Result<T, CubeError>>, | ||
{ | ||
let session_manager = services | ||
.injector() | ||
.get_service_typed::<SessionManager>() | ||
.await; | ||
let session = create_session(services, native_auth_ctx).await?; | ||
let connection_id = session.state.connection_id; | ||
|
||
// From now there's a session we should close before returning, as in `finally` | ||
let result = { f(session).await }; | ||
|
||
session_manager.drop_session(connection_id).await; | ||
|
||
result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.