Skip to content
Merged
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
25 changes: 23 additions & 2 deletions packages/cubejs-backend-native/src/python/cross.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use neon::prelude::*;
use neon::result::Throw;
use neon::types::JsDate;
use pyo3::exceptions::{PyNotImplementedError, PyTypeError};
use pyo3::types::{PyBool, PyDict, PyFloat, PyFunction, PyInt, PyList, PySet, PyString};
use pyo3::types::{PyBool, PyDict, PyFloat, PyFunction, PyInt, PyList, PySet, PyString, PyTuple};
use pyo3::{Py, PyAny, PyErr, PyObject, Python, ToPyObject};
use std::cell::RefCell;
use std::collections::hash_map::{IntoIter, Iter, Keys};
Expand Down Expand Up @@ -59,6 +59,7 @@ pub enum CLReprKind {
Bool,
Float,
Int,
Tuple,
Array,
Object,
JsFunction,
Expand All @@ -77,6 +78,7 @@ pub enum CLRepr {
Bool(bool),
Float(f64),
Int(i64),
Tuple(Vec<CLRepr>),
Array(Vec<CLRepr>),
Object(CLReprObject),
JsFunction(Arc<Root<JsFunction>>),
Expand Down Expand Up @@ -157,6 +159,7 @@ impl CLRepr {
CLRepr::Bool(_) => CLReprKind::Bool,
CLRepr::Float(_) => CLReprKind::Float,
CLRepr::Int(_) => CLReprKind::Int,
CLRepr::Tuple(_) => CLReprKind::Tuple,
CLRepr::Array(_) => CLReprKind::Array,
CLRepr::Object(_) => CLReprKind::Object,
CLRepr::JsFunction(_) => CLReprKind::JsFunction,
Expand Down Expand Up @@ -274,6 +277,15 @@ impl CLRepr {
}

Self::Array(r)
} else if v.get_type().is_subclass_of::<PyTuple>()? {
let l = v.downcast::<PyTuple>()?;
let mut r = Vec::with_capacity(l.len());

for v in l.iter() {
r.push(Self::from_python_ref(v)?);
}

Self::Tuple(r)
} else {
return Err(PyErr::new::<PyTypeError, _>(format!(
"Unable to represent {} type as CLR from Python",
Expand All @@ -295,7 +307,7 @@ impl CLRepr {
CLRepr::Bool(v) => cx.boolean(v).upcast(),
CLRepr::Float(v) => cx.number(v).upcast(),
CLRepr::Int(v) => cx.number(v as f64).upcast(),
CLRepr::Array(arr) => {
CLRepr::Tuple(arr) | CLRepr::Array(arr) => {
let r = cx.empty_array();

for (k, v) in arr.into_iter().enumerate() {
Expand Down Expand Up @@ -389,6 +401,15 @@ impl CLRepr {

PyList::new(py, elements).to_object(py)
}
CLRepr::Tuple(arr) => {
let mut elements = Vec::with_capacity(arr.len());

for el in arr.into_iter() {
elements.push(Self::into_py_impl(el, py)?);
}

PyTuple::new(py, elements).to_object(py)
}
CLRepr::Object(obj) => {
let r = PyDict::new(py);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ pub fn from_minijinja_value(from: &mj::value::Value) -> Result<CLRepr, mj::Error

pub fn to_minijinja_value(from: CLRepr) -> Value {
match from {
CLRepr::Array(inner) => Value::from_seq_object(JinjaSequenceObject { inner }),
CLRepr::Tuple(inner) | CLRepr::Array(inner) => {
Value::from_seq_object(JinjaSequenceObject { inner })
}
CLRepr::Object(inner) => Value::from_object(JinjaDynamicObject { inner }),
CLRepr::String(v) => Value::from(v),
CLRepr::Float(v) => Value::from(v),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ exports[`Jinja render arguments-test.yml.jinja: arguments-test.yml.jinja 1`] = `
arg_null: none
arg_seq_1: [1, 2, 3, 4, 5]
arg_seq_2: [5, 4, 3, 2, 1]
arg_sum_tuple: 3
arg_sum_map: 20"
`;

Expand Down
6 changes: 6 additions & 0 deletions packages/cubejs-backend-native/test/jinja.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ suite('Python model', () => {
arg_sum_integers: expect.any(Object),
arg_str: expect.any(Object),
arg_null: expect.any(Object),
arg_sum_tuple: expect.any(Object),
arg_sum_map: expect.any(Object),
arg_seq: expect.any(Object),
new_int_tuple: expect.any(Object),
new_str_tuple: expect.any(Object),
});
});
});
Expand All @@ -80,8 +83,11 @@ darwinSuite('Scope Python model', () => {
arg_sum_integers: expect.any(Object),
arg_str: expect.any(Object),
arg_null: expect.any(Object),
arg_sum_tuple: expect.any(Object),
arg_sum_map: expect.any(Object),
arg_seq: expect.any(Object),
new_int_tuple: expect.any(Object),
new_str_tuple: expect.any(Object),
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{%- set my_sequence_1 = [1,2,3,4,5] -%}
{%- set my_sequence_2 = [5,4,3,2,1] -%}
{%- set my_map = {'field_a' : 5, 'field_b' : 15} -%}
{%- set my_int_tuple = new_int_tuple() -%}
{%- set my_str_tuple = new_str_tuple() -%}

test:
arg_sum_integers_int_int: {{ arg_sum_integers(1, 1) }}
Expand All @@ -11,4 +13,5 @@ test:
arg_null: {{ arg_null(null) }}
arg_seq_1: {{ arg_seq(my_sequence_1) }}
arg_seq_2: {{ arg_seq(my_sequence_2) }}
arg_sum_tuple: {{ arg_sum_tuple(my_int_tuple) }}
arg_sum_map: {{ arg_sum_map(my_map) }}
12 changes: 12 additions & 0 deletions packages/cubejs-backend-native/test/templates/scoped-utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def arg_str(a):
def arg_null(a):
return a

@context_func
def arg_sum_tuple(tu):
return tu[0] + tu[1]

@context_func
def arg_sum_map(obj):
return obj['field_a'] + obj['field_b']
Expand All @@ -25,6 +29,14 @@ def arg_sum_map(obj):
def arg_seq(a):
return a

@context_func
def new_int_tuple():
return (1,2)

@context_func
def new_str_tuple():
return ("hello", "word")

@context_func
def load_data_sync():
client = MyApiClient("google.com")
Expand Down
12 changes: 12 additions & 0 deletions packages/cubejs-backend-native/test/templates/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def arg_str(a):
def arg_null(a):
return a

@context_func
def arg_sum_tuple(tu):
return tu[0] + tu[1]

@context_func
def arg_sum_map(obj):
return obj['field_a'] + obj['field_b']
Expand All @@ -24,6 +28,14 @@ def arg_sum_map(obj):
def arg_seq(a):
return a

@context_func
def new_int_tuple():
return (1,2)

@context_func
def new_str_tuple():
return ("1", "2")

@context_func
def load_data_sync():
client = MyApiClient("google.com")
Expand Down