Skip to content

Commit 7775bc2

Browse files
committed
in work
1 parent fe62933 commit 7775bc2

36 files changed

+700
-488
lines changed

packages/cubejs-schema-compiler/src/adapter/BaseQuery.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,30 @@ export class BaseQuery {
628628
return res;
629629
}
630630

631+
buildSqlAndParamsTest(exportAnnotatedSql) {
632+
if (!this.options.preAggregationQuery && !this.options.disableExternalPreAggregations && this.externalQueryClass) {
633+
if (this.externalPreAggregationQuery()) { // TODO performance
634+
return this.externalQuery().buildSqlAndParams(exportAnnotatedSql);
635+
}
636+
}
637+
const js_res = this.compilers.compiler.withQuery(
638+
this,
639+
() => this.cacheValue(
640+
['buildSqlAndParams', exportAnnotatedSql],
641+
() => this.paramAllocator.buildSqlAndParams(
642+
this.buildParamAnnotatedSql(),
643+
exportAnnotatedSql,
644+
this.shouldReuseParams
645+
),
646+
{ cache: this.queryCache }
647+
)
648+
);
649+
const rust = this.buildSqlAndParamsRust(exportAnnotatedSql);
650+
console.log('js result: ', js_res[0]);
651+
console.log('rust result: ', rust[0]);
652+
return js_res;
653+
}
654+
631655
get shouldReuseParams() {
632656
return false;
633657
}

rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/sql_templates_render.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::marker::PhantomData;
99
pub trait SqlTemplatesRender {
1010
fn contains_template(&self, template_name: &str) -> bool;
1111
fn render_template(&self, name: &str, ctx: Value) -> Result<String, CubeError>;
12+
fn get_template(&self, template_name: &str) -> Result<&String, CubeError>;
1213
}
1314

1415
pub struct NativeSqlTemplatesRender<IT: InnerTypes> {
@@ -44,6 +45,12 @@ impl<IT: InnerTypes> SqlTemplatesRender for NativeSqlTemplatesRender<IT> {
4445
self.templates.contains_key(template_name)
4546
}
4647

48+
fn get_template(&self, template_name: &str) -> Result<&String, CubeError> {
49+
self.templates
50+
.get(template_name)
51+
.ok_or_else(|| CubeError::user("{template_name} template not found".to_string()))
52+
}
53+
4754
fn render_template(&self, name: &str, ctx: Value) -> Result<String, CubeError> {
4855
Ok(self
4956
.jinja

rust/cubesqlplanner/cubesqlplanner/src/plan/expression.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::planner::sql_templates::PlanSqlTemplates;
12
use crate::planner::{BaseMember, VisitorContext};
23
use cubenativeutils::CubeError;
34
use std::rc::Rc;
@@ -10,9 +11,16 @@ pub enum Expr {
1011
}
1112

1213
impl Expr {
13-
pub fn to_sql(&self, context: Rc<VisitorContext>) -> Result<String, CubeError> {
14+
pub fn to_sql(
15+
&self,
16+
templates: &PlanSqlTemplates,
17+
context: Rc<VisitorContext>,
18+
) -> Result<String, CubeError> {
1419
match self {
15-
Expr::Field(field) => field.to_sql(context),
20+
Expr::Field(field) => {
21+
let sql = field.to_sql(context)?;
22+
Ok(sql)
23+
}
1624
Expr::Reference(cube_alias, field_alias) => {
1725
if let Some(cube_alias) = cube_alias {
1826
Ok(format!("{}.{}", cube_alias, field_alias))

rust/cubesqlplanner/cubesqlplanner/src/plan/from.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
use super::{Join, QueryPlan, Subquery};
2+
use crate::planner::sql_templates::PlanSqlTemplates;
23
use crate::planner::{BaseCube, VisitorContext};
34
use cubenativeutils::CubeError;
45
use std::rc::Rc;
56

7+
#[derive(Clone)]
8+
pub enum SingleSource {
9+
Subquery(Subquery),
10+
Cube(Rc<BaseCube>),
11+
TableReference(String),
12+
}
13+
14+
#[derive(Clone)]
15+
pub struct SingleAliasedSource {
16+
source: SingleSource,
17+
alias: Option<String>,
18+
}
19+
620
#[derive(Clone)]
721
pub enum FromSource {
822
Empty,
9-
Cube(Rc<BaseCube>),
23+
Single(SingleAliasedSource),
1024
Join(Rc<Join>),
11-
Subquery(Subquery),
12-
TableReference(String, Option<String>),
1325
}
1426

1527
#[derive(Clone)]
@@ -38,17 +50,21 @@ impl From {
3850
Self::new(FromSource::Subquery(Subquery::new(plan, alias)))
3951
}
4052

41-
pub fn to_sql(&self, context: Rc<VisitorContext>) -> Result<String, CubeError> {
53+
pub fn to_sql(
54+
&self,
55+
templates: &PlanSqlTemplates,
56+
context: Rc<VisitorContext>,
57+
) -> Result<String, CubeError> {
4258
let sql = match &self.source {
4359
FromSource::Empty => format!(""),
4460
FromSource::Cube(cube) => {
4561
let cubesql = cube.to_sql(context.clone())?;
4662
format!(" {} ", cubesql)
4763
}
4864
FromSource::Join(j) => {
49-
format!("{}", j.to_sql(context.clone())?)
65+
format!("{}", j.to_sql(templates, context.clone())?)
5066
}
51-
FromSource::Subquery(s) => s.to_sql()?,
67+
FromSource::Subquery(s) => s.to_sql(templates)?,
5268
FromSource::TableReference(r, alias) => {
5369
if let Some(alias) = alias {
5470
format!(" {} as {} ", r, alias)

rust/cubesqlplanner/cubesqlplanner/src/plan/join.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use super::{QueryPlan, Select, Subquery};
1+
use super::{QueryPlan, Select, SingleAliasedSource, Subquery};
2+
use crate::planner::sql_templates::PlanSqlTemplates;
23
use crate::planner::{BaseCube, BaseJoinCondition, VisitorContext};
34
use cubenativeutils::CubeError;
45

@@ -27,53 +28,65 @@ impl JoinSource {
2728
Self::TableReference(reference, alias)
2829
}
2930

30-
pub fn to_sql(&self, context: Rc<VisitorContext>) -> Result<String, CubeError> {
31+
pub fn to_sql(
32+
&self,
33+
templates: &PlanSqlTemplates,
34+
context: Rc<VisitorContext>,
35+
) -> Result<String, CubeError> {
3136
let sql = match &self {
3237
JoinSource::Cube(cube) => {
3338
let cubesql = cube.to_sql(context.clone())?;
3439
format!(" {} ", cubesql)
3540
}
36-
JoinSource::Subquery(s) => s.to_sql()?,
41+
JoinSource::Subquery(s) => s.to_sql(templates)?,
3742
JoinSource::TableReference(r, alias) => format!(" {} as {} ", r, alias),
3843
};
3944
Ok(sql)
4045
}
4146
}
4247

4348
pub struct JoinItem {
44-
pub from: JoinSource,
49+
pub from: SingleAliasedSource,
4550
pub on: Rc<dyn BaseJoinCondition>,
4651
pub is_inner: bool,
4752
}
4853

4954
pub struct Join {
50-
pub root: JoinSource,
55+
pub root: SingleAliasedSource,
5156
pub joins: Vec<JoinItem>,
5257
}
5358

5459
impl JoinItem {
55-
pub fn to_sql(&self, context: Rc<VisitorContext>) -> Result<String, CubeError> {
60+
pub fn to_sql(
61+
&self,
62+
templates: &PlanSqlTemplates,
63+
context: Rc<VisitorContext>,
64+
) -> Result<String, CubeError> {
5665
let operator = if self.is_inner { "INNER" } else { "LEFT" };
5766
let on_sql = self.on.to_sql(context.clone())?;
5867
Ok(format!(
5968
"{} JOIN {} ON {}",
6069
operator,
61-
self.from.to_sql(context)?,
70+
self.from.to_sql(templates, context)?,
6271
on_sql
6372
))
6473
}
6574
}
6675

6776
impl Join {
68-
pub fn to_sql(&self, context: Rc<VisitorContext>) -> Result<String, CubeError> {
77+
pub fn to_sql(
78+
&self,
79+
templates: &PlanSqlTemplates,
80+
context: Rc<VisitorContext>,
81+
) -> Result<String, CubeError> {
6982
let joins_sql = self
7083
.joins
7184
.iter()
72-
.map(|j| j.to_sql(context.clone()))
85+
.map(|j| j.to_sql(templates, context.clone()))
7386
.collect::<Result<Vec<_>, _>>()?;
7487
let res = format!(
7588
"{}\n{}",
76-
self.root.to_sql(context.clone())?,
89+
self.root.to_sql(templates, context.clone())?,
7790
joins_sql.join("\n")
7891
);
7992
Ok(res)

rust/cubesqlplanner/cubesqlplanner/src/plan/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ pub mod from;
55
pub mod join;
66
pub mod order;
77
pub mod query_plan;
8+
pub mod schema;
89
pub mod select;
10+
pub mod select_builder;
911
pub mod subquery;
1012
pub mod union;
1113

1214
pub use expression::Expr;
1315
pub use filter::{Filter, FilterGroup, FilterItem};
14-
pub use from::{From, FromSource};
16+
pub use from::{From, FromSource, SingleAliasedSource};
1517
pub use join::{Join, JoinItem, JoinSource};
1618
pub use order::OrderBy;
1719
pub use query_plan::QueryPlan;
1820
pub use select::Select;
21+
pub use select_builder::SelectBuilder;
1922
pub use subquery::Subquery;
2023
pub use union::Union;
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::Select;
22
use super::Union;
3+
use crate::planner::sql_templates::PlanSqlTemplates;
34
use cubenativeutils::CubeError;
45
use std::rc::Rc;
56

@@ -9,10 +10,10 @@ pub enum QueryPlan {
910
}
1011

1112
impl QueryPlan {
12-
pub fn to_sql(&self) -> Result<String, CubeError> {
13+
pub fn to_sql(&self, templates: &PlanSqlTemplates) -> Result<String, CubeError> {
1314
match self {
14-
QueryPlan::Select(s) => s.to_sql(),
15-
QueryPlan::Union(u) => u.to_sql(),
15+
QueryPlan::Select(s) => s.to_sql(templates),
16+
QueryPlan::Union(u) => u.to_sql(templates),
1617
}
1718
}
1819
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use cubenativeutils::CubeError;
2+
3+
pub struct SchemaColumn {
4+
table_name: String,
5+
name: String,
6+
origin_member: String,
7+
}
8+
9+
impl SchemaColumn {
10+
pub fn new(table_name: String, name: String, origin_member: String) -> Self {
11+
Self {
12+
table_name,
13+
name,
14+
origin_member,
15+
}
16+
}
17+
}
18+
pub struct Schema {}

rust/cubesqlplanner/cubesqlplanner/src/plan/select.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
use itertools::Itertools;
22

33
use super::{Expr, Filter, From, OrderBy, Subquery};
4+
use crate::planner::sql_templates::PlanSqlTemplates;
45
use crate::planner::VisitorContext;
56
use cubenativeutils::CubeError;
67
use std::rc::Rc;
78

89
pub struct Select {
9-
pub projection: Vec<Expr>,
10-
pub from: From,
11-
pub filter: Option<Filter>,
12-
pub group_by: Vec<Expr>,
13-
pub having: Option<Filter>,
14-
pub order_by: Vec<OrderBy>,
15-
pub context: Rc<VisitorContext>,
16-
pub ctes: Vec<Rc<Subquery>>,
17-
pub is_distinct: bool,
18-
pub limit: Option<usize>,
19-
pub offset: Option<usize>,
10+
pub(super) projection: Vec<Expr>,
11+
pub(super) from: From,
12+
pub(super) filter: Option<Filter>,
13+
pub(super) group_by: Vec<Expr>,
14+
pub(super) having: Option<Filter>,
15+
pub(super) order_by: Vec<OrderBy>,
16+
pub(super) context: Rc<VisitorContext>,
17+
pub(super) ctes: Vec<Rc<Subquery>>,
18+
pub(super) is_distinct: bool,
19+
pub(super) limit: Option<usize>,
20+
pub(super) offset: Option<usize>,
2021
}
2122

2223
impl Select {
23-
pub fn to_sql(&self) -> Result<String, CubeError> {
24+
pub fn to_sql(&self, templates: &PlanSqlTemplates) -> Result<String, CubeError> {
2425
let projection = self
2526
.projection
2627
.iter()
27-
.map(|p| p.to_sql(self.context.clone()))
28+
.map(|p| p.to_sql(templates, self.context.clone()))
2829
.collect::<Result<Vec<_>, _>>()?
2930
.join(", ");
3031
let where_condition = if let Some(filter) = &self.filter {
@@ -56,7 +57,11 @@ impl Select {
5657
.ctes
5758
.iter()
5859
.map(|cte| -> Result<_, CubeError> {
59-
Ok(format!(" {} as ({})", cte.alias(), cte.query().to_sql()?))
60+
Ok(format!(
61+
" {} as ({})",
62+
cte.alias(),
63+
cte.query().to_sql(templates)?
64+
))
6065
})
6166
.collect::<Result<Vec<_>, _>>()?
6267
.join(",\n");
@@ -78,7 +83,7 @@ impl Select {
7883
};
7984

8085
let distinct = if self.is_distinct { "DISTINCT " } else { "" };
81-
let from = self.from.to_sql(self.context.clone())?;
86+
let from = self.from.to_sql(templates, self.context.clone())?;
8287
let limit = if let Some(limit) = self.limit {
8388
format!(" LIMIT {limit}")
8489
} else {

0 commit comments

Comments
 (0)