@@ -33,7 +33,7 @@ import { JSONTypeAssembler } from '../visitor/jsontypeassembler';
3333import { JSONVectorAssembler } from '../visitor/jsonvectorassembler' ;
3434import { ArrayBufferViewInput , toUint8Array } from '../util/buffer' ;
3535import { Writable , ReadableInterop , ReadableDOMStreamOptions } from '../io/interfaces' ;
36- import { isPromise , isAsyncIterable , isWritableDOMStream , isWritableNodeStream } from '../util/compat' ;
36+ import { isPromise , isAsyncIterable , isWritableDOMStream , isWritableNodeStream , isIterable } from '../util/compat' ;
3737
3838export class RecordBatchWriter < T extends { [ key : string ] : DataType } = any > extends ReadableInterop < Uint8Array > implements Writable < RecordBatch < T > > {
3939
@@ -140,21 +140,36 @@ export class RecordBatchWriter<T extends { [key: string]: DataType } = any> exte
140140 return this ;
141141 }
142142
143- public write ( chunk ?: Table < T > | RecordBatch < T > | null ) {
144- let schema : Schema < T > | null ;
143+ public write ( payload ?: Table < T > | RecordBatch < T > | Iterable < RecordBatch < T > > | null ) {
144+
145+ let schema : Schema < T > | null = null ;
146+
145147 if ( ! this . _sink ) {
146148 throw new Error ( `RecordBatchWriter is closed` ) ;
147- } else if ( ! chunk || ! ( schema = chunk . schema ) ) {
149+ } else if ( payload === null || payload === undefined ) {
150+ return this . finish ( ) && undefined ;
151+ } else if ( payload instanceof Table && ! ( schema = payload . schema ) ) {
148152 return this . finish ( ) && undefined ;
149- } else if ( schema !== this . _schema ) {
153+ } else if ( payload instanceof RecordBatch && ! ( schema = payload . schema ) ) {
154+ return this . finish ( ) && undefined ;
155+ }
156+
157+ if ( schema && ! schema . compareTo ( this . _schema ) ) {
150158 if ( this . _started && this . _autoDestroy ) {
151159 return this . close ( ) ;
152160 }
153161 this . reset ( this . _sink , schema ) ;
154162 }
155- ( chunk instanceof Table )
156- ? this . writeAll ( chunk . chunks )
157- : this . _writeRecordBatch ( chunk ) ;
163+
164+ if ( payload instanceof RecordBatch ) {
165+ if ( payload . length > 0 ) {
166+ this . _writeRecordBatch ( payload ) ;
167+ }
168+ } else if ( payload instanceof Table ) {
169+ this . writeAll ( payload . chunks ) ;
170+ } else if ( isIterable ( payload ) ) {
171+ this . writeAll ( payload ) ;
172+ }
158173 }
159174
160175 protected _writeMessage < T extends MessageHeader > ( message : Message < T > , alignment = 8 ) {
@@ -363,7 +378,11 @@ export class RecordBatchJSONWriter<T extends { [key: string]: DataType } = any>
363378
364379/** @ignore */
365380function writeAll < T extends { [ key : string ] : DataType } = any > ( writer : RecordBatchWriter < T > , input : Table < T > | Iterable < RecordBatch < T > > ) {
366- const chunks = ( input instanceof Table ) ? input . chunks : input ;
381+ let chunks = input as Iterable < RecordBatch < T > > ;
382+ if ( input instanceof Table ) {
383+ chunks = input . chunks ;
384+ writer . reset ( undefined , input . schema ) ;
385+ }
367386 for ( const batch of chunks ) {
368387 writer . write ( batch ) ;
369388 }
0 commit comments