Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
waralexrom committed Jan 17, 2025
1 parent 447e494 commit 4e8e77c
Show file tree
Hide file tree
Showing 63 changed files with 916 additions and 195 deletions.
2 changes: 2 additions & 0 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -3336,6 +3336,7 @@ export class BaseQuery {
sort: '{{ expr }} {% if asc %}ASC{% else %}DESC{% endif %} NULLS {% if nulls_first %}FIRST{% else %}LAST{% endif %}',
order_by: '{% if index %} {{ index }} {% else %} {{ expr }} {% endif %} {% if asc %}ASC{% else %}DESC{% endif %}{% if nulls_first %} NULLS FIRST{% endif %}',
cast: 'CAST({{ expr }} AS {{ data_type }})',
cast_to_string: 'CAST({{ expr }} AS TEXT)',
window_function: '{{ fun_call }} OVER ({% if partition_by_concat %}PARTITION BY {{ partition_by_concat }}{% if order_by_concat or window_frame %} {% endif %}{% endif %}{% if order_by_concat %}ORDER BY {{ order_by_concat }}{% if window_frame %} {% endif %}{% endif %}{% if window_frame %}{{ window_frame }}{% endif %})',
window_frame_bounds: '{{ frame_type }} BETWEEN {{ frame_start }} AND {{ frame_end }}',
in_list: '{{ expr }} {% if negated %}NOT {% endif %}IN ({{ in_exprs_concat }})',
Expand All @@ -3352,6 +3353,7 @@ export class BaseQuery {
like: '{{ expr }} {% if negated %}NOT {% endif %}LIKE {{ pattern }}',
ilike: '{{ expr }} {% if negated %}NOT {% endif %}ILIKE {{ pattern }}',
like_escape: '{{ like_expr }} ESCAPE {{ escape_char }}',
concat_strings: '{{ strings | join(\' || \' ) }}',
},
filters: {
equals: '{{ column }} = {{ value }}{{ is_null_check }}',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::filter_group::{FilterGroup, NativeFilterGroup};
use super::filter_params::{FilterParams, NativeFilterParams};
use super::memeber_sql::{MemberSql, NativeMemberSql};
use super::member_sql::{MemberSql, NativeMemberSql};
use super::security_context::{NativeSecurityContext, SecurityContext};
use super::sql_templates_render::{NativeSqlTemplatesRender, SqlTemplatesRender};
use cubenativeutils::wrappers::serializer::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::memeber_sql::{MemberSql, NativeMemberSql};
use super::member_sql::{MemberSql, NativeMemberSql};
use cubenativeutils::wrappers::serializer::{
NativeDeserialize, NativeDeserializer, NativeSerialize,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::memeber_sql::{MemberSql, NativeMemberSql};
use super::geo_item::{GeoItem, NativeGeoItem};
use super::member_sql::{MemberSql, NativeMemberSql};
use cubenativeutils::wrappers::serializer::{
NativeDeserialize, NativeDeserializer, NativeSerialize,
};
Expand All @@ -25,6 +26,15 @@ pub struct DimenstionDefinitionStatic {

#[nativebridge::native_bridge(DimenstionDefinitionStatic)]
pub trait DimensionDefinition {
#[optional]
#[field]
fn sql(&self) -> Result<Rc<dyn MemberSql>, CubeError>;
fn sql(&self) -> Result<Option<Rc<dyn MemberSql>>, CubeError>;

#[optional]
#[field]
fn latitude(&self) -> Result<Option<Rc<dyn GeoItem>>, CubeError>;

#[optional]
#[field]
fn longitude(&self) -> Result<Option<Rc<dyn GeoItem>>, CubeError>;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::cube_definition::{CubeDefinition, NativeCubeDefinition};
use super::dimension_definition::{DimensionDefinition, NativeDimensionDefinition};
use super::measure_definition::{MeasureDefinition, NativeMeasureDefinition};
use super::memeber_sql::{MemberSql, NativeMemberSql};
use super::member_sql::{MemberSql, NativeMemberSql};
use cubenativeutils::wrappers::serializer::{
NativeDeserialize, NativeDeserializer, NativeSerialize,
};
Expand Down
15 changes: 15 additions & 0 deletions rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/geo_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use super::member_sql::{MemberSql, NativeMemberSql};
use cubenativeutils::wrappers::serializer::{
NativeDeserialize, NativeDeserializer, NativeSerialize,
};
use cubenativeutils::wrappers::NativeContextHolder;
use cubenativeutils::wrappers::NativeObjectHandle;
use cubenativeutils::CubeError;
use std::any::Any;
use std::rc::Rc;

#[nativebridge::native_bridge]
pub trait GeoItem {
#[field]
fn sql(&self) -> Result<Rc<dyn MemberSql>, CubeError>;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::memeber_sql::{MemberSql, NativeMemberSql};
use super::member_sql::{MemberSql, NativeMemberSql};
use cubenativeutils::wrappers::serializer::{
NativeDeserialize, NativeDeserializer, NativeSerialize,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::cube_definition::{CubeDefinition, NativeCubeDefinition};
use super::measure_filter::{MeasureFiltersVec, NativeMeasureFiltersVec};
use super::member_order_by::{MemberOrderByVec, NativeMemberOrderByVec};
use super::memeber_sql::{MemberSql, NativeMemberSql};
use super::member_sql::{MemberSql, NativeMemberSql};
use cubenativeutils::wrappers::serializer::{
NativeDeserialize, NativeDeserializer, NativeSerialize,
};
Expand Down Expand Up @@ -60,6 +60,10 @@ pub trait MeasureDefinition {
#[field]
fn filters(&self) -> Result<Option<Rc<dyn MeasureFiltersVec>>, CubeError>;

#[optional]
#[field]
fn drill_filters(&self) -> Result<Option<Rc<dyn MeasureFiltersVec>>, CubeError>;

#[optional]
#[field]
fn order_by(&self) -> Result<Option<Rc<dyn MemberOrderByVec>>, CubeError>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::memeber_sql::{MemberSql, NativeMemberSql};
use super::member_sql::{MemberSql, NativeMemberSql};
use cubenativeutils::wrappers::object::NativeArray;
use cubenativeutils::wrappers::serializer::{
NativeDeserialize, NativeDeserializer, NativeSerialize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::memeber_sql::{MemberSql, NativeMemberSql};
use super::member_sql::{MemberSql, NativeMemberSql};
use cubenativeutils::wrappers::serializer::{
NativeDeserialize, NativeDeserializer, NativeSerialize,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::memeber_sql::{MemberSql, NativeMemberSql};
use super::member_sql::{MemberSql, NativeMemberSql};
use cubenativeutils::wrappers::object::NativeArray;
use cubenativeutils::wrappers::serializer::{
NativeDeserialize, NativeDeserializer, NativeSerialize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,39 @@ pub enum MemberSqlArg {
pub trait MemberSql {
fn call(&self, args: Vec<MemberSqlArg>) -> Result<String, CubeError>;
fn args_names(&self) -> &Vec<String>;
fn need_deps_resolve(&self) -> bool;
fn as_any(self: Rc<Self>) -> Rc<dyn Any>;
}

pub struct CustomMemberSql {
sql: String,
args_names: Vec<String>,
}

impl CustomMemberSql {
pub fn new(sql: String) -> Rc<Self> {
Rc::new(Self {
sql,
args_names: vec![],
})
}
}

impl MemberSql for CustomMemberSql {
fn call(&self, _args: Vec<MemberSqlArg>) -> Result<String, CubeError> {
Ok(self.sql.clone())
}
fn args_names(&self) -> &Vec<String> {
&self.args_names
}
fn need_deps_resolve(&self) -> bool {
false
}
fn as_any(self: Rc<Self>) -> Rc<dyn Any> {
self.clone()
}
}

pub struct NativeMemberSql<IT: InnerTypes> {
native_object: NativeObjectHandle<IT>,
args_names: Vec<String>,
Expand All @@ -60,6 +90,12 @@ impl<IT: InnerTypes> NativeSerialize<IT> for MemberSqlStruct {
NativeObjectHandle::new(context.to_string_fn(to_string_fn.clone()).into_object()),
)?;
}
if let Some(sql_fn) = &self.sql_fn {
res.set_field(
"sql",
NativeObjectHandle::new(context.to_string_fn(sql_fn.clone()).into_object()),
)?;
}
Ok(NativeObjectHandle::new(res.into_object()))
}
}
Expand Down Expand Up @@ -131,6 +167,9 @@ impl<IT: InnerTypes> MemberSql for NativeMemberSql<IT> {
fn args_names(&self) -> &Vec<String> {
&self.args_names
}
fn need_deps_resolve(&self) -> bool {
!self.args_names.is_empty()
}
}

impl<IT: InnerTypes> NativeSerialize<IT> for NativeMemberSql<IT> {
Expand Down
3 changes: 2 additions & 1 deletion rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod dimension_definition;
pub mod evaluator;
pub mod filter_group;
pub mod filter_params;
pub mod geo_item;
pub mod join_definition;
pub mod join_graph;
pub mod join_item;
Expand All @@ -13,6 +14,6 @@ pub mod measure_definition;
pub mod measure_filter;
pub mod member_definition;
pub mod member_order_by;
pub mod memeber_sql;
pub mod member_sql;
pub mod security_context;
pub mod sql_templates_render;
4 changes: 2 additions & 2 deletions rust/cubesqlplanner/cubesqlplanner/src/plan/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ impl MemberExpression {

pub fn to_sql(
&self,
_templates: &PlanSqlTemplates,
templates: &PlanSqlTemplates,
context: Rc<VisitorContext>,
) -> Result<String, CubeError> {
self.member.to_sql(context)
self.member.to_sql(context, templates)
}
}

Expand Down
2 changes: 1 addition & 1 deletion rust/cubesqlplanner/cubesqlplanner/src/plan/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl FilterItem {
}
}
FilterItem::Item(item) => {
let sql = item.to_sql(context.clone())?;
let sql = item.to_sql(context.clone(), templates)?;
format!("({})", sql)
}
};
Expand Down
2 changes: 1 addition & 1 deletion rust/cubesqlplanner/cubesqlplanner/src/plan/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl SingleSource {
) -> Result<String, CubeError> {
let sql = match &self {
SingleSource::Cube(cube) => {
let cubesql = cube.to_sql(context.clone())?;
let cubesql = cube.to_sql(context.clone(), templates)?;
format!(" {} ", cubesql)
}
SingleSource::Subquery(s) => format!("({})", s.to_sql(templates)?),
Expand Down
2 changes: 1 addition & 1 deletion rust/cubesqlplanner/cubesqlplanner/src/plan/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl JoinCondition {
) -> Result<String, CubeError> {
match &self {
JoinCondition::DimensionJoinCondition(cond) => cond.to_sql(templates, context),
JoinCondition::BaseJoinCondition(cond) => cond.to_sql(context),
JoinCondition::BaseJoinCondition(cond) => cond.to_sql(context, templates),
JoinCondition::RollingWindowJoinCondition(cond) => cond.to_sql(templates, context),
}
}
Expand Down
20 changes: 15 additions & 5 deletions rust/cubesqlplanner/cubesqlplanner/src/planner/base_cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::query_tools::QueryTools;
use super::sql_evaluator::MemberSymbol;
use super::{evaluate_with_context, VisitorContext};
use crate::cube_bridge::cube_definition::CubeDefinition;
use crate::planner::sql_templates::PlanSqlTemplates;
use cubenativeutils::CubeError;
use std::collections::HashSet;
use std::rc::Rc;
Expand Down Expand Up @@ -37,9 +38,17 @@ impl BaseCube {
}))
}

pub fn to_sql(&self, context: Rc<VisitorContext>) -> Result<String, CubeError> {
let cube_sql =
evaluate_with_context(&self.member_evaluator, self.query_tools.clone(), context)?;
pub fn to_sql(
&self,
context: Rc<VisitorContext>,
templates: &PlanSqlTemplates,
) -> Result<String, CubeError> {
let cube_sql = evaluate_with_context(
&self.member_evaluator,
self.query_tools.clone(),
context,
templates,
)?;
Ok(cube_sql)
}

Expand Down Expand Up @@ -69,10 +78,11 @@ impl BaseCube {

pub fn default_alias_with_prefix(&self, prefix: &Option<String>) -> String {
let alias = self.default_alias();
if let Some(prefix) = prefix {
let res = if let Some(prefix) = prefix {
format!("{prefix}_{alias}")
} else {
alias
}
};
self.query_tools.alias_name(&res)
}
}
25 changes: 22 additions & 3 deletions rust/cubesqlplanner/cubesqlplanner/src/planner/base_dimension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::query_tools::QueryTools;
use super::sql_evaluator::{MemberSymbol, SqlCall};
use super::{evaluate_with_context, BaseMember, BaseMemberHelper, VisitorContext};
use crate::cube_bridge::dimension_definition::DimensionDefinition;
use crate::planner::sql_templates::PlanSqlTemplates;
use cubenativeutils::CubeError;
use std::rc::Rc;

Expand All @@ -16,8 +17,17 @@ pub struct BaseDimension {
}

impl BaseMember for BaseDimension {
fn to_sql(&self, context: Rc<VisitorContext>) -> Result<String, CubeError> {
evaluate_with_context(&self.member_evaluator, self.query_tools.clone(), context)
fn to_sql(
&self,
context: Rc<VisitorContext>,
templates: &PlanSqlTemplates,
) -> Result<String, CubeError> {
evaluate_with_context(
&self.member_evaluator,
self.query_tools.clone(),
context,
templates,
)
}

fn alias_name(&self) -> String {
Expand Down Expand Up @@ -88,7 +98,16 @@ impl BaseDimension {

pub fn sql_call(&self) -> Result<Rc<SqlCall>, CubeError> {
match self.member_evaluator.as_ref() {
MemberSymbol::Dimension(d) => Ok(d.member_sql().clone()),
MemberSymbol::Dimension(d) => {
if let Some(sql) = d.member_sql() {
Ok(sql.clone())
} else {
Err(CubeError::user(format!(
"Dimension {} hasn't sql evaluator",
self.full_name()
)))
}
}
_ => Err(CubeError::internal(format!(
"MemberSymbol::Dimension expected"
))),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use super::query_tools::QueryTools;
use super::sql_evaluator::SqlCall;
use super::{evaluate_sql_call_with_context, VisitorContext};
use crate::planner::sql_templates::PlanSqlTemplates;
use cubenativeutils::CubeError;
use std::rc::Rc;
pub trait BaseJoinCondition {
fn to_sql(&self, context: Rc<VisitorContext>) -> Result<String, CubeError>;
fn to_sql(
&self,
context: Rc<VisitorContext>,
templates: &PlanSqlTemplates,
) -> Result<String, CubeError>;
}
pub struct SqlJoinCondition {
sql_call: Rc<SqlCall>,
Expand All @@ -23,7 +28,11 @@ impl SqlJoinCondition {
}

impl BaseJoinCondition for SqlJoinCondition {
fn to_sql(&self, context: Rc<VisitorContext>) -> Result<String, CubeError> {
evaluate_sql_call_with_context(&self.sql_call, self.query_tools.clone(), context)
fn to_sql(
&self,
context: Rc<VisitorContext>,
templates: &PlanSqlTemplates,
) -> Result<String, CubeError> {
evaluate_sql_call_with_context(&self.sql_call, self.query_tools.clone(), context, templates)
}
}
Loading

0 comments on commit 4e8e77c

Please sign in to comment.