Skip to content

Commit

Permalink
modify QueryParamteterValue
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshidan committed Aug 29, 2024
1 parent e07d0b6 commit 1dd7289
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
109 changes: 109 additions & 0 deletions bigquery/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ impl ReadTableOption {
mod tests {
use bigdecimal::BigDecimal;
use serial_test::serial;
use std::collections::HashMap;
use std::ops::AddAssign;
use std::time::Duration;

Expand All @@ -555,6 +556,7 @@ mod tests {
use crate::http::job::query::QueryRequest;
use crate::http::table::{Table, TableReference};
use crate::http::tabledata::insert_all::{InsertAllRequest, Row};
use crate::http::types::{QueryParameter, QueryParameterStructType, QueryParameterType, QueryParameterValue};
use crate::query;
use crate::query::QueryOption;

Expand Down Expand Up @@ -918,6 +920,113 @@ mod tests {
assert_eq!(data.len(), SIZE);
}

#[derive(Debug, Clone)]
struct Val {
pub val1: String,
pub val2: String,
}

#[tokio::test]
#[serial]
async fn test_query_with_parameter() {
let array_val = [
Val {
val1: "val1-1".to_string(),
val2: "val1-2".to_string(),
},
Val {
val1: "val2-1".to_string(),
val2: "val2-2".to_string(),
},
];

let query_parameter = QueryParameter {
name: Some("p1".to_string()),
parameter_type: QueryParameterType {
parameter_type: "ARRAY".to_string(),
array_type: Some(Box::new(QueryParameterType {
parameter_type: "STRUCT".to_string(),
struct_types: Some(vec![
QueryParameterStructType {
name: Some("val1".to_string()),
field_type: QueryParameterType {
parameter_type: "STRING".to_string(),
..Default::default()
},
description: None,
},
QueryParameterStructType {
name: Some("val2".to_string()),
field_type: QueryParameterType {
parameter_type: "STRING".to_string(),
..Default::default()
},
description: None,
},
]),
array_type: None,
})),
struct_types: None,
},
parameter_value: QueryParameterValue {
array_values: Some(
array_val
.iter()
.map(|val| {
let mut param_map = HashMap::new();
param_map.insert(
"val1".to_string(),
QueryParameterValue {
value: Some(val.val1.clone()),
..Default::default()
},
);
param_map.insert(
"val2".to_string(),
QueryParameterValue {
value: Some(val.val2.clone()),
..Default::default()
},
);
QueryParameterValue {
struct_values: Some(param_map),
value: None,
array_values: None,
}
})
.collect(),
),
..Default::default()
},
};
let (client, project_id) = create_client().await;
let mut result = client
.query::<query::row::Row>(
&project_id,
QueryRequest {
query: "
WITH VAL AS (SELECT @p1 AS col1)
SELECT
ARRAY(SELECT val1 FROM UNNEST(col1)) AS val1,
ARRAY(SELECT val2 FROM UNNEST(col1)) AS val2
FROM VAL
"
.to_string(),
query_parameters: vec![query_parameter],
..QueryRequest::default()
},
)
.await
.unwrap();
let row = result.next().await.unwrap().unwrap();
let col = row.column::<Vec<String>>(0).unwrap();
assert_eq!(col[0], "val1-1".to_string());
assert_eq!(col[1], "val2-1".to_string());
let col = row.column::<Vec<String>>(1).unwrap();
assert_eq!(col[0], "val1-2".to_string());
assert_eq!(col[1], "val2-2".to_string());
}

fn assert_data(now: &OffsetDateTime, data: Vec<TestData>) {
for (i, d) in data.iter().enumerate() {
assert_eq!(&TestData::default(i, *now + Duration::from_secs(i as u64)), d);
Expand Down
4 changes: 3 additions & 1 deletion bigquery/src/http/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

#[derive(Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct StandardSqlDataType {
Expand Down Expand Up @@ -130,7 +132,7 @@ pub struct QueryParameterValue {
/// The struct field values.
/// An object containing a list of "key": value pairs.
/// Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }..
pub struct_values: Option<Box<QueryParameterValue>>,
pub struct_values: Option<HashMap<String, QueryParameterValue>>,
}

/// Currently supported connection properties:
Expand Down

0 comments on commit 1dd7289

Please sign in to comment.