Skip to content

Helper functions for query #1330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/handlers/http/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,51 @@ pub struct Query {
pub filter_tags: Option<Vec<String>>,
}

/// A function to execute the query and fetch QueryResponse
/// This won't look in the cache
/// TODO: Improve this function and make this a part of the query API
pub async fn get_records_and_fields(
query_request: &Query,
req: &HttpRequest,
) -> Result<(Option<Vec<RecordBatch>>, Option<Vec<String>>), QueryError> {
let session_state = QUERY_SESSION.state();

// get the logical plan and extract the table name
let raw_logical_plan = session_state
.create_logical_plan(&query_request.query)
.await?;

let time_range =
TimeRange::parse_human_time(&query_request.start_time, &query_request.end_time)?;
// create a visitor to extract the table name
let mut visitor = TableScanVisitor::default();
let _ = raw_logical_plan.visit(&mut visitor);

let tables = visitor.into_inner();
update_schema_when_distributed(&tables).await?;
let query: LogicalQuery = into_query(query_request, &session_state, time_range).await?;

let creds = extract_session_key_from_req(req)?;
let permissions = Users.get_permissions(&creds);

let table_name = query
.first_table_name()
.ok_or_else(|| QueryError::MalformedQuery("No table name found in query"))?;

user_auth_for_datasets(&permissions, &tables)?;

let (records, fields) = execute(query, &table_name, false).await?;

let records = match records {
Either::Left(vec_rb) => vec_rb,
Either::Right(_) => {
return Err(QueryError::CustomError("Reject streaming response".into()))
}
};

Ok((Some(records), Some(fields)))
}

pub async fn query(req: HttpRequest, query_request: Query) -> Result<HttpResponse, QueryError> {
let session_state = QUERY_SESSION.state();
let raw_logical_plan = match session_state
Expand Down
19 changes: 19 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,22 @@ pub fn user_auth_for_datasets(

Ok(())
}

/// A function to extract table names from a SQL string
pub async fn extract_tables(sql: &str) -> Option<Vec<String>> {
let session_state = QUERY_SESSION.state();

// get the logical plan and extract the table name
let raw_logical_plan = match session_state.create_logical_plan(sql).await {
Ok(plan) => plan,
Err(_) => return None,
};

// create a visitor to extract the table name
let mut visitor = TableScanVisitor::default();
let _ = raw_logical_plan.visit(&mut visitor);

let tables = visitor.into_inner();

Some(tables)
}
Loading