Skip to content

Commit 0a0cac5

Browse files
authored
3.0 distinct (#392)
## Usage and product changes Added distinct stage. ## Implementation Added keyword to grammar, parsing, and operator enum.
2 parents 0f1fbbc + 41ff8ae commit 0a0cac5

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

rust/common/token.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ string_enum! { Operator
7373
Limit = "limit",
7474
Reduce = "reduce",
7575
Require = "require",
76+
Distinct = "distinct",
7677
}
7778

7879
string_enum! { LogicOperator

rust/parser/pipeline.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::{
3030
stage::{
3131
delete::{Deletable, DeletableKind},
3232
fetch::FetchSome,
33-
modifier::{Limit, Offset, OrderedVariable, Select, Sort},
33+
modifier::{Distinct, Limit, Offset, OrderedVariable, Require, Select, Sort},
3434
reduce::{Count, Reducer, Stat},
3535
Delete, Fetch, Insert, Match, Operator, Put, Reduce, Stage, Update,
3636
},
@@ -40,7 +40,6 @@ use crate::{
4040
fetch::{
4141
FetchAttribute, FetchList, FetchObject, FetchObjectBody, FetchObjectEntry, FetchSingle, FetchStream,
4242
},
43-
modifier::Require,
4443
reduce::ReduceAssign,
4544
},
4645
Pipeline,
@@ -351,6 +350,7 @@ pub(super) fn visit_operator_stream(node: Node<'_>) -> Operator {
351350
Rule::operator_limit => Operator::Limit(visit_operator_limit(child)),
352351
Rule::operator_reduce => Operator::Reduce(visit_operator_reduce(child)),
353352
Rule::operator_require => Operator::Require(visit_operator_require(child)),
353+
Rule::operator_distinct => Operator::Distinct(visit_operator_distinct(child)),
354354
_ => unreachable!("{}", TypeQLError::IllegalGrammar { input: child.to_string() }),
355355
}
356356
}
@@ -481,3 +481,12 @@ fn visit_operator_require(node: Node<'_>) -> Require {
481481
debug_assert_eq!(children.try_consume_any(), None);
482482
Require::new(span, variables)
483483
}
484+
485+
fn visit_operator_distinct(node: Node<'_>) -> Distinct {
486+
debug_assert_eq!(node.as_rule(), Rule::operator_distinct);
487+
let span = node.span();
488+
let mut children = node.into_children();
489+
let _ = children.skip_expected(Rule::DISTINCT);
490+
debug_assert_eq!(children.try_consume_any(), None);
491+
Distinct::new(span)
492+
}

rust/parser/typeql.pest

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ clause_delete = { DELETE ~ ( statement_deletable ~ SEMICOLON )+ }
3434

3535
// STREAM OPERATORS ===========================================================
3636

37-
operator_stream = { operator_select | operator_sort | operator_offset | operator_limit | operator_require | operator_reduce }
37+
operator_stream = { operator_select | operator_sort | operator_distinct | operator_offset | operator_limit | operator_require | operator_reduce }
3838

3939
operator_select = { SELECT ~ vars ~ SEMICOLON }
4040
operator_sort = { SORT ~ var_order ~ ( COMMA ~ var_order )* ~ SEMICOLON }
4141
operator_offset = { OFFSET ~ integer_literal ~ SEMICOLON }
4242
operator_limit = { LIMIT ~ integer_literal ~ SEMICOLON }
4343
operator_require = { REQUIRE ~ vars ~ SEMICOLON }
44+
operator_distinct = { DISTINCT ~ SEMICOLON }
4445
operator_reduce = { REDUCE ~ reduce_assign ~ ( COMMA ~ reduce_assign )* ~ (GROUPBY ~ vars )? ~ SEMICOLON }
4546

4647
var_order = { var ~ ORDER? }
@@ -413,6 +414,7 @@ LIMIT = @{ "limit" ~ WB }
413414
OFFSET = @{ "offset" ~ WB }
414415
SORT = @{ "sort" ~ WB }
415416
REQUIRE = @{ "require" ~ WB }
417+
DISTINCT = @{ "distinct" ~ WB }
416418

417419
ORDER = ${ ASC | DESC }
418420
ASC = @{ "asc" ~ WB }

rust/query/pipeline/stage/modifier.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,31 @@ impl fmt::Display for Require {
188188
}
189189
}
190190

191+
#[derive(Debug, Clone, Eq, PartialEq)]
192+
pub struct Distinct {
193+
span: Option<Span>,
194+
}
195+
196+
impl Distinct {
197+
pub fn new(span: Option<Span>) -> Self {
198+
Self { span }
199+
}
200+
}
201+
202+
impl Spanned for Distinct {
203+
fn span(&self) -> Option<Span> {
204+
self.span
205+
}
206+
}
207+
208+
impl Pretty for Distinct {}
209+
210+
impl fmt::Display for Distinct {
211+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212+
write!(f, "{};", token::Operator::Distinct)
213+
}
214+
}
215+
191216
#[derive(Debug, Clone, Eq, PartialEq)]
192217
pub enum Operator {
193218
Select(Select),
@@ -196,6 +221,7 @@ pub enum Operator {
196221
Limit(Limit),
197222
Reduce(Reduce),
198223
Require(Require),
224+
Distinct(Distinct),
199225
}
200226

201227
impl Spanned for Operator {
@@ -207,6 +233,7 @@ impl Spanned for Operator {
207233
Self::Limit(inner) => inner.span(),
208234
Self::Reduce(inner) => inner.span(),
209235
Self::Require(inner) => inner.span(),
236+
Self::Distinct(inner) => inner.span(),
210237
}
211238
}
212239
}
@@ -220,6 +247,7 @@ impl Pretty for Operator {
220247
Self::Limit(inner) => Pretty::fmt(inner, indent_level, f),
221248
Self::Reduce(inner) => Pretty::fmt(inner, indent_level, f),
222249
Self::Require(inner) => Pretty::fmt(inner, indent_level, f),
250+
Self::Distinct(inner) => Pretty::fmt(inner, indent_level, f),
223251
}
224252
}
225253
}
@@ -233,6 +261,7 @@ impl fmt::Display for Operator {
233261
Self::Limit(inner) => fmt::Display::fmt(inner, f),
234262
Self::Reduce(inner) => fmt::Display::fmt(inner, f),
235263
Self::Require(inner) => fmt::Display::fmt(inner, f),
264+
Self::Distinct(inner) => fmt::Display::fmt(inner, f),
236265
}
237266
}
238267
}

0 commit comments

Comments
 (0)