Skip to content

Commit

Permalink
feature/issue 44 update examples (#46)
Browse files Browse the repository at this point in the history
* update examples
  • Loading branch information
leo-cai-timeplus authored Sep 14, 2023
1 parent 9e02984 commit cf6d78b
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 115 deletions.
6 changes: 3 additions & 3 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ func format(tz *time.Location, v interface{}) string {
case time.Time:
switch v.Location().String() {
case "Local":
return fmt.Sprintf("toDateTime(%d)", v.Unix())
return fmt.Sprintf("to_datetime(%d)", v.Unix())
case tz.String():
return v.Format("toDateTime('2006-01-02 15:04:05')")
return v.Format("to_datetime('2006-01-02 15:04:05')")
}
return v.Format("toDateTime('2006-01-02 15:04:05', '" + v.Location().String() + "')")
return v.Format("to_datetime('2006-01-02 15:04:05', '" + v.Location().String() + "')")
case []interface{}: // tuple
elements := make([]string, 0, len(v))
for _, e := range v {
Expand Down
4 changes: 2 additions & 2 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func TestFormatTime(t *testing.T) {
tz, err = time.LoadLocation("Europe/London")
)
if assert.NoError(t, err) {
if assert.Equal(t, "toDateTime('2022-01-12 15:00:00')", format(t1.Location(), t1)) {
assert.Equal(t, "toDateTime('2022-01-12 15:00:00', 'UTC')", format(tz, t1))
if assert.Equal(t, "to_datetime('2022-01-12 15:00:00')", format(t1.Location(), t1)) {
assert.Equal(t, "to_datetime('2022-01-12 15:00:00', 'UTC')", format(tz, t1))
}
}
}
Expand Down
26 changes: 13 additions & 13 deletions examples/native/batch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func example() error {
var (
ctx = context.Background()
conn, err = proton.Open(&proton.Options{
Addr: []string{"127.0.0.1:9000"},
Addr: []string{"127.0.0.1:8463"},
Auth: proton.Auth{
Database: "default",
Username: "default",
Expand All @@ -47,30 +47,30 @@ func example() error {
if err != nil {
return err
}
if err := conn.Exec(ctx, `DROP TABLE IF EXISTS example`); err != nil {
if err := conn.Exec(ctx, `DROP STREAM IF EXISTS example`); err != nil {
return err
}
err = conn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS example (
Col1 UInt8
, Col2 String
, Col3 FixedString(3)
, Col4 UUID
, Col5 Map(String, UInt8)
, Col6 Array(String)
, Col7 Tuple(String, UInt8, Array(Map(String, String)))
CREATE STREAM IF NOT EXISTS example (
Col1 uint8
, Col2 string
, Col3 fixed_string(3)
, Col4 uuid
, Col5 map(string, uint8)
, Col6 array(string)
, Col7 tuple(string, uint8, array(map(string, string)))
, Col8 DateTime
) Engine = Null
)
`)
if err != nil {
return err
}

batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
batch, err := conn.PrepareBatch(ctx, "INSERT INTO example (* except _tp_time)")
if err != nil {
return err
}
for i := 0; i < 500_000; i++ {
for i := 0; i < 100; i++ {
err := batch.Append(
uint8(42),
"ClickHouse", "Inc",
Expand Down
17 changes: 10 additions & 7 deletions examples/native/bind/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func example() error {
var (
ctx = context.Background()
conn, err = proton.Open(&proton.Options{
Addr: []string{"127.0.0.1:9000"},
Addr: []string{"127.0.0.1:8463"},
Auth: proton.Auth{
Database: "default",
Username: "default",
Expand All @@ -42,18 +42,21 @@ func example() error {
return err
}
const ddl = `
CREATE TEMPORARY TABLE example (
Col1 UInt8
, Col2 String
CREATE STREAM example (
Col1 uint8
, Col2 string
, Col3 DateTime
)
`
if err := conn.Exec(ctx, `drop stream if exists example`); err != nil {
return err
}
if err := conn.Exec(ctx, ddl); err != nil {
return err
}
datetime := time.Now()
{
batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
batch, err := conn.PrepareBatch(ctx, "INSERT INTO example (* except _tp_time)")
if err != nil {
return err
}
Expand All @@ -73,13 +76,13 @@ func example() error {
Col3 time.Time
}
{
if err := conn.QueryRow(ctx, `SELECT * FROM example WHERE Col1 = $1 AND Col3 = $2`, 2, datetime).ScanStruct(&result); err != nil {
if err := conn.QueryRow(ctx, `SELECT * except _tp_time FROM example WHERE _tp_time > earliest_ts() AND Col1 = $1 AND Col3 = $2 LIMIT 1`, 2, datetime).ScanStruct(&result); err != nil {
return err
}
fmt.Println(result)
}
{
if err := conn.QueryRow(ctx, `SELECT * FROM example WHERE Col1 = @Col1 AND Col3 = @Col2`,
if err := conn.QueryRow(ctx, `SELECT * except _tp_time FROM example WHERE _tp_time > earliest_ts() AND Col1 = @Col1 AND Col3 = @Col2 LIMIT 1`,
proton.Named("Col1", 4),
proton.Named("Col2", datetime),
).ScanStruct(&result); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/native/dynamic-scan-types/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func example() error {
conn, err := proton.Open(&proton.Options{
Addr: []string{"127.0.0.1:9000"},
Addr: []string{"127.0.0.1:8463"},
Auth: proton.Auth{
Database: "default",
Username: "default",
Expand Down
27 changes: 14 additions & 13 deletions examples/native/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func main() {
conn, err := proton.Open(&proton.Options{
Addr: []string{"127.0.0.1:9000"},
Addr: []string{"127.0.0.1:8463"},
Auth: proton.Auth{
Database: "default",
Username: "default",
Expand Down Expand Up @@ -62,28 +62,28 @@ func main() {
fmt.Printf("name: %s, value: %s, type=%s\n", s.Name, s.Value, s.Type)
}

if err = conn.Exec(context.Background(), "TUNCATE TABLE X"); err == nil {
if err = conn.Exec(context.Background(), "TRUNCATE STREAM X"); err == nil {
panic("unexpected")
}
if exception, ok := err.(*proton.Exception); ok {
fmt.Printf("Catch exception [%d]\n", exception.Code)
}
const ddl = `
CREATE TABLE example (
Col1 UInt64
, Col2 FixedString(2)
, Col3 Map(String, String)
, Col4 Array(String)
CREATE STREAM example (
Col1 uint64
, Col2 fixed_string(2)
, Col3 map(string, string)
, Col4 array(string)
, Col5 DateTime64(3)
) Engine Memory
)
`
if err := conn.Exec(context.Background(), "DROP TABLE IF EXISTS example"); err != nil {
if err := conn.Exec(context.Background(), "DROP STREAM IF EXISTS example"); err != nil {
log.Fatal(err)
}
if err := conn.Exec(context.Background(), ddl); err != nil {
log.Fatal(err)
}
batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO example")
batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO example (* except _tp_time)")
if err != nil {
log.Fatal(err)
}
Expand All @@ -107,17 +107,18 @@ func main() {
ctx := proton.Context(context.Background(), proton.WithProgress(func(p *proton.Progress) {
fmt.Println("progress: ", p)
}))

ctx, cancel := context.WithTimeout(ctx, time.Duration(10)*time.Second)
defer cancel()
var count uint64
if err := conn.QueryRow(ctx, "SELECT COUNT() FROM example").Scan(&count); err != nil {
if err := conn.QueryRow(ctx, "SELECT count() FROM example WHERE _tp_time > earliest_ts() LIMIT 1").Scan(&count); err != nil {
log.Fatal(err)
}
fmt.Println("count", count)
var result struct {
Col1 uint64
Count uint64 `ch:"count"`
}
if err := conn.QueryRow(ctx, "SELECT Col1, COUNT() AS count FROM example WHERE Col1 = $1 GROUP BY Col1", 42).ScanStruct(&result); err != nil {
if err := conn.QueryRow(ctx, "SELECT Col1, count() AS count FROM example WHERE _tp_time > earliest_ts() AND Col1 = $1 GROUP BY Col1 LIMIT 1", 42).ScanStruct(&result); err != nil {
log.Fatal(err)
}
fmt.Println("result", result)
Expand Down
18 changes: 10 additions & 8 deletions examples/native/scan_struct/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func example() error {
conn, err := proton.Open(&proton.Options{
Addr: []string{"127.0.0.1:9000"},
Addr: []string{"127.0.0.1:8463"},
Auth: proton.Auth{
Database: "default",
Username: "default",
Expand All @@ -46,7 +46,9 @@ func example() error {
if err != nil {
return err
}
ctx := proton.Context(context.Background(), proton.WithSettings(proton.Settings{
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(3))
defer cancel()
ctx = proton.Context(ctx, proton.WithSettings(proton.Settings{
"max_block_size": 10,
}), proton.WithProgress(func(p *proton.Progress) {
fmt.Println("progress: ", p)
Expand All @@ -57,15 +59,15 @@ func example() error {
}
return err
}
if err := conn.Exec(ctx, `DROP TABLE IF EXISTS example`); err != nil {
if err := conn.Exec(ctx, `DROP STREAM IF EXISTS example`); err != nil {
return err
}
err = conn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS example (
Col1 UInt8,
Col2 String,
CREATE STREAM IF NOT EXISTS example (
Col1 uint8,
Col2 string,
Col3 DateTime
) engine=Memory
)
`)
if err != nil {
return err
Expand All @@ -89,7 +91,7 @@ func example() error {
ColumnWithName time.Time `ch:"Col3"`
}

if err = conn.Select(ctx, &result, "SELECT Col1, Col2, Col3 FROM example"); err != nil {
if err = conn.Select(ctx, &result, "SELECT Col1, Col2, Col3 FROM example WHERE _tp_time > earliest_ts() LIMIT 10"); err != nil {
return err
}

Expand Down
17 changes: 9 additions & 8 deletions examples/native/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func example() error {
conn, err := proton.Open(&proton.Options{
Addr: []string{"127.0.0.1:9000"},
Addr: []string{"127.0.0.1:8463"},
Auth: proton.Auth{
Database: "default",
Username: "default",
Expand All @@ -53,21 +53,23 @@ func example() error {
}), proton.WithProfileInfo(func(p *proton.ProfileInfo) {
fmt.Println("profile info: ", p)
}))
ctx, cancel := context.WithTimeout(ctx, time.Second*time.Duration(5))
defer cancel()
if err := conn.Ping(ctx); err != nil {
if exception, ok := err.(*proton.Exception); ok {
fmt.Printf("Catch exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
}
return err
}
if err := conn.Exec(ctx, `DROP TABLE IF EXISTS example`); err != nil {
if err := conn.Exec(ctx, `DROP STREAM IF EXISTS example`); err != nil {
return err
}
err = conn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS example (
Col1 UInt8,
Col2 String,
CREATE STREAM IF NOT EXISTS example (
Col1 uint8,
Col2 string,
Col3 DateTime
) engine=Memory
)
`)
if err != nil {
return err
Expand All @@ -84,8 +86,7 @@ func example() error {
if err := batch.Send(); err != nil {
return err
}

rows, err := conn.Query(ctx, "SELECT Col1, Col2, Col3 FROM example WHERE Col1 >= $1 AND Col2 <> $2 AND Col3 <= $3", 0, "xxx", time.Now())
rows, err := conn.Query(ctx, "SELECT Col1, Col2, Col3 FROM example WHERE _tp_time > earliest_ts() AND Col1 >= $1 AND Col2 <> $2 AND Col3 <= $3 LIMIT 12", 0, "xxx", time.Now())
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions examples/native/write-async/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ import (
)

const ddl = `
CREATE TEMPORARY TABLE example (
Col1 UInt64
, Col2 String
, Col3 Array(UInt8)
CREATE STREAM example (
Col1 uint64
, Col2 string
, Col3 array(int)
, Col4 DateTime
)
)
`

func main() {
var (
ctx = context.Background()
conn, err = proton.Open(&proton.Options{
Addr: []string{"127.0.0.1:9000"},
Addr: []string{"127.0.0.1:8463"},
Auth: proton.Auth{
Database: "default",
Username: "default",
Expand All @@ -55,6 +55,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
conn.Exec(ctx, `DROP STREAM IF EXISTS example`)
if err := conn.Exec(ctx, ddl); err != nil {
log.Fatal(err)
}
Expand Down
17 changes: 10 additions & 7 deletions examples/native/write-columnar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ import (
)

const ddl = `
CREATE TEMPORARY TABLE example (
Col1 UInt64
, Col2 String
, Col3 Array(UInt8)
CREATE STREAM example (
Col1 uint64
, Col2 string
, Col3 array(uint8)
, Col4 DateTime
)
)
`

func example(conn proton.Conn) error {
batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO example")
batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO example (* except _tp_time)")
if err != nil {
return err
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func main() {
var (
ctx = context.Background()
conn, err = proton.Open(&proton.Options{
Addr: []string{"127.0.0.1:9000"},
Addr: []string{"127.0.0.1:8463"},
Auth: proton.Auth{
Database: "default",
Username: "default",
Expand All @@ -86,6 +86,9 @@ func main() {
if err != nil {
log.Fatal(err)
}
if err := conn.Exec(context.Background(), `DROP STREAM IF EXISTS example`); err != nil {
log.Fatal(err)
}
if err := conn.Exec(ctx, ddl); err != nil {
log.Fatal(err)
}
Expand Down
Loading

0 comments on commit cf6d78b

Please sign in to comment.