diff --git a/mc2mc/internal/client/helper.go b/mc2mc/internal/client/helper.go new file mode 100644 index 0000000..add750f --- /dev/null +++ b/mc2mc/internal/client/helper.go @@ -0,0 +1,57 @@ +package client + +import ( + "fmt" + "strings" +) + +var ( + // reserved keywords https://www.alibabacloud.com/help/en/maxcompute/user-guide/reserved-words-and-keywords + reservedKeywords = []string{ + "add", "after", "all", "alter", "analyze", "and", "archive", "array", "as", "asc", + "before", "between", "bigint", "binary", "blob", "boolean", "both", "decimal", + "bucket", "buckets", "by", "cascade", "case", "cast", "cfile", "change", "cluster", + "clustered", "clusterstatus", "collection", "column", "columns", "comment", "compute", + "concatenate", "continue", "create", "cross", "current", "cursor", "data", "database", + "databases", "date", "datetime", "dbproperties", "deferred", "delete", "delimited", + "desc", "describe", "directory", "disable", "distinct", "distribute", "double", "drop", + "else", "enable", "end", "except", "escaped", "exclusive", "exists", "explain", "export", + "extended", "external", "false", "fetch", "fields", "fileformat", "first", "float", + "following", "format", "formatted", "from", "full", "function", "functions", "grant", + "group", "having", "hold_ddltime", "idxproperties", "if", "import", "in", "index", + "indexes", "inpath", "inputdriver", "inputformat", "insert", "int", "intersect", "into", + "is", "items", "join", "keys", "lateral", "left", "lifecycle", "like", "limit", "lines", + "load", "local", "location", "lock", "locks", "long", "map", "mapjoin", "materialized", + "minus", "msck", "not", "no_drop", "null", "of", "offline", "offset", "on", "option", + "or", "order", "out", "outer", "outputdriver", "outputformat", "over", "overwrite", + "partition", "partitioned", "partitionproperties", "partitions", "percent", "plus", + "preceding", "preserve", "procedure", "purge", "range", "rcfile", "read", "readonly", + "reads", "rebuild", "recordreader", "recordwriter", "reduce", "regexp", "rename", + "repair", "replace", "restrict", "revoke", "right", "rlike", "row", "rows", "schema", + "schemas", "select", "semi", "sequencefile", "serde", "serdeproperties", "set", "shared", + "show", "show_database", "smallint", "sort", "sorted", "ssl", "statistics", "status", + "stored", "streamtable", "string", "struct", "table", "tables", "tablesample", + "tblproperties", "temporary", "terminated", "textfile", "then", "timestamp", "tinyint", + "to", "touch", "transform", "trigger", "true", "type", "unarchive", "unbounded", "undo", + "union", "uniontype", "uniquejoin", "unlock", "unsigned", "update", "use", "using", + "utc", "utc_timestamp", "view", "when", "where", "while", "div", + } + + reservedKeywordsMap map[string]bool +) + +func init() { + reservedKeywordsMap = make(map[string]bool, len(reservedKeywords)) + for _, keyword := range reservedKeywords { + reservedKeywordsMap[keyword] = true + } +} + +func sanitizeColumnName(columnName string) string { + // if column name is a reserved keyword, add backticks around it + if _, ok := reservedKeywordsMap[strings.ToLower(columnName)]; ok { + return fmt.Sprintf("`%s`", columnName) + } + + return columnName +} diff --git a/mc2mc/internal/client/odps.go b/mc2mc/internal/client/odps.go index 46658e9..e90d6cf 100644 --- a/mc2mc/internal/client/odps.go +++ b/mc2mc/internal/client/odps.go @@ -104,7 +104,7 @@ func (c *odpsClient) GetOrderedColumns(tableID string) ([]string, error) { } var columnNames []string for _, column := range table.Schema().Columns { - columnNames = append(columnNames, column.Name) + columnNames = append(columnNames, sanitizeColumnName(column.Name)) } return columnNames, nil