Skip to content

Commit

Permalink
Add analytic functions.
Browse files Browse the repository at this point in the history
* ROW_NUMBER
* RANK
* DENSE_RANK
  • Loading branch information
mithrandie committed Jul 2, 2017
1 parent b207718 commit 4b8d9fe
Show file tree
Hide file tree
Showing 21 changed files with 3,920 additions and 2,334 deletions.
1 change: 1 addition & 0 deletions docs/_includes/side_menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<li><a href="{{ '/reference/cryptographic-hash-functions.html' | relative_url }}">Cryptographic Hash Functions</a></li>
<li><a href="{{ '/reference/cast-functions.html' | relative_url }}">Cast Functions</a></li>
<li><a href="{{ '/reference/system-functions.html' | relative_url }}">System Functions</a></li>
<li><a href="{{ '/reference/analytic-functions.html' | relative_url }}">Analytic Functions</a></li>
</ul>
</div>
</li>
Expand Down
89 changes: 89 additions & 0 deletions docs/_posts/2006-01-02-analytic-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
layout: default
title: Analytic Functions - Reference Manual - csvq
category: reference
---

# Analytic Functions

Analytic functions calculate values of groups.
Analytic Functions can be used only in [Select Clause]({{ '/reference/select-query.html#select_clause' | relative_url }}) and [Order By Clause]({{ '/reference/select-query.html#order_by_clause' | relative_url }})

| name | description |
| :- | :- |
| [ROW_NUMBER](#row_number) | Return sequential numbers |
| [RANK](#rank) | Return ranks |
| [DENSE_RANK](#dense_rank) | Return ranks without any gaps in the ranking |

## Syntax
{: #syntax}

```sql
analytic_function
: function_name([args]) OVER ([partition_clause] [order_by_clause])

args
: value [, value ...]

partition_clause
: PARTITION BY value [, value ...]
```

_value_
: [value]({{ '/reference/value.html' | relative_url }})

_order_by_clause_
: [Order By Clause]({{ '/reference/select_query.html#order_by_clause' | relative_url }})

Analytic Functions sort the result set by _order_by_clause_ and calculate values within each of groups partitioned by _partition_clause_.
If there is no _partition_clause_, then all records of result set are dealt with as one group.

## Definitions

### ROW_NUMBER
{: #row_number}

```
ROW_NUMBER() OVER ([partition_clause] [order_by_clause])
```

_partition_clause_
: [Partition Clause](#syntax)

_order_by_clause_
: [Order By Clause]({{ '/reference/select_query.html#order_by_clause' | relative_url }})

Return sequential numbers of records in a group.


### RANK
{: #rank}

```
RANK() OVER ([partition_clause] [order_by_clause])
```

_partition_clause_
: [Partition Clause](#syntax)

_order_by_clause_
: [Order By Clause]({{ '/reference/select_query.html#order_by_clause' | relative_url }})

Returns ranks of records in a group.


### DENSE_RANK
{: #dense_rank}

```
DENSE_RANK() OVER ([partition_clause] [order_by_clause])
```

_partition_clause_
: [Partition Clause](#syntax)

_order_by_clause_
: [Order By Clause]({{ '/reference/select_query.html#order_by_clause' | relative_url }})

Returns ranks of records without any gaps in the ranking in a group.

4 changes: 2 additions & 2 deletions docs/_posts/2006-01-02-statement.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ IF IN INNER INSERT INTERSECT INTO IS
JOIN
LAST LEFT LIKE LIMIT
NATURAL NOT NULL NULLS
OFFSET ON OPEN OR ORDER OUTER
PERCENT PRINT
OFFSET ON OPEN OR ORDER OUTER OVER
PARTITION PERCENT PRINT
RENAME RIGHT ROLLBACK
SELECT SET SEPARATOR STDIN
TABLE THEN TIES TO
Expand Down
1 change: 1 addition & 0 deletions docs/_posts/2006-01-02-value.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ user.id -- table name and column name
* [Cryptographic Hash Functions]({{ '/reference/cryptographic-hash-functions.html' | relative_url }})
* [Cast Functions]({{ '/reference/cast-functions.html' | relative_url }})
* [System Functions]({{ '/reference/system-functions.html' | relative_url }})
* [Analytic Functions]({{ '/reference/analytic-functions.html' | relative_url }})

### Subquery
{: #subquery}
Expand Down
1 change: 1 addition & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ title: Reference Manual - csvq
* [Cryptographic Hash Functions]({{ '/reference/cryptographic-hash-functions.html' | relative_url }})
* [Cast Functions]({{ '/reference/cast-functions.html' | relative_url }})
* [System Functions]({{ '/reference/system-functions.html' | relative_url }})
* [Analytic Functions]({{ '/reference/analytic-functions.html' | relative_url }})
8 changes: 6 additions & 2 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference.html</loc>
<lastmod>2017-06-29T17:08:49+00:00</lastmod>
<lastmod>2017-07-02T23:29:47+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/install.html</loc>
Expand All @@ -26,7 +26,7 @@
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/value.html</loc>
<lastmod>2017-06-29T17:08:49+00:00</lastmod>
<lastmod>2017-07-02T23:29:47+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/select-query.html</loc>
Expand Down Expand Up @@ -136,6 +136,10 @@
<loc>https://mithrandie.github.io/csvq/reference/system-functions.html</loc>
<lastmod>2017-06-29T17:08:49+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/analytic-functions.html</loc>
<lastmod>2017-07-02T23:29:47+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/license.html</loc>
<lastmod>2017-06-29T17:08:49+00:00</lastmod>
Expand Down
66 changes: 64 additions & 2 deletions lib/parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,14 +918,14 @@ func (si Stdin) String() string {
}

type OrderItem struct {
Item Expression
Value Expression
Direction Token
Nulls string
Position Token
}

func (e OrderItem) String() string {
s := []string{e.Item.String()}
s := []string{e.Value.String()}
if !e.Direction.IsEmpty() {
s = append(s, e.Direction.Literal)
}
Expand Down Expand Up @@ -1007,6 +1007,68 @@ func (gc GroupConcat) String() string {
return gc.GroupConcat + "(" + joinWithSpace(s) + ")"
}

type AnalyticFunction struct {
Name string
Option Option
Over string
AnalyticClause AnalyticClause
}

func (e AnalyticFunction) String() string {
s := []string{
e.Name + "(" + e.Option.String() + ")",
e.Over,
"(" + e.AnalyticClause.String() + ")",
}
return joinWithSpace(s)
}

type AnalyticClause struct {
Partition Expression
OrderByClause Expression
}

func (e AnalyticClause) String() string {
s := []string{}
if e.Partition != nil {
s = append(s, e.Partition.String())
}
if e.OrderByClause != nil {
s = append(s, e.OrderByClause.String())
}
return joinWithSpace(s)
}

func (e AnalyticClause) PartitionValues() []Expression {
if e.Partition == nil {
return nil
}
return e.Partition.(Partition).Values
}

func (e AnalyticClause) OrderValues() []Expression {
if e.OrderByClause == nil {
return nil
}

items := e.OrderByClause.(OrderByClause).Items
result := make([]Expression, len(items))
for i, v := range items {
result[i] = v.(OrderItem).Value
}
return result
}

type Partition struct {
PartitionBy string
Values []Expression
}

func (e Partition) String() string {
s := []string{e.PartitionBy, listExpressions(e.Values)}
return joinWithSpace(s)
}

type Variable struct {
Name string
}
Expand Down
Loading

0 comments on commit 4b8d9fe

Please sign in to comment.