Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace clickhouse-go with ch-go #2134

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions flow/connectors/clickhouse/cdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package connclickhouse

import (
"context"
"database/sql"
"errors"
"fmt"
"log/slog"
"strings"

"github.com/ClickHouse/clickhouse-go/v2"
_ "github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/ClickHouse/ch-go"
chproto "github.com/ClickHouse/ch-go/proto"

"github.com/PeerDB-io/peer-flow/connectors/utils"
"github.com/PeerDB-io/peer-flow/generated/protos"
Expand All @@ -19,8 +17,8 @@ import (
)

const (
checkIfTableExistsSQL = `SELECT exists(SELECT 1 FROM system.tables WHERE database = ? AND name = ?) AS table_exists;`
dropTableIfExistsSQL = "DROP TABLE IF EXISTS `%s`;"
checkIfTableExistsSQL = `SELECT exists(SELECT 1 FROM system.tables WHERE database = %s AND name = %s) AS table_exists`
dropTableIfExistsSQL = `DROP TABLE IF EXISTS %s`
)

// getRawTableName returns the raw table name for the given table identifier.
Expand All @@ -29,17 +27,23 @@ func (c *ClickHouseConnector) getRawTableName(flowJobName string) string {
}

func (c *ClickHouseConnector) checkIfTableExists(ctx context.Context, databaseName string, tableIdentifier string) (bool, error) {
var result sql.NullInt32
err := c.queryRow(ctx, checkIfTableExistsSQL, databaseName, tableIdentifier).Scan(&result)
if err != nil {
return false, fmt.Errorf("error while reading result row: %w", err)
// TODO escape
var existsC chproto.ColUInt8
if err := c.query(ctx, ch.Query{
Body: fmt.Sprintf(checkIfTableExistsSQL, "'"+databaseName+"'", "'"+tableIdentifier+"'"),
Result: chproto.Results{
{Name: "table_exists", Data: &existsC},
},
OnResult: func(ctx context.Context, block chproto.Block) error {
return nil
},
}); err != nil {
return false, fmt.Errorf("[clickhouse] checkIfTableExists: error in query: %w", err)
}

if !result.Valid {
return false, errors.New("[clickhouse] checkIfTableExists: result is not valid")
if len(existsC) != 1 {
return false, fmt.Errorf("[clickhouse] checkIfTableExists: expected 1 row, got %d", len(existsC))
}

return result.Int32 == 1, nil
return existsC[0] != 0, nil
}

func (c *ClickHouseConnector) CreateRawTable(ctx context.Context, req *protos.CreateRawTableInput) (*protos.CreateRawTableOutput, error) {
Expand Down Expand Up @@ -203,7 +207,7 @@ func (c *ClickHouseConnector) RenameTables(
if err := c.execWithLogging(ctx, fmt.Sprintf(dropTableIfExistsSQL, renameRequest.CurrentName)); err != nil {
return nil, fmt.Errorf("unable to drop exchanged table %s: %w", renameRequest.CurrentName, err)
}
} else if ex, ok := err.(*clickhouse.Exception); !ok || ex.Code != 48 {
} else if ex, ok := err.(*ch.Exception); !ok || ex.Code != 48 {
// code 48 == not implemented -> move on to the fallback code, in all other error codes / types
// return, since we know/assume that the exchange would be the sensible action
return nil, fmt.Errorf("unable to exchange tables %s and %s: %w", renameRequest.NewName, renameRequest.CurrentName, err)
Expand Down
Loading
Loading