Skip to content

Commit

Permalink
fix: clickhoue not support alter_sync before v23.3
Browse files Browse the repository at this point in the history
  • Loading branch information
YenchangChan committed May 24, 2024
1 parent 90b0b92 commit 7da534a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion output/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,15 @@ func (c *ClickHouse) ChangeSchema(newKeys *sync.Map) (err error) {
return
}

var version string
if err = conn.QueryRow("SELECT Version()").Scan(&version); err != nil {
return
}
alterTable := func(tbl, col string) error {
query := fmt.Sprintf("ALTER TABLE `%s`.`%s` %s %s SETTINGS alter_sync = 0;", c.dbName, tbl, onCluster, col)
query := fmt.Sprintf("ALTER TABLE `%s`.`%s` %s %s", c.dbName, tbl, onCluster, col)
if util.CompareClickHouseVersion(version, "23.3") >= 0 {
query += " SETTINGS alter_sync = 0"
}
util.Logger.Info(fmt.Sprintf("executing sql=> %s", query), zap.String("task", taskCfg.Name))
return conn.Exec(query)
}
Expand Down
21 changes: 21 additions & 0 deletions util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,24 @@ func TrySetValue(v1, v2 interface{}) bool {
}
return ok
}

func CompareClickHouseVersion(v1, v2 string) int {
s1 := strings.Split(v1, ".")
s2 := strings.Split(v2, ".")
for i := 0; i < len(s1); i++ {
if len(s2) <= i {
break
}
if s1[i] == "x" || s2[i] == "x" {
continue
}
f1, _ := strconv.Atoi(s1[i])
f2, _ := strconv.Atoi(s2[i])
if f1 > f2 {
return 1
} else if f1 < f2 {
return -1
}
}
return 0
}

0 comments on commit 7da534a

Please sign in to comment.