Skip to content

Commit 87727ea

Browse files
committed
base query options yaml
1 parent a21efc9 commit 87727ea

File tree

4 files changed

+395
-8
lines changed

4 files changed

+395
-8
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#[macro_use]
22
mod macros;
33

4-
mod yaml;
4+
pub mod yaml;
55

6-
mod base_query_options;
6+
pub mod base_query_options;
77
mod mock_base_tools;
88
mod mock_case_definition;
99
mod mock_case_else_item;
@@ -34,9 +34,7 @@ mod mock_sql_utils;
3434
mod mock_struct_with_sql_member;
3535
mod mock_timeshift_definition;
3636

37-
pub use base_query_options::{
38-
filter_and, filter_item, filter_or, members_from_strings, MockBaseQueryOptions,
39-
};
37+
pub use base_query_options::{members_from_strings, MockBaseQueryOptions};
4038
pub use mock_base_tools::MockBaseTools;
4139
pub use mock_case_definition::MockCaseDefinition;
4240
pub use mock_case_else_item::MockCaseElseItem;
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
use crate::cube_bridge::base_query_options::{FilterItem, OrderByItem};
2+
use serde::de;
3+
use serde::{Deserialize, Deserializer};
4+
5+
#[derive(Debug, Deserialize)]
6+
pub struct YamlBaseQueryOptions {
7+
#[serde(default)]
8+
pub measures: Option<Vec<String>>,
9+
#[serde(default)]
10+
pub dimensions: Option<Vec<String>>,
11+
#[serde(default)]
12+
pub segments: Option<Vec<String>>,
13+
#[serde(default)]
14+
pub order: Option<Vec<YamlOrderByItem>>,
15+
#[serde(default)]
16+
pub filters: Option<Vec<YamlFilterItem>>,
17+
#[serde(default)]
18+
pub limit: Option<String>,
19+
#[serde(default)]
20+
pub row_limit: Option<String>,
21+
#[serde(default)]
22+
pub offset: Option<String>,
23+
#[serde(default)]
24+
pub ungrouped: Option<bool>,
25+
#[serde(default)]
26+
pub export_annotated_sql: Option<bool>,
27+
#[serde(default)]
28+
pub pre_aggregation_query: Option<bool>,
29+
#[serde(default)]
30+
pub total_query: Option<bool>,
31+
#[serde(default)]
32+
pub cubestore_support_multistage: Option<bool>,
33+
#[serde(default)]
34+
pub disable_external_pre_aggregations: Option<bool>,
35+
}
36+
37+
#[derive(Debug, Deserialize)]
38+
pub struct YamlOrderByItem {
39+
pub id: String,
40+
#[serde(default)]
41+
pub desc: Option<bool>,
42+
}
43+
44+
impl YamlOrderByItem {
45+
pub fn into_order_by_item(self) -> OrderByItem {
46+
OrderByItem {
47+
id: self.id,
48+
desc: self.desc,
49+
}
50+
}
51+
}
52+
53+
#[derive(Debug)]
54+
pub enum YamlFilterItem {
55+
Group(YamlFilterGroup),
56+
Base(YamlBaseFilter),
57+
}
58+
59+
impl<'de> Deserialize<'de> for YamlFilterItem {
60+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
61+
where
62+
D: Deserializer<'de>,
63+
{
64+
let value = serde_yaml::Value::deserialize(deserializer)?;
65+
66+
// Check if it has 'or' or 'and' keys - then it's a Group
67+
if let serde_yaml::Value::Mapping(ref map) = value {
68+
let has_or = map.contains_key(&serde_yaml::Value::String("or".to_string()));
69+
let has_and = map.contains_key(&serde_yaml::Value::String("and".to_string()));
70+
71+
if has_or || has_and {
72+
return serde_yaml::from_value::<YamlFilterGroup>(value)
73+
.map(YamlFilterItem::Group)
74+
.map_err(de::Error::custom);
75+
}
76+
}
77+
78+
// Otherwise it's a Base filter
79+
serde_yaml::from_value::<YamlBaseFilter>(value)
80+
.map(YamlFilterItem::Base)
81+
.map_err(de::Error::custom)
82+
}
83+
}
84+
85+
#[derive(Debug, Deserialize)]
86+
pub struct YamlFilterGroup {
87+
#[serde(default)]
88+
pub or: Option<Vec<YamlFilterItem>>,
89+
#[serde(default)]
90+
pub and: Option<Vec<YamlFilterItem>>,
91+
}
92+
93+
#[derive(Debug, Deserialize)]
94+
pub struct YamlBaseFilter {
95+
#[serde(default)]
96+
pub member: Option<String>,
97+
#[serde(default)]
98+
pub dimension: Option<String>,
99+
#[serde(default)]
100+
pub operator: Option<String>,
101+
#[serde(default)]
102+
pub values: Option<Vec<Option<String>>>,
103+
}
104+
105+
impl YamlFilterItem {
106+
pub fn into_filter_item(self) -> FilterItem {
107+
match self {
108+
YamlFilterItem::Group(group) => FilterItem {
109+
or: group.or.map(|items| {
110+
items
111+
.into_iter()
112+
.map(|item| item.into_filter_item())
113+
.collect()
114+
}),
115+
and: group.and.map(|items| {
116+
items
117+
.into_iter()
118+
.map(|item| item.into_filter_item())
119+
.collect()
120+
}),
121+
member: None,
122+
dimension: None,
123+
operator: None,
124+
values: None,
125+
},
126+
YamlFilterItem::Base(base) => FilterItem {
127+
or: None,
128+
and: None,
129+
member: base.member,
130+
dimension: base.dimension,
131+
operator: base.operator,
132+
values: base.values,
133+
},
134+
}
135+
}
136+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
pub mod base_query_options;
12
pub mod case;
23
pub mod dimension;
34
pub mod measure;
45
pub mod schema;
56
pub mod segment;
67
pub mod timeshift;
78

9+
pub use base_query_options::YamlBaseQueryOptions;
810
pub use dimension::YamlDimensionDefinition;
911
pub use measure::YamlMeasureDefinition;
1012
pub use schema::YamlSchema;

0 commit comments

Comments
 (0)