Skip to content

Commit

Permalink
in work
Browse files Browse the repository at this point in the history
  • Loading branch information
waralexrom committed Dec 11, 2024
1 parent d52804a commit 88832ae
Show file tree
Hide file tree
Showing 24 changed files with 366 additions and 195 deletions.
11 changes: 8 additions & 3 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,8 @@ export class BaseQuery {

buildSqlAndParamsRust(exportAnnotatedSql) {


const order = this.options.order && R.pipe(
R.map((hash) => (!hash || !hash.id) ? null : hash),
R.map((hash) => (!hash || !hash.id) ? null : hash),
R.reject(R.isNil),
)(this.options.order);

Expand All @@ -660,8 +659,14 @@ export class BaseQuery {
return res;
}

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

return Object.keys(fromPath.measures).concat(Object.keys(fromPath.dimensions));
}

getAllocatedParams() {
return this.paramAllocator.getParams()
return this.paramAllocator.getParams();
}

// FIXME helper for native generator, maybe should be moved entire to rust
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class CubeSymbols {
},
set segments(v) {
// Dont allow to modify
}
},
}, cubeDefinition);

if (cubeDefinition.extends) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ describe('SQL Generation', () => {
}]
});

return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
return dbRunner.testQuery(query.buildSqlAndParamsTest()).then(res => {
expect(res).toEqual(
[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ pub trait BaseTools {
date_range: Vec<String>,
) -> Result<Vec<Vec<String>>, CubeError>;
fn get_allocated_params(&self) -> Result<Vec<String>, CubeError>;
fn all_cube_members(&self, path: String) -> Result<Vec<String>, CubeError>;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::dimension_definition::DimenstionDefinitionStatic;
use super::measure_definition::MeasureDefinitionStatic;
use super::memeber_sql::{MemberSql, NativeMemberSql};
use cubenativeutils::wrappers::serializer::{
NativeDeserialize, NativeDeserializer, NativeSerialize,
Expand All @@ -7,6 +9,7 @@ use cubenativeutils::wrappers::NativeObjectHandle;
use cubenativeutils::CubeError;
use serde::{Deserialize, Serialize};
use std::any::Any;
use std::collections::HashMap;
use std::rc::Rc;

#[derive(Serialize, Deserialize, Debug)]
Expand Down
72 changes: 67 additions & 5 deletions rust/cubesqlplanner/cubesqlplanner/src/plan/builder/select.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use crate::plan::{
AliasedExpr, Cte, Expr, Filter, From, MemberExpression, OrderBy, Schema, Select,
SingleAliasedSource, SingleSource,
};

use crate::planner::sql_evaluator::sql_nodes::SqlNodesFactory;
use crate::planner::sql_evaluator::symbols::MemberSymbol;
use crate::planner::{BaseMember, VisitorContext};
use std::collections::HashMap;
use std::rc::Rc;

pub struct SelectBuilder {
Expand All @@ -11,16 +16,17 @@ pub struct SelectBuilder {
group_by: Vec<Expr>,
having: Option<Filter>,
order_by: Vec<OrderBy>,
context: Rc<VisitorContext>,
nodes_factory: SqlNodesFactory,
ctes: Vec<Rc<Cte>>,
is_distinct: bool,
limit: Option<usize>,
offset: Option<usize>,
input_schema: Rc<Schema>,
result_schema: Schema,
}

impl SelectBuilder {
pub fn new(from: From, context: VisitorContext) -> Self {
pub fn new(from: From, nodes_factory: SqlNodesFactory) -> Self {
let input_schema = from.schema.clone();
Self {
projection_columns: vec![],
Expand All @@ -29,12 +35,13 @@ impl SelectBuilder {
group_by: vec![],
having: None,
order_by: vec![],
context: Rc::new(context),
nodes_factory,
ctes: vec![],
is_distinct: false,
limit: None,
offset: None,
input_schema,
result_schema: Schema::empty(),
}
}

Expand All @@ -54,6 +61,7 @@ impl SelectBuilder {
expr,
alias: alias.clone(),
};
self.resolve_render_reference_for_member(&member.member_evaluator());

self.projection_columns.push(aliased_expr);
}
Expand Down Expand Up @@ -85,19 +93,73 @@ impl SelectBuilder {
pub fn set_offset(&mut self, offset: Option<usize>) {
self.offset = offset;
}

pub fn set_ctes(&mut self, ctes: Vec<Rc<Cte>>) {
self.ctes = ctes;
}

pub fn build(self) -> Select {
fn resolve_render_reference_for_member(&mut self, member: &Rc<MemberSymbol>) {
let member_name = member.full_name();
if !self
.nodes_factory
.render_references()
.contains_key(&member_name)
{
if let Some(reference) = self
.input_schema
.resolve_member_reference(&member_name, &None)
{
self.nodes_factory
.add_render_reference(member_name, reference);
} else {
for dep in member.get_dependencies() {
self.resolve_render_reference_for_member(&dep);
}
}
}
}

fn make_cube_references(&self) -> HashMap<String, String> {
let mut refs = HashMap::new();
match &self.from.source {
crate::plan::FromSource::Single(source) => {
self.add_cube_reference_if_needed(source, &mut refs)
}
crate::plan::FromSource::Join(join) => {
self.add_cube_reference_if_needed(&join.root, &mut refs);
for join_item in join.joins.iter() {
self.add_cube_reference_if_needed(&join_item.from, &mut refs);
}
}
crate::plan::FromSource::Empty => {}
}
refs
}

fn add_cube_reference_if_needed(
&self,
source: &SingleAliasedSource,
refs: &mut HashMap<String, String>,
) {
match &source.source {
SingleSource::Cube(cube) => {
refs.insert(cube.name().clone(), source.alias.clone());
}
_ => {}
}
}

pub fn build(mut self) -> Select {
let cube_references = self.make_cube_references();
self.nodes_factory.set_cube_name_references(cube_references);
Select {
projection_columns: self.projection_columns,
from: self.from,
filter: self.filter,
group_by: self.group_by,
having: self.having,
order_by: self.order_by,
context: self.context.clone(),
context: Rc::new(VisitorContext::new(&self.nodes_factory)),
ctes: self.ctes,
is_distinct: self.is_distinct,
limit: self.limit,
Expand Down
26 changes: 25 additions & 1 deletion rust/cubesqlplanner/cubesqlplanner/src/plan/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,44 @@ impl MemberExpression {
}
}

#[derive(Clone)]
pub struct ReferenceExpression {
pub reference: String,
pub source: Option<String>,
}

impl ReferenceExpression {
pub fn new(reference: String, source: Option<String>) -> Self {
Self { reference, source }
}

pub fn to_sql(&self, templates: &PlanSqlTemplates) -> Result<String, CubeError> {
templates.column_reference(&self.source, &self.reference)
}
}

#[derive(Clone)]
pub enum Expr {
Member(MemberExpression),
Reference(ReferenceExpression),
}

impl Expr {
pub fn new_member(member: Rc<dyn BaseMember>, source: Option<String>) -> Self {
Self::Member(MemberExpression::new(member, source))
}
pub fn new_reference(reference: String, source: Option<String>) -> Self {
Self::Reference(ReferenceExpression::new(reference, source))
}
pub fn to_sql(
&self,
templates: &PlanSqlTemplates,
context: Rc<VisitorContext>,
schema: Rc<Schema>,
) -> Result<String, CubeError> {
match self {
Expr::Member(member) => member.to_sql(templates, context, schema),
Self::Member(member) => member.to_sql(templates, context, schema),
Self::Reference(reference) => reference.to_sql(templates),
}
}
}
6 changes: 1 addition & 5 deletions rust/cubesqlplanner/cubesqlplanner/src/plan/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ impl SingleAliasedSource {
pub fn make_schema(&self) -> Schema {
match &self.source {
SingleSource::Subquery(query) => query.make_schema(Some(self.alias.clone())),
SingleSource::Cube(cube) => {
let mut schema = Schema::empty();
schema.add_cube(SchemaCube::new(cube.name().clone(), self.alias.clone()));
schema
}
SingleSource::Cube(cube) => Schema::empty(), //cube.schema().move_to_source(&self.alias),
SingleSource::TableReference(_, schema) => schema.move_to_source(&self.alias),
}
}
Expand Down
10 changes: 7 additions & 3 deletions rust/cubesqlplanner/cubesqlplanner/src/plan/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use std::rc::Rc;
pub struct SchemaColumn {
pub table_name: Option<String>,
pub alias: String,
pub origin_member: String,
pub origin_member: Option<String>,
}

impl SchemaColumn {
pub fn new(table_name: Option<String>, alias: String, origin_member: String) -> Self {
pub fn new(table_name: Option<String>, alias: String, origin_member: Option<String>) -> Self {
Self {
table_name,
alias,
Expand Down Expand Up @@ -107,7 +107,11 @@ impl Schema {
if source.is_some() && source != &col.table_name {
return false;
}
&col.origin_member == member_name
if let Some(origin_member) = &col.origin_member {
origin_member == member_name
} else {
false
}
})
}

Expand Down
10 changes: 9 additions & 1 deletion rust/cubesqlplanner/cubesqlplanner/src/plan/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ impl Select {
let schema_col = SchemaColumn::new(
self_alias.clone(),
col.alias.clone(),
member.member.full_name(),
Some(member.member.full_name()),
);
schema.add_column(schema_col);
}
Expr::Reference(reference) => {
let schema_col = SchemaColumn::new(
self_alias.clone(),
col.alias.clone(),
None, //"".to_string(), //member.member.full_name(),
);
schema.add_column(schema_col);
}
Expand Down
2 changes: 1 addition & 1 deletion rust/cubesqlplanner/cubesqlplanner/src/plan/time_series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl TimeSeries {
let column = SchemaColumn::new(
self_alias,
format!("date_from"),
self.time_dimension_name.clone(),
Some(self.time_dimension_name.clone()),
);
Schema::new(vec![column], vec![])
}
Expand Down
26 changes: 25 additions & 1 deletion rust/cubesqlplanner/cubesqlplanner/src/planner/base_cube.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use super::query_tools::QueryTools;
use super::sql_evaluator::MemberSymbol;
use super::{evaluate_with_context, VisitorContext};
use crate::plan::Schema;
use crate::plan::{Schema, SchemaColumn};
use cubenativeutils::CubeError;
use std::rc::Rc;

pub struct BaseCube {
cube_name: String,
members: Vec<String>,
schema: Schema,
member_evaluator: Rc<MemberSymbol>,
query_tools: Rc<QueryTools>,
}
Expand All @@ -16,8 +18,22 @@ impl BaseCube {
query_tools: Rc<QueryTools>,
member_evaluator: Rc<MemberSymbol>,
) -> Result<Rc<Self>, CubeError> {
let members = query_tools
.base_tools()
.all_cube_members(cube_name.clone())?;
let mut schema = Schema::empty();
for member in members.iter() {
schema.add_column(SchemaColumn::new(
Some(cube_name.clone()),
member.clone(),
Some(format!("{}.{}", cube_name, member)),
));
}

Ok(Rc::new(Self {
cube_name,
members,
schema,
member_evaluator,
query_tools,
}))
Expand All @@ -37,6 +53,14 @@ impl BaseCube {
&self.cube_name
}

pub fn members(&self) -> &Vec<String> {
&self.members
}

pub fn schema(&self) -> &Schema {
&self.schema
}

pub fn default_alias(&self) -> String {
self.query_tools.alias_name(&self.cube_name)
}
Expand Down
10 changes: 5 additions & 5 deletions rust/cubesqlplanner/cubesqlplanner/src/planner/base_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ impl<IT: InnerTypes> BaseQuery<IT> {
}

fn build_sql_and_params_impl(&self) -> Result<Select, CubeError> {
let nodes_factory = if self.request.ungrouped() {
SqlNodesFactory::new_ungroupped()
} else {
SqlNodesFactory::new()
};
let mut nodes_factory = SqlNodesFactory::new();

if self.request.ungrouped() {
nodes_factory.set_ungrouped(true)
}

if self.request.is_simple_query()? {
let planner = SimpleQueryPlanner::new(
Expand Down
Loading

0 comments on commit 88832ae

Please sign in to comment.