Skip to content

Commit

Permalink
bigquery: does not support sync column default value (#95)
Browse files Browse the repository at this point in the history
* bigquery: does not support sync column default value

* update doc
  • Loading branch information
wd0517 authored Mar 1, 2024
1 parent b67731b commit 3fec55c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 26 deletions.
3 changes: 2 additions & 1 deletion docs/bigquery.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To replicate snapshot and incremental data of a TiDB Table to BigQuery:

All DDL which will change the schema of table are supported (except index related), including:

- Add column
- Add column without default value
- Drop column
- Rename column
- Drop table
Expand All @@ -34,3 +34,4 @@ All DDL which will change the schema of table are supported (except index relate
>
> 1. BigQuery has some limitations on modifying table schemas, like BigQuery does not support add a REQUIRED column to an existing table schema, refer to [BigQuery Docs](https://cloud.google.com/bigquery/docs/managing-table-schemas), in some cases, its better to recreate the table.
> 2. The type mapping from TiDB to BigQuery is defined [here](https://github.com/pingcap-inc/tidb2dw/blob/main/pkg/bigquerysql/types.go).
> 3. tidb2dw will not sync column's default value to BigQuery, when you add a new column with default value, it will throw an error, you'd better to recreate the table in BigQuery and start a new replication task.
27 changes: 2 additions & 25 deletions pkg/bigquerysql/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bigquerysql

import (
"fmt"
"strconv"
"strings"

"github.com/pingcap-inc/tidb2dw/pkg/tidbsql"
Expand All @@ -23,13 +22,6 @@ func GetColumnModifyString(diff *tidbsql.ColumnDiff) (string, error) {
// https://cloud.google.com/bigquery/docs/reference/standard-sql/conversion_rules
strs = append(strs, fmt.Sprintf("`%s` SET DATA TYPE %s", diff.After.Name, colType))
}
if diff.Before.Default != diff.After.Default {
if diff.After.Default == nil {
strs = append(strs, fmt.Sprintf("`%s` DROP DEFAULT", diff.After.Name))
} else {
strs = append(strs, fmt.Sprintf("`%s` SET DEFAULT %s", diff.After.Name, getDefaultString(diff.After.Default)))
}
}
if diff.Before.Nullable != diff.After.Nullable {
if diff.After.Nullable == "true" {
strs = append(strs, fmt.Sprintf("`%s` DROP NOT NULL", diff.After.Name))
Expand Down Expand Up @@ -80,12 +72,7 @@ func GenDDLViaColumnsDiff(datasetID, tableID string, prevColumns []cloudstorage.
ddl += colStr + ";"
ddls = append(ddls, ddl)
if item.After.Default != nil {
ddls = append(
ddls,
fmt.Sprintf("ALTER TABLE `%s` ALTER COLUMN `%s` SET DEFAULT %s;", tableFullName, item.After.Name, getDefaultString(item.After.Default)),
fmt.Sprintf("UPDATE `%s` SET `%s` = %s WHERE TRUE;", tableFullName, item.After.Name, getDefaultString(item.After.Default)),
)

return nil, errors.New("BigQuery currently does not support add column with default value")
} else if item.After.Nullable == "true" {
ddls = append(ddls, fmt.Sprintf("ALTER TABLE `%s` ALTER COLUMN `%s` SET DEFAULT NULL;", tableFullName, item.After.Name))
}
Expand All @@ -110,14 +97,6 @@ func GenDDLViaColumnsDiff(datasetID, tableID string, prevColumns []cloudstorage.
return ddls, nil
}

func getDefaultString(val interface{}) string {
_, err := strconv.ParseFloat(fmt.Sprintf("%v", val), 64)
if err != nil {
return fmt.Sprintf("'%v'", val) // FIXME: escape
}
return fmt.Sprintf("%v", val)
}

// GetBigQueryColumnString returns a string describing the column in BigQuery, e.g.
// "id INT NOT NULL DEFAULT '0'"
// Refer to:
Expand All @@ -131,9 +110,7 @@ func GetBigQueryColumnString(column cloudstorage.TableCol, createTable bool) (st
}
sb.WriteString(fmt.Sprintf("`%s` %s", column.Name, colType))
if createTable {
if column.Default != nil {
sb.WriteString(fmt.Sprintf(` DEFAULT %s`, getDefaultString(column.Default)))
} else if column.Nullable == "true" {
if column.Nullable == "true" {
sb.WriteString(" DEFAULT NULL")
}
}
Expand Down

0 comments on commit 3fec55c

Please sign in to comment.