Skip to content

Commit

Permalink
Release v0.2.3
Browse files Browse the repository at this point in the history
* Add analytic functions.
* Add NULLS FIRST|LAST expressions in order by clause.
* Add PERCENTAGE and WITH TIES expressions to limit clause.
  • Loading branch information
mithrandie committed Jul 2, 2017
2 parents d60d166 + d20686b commit 25bcfe6
Show file tree
Hide file tree
Showing 45 changed files with 5,496 additions and 2,821 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ ifeq ($(shell command -v glide 2>/dev/null),)
curl https://glide.sh/get | sh
endif

.PHONY: goyacc
goyacc:
ifeq ($(shell command -v goyacc 2>/dev/null),)
go get -u github.com/cznic/goyacc
endif

.PHONY: yacc
yacc: goyacc
cd lib/parser && \
goyacc -o parser.go -v parser.output parser.y && \
cd ../..

.PHONY: test
test:
go test -cover `glide novendor`
Expand Down
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-arithmetic-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ category: reference

# Arithmetic Operators

| name | description |
| operator | description |
| :- | :- |
| + | Addition |
| \- | Subtraction |
Expand All @@ -26,4 +26,4 @@ _float_
An arithmetic operator calculate float values, and return a integer or float value.
If each of operands is not a float value, the value is converted to a float value.

If either of operands is null or conversion to float failed, return null.
If either of operands is null or conversions to float failed, return null.
10 changes: 5 additions & 5 deletions docs/_posts/2006-01-02-comparison-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ category: reference

# Comparison Operators

| name | description |
| operator | description |
| :- | :- |
| [Relational Operators](#relational_operators) | Compare values |
| [IS](#is) | Compare a value with ternary value |
Expand All @@ -22,7 +22,7 @@ A comparison operator returns a ternary value.
## Relational Operators
{: #relational_operators}

| name | description |
| operator | description |
| :- | :- |
| \= | Equal |
| < | Less than |
Expand Down Expand Up @@ -128,7 +128,7 @@ _multiple_fields_subquery_

Check if a _value_ or _row_value_ is in within a set of _values_ or a result set of _select_query_.

A IN operation is equivalent to a ANY operation that operator is specified as "=".
A IN operation is equivalent to a [ANY](#any) operation that _relational_operator_ is specified as "=".

## LIKE
{: #like}
Expand Down Expand Up @@ -185,7 +185,7 @@ If any of comparison results is TRUE, return TRUE.
If there is no TRUE result and there is at least one UNKNOWN result, return UNKNOWN.
Otherwise return FALSE.

If _select_query_ returns no values, return FALSE.
If _select_query_ returns no record, return FALSE.

## ALL
{: #all}
Expand Down Expand Up @@ -218,7 +218,7 @@ If any of comparison results is FALSE, return FALSE.
If there is no FALSE result and there is at least one UNKNOWN result, return UNKNOWN.
Otherwise return TRUE.

If _select_query_ returns no values, return TRUE.
If _select_query_ returns no record, return TRUE.

## Exists
{: #exists}
Expand Down
4 changes: 2 additions & 2 deletions docs/_posts/2006-01-02-datetime-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ category: reference

| name | description |
| :- | :- |
| [NOW](#now) | Return current date and time |
| [NOW](#now) | Return a datetime value of current date and time |
| [DATETIME_FORMAT](#datetime_format) | Format a datetime |
| [YEAR](#year) | Return year of a datetime |
| [MONTH](#month) | Return month of a datetime |
Expand Down Expand Up @@ -48,7 +48,7 @@ NOW()
_return_
: [datetime]({{ '/reference/value.html#datetime' | relative_url }})

Return current date and time.
Return a datetime value of current date and time.

### DATETIME_FORMAT
{: #datetime_format}
Expand Down
2 changes: 1 addition & 1 deletion docs/_posts/2006-01-02-logic-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ category: reference

# Logic Operators

| name | description |
| operator | description |
| :- | :- |
| [AND](#and) | Logical AND |
| [OR](#or) | Logical OR |
Expand Down
6 changes: 3 additions & 3 deletions docs/_posts/2006-01-02-numeric-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ _return_
: [float]({{ '/reference/value.html#float' | relative_url }}) or [integer]({{ '/reference/value.html#integer' | relative_url }})

Round _number_ up to _place_ decimal place.
If _place_ is negative number, _place_ representing a place in integer part,
If _place_ is a negative number, _place_ representing a place in the integer part.

### FLOOR
{: #floor}
Expand Down Expand Up @@ -100,7 +100,7 @@ _return_
: [float]({{ '/reference/value.html#float' | relative_url }}) or [integer]({{ '/reference/value.html#integer' | relative_url }})

Round _number_ down to _place_ decimal place.
If _place_ is negative number, _place_ representing a place in integer part,
If _place_ is a negative number, _place_ representing a place in the integer part.

### ROUND
{: #round}
Expand Down Expand Up @@ -131,7 +131,7 @@ _return_
: [float]({{ '/reference/value.html#float' | relative_url }}) or [integer]({{ '/reference/value.html#integer' | relative_url }})

Round _number_ to _place_ decimal place.
If _place_ is negative number, _place_ representing a place in integer part,
If _place_ is a negative number, _place_ representing a place in the integer part.

### ABS
{: #abs}
Expand Down
39 changes: 24 additions & 15 deletions docs/_posts/2006-01-02-select-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,40 +218,49 @@ ORDER BY order_item [, order_item ...]

```sql
order_item
: field_name
| field_name [ASC|DESC]
: field_name [order_direction] [null_position]

order_direction
: {ASC|DESC}

null_position
: NULLS {FIRST|LAST}
```

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

If DISTINCT keyword is specified in the select clause, you can use only enumerated fields in the select clause as _field_name_.
If DISTINCT keyword is specified in the select clause, you can use only enumerated fields in the select clause as _field_name_.

Default order direction is ASC.
If order direction is not specified, the next order_item's direction is applied.
_order_direction_
: _ASC_ sorts records in ascending order. _DESC_ sorts in descending order. _ASC_ is the default.

```sql
-- Following expressions return same results
ORDER BY column1, column2
ORDER BY column1 ASC, column2 ASC
_null_position_
: _FIRST_ puts null values first. _LAST_ puts null values last.
If _order_direction_ is specified as _ASC_ then _FIRST_ is the default, otherwise _LAST_ is the default.

-- Following expressions return same results
ORDER BY column1, column2 ASC, column3, column4 DESC
ORDER BY column1 ASC, column2 ASC, column3 DESC, column4 DESC
```

## Limit Clause
{: #limit_clause}

The Limit clause is used to specify the maximum number of records to return.

```sql
LIMIT number
limit_clause
: LIMIT number_of_records [WITH TIES]
| LIMIT percent PERCENT [WITH TIES]
```

_number_
_number_of_records_
: [integer]({{ '/reference/value.html#integer' | relative_url }})

_percent_
: [float]({{ '/reference/value.html#integer' | relative_url }})

If _PERCENT_ keyword is specified, maximum number of records is _percent_ percent of the result set that includes the excluded records by _Offset Clause_.

If _WITH TIES_ keywords are specified, all records that have the same sort keys specified by _Order By Clause_ as the last record of the limited records are included in the records to return.
If there is no _Order By Clause_ in the query, _WITH TIES_ keywords are ignored.

## Offset Clause
{: #offset_clause}
Expand Down
4 changes: 2 additions & 2 deletions docs/_posts/2006-01-02-set-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ category: reference

# Set Operators

| name | description |
| operator | description |
| :- | :- |
| [UNION](#union) | Return the union of result sets |
| [EXCEPT](#except) | Return the relative complement of result sets |
Expand Down Expand Up @@ -37,7 +37,7 @@ select_query EXCEPT [ALL] select_query
_select_query_
: [select_set_entity]({{ '/reference/select-query.html' | relative_url }})

Return records of a first result set that do not appear in a second result set.
Return records of the result set of the left-hand side query that do not appear in the result set of the right-hand side query.

## INTERSECT
{: #intersect}
Expand Down
8 changes: 4 additions & 4 deletions docs/_posts/2006-01-02-statement.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ HAVING
IF IN INNER INSERT INTERSECT INTO IS
JOIN
LAST LEFT LIKE LIMIT
NATURAL NOT NULL
OFFSET ON OPEN OR ORDER OUTER
PRINT
NATURAL NOT NULL NULLS
OFFSET ON OPEN OR ORDER OUTER OVER
PARTITION PERCENT PRINT
RENAME RIGHT ROLLBACK
SELECT SET SEPARATOR STDIN
TABLE THEN TO
TABLE THEN TIES TO
UNION UPDATE USING
VALUES VAR
WHEN WHERE WHILE WITH
Expand Down
2 changes: 1 addition & 1 deletion docs/_posts/2006-01-02-string-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ category: reference

# String Operators

| name | description |
| operator | description |
| :- | :- |
| \|\| | Concatnation |

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 }})
Loading

0 comments on commit 25bcfe6

Please sign in to comment.