Skip to content

Commit b33a5f8

Browse files
authored
chore(tesseract): Native utils functions and errors handling (#9981)
1 parent 9243f4c commit b33a5f8

File tree

29 files changed

+722
-5511
lines changed

29 files changed

+722
-5511
lines changed

packages/cubejs-backend-native/Cargo.lock

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

packages/cubejs-backend-native/js/index.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -448,20 +448,9 @@ export const sql4sql = async (instance: SqlInterfaceInstance, sqlQuery: string,
448448
return native.sql4sql(instance, sqlQuery, disablePostProcessing, securityContext ? JSON.stringify(securityContext) : null);
449449
};
450450

451-
export const buildSqlAndParams = (cubeEvaluator: any): String => {
451+
export const buildSqlAndParams = (cubeEvaluator: any): any[] => {
452452
const native = loadNative();
453-
const safeCallFn = (fn: Function, thisArg: any, ...args: any[]) => {
454-
try {
455-
return {
456-
result: fn.apply(thisArg, args),
457-
};
458-
} catch (e: any) {
459-
return {
460-
error: e.toString(),
461-
};
462-
}
463-
};
464-
return native.buildSqlAndParams(cubeEvaluator, safeCallFn);
453+
return native.buildSqlAndParams(cubeEvaluator);
465454
};
466455

467456
export type ResultRow = Record<string, string>;

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

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ use crate::stream::OnDrainHandler;
2121
use crate::tokio_runtime_node;
2222
use crate::transport::NodeBridgeTransport;
2323
use crate::utils::{batch_to_rows, NonDebugInRelease};
24-
use cubenativeutils::wrappers::neon::context::neon_run_with_guarded_lifetime;
24+
use cubenativeutils::wrappers::neon::context::neon_guarded_funcion_call;
2525
use cubenativeutils::wrappers::neon::inner_types::NeonInnerTypes;
26-
use cubenativeutils::wrappers::neon::object::NeonObject;
27-
use cubenativeutils::wrappers::object_handle::NativeObjectHandle;
28-
use cubenativeutils::wrappers::serializer::NativeDeserialize;
2926
use cubenativeutils::wrappers::NativeContextHolder;
3027
use cubesqlplanner::cube_bridge::base_query_options::NativeBaseQueryOptions;
3128
use cubesqlplanner::planner::base_query::BaseQuery;
@@ -603,43 +600,15 @@ pub fn reset_logger(mut cx: FunctionContext) -> JsResult<JsUndefined> {
603600
//============ sql planner ===================
604601

605602
fn build_sql_and_params(cx: FunctionContext) -> JsResult<JsValue> {
606-
neon_run_with_guarded_lifetime(cx, |neon_context_holder| {
607-
let options = NativeObjectHandle::<NeonInnerTypes<FunctionContext<'static>>>::new(
608-
NeonObject::new(
609-
neon_context_holder.clone(),
610-
neon_context_holder
611-
.with_context(|cx| cx.argument::<JsValue>(0))
612-
.unwrap()?,
613-
)
614-
.unwrap(),
615-
);
616-
617-
let safe_call_fn = neon_context_holder
618-
.with_context(|cx| {
619-
if let Ok(func) = cx.argument::<JsFunction>(1) {
620-
Some(func)
621-
} else {
622-
None
623-
}
624-
})
625-
.unwrap();
626-
627-
neon_context_holder.set_safe_call_fn(safe_call_fn).unwrap();
628-
629-
let context_holder = NativeContextHolder::<NeonInnerTypes<FunctionContext<'static>>>::new(
630-
neon_context_holder,
631-
);
632-
633-
let base_query_options = Rc::new(NativeBaseQueryOptions::from_native(options).unwrap());
603+
neon_guarded_funcion_call(
604+
cx,
605+
|context_holder: NativeContextHolder<_>,
606+
options: NativeBaseQueryOptions<NeonInnerTypes<FunctionContext<'static>>>| {
607+
let base_query = BaseQuery::try_new(context_holder.clone(), Rc::new(options))?;
634608

635-
let base_query = BaseQuery::try_new(context_holder.clone(), base_query_options).unwrap();
636-
637-
let res = base_query.build_sql_and_params();
638-
639-
let result: NeonObject<FunctionContext<'static>> = res.into_object();
640-
let result = result.get_object().unwrap();
641-
Ok(result)
642-
})
609+
base_query.build_sql_and_params()
610+
},
611+
)
643612
}
644613

645614
fn debug_js_to_clrepr_to_js(mut cx: FunctionContext) -> JsResult<JsValue> {

packages/cubejs-schema-compiler/src/adapter/BaseQuery.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -900,24 +900,21 @@ export class BaseQuery {
900900
cubestoreSupportMultistage: this.options.cubestoreSupportMultistage ?? getEnv('cubeStoreRollingWindowJoin')
901901
};
902902

903-
const buildResult = nativeBuildSqlAndParams(queryParams);
903+
try {
904+
const buildResult = nativeBuildSqlAndParams(queryParams);
904905

905-
if (buildResult.error) {
906-
if (buildResult.error.cause && buildResult.error.cause === 'User') {
907-
throw new UserError(buildResult.error.message);
908-
} else {
909-
throw new Error(buildResult.error.message);
906+
const [query, params, preAggregation] = buildResult;
907+
const paramsArray = [...params];
908+
if (preAggregation) {
909+
this.preAggregations.preAggregationForQuery = preAggregation;
910910
}
911+
return [query, paramsArray];
912+
} catch (e) {
913+
if (e.name === 'TesseractUserError') {
914+
throw new UserError(e.message);
915+
}
916+
throw e;
911917
}
912-
913-
const res = buildResult.result;
914-
const [query, params, preAggregation] = res;
915-
// FIXME
916-
const paramsArray = [...params];
917-
if (preAggregation) {
918-
this.preAggregations.preAggregationForQuery = preAggregation;
919-
}
920-
return [query, paramsArray];
921918
}
922919

923920
// FIXME Temporary solution
@@ -953,15 +950,7 @@ export class BaseQuery {
953950

954951
const buildResult = nativeBuildSqlAndParams(queryParams);
955952

956-
if (buildResult.error) {
957-
if (buildResult.error.cause === 'User') {
958-
throw new UserError(buildResult.error.message);
959-
} else {
960-
throw new Error(buildResult.error.message);
961-
}
962-
}
963-
964-
const [, , preAggregation] = buildResult.result;
953+
const [, , preAggregation] = buildResult;
965954
return preAggregation;
966955
}
967956

rust/cubenativeutils/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ node_modules
1111
/cubesql/egraph-debug-intermediate
1212
egraph-debug
1313
/cubesql/debug-qtrace
14+
CLAUDE.md

0 commit comments

Comments
 (0)