-
Notifications
You must be signed in to change notification settings - Fork 54
/
insert.go
155 lines (121 loc) · 2.83 KB
/
insert.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package postgres
import (
"bufio"
"database/sql"
"fmt"
"github.com/lib/pq"
"github.com/scratchdata/scratchdata/pkg/util"
"github.com/tidwall/gjson"
"os"
"strings"
"github.com/rs/zerolog/log"
)
func (s *PostgresServer) createColumns(table string, jsonTypes map[string]string) error {
for colName, jsonType := range jsonTypes {
colType := "VARCHAR"
switch jsonType {
case "int":
colType = "BIGINT"
case "bool":
colType = "BOOLEAN"
case "float":
colType = "DOUBLE PRECISION"
case "string":
colType = "VARCHAR"
default:
colType = "VARCHAR"
}
sql := fmt.Sprintf("ALTER TABLE %s ADD COLUMN \"%s\" %s", s.Schema+"."+table, colName, colType)
_, err := s.conn.Exec(sql)
if err != nil {
if !strings.Contains(err.Error(), "already exists") {
log.Error().Err(err).Str("column", colName).Msg("createColumns: cannot create column")
return err
}
}
log.Trace().Str("name", colName).Str("type", colType).Msg("Created column")
}
return nil
}
func (s *PostgresServer) CreateColumns(table string, fileName string) error {
input, err := os.Open(fileName)
if err != nil {
return err
}
// Infer JSON types for the input
jsonTypes, err := util.GetJSONTypes(input)
if err != nil {
return err
}
err = s.createColumns(table, jsonTypes)
if err != nil {
return err
}
err = input.Close()
if err != nil {
log.Error().Err(err).Str("filename", fileName).Msg("Unable to close file")
}
return nil
}
func (s *PostgresServer) CreateEmptyTable(table string) error {
sql := fmt.Sprintf("CREATE TABLE IF NOT EXISTS \"%s\" (__row_id BIGINT)", table)
_, err := s.conn.Exec(sql)
return err
}
func (s *PostgresServer) InsertFromNDJsonFile(table string, filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
const maxCapacity int = 50 * 1000000 // 50 MB line max
buf := make([]byte, maxCapacity)
scanner.Buffer(buf, maxCapacity)
txn, err := s.conn.Begin()
if err != nil {
return err
}
var stmt *sql.Stmt
recordsScanned := 0
keys := make([]string, 0)
var values []any
for scanner.Scan() {
line := gjson.ParseBytes(scanner.Bytes())
if recordsScanned == 0 {
keyJson := line.Get("@keys")
for _, key := range keyJson.Array() {
keys = append(keys, key.String())
}
values = make([]any, len(keys))
stmt, err = txn.Prepare(pq.CopyInSchema(s.Schema, table, keys...))
if err != nil {
return err
}
}
for i, key := range keys {
values[i] = line.Get(key).String()
}
_, err = stmt.Exec(values...)
if err != nil {
return err
}
recordsScanned++
}
if err := scanner.Err(); err != nil {
return err
}
_, err = stmt.Exec()
if err != nil {
return err
}
err = stmt.Close()
if err != nil {
return err
}
err = txn.Commit()
if err != nil {
return err
}
return nil
}