Skip to content

Commit ff93bfe

Browse files
committed
switch to use DBResponsePrimitive instead of just Strings
1 parent 404c3fb commit ff93bfe

File tree

4 files changed

+65
-24
lines changed

4 files changed

+65
-24
lines changed

packages/cubejs-backend-native/src/orchestrator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn get_cubestore_result(mut cx: FunctionContext) -> JsResult<JsValue> {
7575
let js_row = JsObject::new(&mut cx);
7676
for (key, value) in result.columns.iter().zip(row.iter()) {
7777
let js_key = cx.string(key);
78-
let js_value = cx.string(value);
78+
let js_value = cx.string(value.to_string());
7979
js_row.set(&mut cx, js_key, js_value)?;
8080
}
8181
Ok(js_row)

rust/cubeorchestrator/src/cubestore_message_parser.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use crate::transport::{DBResponsePrimitive, DBResponseValue};
12
use cubeshared::codegen::{root_as_http_message, HttpCommand};
23
use neon::prelude::Finalize;
4+
use serde::Deserialize;
35
use std::collections::HashMap;
46

57
#[derive(Debug)]
@@ -27,9 +29,10 @@ impl std::fmt::Display for ParseError {
2729

2830
impl std::error::Error for ParseError {}
2931

32+
#[derive(Debug, Clone, Deserialize)]
3033
pub struct CubeStoreResult {
3134
pub columns: Vec<String>,
32-
pub rows: Vec<Vec<String>>,
35+
pub rows: Vec<Vec<DBResponseValue>>,
3336
pub columns_pos: HashMap<String, usize>,
3437
}
3538

@@ -83,7 +86,11 @@ impl CubeStoreResult {
8386
let values = row.values().ok_or(ParseError::NullRow)?;
8487
let row_obj: Vec<_> = values
8588
.iter()
86-
.map(|val| val.string_value().unwrap_or("").to_owned())
89+
.map(|val| {
90+
DBResponseValue::Primitive(DBResponsePrimitive::String(
91+
val.string_value().unwrap_or("").to_owned(),
92+
))
93+
})
8794
.collect();
8895

8996
result.rows.push(row_obj);

rust/cubeorchestrator/src/cubestore_result_transform.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::{
22
cubestore_message_parser::CubeStoreResult,
33
transport::{
4-
ConfigItem, MembersMap, NormalizedQuery, QueryTimeDimension, QueryType, ResultType,
5-
TransformDataRequest, BLENDING_QUERY_KEY_PREFIX, BLENDING_QUERY_RES_SEPARATOR,
6-
COMPARE_DATE_RANGE_FIELD, COMPARE_DATE_RANGE_SEPARATOR, MEMBER_SEPARATOR,
4+
ConfigItem, DBResponsePrimitive, DBResponseValue, MembersMap, NormalizedQuery,
5+
QueryTimeDimension, QueryType, ResultType, TransformDataRequest, BLENDING_QUERY_KEY_PREFIX,
6+
BLENDING_QUERY_RES_SEPARATOR, COMPARE_DATE_RANGE_FIELD, COMPARE_DATE_RANGE_SEPARATOR,
7+
MEMBER_SEPARATOR,
78
},
89
};
910
use anyhow::{bail, Context, Result};
@@ -17,18 +18,28 @@ use std::{
1718
};
1819

1920
/// Transform specified `value` with specified `type` to the network protocol type.
20-
pub fn transform_value(value: String, type_: &str) -> String {
21-
if type_ == "time" || type_.is_empty() {
22-
DateTime::parse_from_rfc3339(&value)
23-
.map(|dt| dt.to_rfc3339_opts(SecondsFormat::Millis, true))
24-
.unwrap_or_else(|_| value)
25-
} else {
26-
value
21+
pub fn transform_value(value: DBResponseValue, type_: &str) -> DBResponsePrimitive {
22+
match value {
23+
DBResponseValue::DateTime(dt) if type_ == "time" || type_.is_empty() => {
24+
let formatted = dt.to_rfc3339_opts(SecondsFormat::Millis, true);
25+
DBResponsePrimitive::String(formatted)
26+
}
27+
DBResponseValue::Primitive(DBResponsePrimitive::String(ref s)) if type_ == "time" => {
28+
let formatted = DateTime::parse_from_rfc3339(s)
29+
.map(|dt| dt.to_rfc3339_opts(SecondsFormat::Millis, true))
30+
.unwrap_or_else(|_| s.clone());
31+
DBResponsePrimitive::String(formatted)
32+
}
33+
DBResponseValue::Primitive(p) => p,
34+
DBResponseValue::Object { value } => value,
35+
_ => DBResponsePrimitive::Null,
2736
}
2837
}
2938

3039
/// Parse date range value from time dimension.
31-
pub fn get_date_range_value(time_dimensions: Option<&Vec<QueryTimeDimension>>) -> Result<String> {
40+
pub fn get_date_range_value(
41+
time_dimensions: Option<&Vec<QueryTimeDimension>>,
42+
) -> Result<DBResponsePrimitive> {
3243
let time_dimensions = match time_dimensions {
3344
Some(time_dimensions) => time_dimensions,
3445
None => bail!("QueryTimeDimension should be specified for the compare date range query."),
@@ -51,7 +62,9 @@ pub fn get_date_range_value(time_dimensions: Option<&Vec<QueryTimeDimension>>) -
5162
);
5263
}
5364

54-
Ok(date_range.join(COMPARE_DATE_RANGE_SEPARATOR))
65+
Ok(DBResponsePrimitive::String(
66+
date_range.join(COMPARE_DATE_RANGE_SEPARATOR),
67+
))
5568
}
5669

5770
/// Parse blending query key from time dimension for query.
@@ -169,10 +182,10 @@ pub fn get_compact_row(
169182
query_type: &QueryType,
170183
members: &[String],
171184
time_dimensions: Option<&Vec<QueryTimeDimension>>,
172-
db_row: &[String],
185+
db_row: &[DBResponseValue],
173186
columns_pos: &HashMap<String, usize>,
174-
) -> Result<Vec<String>> {
175-
let mut row: Vec<String> = Vec::with_capacity(members.len());
187+
) -> Result<Vec<DBResponsePrimitive>> {
188+
let mut row: Vec<DBResponsePrimitive> = Vec::with_capacity(members.len());
176189

177190
for m in members {
178191
if let Some(annotation_item) = annotation.get(m) {
@@ -217,9 +230,9 @@ pub fn get_vanilla_row(
217230
annotation: &HashMap<String, ConfigItem>,
218231
query_type: &QueryType,
219232
query: &NormalizedQuery,
220-
db_row: &[String],
233+
db_row: &[DBResponseValue],
221234
columns_pos: &HashMap<String, usize>,
222-
) -> Result<HashMap<String, String>> {
235+
) -> Result<HashMap<String, DBResponsePrimitive>> {
223236
let mut row = HashMap::new();
224237

225238
for (alias, &index) in columns_pos {
@@ -379,9 +392,9 @@ pub fn get_final_cubestore_result_array(
379392
pub enum TransformedData {
380393
Compact {
381394
members: Vec<String>,
382-
dataset: Vec<Vec<String>>,
395+
dataset: Vec<Vec<DBResponsePrimitive>>,
383396
},
384-
Vanilla(Vec<HashMap<String, String>>),
397+
Vanilla(Vec<HashMap<String, DBResponsePrimitive>>),
385398
}
386399

387400
impl TransformedData {

rust/cubeorchestrator/src/transport.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub const BLENDING_QUERY_KEY_PREFIX: &str = "time.";
99
pub const BLENDING_QUERY_RES_SEPARATOR: &str = ".";
1010
pub const MEMBER_SEPARATOR: &str = ".";
1111

12-
// TODO: Seems to be unused
1312
#[derive(Debug, Clone, Serialize, Deserialize)]
1413
pub enum DBResponsePrimitive {
1514
Null,
@@ -18,7 +17,18 @@ pub enum DBResponsePrimitive {
1817
String(String),
1918
}
2019

21-
// TODO: Seems to be unused
20+
impl Display for DBResponsePrimitive {
21+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22+
let str = match self {
23+
DBResponsePrimitive::Null => "null".to_string(),
24+
DBResponsePrimitive::Boolean(b) => b.to_string(),
25+
DBResponsePrimitive::Number(n) => n.to_string(),
26+
DBResponsePrimitive::String(s) => s.clone(),
27+
};
28+
write!(f, "{}", str)
29+
}
30+
}
31+
2232
#[derive(Debug, Clone, Deserialize)]
2333
pub enum DBResponseValue {
2434
DateTime(DateTime<Utc>),
@@ -27,6 +37,17 @@ pub enum DBResponseValue {
2737
Object { value: DBResponsePrimitive },
2838
}
2939

40+
impl Display for DBResponseValue {
41+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42+
let str = match self {
43+
DBResponseValue::DateTime(dt) => dt.to_rfc3339(),
44+
DBResponseValue::Primitive(p) => p.to_string(),
45+
DBResponseValue::Object { value } => value.to_string(),
46+
};
47+
write!(f, "{}", str)
48+
}
49+
}
50+
3051
#[derive(Debug, Clone, Serialize, Deserialize)]
3152
pub enum ResultType {
3253
#[serde(rename = "default")]

0 commit comments

Comments
 (0)