Skip to content
Draft
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
19 changes: 18 additions & 1 deletion packages/cubejs-backend-native/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,19 @@ export const execSql = async (instance: SqlInterfaceInstance, sqlQuery: string,

export const buildSqlAndParams = (cubeEvaluator: any): String => {
const native = loadNative();
const safeCallFn = (fn: Function, thisArg: any, ...args: any[]) => {
try {
return {
result: fn.apply(thisArg, args),
};
} catch (e: any) {
return {
error: e.toString(),
};
}
};

return native.buildSqlAndParams(cubeEvaluator);
return native.buildSqlAndParams(cubeEvaluator, safeCallFn);
};

export type ResultRow = Record<string, string>;
Expand Down Expand Up @@ -405,6 +416,12 @@ export const getFinalQueryResultMulti = (transformDataArr: Object[], rows: any[]
return native.getFinalQueryResultMulti(transformDataArr, rows, responseData);
};

export const nativeProxy = (nativeResolver: any, resolveFunc: any) => new Proxy({}, {
get(_, prop) {
return resolveFunc(nativeResolver, prop);
}
});

export interface PyConfiguration {
repositoryFactory?: (ctx: unknown) => Promise<unknown>,
logger?: (msg: string, params: Record<string, any>) => void,
Expand Down
12 changes: 12 additions & 0 deletions packages/cubejs-backend-native/src/node_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,18 @@ fn build_sql_and_params(cx: FunctionContext) -> JsResult<JsValue> {
.unwrap()?,
));

let safe_call_fn = neon_context_holder
.with_context(|cx| {
if let Ok(func) = cx.argument::<JsFunction>(1) {
Some(func)
} else {
None
}
})
.unwrap();

neon_context_holder.set_safe_call_fn(safe_call_fn).unwrap();

let context_holder = NativeContextHolder::<NeonInnerTypes<FunctionContext<'static>>>::new(
neon_context_holder,
);
Expand Down
8 changes: 8 additions & 0 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,14 @@ export class BaseQuery {
return res;
}

nativeProxy(nativeResolver, resolveFunc) {
return new Proxy({}, {
get(_, prop) {
return resolveFunc(nativeResolver, prop.valueOf());
}
});
}

allCubeMembers(path) {
const fromPath = this.cubeEvaluator.cubeFromPath(path);

Expand Down
19 changes: 19 additions & 0 deletions packages/cubejs-schema-compiler/src/compiler/CubeSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,25 @@ export class CubeSymbols {
});
}

isNameOfCube(name) {
if (this.symbols[name]) {
return true;
} else {
return false;
}
}

isNameOfSymbolInCube(cubeName, name) {
const cube = this.symbols[cubeName];
if (!cube) {
return false;
}
if (cube[name]) {
return true;
}
return false;
}

filterGroupFunctionDep() {
return (...filterParamArgs) => '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ describe('SQL Generation', () => {
});
`);

it('simple join', async () => {
it('simple join 1', async () => {
await compiler.compile();

console.log(joinGraph.buildJoin(['visitor_checkins', 'visitors']));
Expand Down
2 changes: 1 addition & 1 deletion rust/cubenativeutils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ convert_case = "0.6.0"
[dependencies.neon]
version = "=1"
default-features = false
features = ["napi-1", "napi-4", "napi-6", "futures"]
features = ["napi-1", "napi-4", "napi-5", "napi-6", "futures"]
81 changes: 75 additions & 6 deletions rust/cubenativeutils/src/wrappers/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{inner_types::InnerTypes, object_handle::NativeObjectHandle};
use super::{inner_types::InnerTypes, object_handle::NativeObjectHandle, NativeBox};
use cubesql::CubeError;
use std::{any::Any, rc::Rc};

pub trait NativeContext<IT: InnerTypes>: Clone {
fn boolean(&self, v: bool) -> Result<IT::Boolean, CubeError>;
Expand All @@ -8,8 +9,30 @@ pub trait NativeContext<IT: InnerTypes>: Clone {
fn undefined(&self) -> Result<NativeObjectHandle<IT>, CubeError>;
fn empty_array(&self) -> Result<IT::Array, CubeError>;
fn empty_struct(&self) -> Result<IT::Struct, CubeError>;
//fn boxed<T: 'static>(&self, value: T) -> impl NativeBox<IT, T>;
fn to_string_fn(&self, result: String) -> Result<IT::Function, CubeError>;

//IMPORTANT NOTE: Using of any native args in callback (as well as any native objects created
//with NativeContextHolder passed to callback) outside of callback will cause error from
//runtime lifetime check
fn function<F>(&self, function: F) -> Result<IT::Function, CubeError>
where
F: Fn(
Rc<NativeContextHolder<IT::FunctionIT>>,
Vec<NativeObjectHandle<IT::FunctionIT>>,
) -> Result<NativeObjectHandle<IT::FunctionIT>, CubeError>
+ 'static;

fn boxed<T: NativeFinalize + 'static>(
&self,
object: T,
) -> Result<impl NativeBox<IT, T>, CubeError>;
fn global(&self, name: &str) -> Result<NativeObjectHandle<IT>, CubeError>;
}

//Top level reference to ContextHolder for using in top level interfaces. Should be downcaster to
//specific context for use
pub trait NativeContextHolderRef {
fn as_any(self: Rc<Self>) -> Rc<dyn Any>;
}

#[derive(Clone)]
Expand All @@ -18,10 +41,10 @@ pub struct NativeContextHolder<IT: InnerTypes> {
}

impl<IT: InnerTypes> NativeContextHolder<IT> {
pub fn new(context: IT::Context) -> Self {
Self { context }
pub fn new(context: IT::Context) -> Rc<Self> {
Rc::new(Self { context })
}
pub fn context(&self) -> &impl NativeContext<IT> {
pub fn context(&self) -> &IT::Context {
&self.context
}
pub fn boolean(&self, v: bool) -> Result<IT::Boolean, CubeError> {
Expand All @@ -42,8 +65,54 @@ impl<IT: InnerTypes> NativeContextHolder<IT> {
pub fn empty_struct(&self) -> Result<IT::Struct, CubeError> {
self.context.empty_struct()
}
#[allow(dead_code)]
pub fn to_string_fn(&self, result: String) -> Result<IT::Function, CubeError> {
self.context.to_string_fn(result)
}
pub fn function<F>(&self, function: F) -> Result<IT::Function, CubeError>
where
F: Fn(
Rc<NativeContextHolder<IT::FunctionIT>>,
Vec<NativeObjectHandle<IT::FunctionIT>>,
) -> Result<NativeObjectHandle<IT::FunctionIT>, CubeError>
+ 'static,
{
self.context.function(function)
}
pub fn boxed<T: NativeFinalize + 'static>(
&self,
object: T,
) -> Result<impl NativeBox<IT, T> + '_, CubeError> {
self.context.boxed(object)
}
pub fn global(&self, name: &str) -> Result<NativeObjectHandle<IT>, CubeError> {
self.context.global(name)
}
pub fn as_context_ref(self: &Rc<Self>) -> Rc<dyn NativeContextHolderRef> {
self.clone()
}
}

impl<IT: InnerTypes> NativeContextHolderRef for NativeContextHolder<IT> {
fn as_any(self: Rc<Self>) -> Rc<dyn Any> {
self.clone()
}
}

//FIXME For now we don't allow js calls on finalize, so it's only to clean rust resources
pub trait NativeFinalize: Sized {
fn finalize(self) {}
}

impl<T: NativeFinalize> NativeFinalize for std::rc::Rc<T> {
fn finalize(self) {
if let Ok(v) = std::rc::Rc::try_unwrap(self) {
v.finalize();
}
}
}

impl<T: NativeFinalize> NativeFinalize for std::cell::RefCell<T> {
fn finalize(self) {
self.into_inner().finalize();
}
}
6 changes: 4 additions & 2 deletions rust/cubenativeutils/src/wrappers/inner_types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{
context::NativeContext,
object::{
NativeArray, NativeBoolean, NativeFunction, NativeNumber, NativeObject, NativeString,
NativeStruct,
NativeArray, NativeBoolean, NativeFunction, NativeNumber, NativeObject, NativeRoot,
NativeString, NativeStruct,
},
};
pub trait InnerTypes: Clone + 'static {
Expand All @@ -14,4 +14,6 @@ pub trait InnerTypes: Clone + 'static {
type Function: NativeFunction<Self>;
type Number: NativeNumber<Self>;
type Context: NativeContext<Self>;
type FunctionIT: InnerTypes;
type Root: NativeRoot<Self>;
}
8 changes: 5 additions & 3 deletions rust/cubenativeutils/src/wrappers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ pub mod inner_types;
pub mod neon;
pub mod object;
pub mod object_handle;
pub mod root_holder;
pub mod serializer;

pub use context::NativeContextHolder;
pub use context::{NativeContextHolder, NativeContextHolderRef};
pub use object::{
NativeArray, NativeBoolean, NativeFunction, NativeNumber, NativeString, NativeStruct,
NativeType,
NativeArray, NativeBoolean, NativeBox, NativeFunction, NativeNumber, NativeRoot, NativeString,
NativeStruct, NativeType,
};
pub use object_handle::NativeObjectHandle;
pub use root_holder::{RootHolder, Rootable};
Loading
Loading