Skip to content

Commit

Permalink
Release v0.2.1
Browse files Browse the repository at this point in the history
* Add set operations.
  • Loading branch information
mithrandie committed Jun 24, 2017
2 parents 92acc0e + c2bee09 commit b58e623
Show file tree
Hide file tree
Showing 32 changed files with 7,266 additions and 5,773 deletions.
1 change: 1 addition & 0 deletions docs/_includes/side_menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<li><a href="{{ '/reference/comparison-operators.html' | relative_url }}">Comparison Operators</a></li>
<li><a href="{{ '/reference/logic-operators.html' | relative_url }}">Logic Operators</a></li>
<li><a href="{{ '/reference/string-operators.html' | relative_url }}">String Operators</a></li>
<li><a href="{{ '/reference/set-operators.html' | relative_url }}">Set Operators</a></li>
</ul>
</div>
</li>
Expand Down
2 changes: 1 addition & 1 deletion docs/_posts/2006-01-02-arithmetic-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ category: reference
# Arithmetic Operators

| name | description |
| :-: | :- |
| :- | :- |
| + | Addition |
| \- | Subtraction |
| * | Multiplication |
Expand Down
6 changes: 3 additions & 3 deletions docs/_posts/2006-01-02-comparison-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ category: reference
# Comparison Operators

| name | description |
| :-: | :- |
| :- | :- |
| [Relational Operators](#relational_operators) | Compare values |
| [IS](#is) | Compare a value with ternary value |
| [BETWEEN](#between) | Check if a value is with in a range of values |
Expand All @@ -23,7 +23,7 @@ A comparison operator returns a ternary value.
{: #relational_operators}

| name | description |
| :-: | :- |
| :- | :- |
| \= | Equal |
| < | Less than |
| <\= | Less than or equal |
Expand All @@ -36,7 +36,7 @@ value operator value
```

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

At first, a relational operator attempt to convert both of operands to float values, and if both convertions are successful then compare them.
If conversions failed, next a relational operater attempt to convert to datetime, and next to boolean, at last to string.
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 @@ -7,7 +7,7 @@ category: reference
# Logic Operators

| name | description |
| :-: | :- |
| :- | :- |
| [AND](#and) | Logical AND |
| [OR](#or) | Logical OR |
| [NOT](#not) | Logical NOT |
Expand Down
3 changes: 3 additions & 0 deletions docs/_posts/2006-01-02-operator-precedence.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ The following table list operators from highest precedence to lowest.
| 5 | [NOT]({{ '/reference/logic-operators.html#not' | relative_url }}) | Right-to-left |
| 6 | [AND]({{ '/reference/logic-operators.html#and' | relative_url }}) | Left-to-right |
| 7 | [OR]({{ '/reference/logic-operators.html#or' | relative_url }}) | Left-to-right |
| 8 | [INTERSECT]({{ '/reference/set-operators.html#intersect' | relative_url }}) | Left-to-right |
| 9 | [UNION]({{ '/reference/set-operators.html#union' | relative_url }}) | Left-to-right |
| | [EXCEPT]({{ '/reference/set-operators.html#except' | relative_url }}) | Left-to-right |

49 changes: 35 additions & 14 deletions docs/_posts/2006-01-02-select-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,43 @@ category: reference
Select query is used to retrieve data from csv files.

```
select_clause
[from_clause]
[where_clause]
[group_by_clause]
[having_clause]
[order_by_clause]
[limit_clause]
select_query
: select_entity
[order_by_clause]
[limit_clause]
select_entity
: select_clause
[from_clause]
[where_clause]
[group_by_clause]
[having_clause]
| select_entity set_operator [ALL] select_entity
```

* [Select Clause](#select_clause)
* [From Clause](#from_clause)
* [Where Clause](#where_clause)
* [Group By Clause](#group_by_clause)
* [Having Clause](#having_clause)
* [Order By Clause](#order_by_clause)
* [Limit Clause](#limit_clause)
_select_clause_
: [Select Clause](#select_clause)

_from_clause_
: [From Clause](#from_clause)

_where_clause_
: [Where Clause](#where_clause)

_group_by_clause_
: [Group By Clause](#group_by_clause)

_having_clause_
: [Having Clause](#having_clause)

_order_by_clause_
: [Order By Clause](#order_by_clause)

_limit_clause_
: [Limit Clause](#limit_clause)

_set_operator_
: [Set Operators]({{ '/reference/set-operators.html' | relative_url }})


## Select Clause
Expand Down
52 changes: 52 additions & 0 deletions docs/_posts/2006-01-02-set-operators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
layout: default
title: Set Operators - Reference Manual - csvq
category: reference
---

# Set Operators

| name | description |
| :- | :- |
| [UNION](#union) | Return the union of result sets |
| [EXCEPT](#except) | Return the relative complement of result sets |
| [INTERSECT](#intersect) | Return the intersection of result sets |

A set operation combines result sets retrieved by select queries into a single result set.
If the ALL keyword is specified, the result is distinguished.

## UNION
{: #union}

```sql
select_query UNION [ALL] select_query
```

_select_query_
: [select_entity]({{ '/reference/select-query.html' | relative_url }})

Return all records of both result sets.

## EXCEPT
{: #except}

```sql
select_query EXCEPT [ALL] select_query
```

_select_query_
: [select_entity]({{ '/reference/select-query.html' | relative_url }})

Return records of a first result set that do not appear in a second result set.

## INTERSECT
{: #intersect}

```sql
select_query INTERSECT [ALL] select_query
```

_select_query_
: [select_entity]({{ '/reference/select-query.html' | relative_url }})

Return only records that appear in both result sets.
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 @@ -132,11 +132,11 @@ ADD AFTER ALTER ALL AND ANY AS ASC
BEFORE BETWEEN BREAK BY
CASE COMMIT CREATE CLOSE CONTINUE CROSS CURSOR
DECLARE DEFAULT DELETE DESC DISPOSE DISTINCT DO DROP DUAL
ELSE ELSEIF END EXISTS EXIT
ELSE ELSEIF END EXCEPT EXISTS EXIT
FETCH FIRST FOR FROM FULL
GROUP GROUP_CONCAT
HAVING
IF IN INNER INSERT INTO IS
IF IN INNER INSERT INTERSECT INTO IS
JOIN
LAST LEFT LIKE LIMIT
NATURAL NOT NULL
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 @@ -7,7 +7,7 @@ category: reference
# String Operators

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

## Syntax
Expand Down
1 change: 1 addition & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ title: Reference Manual - csvq
* [Comparison Operators]({{ '/reference/comparison-operators.html' | relative_url }})
* [Logic Operators]({{ '/reference/logic-operators.html' | relative_url }})
* [String Operators]({{ '/reference/string-operators.html' | relative_url }})
* [Set Operators]({{ '/reference/set-operators.html' | relative_url }})
* Functions
* [Aggregate Functions]({{ '/reference/aggregate-functions.html' | relative_url }})
* [Logical Functions]({{ '/reference/logical-functions.html' | relative_url }})
Expand Down
6 changes: 3 additions & 3 deletions lib/action/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ func Calc(expr string) error {
if err != nil {
return err
}
selectQuery, _ := program[0].(parser.SelectQuery)
selectEntity, _ := program[0].(parser.SelectQuery).SelectEntity.(parser.SelectEntity)

view := query.NewView()
err = view.Load(selectQuery.FromClause.(parser.FromClause), nil)
err = view.Load(selectEntity.FromClause.(parser.FromClause), nil)
if err != nil {
return err
}

clause := selectQuery.SelectClause.(parser.SelectClause)
clause := selectEntity.SelectClause.(parser.SelectClause)

var filter query.Filter = []query.FilterRecord{{View: view, RecordIndex: 0}}
values := make([]string, len(clause.Fields))
Expand Down
61 changes: 43 additions & 18 deletions lib/parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,34 +386,59 @@ func (p Parentheses) String() string {
}

type SelectQuery struct {
SelectEntity Expression
OrderByClause Expression
LimitClause Expression
}

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

type SelectSet struct {
LHS Expression
Operator Token
All Token
RHS Expression
}

func (e SelectSet) String() string {
s := []string{e.LHS.String(), e.Operator.Literal}
if !e.All.IsEmpty() {
s = append(s, e.All.Literal)
}
s = append(s, e.RHS.String())
return joinWithSpace(s)
}

type SelectEntity struct {
SelectClause Expression
FromClause Expression
WhereClause Expression
GroupByClause Expression
HavingClause Expression
OrderByClause Expression
LimitClause Expression
}

func (sq SelectQuery) String() string {
s := []string{sq.SelectClause.String()}
if sq.FromClause != nil {
s = append(s, sq.FromClause.String())
}
if sq.WhereClause != nil {
s = append(s, sq.WhereClause.String())
}
if sq.GroupByClause != nil {
s = append(s, sq.GroupByClause.String())
func (e SelectEntity) String() string {
s := []string{e.SelectClause.String()}
if e.FromClause != nil {
s = append(s, e.FromClause.String())
}
if sq.HavingClause != nil {
s = append(s, sq.HavingClause.String())
if e.WhereClause != nil {
s = append(s, e.WhereClause.String())
}
if sq.OrderByClause != nil {
s = append(s, sq.OrderByClause.String())
if e.GroupByClause != nil {
s = append(s, e.GroupByClause.String())
}
if sq.LimitClause != nil {
s = append(s, sq.LimitClause.String())
if e.HavingClause != nil {
s = append(s, e.HavingClause.String())
}
return joinWithSpace(s)
}
Expand Down
Loading

0 comments on commit b58e623

Please sign in to comment.