Skip to content

Commit

Permalink
feat(cubesql): Send expressions as objects (#8216)
Browse files Browse the repository at this point in the history
  • Loading branch information
waralexrom committed May 15, 2024
1 parent 6c39f37 commit 4deee84
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
28 changes: 16 additions & 12 deletions packages/cubejs-api-gateway/src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1271,18 +1271,22 @@ class ApiGateway {
}

private parseMemberExpression(memberExpression: string): string | MemberExpression {
const match = memberExpression.match(memberExpressionRegex);
if (match) {
const args = match[3].split(',');
args.push(`return \`${match[4]}\``);
return {
cubeName: match[1],
name: match[2],
expressionName: match[2],
expression: Function.constructor.apply(null, args),
definition: memberExpression,
};
} else {
try {
if (memberExpression.startsWith('{')) {
const obj = JSON.parse(memberExpression);
const args = obj.cube_params;
args.push(`return \`${obj.expr}\``);
return {
cubeName: obj.cube_name,
name: obj.alias,
expressionName: obj.alias,
expression: Function.constructor.apply(null, args),
definition: memberExpression,
};
} else {
return memberExpression;
}
} catch {
return memberExpression;
}
}
Expand Down
39 changes: 25 additions & 14 deletions rust/cubesql/cubesql/src/compile/engine/df/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ pub struct SqlQuery {
pub values: Vec<Option<String>>,
}

#[derive(Debug, Clone, Serialize)]
struct UngrouppedMemberDef {
cube_name: String,
alias: String,
cube_params: Vec<String>,
expr: String,
}

impl SqlQuery {
pub fn new(sql: String, values: Vec<Option<String>>) -> Self {
Self { sql, values }
Expand Down Expand Up @@ -441,7 +449,7 @@ impl CubeScanWrapperNode {
Some(Arc::new(cube_scan_node.clone()))
} else {
return Err(CubeError::internal(format!(
"Expected ubeScan node but found: {:?}",
"Expected CubeScan node but found: {:?}",
plan
)));
}
Expand Down Expand Up @@ -977,19 +985,22 @@ impl CubeScanWrapperNode {
}

fn ungrouped_member_def(column: &AliasedColumn, used_cubes: &Vec<String>) -> Result<String> {
let cube_params = used_cubes.iter().join(",");
Ok(format!(
"{}.{}:({}):{}",
used_cubes.iter().next().ok_or_else(|| {
DataFusionError::Internal(format!(
"Can't generate SQL for column without cubes: {:?}",
column
))
})?,
column.alias,
cube_params,
column.expr
))
let res = UngrouppedMemberDef {
cube_name: used_cubes
.iter()
.next()
.ok_or_else(|| {
DataFusionError::Internal(format!(
"Can't generate SQL for column without cubes: {:?}",
column
))
})?
.to_string(),
alias: column.alias.clone(),
cube_params: used_cubes.clone(),
expr: column.expr.clone(),
};
Ok(serde_json::json!(res).to_string())
}

pub fn generate_sql_for_expr(
Expand Down
8 changes: 4 additions & 4 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20035,7 +20035,7 @@ ORDER BY "source"."str0" ASC
}

#[tokio::test]
async fn test_simple_subquery_wrapper_projection_1() {
async fn test_simple_subquery_wrapper_projection() {
if !Rewriter::sql_push_down_enabled() {
return;
}
Expand All @@ -20060,7 +20060,7 @@ ORDER BY "source"."str0" ASC
.wrapped_sql
.unwrap()
.sql
.contains("\\\"limit\\\":1"));
.contains("\\\\\\\"limit\\\\\\\":1"));

let _physical_plan = query_plan.as_physical_plan().await.unwrap();
}
Expand Down Expand Up @@ -20116,7 +20116,7 @@ ORDER BY "source"."str0" ASC
.wrapped_sql
.unwrap()
.sql
.contains("\\\"limit\\\":1"));
.contains("\\\\\\\"limit\\\\\\\":1"));

let _physical_plan = query_plan.as_physical_plan().await.unwrap();
}
Expand Down Expand Up @@ -20456,7 +20456,7 @@ ORDER BY "source"."str0" ASC
.wrapped_sql
.unwrap()
.sql
.contains("__user:(KibanaSampleDataEcommerce):$"),
.contains("\\\"cube_name\\\":\\\"KibanaSampleDataEcommerce\\\",\\\"alias\\\":\\\"__user\\\""),
"SQL contains __user:(KibanaSampleDataEcommerce):$: {}",
logical_plan
.find_cube_scan_wrapper()
Expand Down

0 comments on commit 4deee84

Please sign in to comment.