-
Notifications
You must be signed in to change notification settings - Fork 654
Expand file tree
/
Copy pathbatch.go
More file actions
67 lines (55 loc) · 1.94 KB
/
batch.go
File metadata and controls
67 lines (55 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package clickhouse
import (
"fmt"
"regexp"
"strings"
)
var normalizeInsertQueryMatch = regexp.MustCompile(`(?i)(?:(?:--[^\n]*|#![^\n]*|#\s[^\n]*)\n\s*)*(INSERT\s+INTO\s+([^(]+)(?:\s*\([^()]*(?:\([^()]*\)[^()]*)*\))?)(?:\s*VALUES)?`)
var truncateFormat = regexp.MustCompile(`(?i)\sFORMAT\s+[^\s]+`)
var truncateValues = regexp.MustCompile(`\sVALUES\s.*$`)
var extractInsertColumnsMatch = regexp.MustCompile(`(?si)INSERT INTO .+\s\((?P<Columns>.+)\)$`)
func splitColumnsRespectingQuotes(columnsStr string) []string {
var columns []string
var current strings.Builder
inBacktick := false
inDoubleQuote := false
for i := 0; i < len(columnsStr); i++ {
c := columnsStr[i]
if c == '`' && !inDoubleQuote {
inBacktick = !inBacktick
current.WriteByte(c)
} else if c == '"' && !inBacktick {
inDoubleQuote = !inDoubleQuote
current.WriteByte(c)
} else if c == ',' && !inBacktick && !inDoubleQuote {
columns = append(columns, strings.TrimSpace(current.String()))
current.Reset()
} else {
current.WriteByte(c)
}
}
if current.Len() > 0 {
columns = append(columns, strings.TrimSpace(current.String()))
}
return columns
}
func extractNormalizedInsertQueryAndColumns(query string) (normalizedQuery string, tableName string, columns []string, err error) {
query = truncateFormat.ReplaceAllString(query, "")
query = truncateValues.ReplaceAllString(query, "")
matches := normalizeInsertQueryMatch.FindStringSubmatch(query)
if len(matches) == 0 {
err = fmt.Errorf("invalid INSERT query: %s", query)
return
}
normalizedQuery = fmt.Sprintf("%s FORMAT Native", matches[1])
tableName = strings.TrimSpace(matches[2])
columns = make([]string, 0)
matches = extractInsertColumnsMatch.FindStringSubmatch(matches[1])
if len(matches) == 2 {
rawColumns := splitColumnsRespectingQuotes(matches[1])
for _, col := range rawColumns {
columns = append(columns, strings.ReplaceAll(strings.Trim(strings.TrimSpace(col), "\""), "`", ""))
}
}
return
}