Skip to content

Put default value within aggregate #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion query_language_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ Query:

```
find {foo: =="group2"}
return [group(.baz order=asc) default="a", group(.bar order=desc) default="c", count()];
return [group(.baz default="a" order=asc), group(.bar default="c" order=desc), count()];
```

Results:
Expand Down
6 changes: 3 additions & 3 deletions repl-tests/group.noise
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ add {"_id":"13", "foo":"group", "baz": "c"};
"13"

find {foo: =="group"}
return {max: max(.bar) default=120};
return {max: max(.bar default=120)};
[
{"max":120}
]

find {foo: =="group"}
return {max: max(.bar) default=1};
return {max: max(.bar default=1)};
[
{"max":3}
]
Expand Down Expand Up @@ -167,7 +167,7 @@ add {"_id":"9", "foo":"group3", "baz": "a", "bar": "f"};
"9"

find {foo: =="group3"}
return [group(.baz order=asc) default="a", group(.bar order=desc) default="c", count()];
return [group(.baz default="a" order=asc), group(.bar default="c" order=desc), count()];
[
["a","f",1],
["a","c",1],
Expand Down
14 changes: 10 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ struct Aggregate {
keypath: ReturnPath,
// The concat aggregate has an additional "sep" parameter
sep: Option<JsonValue>,
// The default default value is always `null` hence we don't need an option type here
default: JsonValue,
}

pub struct Parser<'a, 'c> {
Expand Down Expand Up @@ -242,12 +244,14 @@ impl<'a, 'c> Parser<'a, 'c> {
fun: aggregate_fun,
bind_name: None,
keypath: ReturnPath::new(),
sep: None
sep: None,
default: JsonValue::Null,
}))
} else if aggregate_fun == AggregateFun::Concat {
let bind_name_option = self.consume_field();

if let Some(rp) = try!(self.consume_keypath()) {
let default = self.consume_default()?.unwrap_or(JsonValue::Null);
let sep = if self.consume("sep") {
try!(self.must_consume("="));
JsonValue::String(try!(self.must_consume_string_literal()))
Expand All @@ -260,6 +264,7 @@ impl<'a, 'c> Parser<'a, 'c> {
bind_name: bind_name_option,
keypath: rp,
sep: Some(sep),
default: default,
}))
} else {
Err(Error::Parse("Expected keypath or bind variable".to_string()))
Expand All @@ -268,6 +273,7 @@ impl<'a, 'c> Parser<'a, 'c> {
let bind_name_option = self.consume_field();

if let Some(rp) = try!(self.consume_keypath()) {
let default = self.consume_default()?.unwrap_or(JsonValue::Null);
if self.consume("order") {
try!(self.must_consume("="));
if self.consume("asc") {
Expand All @@ -285,6 +291,7 @@ impl<'a, 'c> Parser<'a, 'c> {
bind_name: bind_name_option,
keypath: rp,
sep: None,
default: default,
}))
} else {
Err(Error::Parse("Expected keypath or bind variable".to_string()))
Expand Down Expand Up @@ -1135,19 +1142,18 @@ impl<'a, 'c> Parser<'a, 'c> {
}

if let Some(aggregate) = try!(self.consume_aggregate()) {
let default = self.consume_default()?.unwrap_or(JsonValue::Null);
match aggregate.bind_name {
Some(bind_name) => Ok(Some(Box::new(RetBind {
bind_name: bind_name,
extra_rp: aggregate.keypath,
ag: Some((aggregate.fun, aggregate.sep)),
default: default,
default: aggregate.default,
order_info: None,
}))),
None => Ok(Some(Box::new(RetValue {
rp: aggregate.keypath,
ag: Some((aggregate.fun, aggregate.sep)),
default: default,
default: aggregate.default,
order_info: None,
}))),
}
Expand Down