Skip to content

Commit

Permalink
Addressed comments 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidwys committed Nov 29, 2018
1 parent a61d6f9 commit 322a43a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
14 changes: 8 additions & 6 deletions docs/dev/table/streaming/match_recognize.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,13 @@ If a condition is not defined for a pattern variable, a default condition will b

For a more detailed explanation about expressions that can be used in those clauses, please have a look at the [event stream navigation](#pattern-navigation) section.

### Scalar & Aggregate functions
### Aggregations

One can use scalar and aggregate functions in those clauses, both [built-in]({{ site.baseurl }}/dev/table/sql.html#built-in-functions) as well as provide [user defined]({{ site.baseurl }}/dev/table/udfs.html) functions.
Aggregations can be used in `DEFINE` and `MEASURES` clauses. Both [built-in]({{ site.baseurl }}/dev/table/sql.html#built-in-functions) and custom [user defined]({{ site.baseurl }}/dev/table/udfs.html) functions are supported.

Aggregate functions are applied to subset of rows mapped to a match. To understand how those subsets are evaluated have a look at the [event stream navigation](#pattern-navigation) section.
Aggregate functions are applied to each subset of rows mapped to a match. In order to understand how those subsets are evaluated have a look at the [event stream navigation](#pattern-navigation) section.

With a task to find the longest period of time for which the average price of a ticker did not go below certain threshold, one can see how expressible `MATCH_RECOGNIZE` can become with aggregations.
The task of the following example is to find the longest period of time for which the average price of a ticker did not go below certain threshold. It shows how expressible `MATCH_RECOGNIZE` can become with aggregations.
This task can be performed with the following query:

{% highlight sql %}
Expand Down Expand Up @@ -256,8 +256,8 @@ symbol rowtime price tax
'ACME' '01-Apr-11 10:00:10' 30 1
{% endhighlight %}

The query will accumulate events as part of `A` pattern variable as long as the average price of them does not exceed 15. Which will happen at `01-Apr-11 10:00:04`. The next such period that starts then will
exceed average price of 15 at `01-Apr-11 10:00:10`. Thus the results for said query will be:
The query will accumulate events as part of the pattern variable `A` as long as the average price of them does not exceed `15`. For example, such a limit exceeding happens at `01-Apr-11 10:00:04`.
The following period exceeds the average price of `15` again at `01-Apr-11 10:00:10`. Thus the results for said query will be:

{% highlight text %}
symbol start_tstamp end_tstamp avgPrice
Expand All @@ -266,6 +266,8 @@ ACME 01-APR-11 10:00:00 01-APR-11 10:00:03 14.5
ACME 01-APR-11 10:00:04 01-APR-11 10:00:09 13.5
{% endhighlight %}

<span class="label label-info">Note</span> Aggregations can be applied to expressions, but only if they reference a single pattern variable. Thus `SUM(A.price * A.tax)` is a valid one, but `AVG(A.price * B.tax)` is not.

An important thing to have in mind is how aggregates behave in situation when no rows where mapped to certain pattern variable. Every aggregate, beside `COUNT` will produce `null` in those cases. `COUNT` on the other hand will
produce 0.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@

/*
* THIS FILE HAS BEEN COPIED FROM THE APACHE CALCITE PROJECT UNTIL CALCITE-2707 IS FIXED.
* (Added lines: 5937-5943)
*/

/**
Expand Down Expand Up @@ -5934,11 +5935,13 @@ private static class NavigationExpander extends NavigationModifier {
List<SqlNode> operands = call.getOperandList();
List<SqlNode> newOperands = new ArrayList<>();

// This code is a workaround for CALCITE-2707
if (call.getFunctionQuantifier() != null
&& call.getFunctionQuantifier().getValue() == SqlSelectKeyword.DISTINCT) {
final SqlParserPos pos = call.getParserPosition();
throw SqlUtil.newContextException(pos, Static.RESOURCE.functionQuantifierNotAllowed(call.toString()));
}
// This code is a workaround for CALCITE-2707

if (isLogicalNavigation(kind) || isPhysicalNavigation(kind)) {
SqlNode inner = operands.get(0);
Expand Down

0 comments on commit 322a43a

Please sign in to comment.