-
-
Notifications
You must be signed in to change notification settings - Fork 721
Add JSONL support to dolt table {import,export}
#10577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4196a34
fe5d1cf
ca3a329
06da432
ae32b5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -47,6 +47,8 @@ func DFFromString(dfStr string) DataFormat { | |||||
| return XlsxFile | ||||||
| case "json", ".json": | ||||||
| return JsonFile | ||||||
| case "jsonl", ".jsonl": | ||||||
| return JsonlFile | ||||||
|
Comment on lines
+50
to
+51
|
||||||
| case "jsonl", ".jsonl": | |
| return JsonlFile |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,174 @@ | ||||||
| // Copyright 2026 Dolthub, Inc. | ||||||
| // | ||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| // you may not use this file except in compliance with the License. | ||||||
| // You may obtain a copy of the License at | ||||||
| // | ||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| // | ||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| // See the License for the specific language governing permissions and | ||||||
| // limitations under the License. | ||||||
|
|
||||||
| package json | ||||||
|
|
||||||
| import ( | ||||||
| "bufio" | ||||||
| "context" | ||||||
| stdjson "encoding/json" | ||||||
| "errors" | ||||||
| "fmt" | ||||||
| "io" | ||||||
| "strings" | ||||||
|
|
||||||
| "github.com/dolthub/go-mysql-server/sql" | ||||||
| "golang.org/x/text/encoding/unicode" | ||||||
| "golang.org/x/text/transform" | ||||||
|
|
||||||
| "github.com/dolthub/dolt/go/libraries/doltcore/row" | ||||||
| "github.com/dolthub/dolt/go/libraries/doltcore/schema" | ||||||
| "github.com/dolthub/dolt/go/libraries/doltcore/table" | ||||||
| "github.com/dolthub/dolt/go/libraries/utils/filesys" | ||||||
| "github.com/dolthub/dolt/go/libraries/utils/iohelp" | ||||||
| "github.com/dolthub/dolt/go/store/types" | ||||||
| ) | ||||||
|
|
||||||
| // JSONLReader reads newline-delimited JSON objects, one per line. | ||||||
| type JSONLReader struct { | ||||||
| vrw types.ValueReadWriter | ||||||
| closer io.Closer | ||||||
| sch schema.Schema | ||||||
| bRd *bufio.Reader | ||||||
| numLine int | ||||||
| sampleRow sql.Row | ||||||
| } | ||||||
|
|
||||||
| var _ table.SqlTableReader = (*JSONLReader)(nil) | ||||||
|
|
||||||
| func OpenJSONLReader(vrw types.ValueReadWriter, path string, fs filesys.ReadableFS, sch schema.Schema) (*JSONLReader, error) { | ||||||
| r, err := fs.OpenForRead(path) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| return NewJSONLReader(vrw, r, sch) | ||||||
| } | ||||||
|
|
||||||
| // NewJSONLReader creates a JSONL reader. The bytes of the supplied reader are treated as UTF-8. If there is a UTF8, | ||||||
| // UTF16LE or UTF16BE BOM at the first bytes read, then it is stripped and the remaining contents of the reader are | ||||||
| // treated as that encoding. | ||||||
| func NewJSONLReader(vrw types.ValueReadWriter, r io.ReadCloser, sch schema.Schema) (*JSONLReader, error) { | ||||||
| if sch == nil { | ||||||
| return nil, errors.New("schema must be provided to JSONLReader") | ||||||
| } | ||||||
|
|
||||||
| textReader := transform.NewReader(r, unicode.BOMOverride(unicode.UTF8.NewDecoder())) | ||||||
| br := bufio.NewReaderSize(textReader, ReadBufSize) | ||||||
|
|
||||||
| return &JSONLReader{ | ||||||
| vrw: vrw, | ||||||
| closer: r, | ||||||
| sch: sch, | ||||||
| bRd: br, | ||||||
| }, nil | ||||||
| } | ||||||
|
|
||||||
| func (r *JSONLReader) Close(ctx context.Context) error { | ||||||
| if r.closer != nil { | ||||||
| err := r.closer.Close() | ||||||
| r.closer = nil | ||||||
| return err | ||||||
| } | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func (r *JSONLReader) GetSchema() schema.Schema { | ||||||
| return r.sch | ||||||
| } | ||||||
|
|
||||||
| func (r *JSONLReader) VerifySchema(sch schema.Schema) (bool, error) { | ||||||
| if r.sampleRow == nil { | ||||||
| row, err := r.ReadSqlRow(context.Background()) | ||||||
| if err == nil { | ||||||
| r.sampleRow = row | ||||||
| return true, nil | ||||||
| } | ||||||
| if err == io.EOF { | ||||||
| return false, nil | ||||||
| } | ||||||
| return false, err | ||||||
| } | ||||||
| return true, nil | ||||||
| } | ||||||
|
|
||||||
| func (r *JSONLReader) ReadRow(ctx context.Context) (row.Row, error) { | ||||||
| panic("deprecated") | ||||||
|
||||||
| panic("deprecated") | |
| return nil, fmt.Errorf("ReadRow is deprecated for JSONLReader; use ReadSqlRow instead") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The help text says "See the help for
dolt table importas the options are the same", but export now supportsjsonl(and also listssql/parquet) while table import help and validation do not list / support these formats. This is likely to confuse users; consider either updating the wording to avoid claiming parity with import, or explicitly calling out which--file-typevalues are valid for export vs import (and that stdout export is limited to csv/psv).