-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjsonl.go
More file actions
423 lines (377 loc) · 10.1 KB
/
Copy pathjsonl.go
File metadata and controls
423 lines (377 loc) · 10.1 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package otters
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"math"
"os"
"strconv"
"strings"
"time"
)
// JSONLOptions provides options for JSONL reading
type JSONLOptions struct {
SkipRows int // Number of lines to skip at the beginning
MaxRows int // Maximum number of rows to read (0 = unlimited)
}
// ReadJSONL reads a JSON Lines file (one flat JSON object per line) and
// returns a DataFrame with automatic type inference.
//
// The schema is the union of keys across all lines, in first-seen order.
// Missing keys and JSON nulls fill with the column type's zero value, the
// same convention as empty CSV cells. JSON's native types are respected: a
// JSON string "123" stays a string, and integer-valued numbers produce
// Int64Type columns. A string column where every non-empty value parses with
// the shared time formats becomes TimeType. Columns whose values mix types
// across lines, or contain nested objects/arrays, become StringType (nested
// values are stringified as compact JSON).
func ReadJSONL(filename string) (*DataFrame, error) {
return ReadJSONLWithOptions(filename, JSONLOptions{})
}
// ReadJSONLWithOptions reads a JSON Lines file with custom options
func ReadJSONLWithOptions(filename string, options JSONLOptions) (*DataFrame, error) {
file, err := os.Open(filename)
if err != nil {
return nil, wrapError("ReadJSONL", err)
}
defer file.Close()
return readJSONL(file, options, "ReadJSONL")
}
// ReadJSONLFromString reads JSON Lines data from a string
func ReadJSONLFromString(data string) (*DataFrame, error) {
return ReadJSONLFromStringWithOptions(data, JSONLOptions{})
}
// ReadJSONLFromStringWithOptions reads JSON Lines data from a string with options
func ReadJSONLFromStringWithOptions(data string, options JSONLOptions) (*DataFrame, error) {
return readJSONL(strings.NewReader(data), options, "ReadJSONLFromString")
}
// maxJSONLLineSize bounds a single JSONL line (16 MB).
const maxJSONLLineSize = 16 * 1024 * 1024
// readJSONL parses JSONL from r into a DataFrame.
func readJSONL(r io.Reader, options JSONLOptions, operation string) (*DataFrame, error) {
scanner := bufio.NewScanner(r)
scanner.Buffer(make([]byte, 0, 64*1024), maxJSONLLineSize)
var rows []map[string]any
var order []string
seen := make(map[string]bool)
lineNum := 0
for scanner.Scan() {
lineNum++
if lineNum <= options.SkipRows {
continue
}
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
obj, keys, err := decodeJSONLine(line)
if err != nil {
return nil, &OtterError{
Op: operation,
Row: lineNum,
Message: fmt.Sprintf("invalid JSONL line: %v", err),
Cause: err,
}
}
for _, key := range keys {
if !seen[key] {
seen[key] = true
order = append(order, key)
}
}
rows = append(rows, obj)
if options.MaxRows > 0 && len(rows) >= options.MaxRows {
break
}
}
if err := scanner.Err(); err != nil {
return nil, wrapError(operation, err)
}
return buildDataFrameFromJSONLRows(order, rows, operation)
}
// decodeJSONLine decodes one JSONL line into a value map plus the object's
// keys in their order of appearance (a plain map decode would lose it).
func decodeJSONLine(line string) (map[string]any, []string, error) {
dec := json.NewDecoder(strings.NewReader(line))
dec.UseNumber()
tok, err := dec.Token()
if err != nil {
return nil, nil, err
}
if delim, ok := tok.(json.Delim); !ok || delim != '{' {
return nil, nil, fmt.Errorf("line is not a JSON object")
}
obj := make(map[string]any)
var keys []string
for dec.More() {
keyTok, err := dec.Token()
if err != nil {
return nil, nil, err
}
key := keyTok.(string)
var value any
if err := dec.Decode(&value); err != nil {
return nil, nil, err
}
if _, duplicate := obj[key]; !duplicate {
keys = append(keys, key)
}
obj[key] = value
}
if _, err := dec.Token(); err != nil { // consume closing '}'
return nil, nil, err
}
if _, err := dec.Token(); err != io.EOF {
return nil, nil, fmt.Errorf("unexpected data after JSON object")
}
return obj, keys, nil
}
// buildDataFrameFromJSONLRows constructs a DataFrame from decoded JSONL rows
func buildDataFrameFromJSONLRows(order []string, rows []map[string]any, operation string) (*DataFrame, error) {
if len(order) == 0 {
return NewDataFrame(), nil
}
series := make([]*Series, 0, len(order))
for _, name := range order {
values := make([]any, len(rows))
for i, row := range rows {
values[i] = row[name] // missing key yields nil, same as JSON null
}
colType := inferJSONLColumnType(values)
s, err := buildJSONLSeries(name, values, colType)
if err != nil {
return nil, wrapColumnError(operation, name, err)
}
series = append(series, s)
}
return NewDataFrameFromSeries(series...)
}
// inferJSONLColumnType picks a column type from decoded JSON values.
// Unlike CSV inference, JSON values carry their own types, so strings are
// never reinterpreted as numbers or bools; the only string promotion is to
// TimeType when every non-empty value parses as a time.
func inferJSONLColumnType(values []any) ColumnType {
sawNumber := false
sawFloat := false
sawBool := false
sawString := false
sawNested := false
allStringsAreTime := true
sawNonEmptyString := false
for _, v := range values {
switch t := v.(type) {
case nil:
continue
case json.Number:
sawNumber = true
if _, err := strconv.ParseInt(string(t), 10, 64); err != nil {
sawFloat = true
}
case bool:
sawBool = true
case string:
sawString = true
trimmed := strings.TrimSpace(t)
if trimmed != "" {
sawNonEmptyString = true
if !isTimeValue(trimmed) {
allStringsAreTime = false
}
}
default: // map[string]any or []any
sawNested = true
}
}
categories := 0
for _, saw := range []bool{sawNumber, sawBool, sawString, sawNested} {
if saw {
categories++
}
}
// No values, mixed types, or nested values: fall back to string.
if categories != 1 || sawNested {
return StringType
}
if sawNumber {
if sawFloat {
return Float64Type
}
return Int64Type
}
if sawBool {
return BoolType
}
if sawNonEmptyString && allStringsAreTime {
return TimeType
}
return StringType
}
// buildJSONLSeries converts decoded JSON values into a typed Series.
// nil (JSON null or missing key) becomes the column type's zero value.
func buildJSONLSeries(name string, values []any, colType ColumnType) (*Series, error) {
switch colType {
case Int64Type:
data := make([]int64, len(values))
for i, v := range values {
if v == nil {
continue
}
n, err := v.(json.Number).Int64()
if err != nil {
return nil, wrapError("buildJSONLSeries", err)
}
data[i] = n
}
return newSeriesOwned(name, data)
case Float64Type:
data := make([]float64, len(values))
for i, v := range values {
if v == nil {
continue
}
f, err := v.(json.Number).Float64()
if err != nil {
return nil, wrapError("buildJSONLSeries", err)
}
data[i] = f
}
return newSeriesOwned(name, data)
case BoolType:
data := make([]bool, len(values))
for i, v := range values {
if v == nil {
continue
}
data[i] = v.(bool)
}
return newSeriesOwned(name, data)
case TimeType:
data := make([]time.Time, len(values))
for i, v := range values {
if v == nil {
continue
}
trimmed := strings.TrimSpace(v.(string))
if trimmed == "" {
continue
}
t, err := parseTimeValue(trimmed)
if err != nil {
return nil, wrapError("buildJSONLSeries", err)
}
data[i] = t
}
return newSeriesOwned(name, data)
default: // StringType
data := make([]string, len(values))
for i, v := range values {
s, err := formatJSONValueAsString(v)
if err != nil {
return nil, wrapError("buildJSONLSeries", err)
}
data[i] = s
}
return newSeriesOwned(name, data)
}
}
// formatJSONValueAsString renders a decoded JSON value canonically for a
// string column; nested values become compact JSON.
func formatJSONValueAsString(v any) (string, error) {
switch t := v.(type) {
case nil:
return "", nil
case string:
return t, nil
case json.Number:
return string(t), nil
case bool:
return strconv.FormatBool(t), nil
default:
raw, err := json.Marshal(t)
if err != nil {
return "", err
}
return string(raw), nil
}
}
// WriteJSONL writes a DataFrame to a JSON Lines file, one object per row
// with keys in column order. Times are written as RFC3339 strings; zero
// times, NaN, and ±Inf are written as null.
func (df *DataFrame) WriteJSONL(filename string) error {
if df.err != nil {
return df.err
}
file, err := os.Create(filename)
if err != nil {
return wrapError("WriteJSONL", err)
}
defer file.Close()
writer := bufio.NewWriter(file)
var buf bytes.Buffer
for i := 0; i < df.length; i++ {
buf.Reset()
buf.WriteByte('{')
for j, colName := range df.order {
if j > 0 {
buf.WriteByte(',')
}
key, err := json.Marshal(colName)
if err != nil {
return wrapColumnError("WriteJSONL", colName, err)
}
buf.Write(key)
buf.WriteByte(':')
value, err := df.columns[colName].Get(i)
if err != nil {
return wrapColumnError("WriteJSONL", colName, err)
}
formatted, err := formatValueForJSONL(value)
if err != nil {
return wrapColumnError("WriteJSONL", colName, err)
}
buf.WriteString(formatted)
}
buf.WriteByte('}')
buf.WriteByte('\n')
if _, err := writer.Write(buf.Bytes()); err != nil {
return wrapError("WriteJSONL", err)
}
}
if err := writer.Flush(); err != nil {
return wrapError("WriteJSONL", err)
}
return nil
}
// formatValueForJSONL formats a single cell as a JSON value
func formatValueForJSONL(value any) (string, error) {
switch v := value.(type) {
case string:
raw, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(raw), nil
case int64:
return strconv.FormatInt(v, 10), nil
case float64:
if math.IsNaN(v) || math.IsInf(v, 0) {
return "null", nil
}
raw, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(raw), nil
case bool:
return strconv.FormatBool(v), nil
case time.Time:
if v.IsZero() {
return "null", nil
}
return `"` + v.Format(time.RFC3339) + `"`, nil
default:
return "", fmt.Errorf("unsupported value type: %T", value)
}
}