Skip to content

Commit

Permalink
Release v1.11.5
Browse files Browse the repository at this point in the history
- Add a built-in function. ([Github #10](#10))
  - String Function
      - SUBSTRING
  • Loading branch information
mithrandie committed Jul 6, 2019
2 parents 588d222 + 1d1879e commit 7527eeb
Show file tree
Hide file tree
Showing 18 changed files with 1,748 additions and 1,355 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## Version 1.11.5

Released on July 6, 2019

- Add a built-in function. ([Github #10](https://github.com/mithrandie/csvq/pull/10))
- String Function
- SUBSTRING

## Version 1.11.4

Released on Jun 1, 2019
Expand Down
29 changes: 21 additions & 8 deletions docs/_posts/2006-01-02-string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ category: reference
| [WIDTH](#width) | Return the string width of a string |
| [LPAD](#lpad) | Return a string left-side padded |
| [RPAD](#rpad) | Return a string right-side padded |
| [SUBSTR](#substr) | Return the substring of a string |
| [SUBSTRING](#substring) | Return the substring of a string |
| [SUBSTR](#substr) | Return the substring of a string using zero-based indexing |
| [INSTR](#instr) | Return the index of the first occurrence of a substring |
| [LIST_ELEM](#list_elem) | Return a element of a list |
| [REPLACE](#replace) | Return a string replaced the substrings with another string |
Expand Down Expand Up @@ -327,11 +328,13 @@ _return_

Returns the string value of _str_ padded with trailing _padstr_ to a length specified by _len_.

### SUBSTR
{: #substr}

### SUBSTRING
{: #substring}

```
SUBSTR(str, position)
SUBSTRING(str FROM position [FOR len])
SUBSTRING(str, position [, len])
```

_str_
Expand All @@ -340,14 +343,24 @@ _str_
_position_
: [integer]({{ '/reference/value.html#integer' | relative_url }})

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

_return_
: [string]({{ '/reference/value.html#string' | relative_url }})

Returns the substring of _str_ from at _position_ to the end.
Returns the _len_ characters in _str_ starting from the _position_-th character using one-based positional indexing.

If _position_ is 0, then it is treated as 1.<br />
if _len_ is not specified or _len_ is longer than the length from _position_ to the end, then returns the substring from _position_ to the end.<br />
If _position_ is negative, then starting position is _position_ from the end of the _str_.


### SUBSTR
{: #substr}

```
SUBSTR(str, position, len)
SUBSTR(str, position [, len])
```

_str_
Expand All @@ -362,8 +375,8 @@ _len_
_return_
: [string]({{ '/reference/value.html#string' | relative_url }})

Returns the _len_ characters in _str_ from at _position_.
if _len_ is less than the length from _position_ to the end, then returns the substring from _position_ to the end.
This function behaves the same as [SUBSTRING](#substring), but uses zero-based positional indexing.


### INSTR
{: #instr}
Expand Down
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ title: Change Log - csvq

# Change Log

## Version 1.11.5

Released on July 6, 2019

- Add a built-in function. ([Github #10](https://github.com/mithrandie/csvq/pull/10))
- String Function
- SUBSTRING

## Version 1.11.4

Released on Jun 1, 2019
Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ In the multiple operations, you can use variables, cursors, temporary tables, an

## Latest Release

Version 1.11.4
: Released on Jun 1, 2019
Version 1.11.5
: Released on July 6, 2019

<a class="waves-effect waves-light btn" href="https://github.com/mithrandie/csvq/releases/tag/v1.11.4">
<a class="waves-effect waves-light btn" href="https://github.com/mithrandie/csvq/releases/tag/v1.11.5">
<i class="material-icons left">file_download</i>download
</a>

Expand Down
4 changes: 2 additions & 2 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>https://mithrandie.github.io/csvq/</loc>
<lastmod>2019-06-01T12:10:32+00:00</lastmod>
<lastmod>2019-07-06T02:12:43+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference.html</loc>
Expand Down Expand Up @@ -150,7 +150,7 @@
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/string-functions.html</loc>
<lastmod>2019-04-25T16:32:19+00:00</lastmod>
<lastmod>2019-07-06T02:12:43+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/numeric-functions.html</loc>
Expand Down
15 changes: 14 additions & 1 deletion lib/parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,23 @@ type Function struct {
*BaseExpr
Name string
Args []QueryExpression
From string
For string
}

func (e Function) String() string {
return e.Name + "(" + listQueryExpressions(e.Args) + ")"
var args string
if strings.EqualFold(e.Name, TokenLiteral(SUBSTRING)) && 0 < len(e.From) {
elems := make([]string, 0, 5)
elems = append(elems, e.Args[0].String(), e.From, e.Args[1].String())
if 0 < len(e.For) {
elems = append(elems, e.For, e.Args[2].String())
}
args = joinWithSpace(elems)
} else {
args = listQueryExpressions(e.Args)
}
return e.Name + "(" + args + ")"
}

type AggregateFunction struct {
Expand Down
41 changes: 41 additions & 0 deletions lib/parser/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,47 @@ func TestFunction_String(t *testing.T) {
if e.String() != expect {
t.Errorf("string = %q, want %q for %#v", e.String(), expect, e)
}

e = Function{
Name: "substring",
Args: []QueryExpression{
Identifier{Literal: "column"},
NewIntegerValue(2),
NewIntegerValue(5),
},
}
expect = "substring(column, 2, 5)"
if e.String() != expect {
t.Errorf("string = %q, want %q for %#v", e.String(), expect, e)
}

e = Function{
Name: "substring",
Args: []QueryExpression{
Identifier{Literal: "column"},
NewIntegerValue(2),
},
From: "from",
}
expect = "substring(column from 2)"
if e.String() != expect {
t.Errorf("string = %q, want %q for %#v", e.String(), expect, e)
}

e = Function{
Name: "substring",
Args: []QueryExpression{
Identifier{Literal: "column"},
NewIntegerValue(2),
NewIntegerValue(5),
},
From: "from",
For: "for",
}
expect = "substring(column from 2 for 5)"
if e.String() != expect {
t.Errorf("string = %q, want %q for %#v", e.String(), expect, e)
}
}

func TestAggregateFunction_String(t *testing.T) {
Expand Down
Loading

0 comments on commit 7527eeb

Please sign in to comment.