Skip to content

Commit 2a34102

Browse files
committed
Add some examples for ephemeral column
Covers 1. Examples with both `std` and `clickhouse_api` interface 2. Examples support both `Native` and `HTTP` protocol Signed-off-by: Kaviraj <[email protected]>
1 parent df28730 commit 2a34102

File tree

6 files changed

+246
-0
lines changed

6 files changed

+246
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package clickhouse_api
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
func EphemeralColumnHTTP() error {
9+
conn, err := GetHTTPConnection("http-ephemeral-column", nil, nil, nil)
10+
if err != nil {
11+
return fmt.Errorf("Failed to connect: %v\n", err)
12+
}
13+
defer conn.Close()
14+
15+
ctx := context.Background()
16+
ddl := `CREATE OR REPLACE TABLE test
17+
(
18+
id UInt64,
19+
unhexed String EPHEMERAL,
20+
hexed FixedString(4) DEFAULT unhex(unhexed)
21+
)
22+
ENGINE = MergeTree
23+
ORDER BY id;`
24+
if err := conn.Exec(ctx, ddl); err != nil {
25+
return err
26+
}
27+
28+
i := `INSERT INTO test (id, unhexed) VALUES (1, '5a90b714');`
29+
30+
if err := conn.Exec(ctx, i); err != nil {
31+
return err
32+
}
33+
34+
query := `SELECT
35+
id,
36+
hexed,
37+
hex(hexed)
38+
FROM test;`
39+
40+
rows, err := conn.Query(ctx, query)
41+
if err != nil {
42+
return err
43+
}
44+
45+
for rows.Next() {
46+
var (
47+
id uint64
48+
un string
49+
hex string
50+
)
51+
if err := rows.Scan(&id, &un, &hex); err != nil {
52+
panic(err)
53+
}
54+
fmt.Println("id: ", id, "un: ", un, "hex: ", hex)
55+
}
56+
return nil
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package clickhouse_api
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
func EphemeralColumnNative() error {
9+
conn, err := GetNativeConnection(nil, nil, nil)
10+
if err != nil {
11+
return fmt.Errorf("Failed to connect: %v\n", err)
12+
}
13+
defer conn.Close()
14+
15+
ctx := context.Background()
16+
ddl := `CREATE OR REPLACE TABLE test
17+
(
18+
id UInt64,
19+
unhexed String EPHEMERAL,
20+
hexed FixedString(4) DEFAULT unhex(unhexed)
21+
)
22+
ENGINE = MergeTree
23+
ORDER BY id;`
24+
if err := conn.Exec(ctx, ddl); err != nil {
25+
return err
26+
}
27+
28+
i := `INSERT INTO test (id, unhexed) VALUES (1, '5a90b714');`
29+
30+
if err := conn.Exec(ctx, i); err != nil {
31+
return err
32+
}
33+
34+
query := `SELECT
35+
id,
36+
hexed,
37+
hex(hexed)
38+
FROM test;`
39+
40+
rows, err := conn.Query(ctx, query)
41+
if err != nil {
42+
return err
43+
}
44+
45+
for rows.Next() {
46+
var (
47+
id uint64
48+
un string
49+
hex string
50+
)
51+
if err := rows.Scan(&id, &un, &hex); err != nil {
52+
panic(err)
53+
}
54+
fmt.Println("id: ", id, "un: ", un, "hex: ", hex)
55+
}
56+
return nil
57+
}

examples/clickhouse_api/main_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ func TestAsyncInsert(t *testing.T) {
6060
require.NoError(t, AsyncInsertHTTP())
6161
}
6262

63+
func TestEphemeralColumn(t *testing.T) {
64+
require.NoError(t, EphemeralColumnNative())
65+
require.NoError(t, EphemeralColumnNative())
66+
}
67+
6368
func TestBatchInsert(t *testing.T) {
6469
require.NoError(t, BatchInsert())
6570
}

examples/std/ephemeral_http.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package std
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/ClickHouse/clickhouse-go/v2"
8+
)
9+
10+
func EphemeralColumnHTTP() error {
11+
conn, err := GetStdOpenDBConnection(clickhouse.HTTP, nil, nil, nil)
12+
if err != nil {
13+
return fmt.Errorf("Failed to connect: %v\n", err)
14+
}
15+
defer conn.Close()
16+
17+
ctx := context.Background()
18+
ddl := `CREATE OR REPLACE TABLE test
19+
(
20+
id UInt64,
21+
unhexed String EPHEMERAL,
22+
hexed FixedString(4) DEFAULT unhex(unhexed)
23+
)
24+
ENGINE = MergeTree
25+
ORDER BY id;`
26+
_, err = conn.ExecContext(ctx, ddl)
27+
if err != nil {
28+
return err
29+
}
30+
31+
i := `INSERT INTO test (id, unhexed) VALUES (1, '5a90b714');`
32+
33+
_, err = conn.ExecContext(ctx, i)
34+
if err != nil {
35+
return err
36+
}
37+
38+
query := `SELECT
39+
id,
40+
hexed,
41+
hex(hexed)
42+
FROM test;`
43+
44+
rows, err := conn.QueryContext(ctx, query)
45+
if err != nil {
46+
return err
47+
}
48+
49+
for rows.Next() {
50+
var (
51+
id uint64
52+
un string
53+
hex string
54+
)
55+
if err := rows.Scan(&id, &un, &hex); err != nil {
56+
panic(err)
57+
}
58+
fmt.Println("id: ", id, "un: ", un, "hex: ", hex)
59+
}
60+
return nil
61+
}

examples/std/ephemeral_native.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package std
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/ClickHouse/clickhouse-go/v2"
8+
)
9+
10+
func EphemeralColumnNative() error {
11+
conn, err := GetStdOpenDBConnection(clickhouse.Native, nil, nil, nil)
12+
if err != nil {
13+
return fmt.Errorf("Failed to connect: %v\n", err)
14+
}
15+
defer conn.Close()
16+
17+
ctx := context.Background()
18+
ddl := `CREATE OR REPLACE TABLE test
19+
(
20+
id UInt64,
21+
unhexed String EPHEMERAL,
22+
hexed FixedString(4) DEFAULT unhex(unhexed)
23+
)
24+
ENGINE = MergeTree
25+
ORDER BY id;`
26+
_, err = conn.ExecContext(ctx, ddl)
27+
if err != nil {
28+
return err
29+
}
30+
31+
i := `INSERT INTO test (id, unhexed) VALUES (1, '5a90b714');`
32+
33+
_, err = conn.ExecContext(ctx, i)
34+
if err != nil {
35+
return err
36+
}
37+
38+
query := `SELECT
39+
id,
40+
hexed,
41+
hex(hexed)
42+
FROM test;`
43+
44+
rows, err := conn.QueryContext(ctx, query)
45+
if err != nil {
46+
return err
47+
}
48+
49+
for rows.Next() {
50+
var (
51+
id uint64
52+
un string
53+
hex string
54+
)
55+
if err := rows.Scan(&id, &un, &hex); err != nil {
56+
panic(err)
57+
}
58+
fmt.Println("id: ", id, "un: ", un, "hex: ", hex)
59+
}
60+
return nil
61+
}

examples/std/main_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ func TestStdAsyncInsert(t *testing.T) {
9494
require.NoError(t, AsyncInsertHTTP_WithPrepare())
9595
}
9696

97+
func TestStdEphemeralColumn(t *testing.T) {
98+
require.NoError(t, EphemeralColumnNative())
99+
require.NoError(t, EphemeralColumnHTTP())
100+
}
101+
97102
func TestStdMapInsertRead(t *testing.T) {
98103
require.NoError(t, MapInsertRead())
99104
}

0 commit comments

Comments
 (0)