Skip to content

Commit f22e543

Browse files
committed
in work
1 parent 7bdc47f commit f22e543

File tree

6 files changed

+84
-24
lines changed

6 files changed

+84
-24
lines changed

packages/cubejs-schema-compiler/src/compiler/CubeValidator.ts

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -657,30 +657,51 @@ const SwitchDimension = Joi.object({
657657
values: Joi.array().items(Joi.string()).min(1).required()
658658
});
659659

660+
const CaseSchema = Joi.object().keys({
661+
when: Joi.array().items(Joi.object().keys({
662+
sql: Joi.func().required(),
663+
label: Joi.alternatives([
664+
Joi.string(),
665+
Joi.object().keys({
666+
sql: Joi.func().required()
667+
})
668+
])
669+
})),
670+
else: Joi.object().keys({
671+
label: Joi.alternatives([
672+
Joi.string(),
673+
Joi.object().keys({
674+
sql: Joi.func().required()
675+
})
676+
])
677+
})
678+
}).required();
679+
680+
const SwitchCaseSchema = Joi.object().keys({
681+
switch: Joi.alternatives([
682+
Joi.string(),
683+
Joi.func()
684+
]).required(),
685+
when: Joi.array().items(Joi.object().keys({
686+
value: Joi.string().required(),
687+
sql: Joi.func().required()
688+
})),
689+
else: Joi.object().keys({
690+
sql: Joi.func().required()
691+
})
692+
}).required();
693+
694+
const CaseVariants = Joi.alternatives().try(
695+
CaseSchema,
696+
SwitchCaseSchema
697+
);
698+
660699
const DimensionsSchema = Joi.object().pattern(identifierRegex, Joi.alternatives().conditional(Joi.ref('.type'), {
661700
is: 'switch',
662701
then: SwitchDimension,
663702
otherwise: Joi.alternatives().try(
664703
inherit(BaseDimensionWithoutSubQuery, {
665-
case: Joi.object().keys({
666-
when: Joi.array().items(Joi.object().keys({
667-
sql: Joi.func().required(),
668-
label: Joi.alternatives([
669-
Joi.string(),
670-
Joi.object().keys({
671-
sql: Joi.func().required()
672-
})
673-
])
674-
})),
675-
else: Joi.object().keys({
676-
label: Joi.alternatives([
677-
Joi.string(),
678-
Joi.object().keys({
679-
sql: Joi.func().required()
680-
})
681-
])
682-
})
683-
}).required()
704+
case: CaseVariants
684705
}),
685706
inherit(BaseDimensionWithoutSubQuery, {
686707
latitude: Joi.object().keys({

packages/cubejs-schema-compiler/src/compiler/transpilers/CubePropContextTranspiler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const transpiledFieldsPatterns: Array<RegExp> = [
2020
/^measures\.[_a-zA-Z][_a-zA-Z0-9]*\.(timeShift|time_shift)\.[0-9]+\.(timeDimension|time_dimension)$/,
2121
/^measures\.[_a-zA-Z][_a-zA-Z0-9]*\.(reduceBy|reduce_by|groupBy|group_by|addGroupBy|add_group_by)$/,
2222
/^dimensions\.[_a-zA-Z][_a-zA-Z0-9]*\.(reduceBy|reduce_by|groupBy|group_by|addGroupBy|add_group_by)$/,
23+
/^dimensions\.[_a-zA-Z][_a-zA-Z0-9]*\.case\.switch$/,
2324
/^(preAggregations|pre_aggregations)\.[_a-zA-Z][_a-zA-Z0-9]*\.indexes\.[_a-zA-Z][_a-zA-Z0-9]*\.columns$/,
2425
/^(preAggregations|pre_aggregations)\.[_a-zA-Z][_a-zA-Z0-9]*\.(timeDimensionReference|timeDimension|time_dimension|segments|dimensions|measures|rollups|segmentReferences|dimensionReferences|measureReferences|rollupReferences)$/,
2526
/^(preAggregations|pre_aggregations)\.[_a-zA-Z][_a-zA-Z0-9]*\.(timeDimensions|time_dimensions)\.\d+\.dimension$/,

packages/cubejs-schema-compiler/test/integration/postgres/calc-groups.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ cubes:
5454
- name: test
5555
type: string
5656
case:
57+
switch: "{CUBE}.currency"
5758
when:
58-
- sql: "{CUBE.currency} = 'USD'"
59-
label: '111'
60-
- sql: "{CUBE.currency} = 'EUR'"
61-
label: '333'
59+
- value: USD
60+
sql: "'111'"
61+
- value: EUR
62+
sql: "'333'"
6263
else:
63-
label: 'def'
64+
sql: "'def'"
6465
6566
- name: strategy
6667
type: switch
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::cube_bridge::member_sql::{MemberSql, NativeMemberSql};
2+
3+
use super::case_label::CaseLabel;
4+
use cubenativeutils::wrappers::serializer::{
5+
NativeDeserialize, NativeDeserializer, NativeSerialize,
6+
};
7+
use cubenativeutils::wrappers::NativeContextHolder;
8+
use cubenativeutils::wrappers::NativeObjectHandle;
9+
use cubenativeutils::CubeError;
10+
use std::any::Any;
11+
use std::rc::Rc;
12+
13+
#[nativebridge::native_bridge]
14+
pub trait CaseSwitchElseItem {
15+
#[nbridge(field)]
16+
fn sql(&self) -> Result<Rc<dyn MemberSql>, CubeError>;
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use super::case_label::CaseLabel;
2+
use super::member_sql::{MemberSql, NativeMemberSql};
3+
use cubenativeutils::wrappers::serializer::{
4+
NativeDeserialize, NativeDeserializer, NativeSerialize,
5+
};
6+
use cubenativeutils::wrappers::NativeContextHolder;
7+
use cubenativeutils::wrappers::NativeObjectHandle;
8+
use cubenativeutils::CubeError;
9+
use std::any::Any;
10+
use std::rc::Rc;
11+
12+
#[nativebridge::native_bridge]
13+
pub trait CaseSwitchItem {
14+
#[nbridge(field)]
15+
fn value(&self) -> Result<String, CubeError>;
16+
#[nbridge(field)]
17+
fn sql(&self) -> Result<Rc<dyn MemberSql>, CubeError>;
18+
}

rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pub mod case_definition;
44
pub mod case_else_item;
55
pub mod case_item;
66
pub mod case_label;
7+
pub mod case_switch_else_item;
8+
pub mod case_switch_item;
79
pub mod cube_definition;
810
pub mod dimension_definition;
911
pub mod driver_tools;

0 commit comments

Comments
 (0)