Skip to content

Commit 36f2f80

Browse files
authored
feat: handle reserved keywords in GetOrderedColumns (#88)
* feat: handle reserved keywords in GetOrderedColumns * move to new helper file
1 parent c9809df commit 36f2f80

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

mc2mc/internal/client/helper.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package client
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
var (
9+
// reserved keywords https://www.alibabacloud.com/help/en/maxcompute/user-guide/reserved-words-and-keywords
10+
reservedKeywords = []string{
11+
"add", "after", "all", "alter", "analyze", "and", "archive", "array", "as", "asc",
12+
"before", "between", "bigint", "binary", "blob", "boolean", "both", "decimal",
13+
"bucket", "buckets", "by", "cascade", "case", "cast", "cfile", "change", "cluster",
14+
"clustered", "clusterstatus", "collection", "column", "columns", "comment", "compute",
15+
"concatenate", "continue", "create", "cross", "current", "cursor", "data", "database",
16+
"databases", "date", "datetime", "dbproperties", "deferred", "delete", "delimited",
17+
"desc", "describe", "directory", "disable", "distinct", "distribute", "double", "drop",
18+
"else", "enable", "end", "except", "escaped", "exclusive", "exists", "explain", "export",
19+
"extended", "external", "false", "fetch", "fields", "fileformat", "first", "float",
20+
"following", "format", "formatted", "from", "full", "function", "functions", "grant",
21+
"group", "having", "hold_ddltime", "idxproperties", "if", "import", "in", "index",
22+
"indexes", "inpath", "inputdriver", "inputformat", "insert", "int", "intersect", "into",
23+
"is", "items", "join", "keys", "lateral", "left", "lifecycle", "like", "limit", "lines",
24+
"load", "local", "location", "lock", "locks", "long", "map", "mapjoin", "materialized",
25+
"minus", "msck", "not", "no_drop", "null", "of", "offline", "offset", "on", "option",
26+
"or", "order", "out", "outer", "outputdriver", "outputformat", "over", "overwrite",
27+
"partition", "partitioned", "partitionproperties", "partitions", "percent", "plus",
28+
"preceding", "preserve", "procedure", "purge", "range", "rcfile", "read", "readonly",
29+
"reads", "rebuild", "recordreader", "recordwriter", "reduce", "regexp", "rename",
30+
"repair", "replace", "restrict", "revoke", "right", "rlike", "row", "rows", "schema",
31+
"schemas", "select", "semi", "sequencefile", "serde", "serdeproperties", "set", "shared",
32+
"show", "show_database", "smallint", "sort", "sorted", "ssl", "statistics", "status",
33+
"stored", "streamtable", "string", "struct", "table", "tables", "tablesample",
34+
"tblproperties", "temporary", "terminated", "textfile", "then", "timestamp", "tinyint",
35+
"to", "touch", "transform", "trigger", "true", "type", "unarchive", "unbounded", "undo",
36+
"union", "uniontype", "uniquejoin", "unlock", "unsigned", "update", "use", "using",
37+
"utc", "utc_timestamp", "view", "when", "where", "while", "div",
38+
}
39+
40+
reservedKeywordsMap map[string]bool
41+
)
42+
43+
func init() {
44+
reservedKeywordsMap = make(map[string]bool, len(reservedKeywords))
45+
for _, keyword := range reservedKeywords {
46+
reservedKeywordsMap[keyword] = true
47+
}
48+
}
49+
50+
func sanitizeColumnName(columnName string) string {
51+
// if column name is a reserved keyword, add backticks around it
52+
if _, ok := reservedKeywordsMap[strings.ToLower(columnName)]; ok {
53+
return fmt.Sprintf("`%s`", columnName)
54+
}
55+
56+
return columnName
57+
}

mc2mc/internal/client/odps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (c *odpsClient) GetOrderedColumns(tableID string) ([]string, error) {
104104
}
105105
var columnNames []string
106106
for _, column := range table.Schema().Columns {
107-
columnNames = append(columnNames, column.Name)
107+
columnNames = append(columnNames, sanitizeColumnName(column.Name))
108108
}
109109

110110
return columnNames, nil

0 commit comments

Comments
 (0)