Skip to content

Commit

Permalink
feat: ttc intergration (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
forsaken628 authored Jan 21, 2025
1 parent 12f1c8d commit f641030
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 15 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/ttc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: TTC docker publish

on:
push:
branches:
- main
tags:
- v*
paths:
- '*.go'
- 'tests/ttc/**'

jobs:
docker:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set Docker tag
shell: bash
run: |
ref_v="${{ github.ref }}"
if [[ $ref_v == refs/tags/* ]]; then
DOCKER_TAG=${ref_v:10}
else
DOCKER_TAG="latest"
fi
echo "DOCKER_TAG=$DOCKER_TAG" >> $GITHUB_ENV
- name: Print Docker tag
shell: bash
run: |
echo "Start to build and publish: ghcr.io/databendlabs/ttc-go:$DOCKER_TAG"
- name: TTC Build and push
uses: docker/build-push-action@v6
with:
push: true
file: ./tests/ttc/Dockerfile
tags: ghcr.io/databendlabs/ttc-go:${{ env.DOCKER_TAG }}
4 changes: 2 additions & 2 deletions access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package godatabend

import (
"context"
"io/ioutil"
"os"

"github.com/BurntSushi/toml"
)
Expand Down Expand Up @@ -44,7 +44,7 @@ func NewFileAccessTokenLoader(path string) *FileAccessTokenLoader {

// try decode as toml, if not toml, return the plain key content
func (l *FileAccessTokenLoader) LoadAccessToken(ctx context.Context, forceRotate bool) (string, error) {
buf, err := ioutil.ReadFile(l.path)
buf, err := os.ReadFile(l.path)
if err != nil {
return "", err
}
Expand Down
3 changes: 2 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"fmt"
"io"
"maps"
"math/rand"
"mime/multipart"
"net"
Expand Down Expand Up @@ -179,7 +180,7 @@ func NewAPIClientFromConfig(cfg *Config) *APIClient {
Database: cfg.Database,
Role: cfg.Role,
SecondaryRoles: secondaryRoles,
Settings: cfg.Params,
Settings: maps.Clone(cfg.Params), // Config may shared
}
sessionStateRawJson, _ := json.Marshal(sessionState)
sessionStateRaw := json.RawMessage(sessionStateRawJson)
Expand Down
6 changes: 3 additions & 3 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ func (dc *DatabendConn) PrepareContext(ctx context.Context, query string) (drive
return dc.prepare(ctx, query)
}

func buildDatabendConn(ctx context.Context, config Config) (*DatabendConn, error) {
func buildDatabendConn(ctx context.Context, config *Config) (*DatabendConn, error) {
dc := &DatabendConn{
ctx: ctx,
cfg: &config,
rest: NewAPIClientFromConfig(&config),
cfg: config,
rest: NewAPIClientFromConfig(config),
}
if config.Debug {
dc.logger = log.New(os.Stderr, "databend: ", log.LstdFlags)
Expand Down
18 changes: 10 additions & 8 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@ import (
)

// DatabendDriver is a context of Go Driver
type DatabendDriver struct {
commit func() error
}
type DatabendDriver struct{}

// Open creates a new connection.
func (d DatabendDriver) Open(dsn string) (driver.Conn, error) {
logger.Info("Open")
ctx := context.TODO()
ctx := context.Background()
cfg, err := ParseDSN(dsn)
if err != nil {
return nil, err
}
return d.OpenWithConfig(ctx, *cfg)
return d.OpenWithConfig(ctx, cfg)
}

func (d DatabendDriver) OpenConnector(dsn string) (driver.Connector, error) {
return ParseDSN(dsn)
}

// OpenWithConfig creates a new connection with the given Config.
func (d DatabendDriver) OpenWithConfig(
ctx context.Context,
config Config) (
driver.Conn, error) {
config *Config,
) (driver.Conn, error) {
logger.Info("OpenWithConfig")
dc, err := buildDatabendConn(ctx, config)
if err != nil {
Expand All @@ -38,6 +40,6 @@ func (d DatabendDriver) OpenWithConfig(
var logger = CreateDefaultLogger()

func init() {
sql.Register("databend", &DatabendDriver{})
sql.Register("databend", DatabendDriver{})
logger.SetLogLevel("error")
}
10 changes: 10 additions & 0 deletions dsn.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package godatabend

import (
"context"
"database/sql/driver"
"fmt"
"net"
"net/url"
Expand Down Expand Up @@ -254,3 +256,11 @@ func ParseDSN(dsn string) (*Config, error) {

return cfg, nil
}

func (cfg *Config) Connect(ctx context.Context) (driver.Conn, error) {
return DatabendDriver{}.OpenWithConfig(ctx, cfg)
}

func (cfg *Config) Driver() driver.Driver {
return DatabendDriver{}
}
2 changes: 1 addition & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/test-go/testify/require"
"github.com/stretchr/testify/require"
)

func Test_SessionState(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"sync/atomic"
)

var rowsHack = false

type resultSchema struct {
columns []string
types []string
Expand All @@ -24,6 +26,7 @@ type nextRows struct {
dc *DatabendConn
ctx context.Context
respData *QueryResponse
latestRow []*string
}

func waitForData(ctx context.Context, dc *DatabendConn, response *QueryResponse) (*QueryResponse, error) {
Expand Down Expand Up @@ -154,6 +157,9 @@ func (r *nextRows) Next(dest []driver.Value) error {

lineData := r.respData.Data[0]
r.respData.Data = r.respData.Data[1:]
if rowsHack {
r.latestRow = lineData
}

for j := range lineData {
val := lineData[j]
Expand Down
23 changes: 23 additions & 0 deletions rows_hack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build rows_hack

package godatabend

import (
"database/sql"
"database/sql/driver"
"reflect"
"unsafe"
)

func init() {
rowsHack = true
}

func LastRawRow(rows *sql.Rows) []*string {
field, ok := reflect.TypeOf((*sql.Rows)(nil)).Elem().FieldByName("rowsi")
if !ok {
panic("rowsi field not found")
}
rowsi := *(*driver.Rows)(unsafe.Pointer(uintptr(unsafe.Pointer(rows)) + field.Offset))
return rowsi.(*nextRows).latestRow
}
13 changes: 13 additions & 0 deletions tests/ttc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:latest AS builder

WORKDIR /usr/src

COPY . .

RUN CGO_ENABLED=0 go build -tags rows_hack ./tests/ttc

FROM debian:bullseye-slim
COPY --from=builder /usr/src/ttc /usr/local/bin/ttc-server

# docker run --net host -e TTC_PORT=9093 -e DATABEND_DSN=databend://default:@127.0.0.1:8000 ghcr.io/databendlabs/ttc-go:latest
CMD ["ttc-server"]
Loading

0 comments on commit f641030

Please sign in to comment.