Skip to content

Commit

Permalink
Merge pull request #27 from goccy/support-string-functions
Browse files Browse the repository at this point in the history
Supports all string functions except collate and contains_substr
  • Loading branch information
goccy authored Sep 21, 2022
2 parents 343643a + 7d62682 commit e1c6bc3
Show file tree
Hide file tree
Showing 16 changed files with 3,306 additions and 149 deletions.
134 changes: 63 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ CXX=clang++

# Synopsis

You can pass ZetaSQL queries to Query/Exec function of database/sql package.

```go
package main

Expand All @@ -47,29 +49,19 @@ func main() {
}
defer db.Close()

rows, err := db.Query(`
SELECT
val,
CASE val
WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
WHEN 3 THEN 'three'
ELSE 'four'
END
FROM UNNEST([1, 2, 3, 4]) AS val`)
rows, err := db.Query(`SELECT * FROM UNNEST([?, ?, ?])`, 1, 2, 3)
if err != nil {
panic(err)
}
var ids []int64
for rows.Next() {
var (
num int64
text string
)
if err := rows.Scan(&num, &text); err != nil {
panic(err)
var id int64
if err := rows.Scan(&id); err != nil {
panic(err)
}
fmt.Println("num = ", num, "text = ", text)
ids = append(ids, id)
}
fmt.Println(ids) // [1 2 3]
}
```

Expand All @@ -85,17 +77,17 @@ A list of ZetaSQL specifications and features supported by go-zetasqlite.
- [x] FLOAT64 ( `FLOAT` )
- [x] BOOL ( `BOOLEAN` )
- [x] STRING
- [ ] BYTES
- [x] BYTES
- [x] DATE
- [x] TIME
- [x] DATETIME
- [x] TIMESTAMP
- [ ] INTERVAL
- [x] ARRAY
- [x] STRUCT
- [ ] GEOGRAPHY
- [x] JSON
- [x] RECORD
- [ ] GEOGRAPHY

## Statements

Expand Down Expand Up @@ -225,7 +217,7 @@ A list of ZetaSQL specifications and features supported by go-zetasqlite.
- [x] CAST AS ARRAY
- [ ] CAST AS BIGNUMERIC
- [x] CAST AS BOOL
- [ ] CAST AS BYTES
- [x] CAST AS BYTES
- [x] CAST AS DATE
- [x] CAST AS DATETIME
- [x] CAST AS FLOAT64
Expand Down Expand Up @@ -305,58 +297,58 @@ A list of ZetaSQL specifications and features supported by go-zetasqlite.

### String functions

- [ ] ASCII
- [ ] BYTE_LENGTH
- [ ] CHAR_LENGTH
- [ ] CHARACTER_LENGTH
- [ ] CHR
- [ ] CODE_POINTS_TO_BYTES
- [ ] CODE_POINTS_TO_STRING
- [x] ASCII
- [x] BYTE_LENGTH
- [x] CHAR_LENGTH
- [x] CHARACTER_LENGTH
- [x] CHR
- [x] CODE_POINTS_TO_BYTES
- [x] CODE_POINTS_TO_STRING
- [ ] COLLATE
- [ ] CONCAT
- [x] CONCAT
- [ ] CONTAINS_SUBSTR
- [ ] ENDS_WITH
- [x] ENDS_WITH
- [x] FORMAT
- [ ] FROM_BASE32
- [ ] FROM_BASE64
- [ ] FROM_HEX
- [ ] INITCAP
- [ ] INSTR
- [ ] LEFT
- [ ] LENGTH
- [ ] LPAD
- [ ] LOWER
- [ ] LTRIM
- [ ] NORMALIZE
- [ ] NORMALIZE_AND_CASEFOLD
- [ ] OCTET_LENGTH
- [ ] REGEXP_CONTAINS
- [ ] REGEXP_EXTRACT
- [ ] REGEXP_EXTRACT_ALL
- [ ] REGEXP_INSTR
- [ ] REGEXP_REPLACE
- [ ] REGEXP_SUBSTR
- [ ] REPLACE
- [ ] REPEAT
- [ ] REVERSE
- [ ] RIGHT
- [ ] RPAD
- [ ] RTRIM
- [ ] SAFE_CONVERT_BYTES_TO_STRING
- [ ] SOUNDEX
- [ ] SPLIT
- [ ] STARTS_WITH
- [ ] STRPOS
- [ ] SUBSTR
- [ ] SUBSTRING
- [ ] TO_BASE32
- [ ] TO_BASE64
- [ ] TO_CODE_POINTS
- [ ] TO_HEX
- [ ] TRANSALTE
- [ ] TRIM
- [ ] UNICODE
- [ ] UPPER
- [x] FROM_BASE32
- [x] FROM_BASE64
- [x] FROM_HEX
- [x] INITCAP
- [x] INSTR
- [x] LEFT
- [x] LENGTH
- [x] LPAD
- [x] LOWER
- [x] LTRIM
- [x] NORMALIZE
- [x] NORMALIZE_AND_CASEFOLD
- [x] OCTET_LENGTH
- [x] REGEXP_CONTAINS
- [x] REGEXP_EXTRACT
- [x] REGEXP_EXTRACT_ALL
- [x] REGEXP_INSTR
- [x] REGEXP_REPLACE
- [x] REGEXP_SUBSTR
- [x] REPLACE
- [x] REPEAT
- [x] REVERSE
- [x] RIGHT
- [x] RPAD
- [x] RTRIM
- [x] SAFE_CONVERT_BYTES_TO_STRING
- [x] SOUNDEX
- [x] SPLIT
- [x] STARTS_WITH
- [x] STRPOS
- [x] SUBSTR
- [x] SUBSTRING
- [x] TO_BASE32
- [x] TO_BASE64
- [x] TO_CODE_POINTS
- [x] TO_HEX
- [x] TRANSALTE
- [x] TRIM
- [x] UNICODE
- [x] UPPER

### JSON functions

Expand All @@ -370,8 +362,8 @@ A list of ZetaSQL specifications and features supported by go-zetasqlite.
- [ ] JSON_VALUE_ARRAY
- [ ] PARSE_JSON
- [x] TO_JSON
- [ ] TO_JSON_STRING
- [ ] STRING
- [x] TO_JSON_STRING
- [x] STRING
- [x] BOOL
- [x] INT64
- [x] FLOAT64
Expand Down
30 changes: 30 additions & 0 deletions _examples/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"database/sql"
"fmt"

_ "github.com/goccy/go-zetasqlite"
)

func main() {
db, err := sql.Open("zetasqlite", ":memory:")
if err != nil {
panic(err)
}
defer db.Close()

rows, err := db.Query(`SELECT * FROM UNNEST([?, ?, ?])`, 1, 2, 3)
if err != nil {
panic(err)
}
var ids []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
panic(err)
}
ids = append(ids, id)
}
fmt.Println(ids) // [1 2 3]
}
16 changes: 16 additions & 0 deletions _examples/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module example

go 1.18

require github.com/goccy/go-zetasqlite v0.6.6

require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/goccy/go-json v0.9.10 // indirect
github.com/goccy/go-zetasql v0.3.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/mattn/go-sqlite3 v1.14.14 // indirect
gonum.org/v1/gonum v0.11.0 // indirect
)

replace github.com/goccy/go-zetasqlite => ../
14 changes: 14 additions & 0 deletions _examples/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WAFKLNi6ZS0675eEUC9y3AlwSbQu1Y=
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/goccy/go-json v0.9.10 h1:hCeNmprSNLB8B8vQKWl6DpuH0t60oEs+TAk9a7CScKc=
github.com/goccy/go-json v0.9.10/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-zetasql v0.3.3 h1:+Ar/GZ4k2vNgaljRPt5lpD8JSIiq0WSEG38Fbowj5fM=
github.com/goccy/go-zetasql v0.3.3/go.mod h1:6W14CJVKh7crrSPyj6NPk4c49L2NWnxvyDLsRkOm4BI=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-sqlite3 v1.14.14 h1:qZgc/Rwetq+MtyE18WhzjokPD93dNqLGNT3QJuLvBGw=
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3 h1:n9HxLrNxWWtEb1cA950nuEEj3QnKbtsCJ6KjcgisNUs=
gonum.org/v1/gonum v0.11.0 h1:f1IJhK4Km5tBJmaiJXtk/PkL4cdVX6J+tGiM187uT5E=
gonum.org/v1/gonum v0.11.0/go.mod h1:fSG4YDCxxUZQJ7rKsQrj0gMOg00Il0Z96/qMA4bVQhA=
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ require github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13
require github.com/google/uuid v1.3.0

require gonum.org/v1/gonum v0.11.0

require golang.org/x/text v0.3.7
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
2 changes: 1 addition & 1 deletion internal/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (a *Analyzer) AnalyzeIterator(ctx context.Context, conn *Conn, query string
}
stmts, err := a.parseScript(query)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to parse statements: %w", err)
}
resultStmts := make([]*Statement, 0, len(stmts))
fullNamePathMap, err := a.getFullNamePathMap(stmts)
Expand Down
23 changes: 0 additions & 23 deletions internal/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,6 @@ func ARRAY_SAFE_ORDINAL(v Value, idx int) (Value, error) {
return array.values[idx-1], nil
}

func CONCAT(args ...Value) (Value, error) {
var ret string
for _, v := range args {
s, err := v.ToString()
if err != nil {
return nil, err
}
ret += s
}
return StringValue(ret), nil
}

func LIKE(a, b Value) (Value, error) {
va, err := a.ToString()
if err != nil {
Expand Down Expand Up @@ -496,17 +484,6 @@ func NULLIF(expr, exprToMatch Value) (Value, error) {
return expr, nil
}

func LENGTH(v Value) (Value, error) {
if v == nil {
return IntValue(0), nil
}
s, err := v.ToString()
if err != nil {
return nil, err
}
return IntValue(int64(len(s))), nil
}

func DECODE_ARRAY(v string) (Value, error) {
json, err := jsonArrayFromEncodedString(v)
if err != nil {
Expand Down
Loading

0 comments on commit e1c6bc3

Please sign in to comment.