Skip to content

Commit 72f1969

Browse files
committed
prepare transformQueryData native wrapper
1 parent 3ffd32a commit 72f1969

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

packages/cubejs-backend-native/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ use cubesql::{telemetry::ReportingLogger, CubeError};
3838

3939
use neon::prelude::*;
4040
use neon::types::buffer::TypedArray;
41+
use serde::Deserialize;
42+
use cubeorchestrator::cubestore_result_transform::transform_data;
43+
use cubeorchestrator::types::{TransformDataRequest};
4144

4245
struct SQLInterface {
4346
services: Arc<NodeCubeServices>,
@@ -543,6 +546,56 @@ fn get_cubestore_result(mut cx: FunctionContext) -> JsResult<JsValue> {
543546
Ok(js_array.upcast())
544547
}
545548

549+
fn js_object_to_struct<T>(cx: &mut FunctionContext) -> Result<T, neon::result::Throw>
550+
where
551+
T: for<'de> Deserialize<'de>,
552+
{
553+
let json_str = cx
554+
.argument::<JsString>(0)?
555+
.value(cx);
556+
557+
let result: Result<T, serde_json::Error> = serde_json::from_str(&json_str);
558+
match result {
559+
Ok(value) => Ok(value),
560+
Err(err) => cx.throw_error(err.to_string()),
561+
}
562+
}
563+
564+
fn transform_query_data(mut cx: FunctionContext) -> JsResult<JsObject> {
565+
let request_data: TransformDataRequest = js_object_to_struct(&mut cx)?;
566+
567+
let alias_to_member_name_map = &request_data.alias_to_member_name_map;
568+
let annotation = &request_data.annotation;
569+
let data = &request_data.data;
570+
let query = &request_data.query;
571+
let query_type = &request_data.query_type;
572+
let res_type = &request_data.res_type;
573+
574+
let transformed = match transform_data(
575+
alias_to_member_name_map,
576+
annotation,
577+
data.clone(),
578+
query,
579+
query_type,
580+
res_type.clone(),
581+
) {
582+
Ok(data) => data,
583+
Err(err) => return cx.throw_error(err.to_string())
584+
};
585+
586+
let json_data = match serde_json::to_string(&transformed) {
587+
Ok(data) => data,
588+
Err(e) => return cx.throw_error(format!("Serialization error: {}", e)),
589+
};
590+
591+
let js_string = cx.string(json_data);
592+
593+
let js_result = cx.empty_object();
594+
js_result.set(&mut cx, "result", js_string)?;
595+
596+
Ok(js_result)
597+
}
598+
546599
pub fn register_module_exports<C: NodeConfiguration + 'static>(
547600
mut cx: ModuleContext,
548601
) -> NeonResult<()> {
@@ -562,6 +615,7 @@ pub fn register_module_exports<C: NodeConfiguration + 'static>(
562615
parse_cubestore_ws_result_message,
563616
)?;
564617
cx.export_function("getCubestoreResult", get_cubestore_result)?;
618+
cx.export_function("transformQueryData", transform_query_data)?;
565619

566620
crate::template::template_register_module(&mut cx)?;
567621

rust/cubeorchestrator/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubeorchestrator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
chrono = "0.4.31"
7+
chrono = { version = "0.4.31", features = ["serde"] }
88
cubeshared = { path = "../cubeshared" }
99
serde = { version = "1.0.215", features = ["derive"] }
1010
serde_json = "1.0.133"

rust/cubeorchestrator/src/types.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum DBResponsePrimitive {
1717
String(String),
1818
}
1919

20-
#[derive(Debug, Clone)]
20+
#[derive(Debug, Clone, Deserialize)]
2121
pub enum DBResponseValue {
2222
DateTime(DateTime<Utc>),
2323
Primitive(DBResponsePrimitive),
@@ -148,7 +148,7 @@ pub type AliasToMemberMap = HashMap<String, String>;
148148

149149
pub type MembersMap = HashMap<String, String>;
150150

151-
#[derive(Debug, Serialize, Deserialize)]
151+
#[derive(Debug, Clone, Serialize, Deserialize)]
152152
pub struct GranularityMeta {
153153
pub name: String,
154154
pub title: String,
@@ -159,7 +159,7 @@ pub struct GranularityMeta {
159159
pub origin: Option<String>,
160160
}
161161

162-
#[derive(Debug, Serialize, Deserialize)]
162+
#[derive(Debug, Clone, Serialize, Deserialize)]
163163
pub struct ConfigItem {
164164
pub title: String,
165165
pub short_title: String,
@@ -278,3 +278,16 @@ pub enum TransformedData {
278278
},
279279
Vanilla(Vec<HashMap<String, DBResponsePrimitive>>),
280280
}
281+
282+
#[derive(Debug, Clone, Deserialize)]
283+
pub struct TransformDataRequest {
284+
#[serde(rename = "aliasToMemberNameMap")]
285+
pub alias_to_member_name_map: HashMap<String, String>,
286+
pub annotation: HashMap<String, ConfigItem>,
287+
pub data: Vec<HashMap<String, DBResponseValue>>,
288+
pub query: NormalizedQuery,
289+
#[serde(rename = "queryType")]
290+
pub query_type: QueryType,
291+
#[serde(rename = "resType")]
292+
pub res_type: Option<ResultType>,
293+
}

0 commit comments

Comments
 (0)