Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/inputflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (f *Flags) SetFlags(fs *flag.FlagSet) {
fs.BoolVar(&f.Dynamic, "dynamic", false, "disable static type checking of inputs")
fs.StringVar(&opts.Format, "i", opts.Format, "format of input data [auto,arrows,bsup,csv,json,line,parquet,sup,tsv,zeek]")
fs.IntVar(&opts.InputCap, "inputcap", 0, "limit size of batched units of input")
fs.IntVar(&opts.JSON.InputCapBytes, "json.inputcapbytes", 0, "limit size in bytes of batched units of JSON input")
fs.BoolVar(&f.Static, "static", false, "statically type check all inputs (reading into memory inputs that can be read only once)")
}

Expand Down
2 changes: 1 addition & 1 deletion sio/anyio/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func lookupReader(ctx context.Context, sctx *super.Context, r io.Reader, opts Re
case "line":
return newVioPuller(sctx, lineio.NewReader(r)), nil
case "json":
return jsonio.NewReader(context.Background(), sctx, r, opts.Pushdown, opts.ConcurrentReaders, opts.InputCap), nil
return jsonio.NewReader(context.Background(), sctx, r, opts.Pushdown, opts.ConcurrentReaders, opts.InputCap, opts.JSON), nil
case "parquet":
return parquetio.NewReader(ctx, sctx, r, opts.Pushdown, opts.ConcurrentReaders)
case "sup":
Expand Down
5 changes: 3 additions & 2 deletions sio/anyio/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ReaderOpts struct {
ConcurrentReaders int
CSV csvio.ReaderOpts
InputCap int
JSON jsonio.ReaderOpts
}

func NewReader(ctx context.Context, sctx *super.Context, r io.Reader, opts ReaderOpts) (vio.Puller, error) {
Expand Down Expand Up @@ -93,7 +94,7 @@ func NewReader(ctx context.Context, sctx *super.Context, r io.Reader, opts Reade
// sake of tests.
jsonErr := isJSONStream(track, 10)
if jsonErr == nil {
return jsonio.NewReader(ctx, sctx, track.Reader(), opts.Pushdown, opts.ConcurrentReaders, opts.InputCap), nil
return jsonio.NewReader(ctx, sctx, track.Reader(), opts.Pushdown, opts.ConcurrentReaders, opts.InputCap, opts.JSON), nil
}
jsonErr = fmt.Errorf("json: %w", jsonErr)
track.Reset()
Expand Down Expand Up @@ -171,7 +172,7 @@ func isCSVStream(track *Track, delim rune, name string) error {
}

func isJSONStream(track *Track, want int) error {
r := jsonio.NewValReader(track, 0)
r := jsonio.NewValReader(track)
for range want {
if _, err := r.Next(); err != nil {
if errors.Is(err, io.EOF) {
Expand Down
29 changes: 25 additions & 4 deletions sio/jsonio/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,39 @@ import (

const defaultInputCap = 10 * 1024

// defaultByteCap bounds the raw input bytes in a batch so that inputs with
// large values don't produce huge batches. Inputs with small values hit
// defaultInputCap first. Memory-constrained environments (e.g., wasm) may
// want a smaller cap via ReaderOpts.InputCapBytes.
const defaultByteCap = 16 * 1024 * 1024

type ReaderOpts struct {
// InputCapBytes limits the raw input bytes in a batch. Zero means
// defaultByteCap.
InputCapBytes int
}

type Reader struct {
sctx *super.Context
ctx context.Context
stream *stream
byteCap int
projection field.Projection

hasClosed atomic.Bool
}

func NewReader(ctx context.Context, sctx *super.Context, r io.Reader, p vio.Pushdown, concurrentReaders, inputCap int) *Reader {
// NewReader returns a Reader that parses input in batches of at most
// inputCap values or about opts.InputCapBytes bytes, whichever is reached
// first. Zero means the default for either limit.
func NewReader(ctx context.Context, sctx *super.Context, r io.Reader, p vio.Pushdown, concurrentReaders, inputCap int, opts ReaderOpts) *Reader {
if inputCap == 0 {
inputCap = defaultInputCap
}
byteCap := opts.InputCapBytes
if byteCap == 0 {
byteCap = defaultByteCap
}
var proj field.Projection
if p != nil {
// Type-only projections aren't supported so the data for
Expand All @@ -41,7 +61,8 @@ func NewReader(ctx context.Context, sctx *super.Context, r io.Reader, p vio.Push
return &Reader{
sctx: sctx,
ctx: ctx,
stream: newStream(ctx, r, concurrentReaders, inputCap),
byteCap: byteCap,
stream: newStream(ctx, r, concurrentReaders, inputCap, byteCap),
projection: proj,
}
}
Expand All @@ -63,12 +84,12 @@ func (r *Reader) ConcurrentPull(done bool, _ int) (vector.Any, error) {
for i := range table.Len() {
if err := ast.Preorder(byteconv.UnsafeString(table.Bytes(i)), builder, nil); err != nil {
err = preorderErr(i, start, table, err)
bytesTablePool.Put(table)
putBytesTable(table, r.byteCap)
r.close()
return nil, err
}
}
bytesTablePool.Put(table)
putBytesTable(table, r.byteCap)
return jsonvec.Materialize(r.sctx, builder), nil
}

Expand Down
31 changes: 24 additions & 7 deletions sio/jsonio/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ type stream struct {
once sync.Once
ctx context.Context
inputCap int
byteCap int
}

func newStream(ctx context.Context, r io.Reader, n int, inputCap int) *stream {
func newStream(ctx context.Context, r io.Reader, n, inputCap, byteCap int) *stream {
return &stream{
r: r,
ch: make(chan result, n),
ctx: ctx,
done: make(chan struct{}),
inputCap: inputCap,
byteCap: byteCap,
}
}

Expand All @@ -46,9 +48,9 @@ func (s *stream) next() (*vector.BytesTable, int, error) {

func (s *stream) run() {
defer close(s.ch)
r := NewValReader(s.r, s.inputCap)
r := NewValReader(s.r)
for {
batch, startLineNum, err := readBatch(r)
batch, startLineNum, err := readBatch(r, s.inputCap, s.byteCap)
select {
case s.ch <- result{batch, startLineNum, err}:
if batch == nil || err != nil {
Expand Down Expand Up @@ -77,19 +79,34 @@ func newBytesTable(inputCap int) *vector.BytesTable {
return b
}

func readBatch(r *valReader) (*vector.BytesTable, int, error) {
t := newBytesTable(r.inputCap)
// putBytesTable returns t to the pool unless its buffer grew well beyond
// byteCap (e.g., to hold a single oversized value), in which case it is
// dropped so the pool doesn't pin that memory.
func putBytesTable(t *vector.BytesTable, byteCap int) {
if cap(t.RawBytes()) <= 2*byteCap {
bytesTablePool.Put(t)
}
}

// readBatch reads values from r until the batch holds inputCap values or
// at least byteCap bytes, whichever comes first. A batch always holds at
// least one value so a value larger than byteCap is still read.
func readBatch(r *valReader, inputCap, byteCap int) (*vector.BytesTable, int, error) {
t := newBytesTable(inputCap)
start := r.lineNumber()
var err error
for range r.inputCap {
for range inputCap {
var b []byte
if b, err = r.Next(); err != nil {
break
}
t.Append(b)
if len(t.RawBytes()) >= byteCap {
break
}
}
if t.Len() == 0 {
bytesTablePool.Put(t)
putBytesTable(t, byteCap)
t = nil
}
if errors.Is(err, io.EOF) {
Expand Down
15 changes: 7 additions & 8 deletions sio/jsonio/valreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ import (
const maxSize = 512 * 1024 * 1024

type valReader struct {
r io.Reader
buf []byte
cursor []byte
EOF bool
line int
inputCap int
r io.Reader
buf []byte
cursor []byte
EOF bool
line int
}

func NewValReader(r io.Reader, inputCap int) *valReader {
return &valReader{r: r, buf: make([]byte, 512*1024), inputCap: inputCap}
func NewValReader(r io.Reader) *valReader {
return &valReader{r: r, buf: make([]byte, 512*1024)}
}

func (r *valReader) Next() ([]byte, error) {
Expand Down
Loading