Skip to content

Commit

Permalink
in work
Browse files Browse the repository at this point in the history
  • Loading branch information
waralexrom committed Dec 14, 2024
1 parent 1854ae8 commit 0ed925a
Show file tree
Hide file tree
Showing 63 changed files with 1,168 additions and 892 deletions.
32 changes: 2 additions & 30 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,34 +605,9 @@ export class BaseQuery {
}
}

buildSqlAndParamsTest(exportAnnotatedSql) {
if (!this.options.preAggregationQuery && !this.options.disableExternalPreAggregations && this.externalQueryClass) {
if (this.externalPreAggregationQuery()) { // TODO performance
return this.externalQuery().buildSqlAndParams(exportAnnotatedSql);
}
}
const js_res = this.compilers.compiler.withQuery(
this,
() => this.cacheValue(
['buildSqlAndParams', exportAnnotatedSql],
() => this.paramAllocator.buildSqlAndParams(
this.buildParamAnnotatedSql(),
exportAnnotatedSql,
this.shouldReuseParams
),
{ cache: this.queryCache }
)
);
console.log('js result: ', js_res[0]);
const rust = this.buildSqlAndParamsRust(exportAnnotatedSql);
console.log('rust result: ', rust[0]);
return js_res;
}

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 @@ -644,7 +619,7 @@ export class BaseQuery {
joinRoot: this.join.root,
joinGraph: this.joinGraph,
cubeEvaluator: this.cubeEvaluator,
order: order,
order,
filters: this.options.filters,
limit: this.options.limit ? this.options.limit.toString() : null,
rowLimit: this.options.rowLimit ? this.options.rowLimit.toString() : null,
Expand Down Expand Up @@ -847,7 +822,6 @@ export class BaseQuery {
} = this.fullKeyQueryAggregateMeasures();

if (!multipliedMeasures.length && !cumulativeMeasures.length && !multiStageMembers.length) {
console.log("!!!!! LLLOOOO!!!!");
return this.simpleQuery();
}

Expand Down Expand Up @@ -1061,8 +1035,6 @@ export class BaseQuery {
const allMemberChildren = this.collectAllMemberChildren(context);
const memberToIsMultiStage = this.collectAllMultiStageMembers(allMemberChildren);

console.log("!!! measure to her ", measureToHierarchy);

const hasMultiStageMembers = (m) => {
if (memberToIsMultiStage[m]) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { StartedTestContainer } from 'testcontainers';
import { format as formatSql } from 'sqlstring';
import { v4 as uuidv4 } from 'uuid';
import { ClickHouseQuery } from '../../../src/adapter/ClickHouseQuery';
import { BaseDbRunner } from "../utils/BaseDbRunner";
import { BaseDbRunner } from '../utils/BaseDbRunner';

process.env.TZ = 'GMT';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2348,7 +2348,7 @@ describe('SQL Generation', () => {
]
));

it('rank measure', async () => runQueryTest(
it('rank measure 1', async () => runQueryTest(
{
measures: ['visitors.revenue_rank'],
},
Expand Down
4 changes: 2 additions & 2 deletions packages/cubejs-schema-compiler/test/unit/base-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@ describe('SQL Generation', () => {
sql: 'product_id',
type: 'avg',
filters: [
{ sql: `{FILTER_PARAMS.Order.category.filter('category')}` }
{ sql: '{FILTER_PARAMS.Order.category.filter(\'category\')}' }
]
}
],
Expand All @@ -1613,7 +1613,7 @@ describe('SQL Generation', () => {
},
{
name: 'proxied',
sql: `{FILTER_PARAMS.Order.type.filter("x => type = 'online'")}`,
sql: '{FILTER_PARAMS.Order.type.filter("x => type = \'online\'")}',
type: 'boolean',
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ describe('pre-aggregations', () => {
console.log(JSON.stringify(preAggregationsDescription, null, 2));

expect(preAggregationsDescription.length).toEqual(2);
expect(preAggregationsDescription[0].preAggregationId).toEqual("Orders.simple1");
expect(preAggregationsDescription[1].preAggregationId).toEqual("Orders.simple2");
expect(preAggregationsDescription[0].preAggregationId).toEqual('Orders.simple1');
expect(preAggregationsDescription[1].preAggregationId).toEqual('Orders.simple2');
});

// @link https://github.com/cube-js/cube/issues/6623
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
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 @@ -9,7 +7,6 @@ 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
54 changes: 34 additions & 20 deletions rust/cubesqlplanner/cubesqlplanner/src/plan/builder/select.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,60 @@
use crate::plan::{
AliasedExpr, Cte, Expr, Filter, From, MemberExpression, OrderBy, Schema, Select,
AliasedExpr, Cte, Expr, Filter, From, MemberExpression, OrderBy, Schema, SchemaColumn, 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 {
projection_columns: Vec<AliasedExpr>,
from: From,
from: Rc<From>,
filter: Option<Filter>,
group_by: Vec<Expr>,
having: Option<Filter>,
order_by: Vec<OrderBy>,
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, nodes_factory: SqlNodesFactory) -> Self {
let input_schema = from.schema.clone();
pub fn new(from: Rc<From>) -> Self {
Self {
projection_columns: vec![],
from,
filter: None,
group_by: vec![],
having: None,
order_by: vec![],
nodes_factory,
ctes: vec![],
is_distinct: false,
limit: None,
offset: None,
input_schema,
result_schema: Schema::empty(),
}
}

pub fn add_projection_member(
&mut self,
member: &Rc<dyn BaseMember>,
source: Option<String>,
alias: Option<String>,
) {
pub fn add_projection_member(&mut self, member: &Rc<dyn BaseMember>, alias: Option<String>) {
let alias = if let Some(alias) = alias {
alias
} else {
self.input_schema.resolve_member_alias(&member, &source)
member.alias_name()
};
let expr = Expr::Member(MemberExpression::new(member.clone(), source));

let expr = Expr::Member(MemberExpression::new(member.clone()));
let aliased_expr = AliasedExpr {
expr,
alias: alias.clone(),
};

self.projection_columns.push(aliased_expr);
self.result_schema
.add_column(SchemaColumn::new(alias.clone(), Some(member.full_name())));
}

pub fn set_filter(&mut self, filter: Option<Filter>) {
Expand Down Expand Up @@ -127,21 +119,43 @@ impl SelectBuilder {
}
}

pub fn build(mut self) -> Select {
fn make_asteriks_schema(&self) -> Rc<Schema> {
let schema = match &self.from.source {
crate::plan::FromSource::Empty => Rc::new(Schema::empty()),
crate::plan::FromSource::Single(source) => source.source.schema(),
crate::plan::FromSource::Join(join) => {
let mut schema = Schema::empty();
schema.merge(join.root.source.schema().as_ref());
for itm in join.joins.iter() {
schema.merge(itm.from.source.schema().as_ref())
}
Rc::new(schema)
}
};
schema
}

pub fn build(self, mut nodes_factory: SqlNodesFactory) -> Select {
let cube_references = self.make_cube_references();
self.nodes_factory.set_cube_name_references(cube_references);
nodes_factory.set_cube_name_references(cube_references);
let schema = if self.projection_columns.is_empty() {
self.make_asteriks_schema()
} else {
Rc::new(self.result_schema)
};
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: Rc::new(VisitorContext::new(&self.nodes_factory)),
context: Rc::new(VisitorContext::new(&nodes_factory)),
ctes: self.ctes,
is_distinct: self.is_distinct,
limit: self.limit,
offset: self.offset,
schema,
}
}
}
6 changes: 1 addition & 5 deletions rust/cubesqlplanner/cubesqlplanner/src/plan/cte.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{QueryPlan, Schema, Select};
use super::{QueryPlan, Select};
use crate::planner::sql_templates::PlanSqlTemplates;
use cubenativeutils::CubeError;

Expand All @@ -22,10 +22,6 @@ impl Cte {
}
}

pub fn make_schema(&self) -> Schema {
self.query.make_schema(Some(self.name().clone()))
}

pub fn query(&self) -> &Rc<QueryPlan> {
&self.query
}
Expand Down
51 changes: 14 additions & 37 deletions rust/cubesqlplanner/cubesqlplanner/src/plan/expression.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Schema;
use super::QualifiedColumnName;
use crate::planner::sql_templates::PlanSqlTemplates;
use crate::planner::{BaseMember, VisitorContext};
use cubenativeutils::CubeError;
Expand All @@ -7,68 +7,45 @@ use std::rc::Rc;
#[derive(Clone)]
pub struct MemberExpression {
pub member: Rc<dyn BaseMember>,
pub source: Option<String>,
}

impl MemberExpression {
pub fn new(member: Rc<dyn BaseMember>, source: Option<String>) -> Self {
Self { member, source }
pub fn new(member: Rc<dyn BaseMember>) -> Self {
Self { member }
}

pub fn to_sql(
&self,
templates: &PlanSqlTemplates,
_templates: &PlanSqlTemplates,
context: Rc<VisitorContext>,
schema: Rc<Schema>,
) -> Result<String, CubeError> {
if let Some(reference_column) =
schema.find_column_for_member(&self.member.full_name(), &self.source)
{
templates.column_reference(&reference_column.table_name, &reference_column.alias)
} else {
self.member.to_sql(context, schema)
}
}
}

#[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)
self.member.to_sql(context)
}
}

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

impl Expr {
pub fn new_member(member: Rc<dyn BaseMember>, source: Option<String>) -> Self {
Self::Member(MemberExpression::new(member, source))
pub fn new_member(member: Rc<dyn BaseMember>) -> Self {
Self::Member(MemberExpression::new(member))
}
pub fn new_reference(reference: String, source: Option<String>) -> Self {
Self::Reference(ReferenceExpression::new(reference, source))
pub fn new_reference(source: Option<String>, reference: String) -> Self {
Self::Reference(QualifiedColumnName::new(source, reference))
}
pub fn to_sql(
&self,
templates: &PlanSqlTemplates,
context: Rc<VisitorContext>,
schema: Rc<Schema>,
) -> Result<String, CubeError> {
match self {
Self::Member(member) => member.to_sql(templates, context, schema),
Self::Reference(reference) => reference.to_sql(templates),
Self::Member(member) => member.to_sql(templates, context),
Self::Reference(reference) => {
templates.column_reference(reference.source(), &reference.name())
}
}
}
}
9 changes: 3 additions & 6 deletions rust/cubesqlplanner/cubesqlplanner/src/plan/filter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::Schema;
use crate::planner::filter::BaseFilter;
use crate::planner::sql_templates::PlanSqlTemplates;
use crate::planner::VisitorContext;
Expand Down Expand Up @@ -54,15 +53,14 @@ impl FilterItem {
&self,
templates: &PlanSqlTemplates,
context: Rc<VisitorContext>,
schema: Rc<Schema>,
) -> Result<String, CubeError> {
let res = match self {
FilterItem::Group(group) => {
let operator = format!(" {} ", group.operator.to_string());
let items_sql = group
.items
.iter()
.map(|itm| itm.to_sql(templates, context.clone(), schema.clone()))
.map(|itm| itm.to_sql(templates, context.clone()))
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.filter(|itm| !itm.is_empty())
Expand All @@ -75,7 +73,7 @@ impl FilterItem {
}
}
FilterItem::Item(item) => {
let sql = item.to_sql(context.clone(), schema)?;
let sql = item.to_sql(context.clone())?;
format!("({})", sql)
}
};
Expand All @@ -88,12 +86,11 @@ impl Filter {
&self,
templates: &PlanSqlTemplates,
context: Rc<VisitorContext>,
schema: Rc<Schema>,
) -> Result<String, CubeError> {
let res = self
.items
.iter()
.map(|itm| itm.to_sql(templates, context.clone(), schema.clone()))
.map(|itm| itm.to_sql(templates, context.clone()))
.collect::<Result<Vec<_>, _>>()?
.join(" AND ");
Ok(res)
Expand Down
Loading

0 comments on commit 0ed925a

Please sign in to comment.