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

Clickhouse SetupNormalize: changes to table definition #2008

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions flow/connectors/clickhouse/cdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ func (c *ClickhouseConnector) RenameTables(ctx context.Context, req *protos.Rena
c.logger.Info(fmt.Sprintf("handling soft-deletes for table '%s'...", renameRequest.NewName))
err := c.execWithLogging(ctx,
fmt.Sprintf("INSERT INTO %s(%s) SELECT %s,true AS %s FROM %s WHERE (%s) NOT IN (SELECT %s FROM %s)",
renameRequest.CurrentName, fmt.Sprintf("%s,%s", allCols, signColName), allCols,
signColName,
renameRequest.CurrentName, fmt.Sprintf("%s,%s", allCols, isDeletedColName), allCols,
isDeletedColName,
renameRequest.NewName, pkeyCols, pkeyCols, renameRequest.CurrentName))
if err != nil {
return nil, fmt.Errorf("unable to handle soft-deletes for table %s: %w", renameRequest.NewName, err)
Expand Down
17 changes: 9 additions & 8 deletions flow/connectors/clickhouse/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
)

const (
signColName = "_peerdb_is_deleted"
signColType = "Int8"
versionColName = "_peerdb_version"
versionColType = "Int64"
isDeletedColName = "_peerdb_is_deleted"
isDeletedColType = "UInt8"
versionColName = "_peerdb_version"
versionColType = "UInt64"
)

func (c *ClickhouseConnector) StartSetupNormalizedTables(_ context.Context) (interface{}, error) {
Expand Down Expand Up @@ -106,8 +106,9 @@ func generateCreateTableSQLForNormalizedTable(

// add sign and version columns
stmtBuilder.WriteString(fmt.Sprintf(
"`%s` %s, `%s` %s) ENGINE = ReplacingMergeTree(`%s`)",
signColName, signColType, versionColName, versionColType, versionColName))
"`%s` %s, `%s` %s) ENGINE = ReplacingMergeTree(`%s`, `%s`)",
isDeletedColName, isDeletedColType, versionColName, versionColType, versionColName,
isDeletedColName))

pkeys := tableSchema.PrimaryKeyColumns
if len(pkeys) > 0 {
Expand Down Expand Up @@ -207,8 +208,8 @@ func (c *ClickhouseConnector) NormalizeRecords(ctx context.Context,
}

// add _peerdb_sign as _peerdb_record_type / 2
projection.WriteString(fmt.Sprintf("intDiv(_peerdb_record_type, 2) AS `%s`,", signColName))
colSelector.WriteString(fmt.Sprintf("`%s`,", signColName))
projection.WriteString(fmt.Sprintf("intDiv(_peerdb_record_type, 2) AS `%s`,", isDeletedColName))
colSelector.WriteString(fmt.Sprintf("`%s`,", isDeletedColName))

// add _peerdb_timestamp as _peerdb_version
projection.WriteString(fmt.Sprintf("_peerdb_timestamp AS `%s`", versionColName))
Expand Down
2 changes: 1 addition & 1 deletion flow/connectors/clickhouse/qrep_avro_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (s *ClickhouseAvroSyncMethod) SyncQRepRecords(
for _, col := range dstTableSchema {
colName := col.Name()
if strings.EqualFold(colName, config.SoftDeleteColName) ||
strings.EqualFold(colName, signColName) ||
strings.EqualFold(colName, isDeletedColName) ||
strings.EqualFold(colName, config.SyncedAtColName) ||
strings.EqualFold(colName, versionColName) {
continue
Expand Down
Loading