Skip to content

Commit

Permalink
atlas: aggr function with empty tags
Browse files Browse the repository at this point in the history
Uses a placeholder name of `unknown` if the query for an
aggregate function has no exact tags. This matches the
behavior of the backend.
  • Loading branch information
brharrington committed Dec 2, 2023
1 parent 3fdaece commit f26ef2c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ final class All implements DataExpr {

/** Base type for simple aggregate functions. */
interface AggregateFunction extends DataExpr {

/** Return the exact matches from the query clause. */
Map<String, String> queryTags();

@Override default Map<String, String> resultTags(Map<String, String> tags) {
Map<String, String> ts = queryTags();
return ts.isEmpty()
? Collections.singletonMap("name", "unknown")
: ts;
}
}

/**
Expand All @@ -202,7 +212,7 @@ final class Sum implements AggregateFunction {
return true;
}

@Override public Map<String, String> resultTags(Map<String, String> tags) {
@Override public Map<String, String> queryTags() {
return queryTags;
}

Expand Down Expand Up @@ -269,7 +279,7 @@ final class Min implements AggregateFunction {
return false;
}

@Override public Map<String, String> resultTags(Map<String, String> tags) {
@Override public Map<String, String> queryTags() {
return queryTags;
}

Expand Down Expand Up @@ -336,7 +346,7 @@ final class Max implements AggregateFunction {
return false;
}

@Override public Map<String, String> resultTags(Map<String, String> tags) {
@Override public Map<String, String> queryTags() {
return queryTags;
}

Expand Down Expand Up @@ -407,7 +417,7 @@ final class Count implements AggregateFunction {
return true;
}

@Override public Map<String, String> resultTags(Map<String, String> tags) {
@Override public Map<String, String> queryTags() {
return queryTags;
}

Expand Down Expand Up @@ -496,7 +506,7 @@ private Map<String, String> keyTags(Map<String, String> tags) {
if (resultTags == null) {
return null;
} else {
resultTags.putAll(af.resultTags(tags));
resultTags.putAll(af.queryTags());
return resultTags;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,20 @@ public void mismatchedClosingParen() {
() -> Parser.parseDataExpr("key,(,key,),),:in,:sum"));
}

@Test
public void orClauseTags() {
DataExpr expr = parse("name,foo,:eq,name,bar,:eq,:or,:sum");
Map<String, String> tags = expr.resultTags(Collections.singletonMap("name", "foo"));
Assertions.assertEquals(Collections.singletonMap("name", "unknown"), tags);
}

@Test
public void orClauseTagsGroupBy() {
DataExpr expr = parse("name,foo,:eq,name,bar,:eq,:or,:sum,(,name,),:by");
Map<String, String> tags = expr.resultTags(Collections.singletonMap("name", "foo"));
Assertions.assertEquals(Collections.singletonMap("name", "foo"), tags);
}

@Test
public void allEqualsContract() {
EqualsVerifier
Expand Down

0 comments on commit f26ef2c

Please sign in to comment.