15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
+ import { RecordBatchStreamWriterOptions } from 'apache-arrow/ipc/writer.js' ;
18
19
import {
19
20
generateDictionaryTables , generateRandomTables
20
21
} from '../../../data/tables.js' ;
@@ -23,6 +24,7 @@ import { validateRecordBatchIterator } from '../validate.js';
23
24
24
25
import {
25
26
builderThroughIterable ,
27
+ CompressionType ,
26
28
Dictionary ,
27
29
Int32 ,
28
30
RecordBatch ,
@@ -32,6 +34,8 @@ import {
32
34
Uint32 ,
33
35
Vector
34
36
} from 'apache-arrow' ;
37
+ import { Codec , compressionRegistry } from 'apache-arrow/ipc/compression/registry' ;
38
+ import * as lz4js from 'lz4js' ;
35
39
36
40
describe ( 'RecordBatchFileWriter' , ( ) => {
37
41
for ( const table of generateRandomTables ( [ 10 , 20 , 30 ] ) ) {
@@ -41,6 +45,24 @@ describe('RecordBatchFileWriter', () => {
41
45
testFileWriter ( table , `${ table . schema . fields [ 0 ] } ` ) ;
42
46
}
43
47
48
+ const compressionTypes = [ CompressionType . LZ4_FRAME /*, CompressionType.ZSTD*/ ] ;
49
+
50
+ const lz4Codec : Codec = {
51
+ encode ( data : Uint8Array ) : Uint8Array {
52
+ return lz4js . compress ( data ) ;
53
+ } ,
54
+ decode ( data : Uint8Array ) : Uint8Array {
55
+ return lz4js . decompress ( data ) ;
56
+ }
57
+ } ;
58
+ compressionRegistry . set ( CompressionType . LZ4_FRAME , lz4Codec ) ;
59
+
60
+ const table = generate . table ( [ 10 , 20 , 30 ] ) . table ;
61
+ for ( const compressionType of compressionTypes ) {
62
+ const testName = `[${ table . schema . fields . join ( ', ' ) } ] - ${ CompressionType [ compressionType ] } compressed` ;
63
+ testFileWriter ( table , testName , { compressionType } ) ;
64
+ }
65
+
44
66
it ( 'should throw if attempting to write replacement dictionary batches' , async ( ) => {
45
67
const type = new Dictionary < Uint32 , Int32 > ( new Uint32 , new Int32 , 0 ) ;
46
68
const writer = new RecordBatchFileWriter ( ) ;
@@ -91,14 +113,14 @@ describe('RecordBatchFileWriter', () => {
91
113
} ) ;
92
114
} ) ;
93
115
94
- function testFileWriter ( table : Table , name : string ) {
116
+ function testFileWriter ( table : Table , name : string , options ?: RecordBatchStreamWriterOptions ) {
95
117
describe ( `should write the Arrow IPC file format (${ name } )` , ( ) => {
96
- test ( `Table` , validateTable . bind ( 0 , table ) ) ;
118
+ test ( `Table` , validateTable . bind ( 0 , table , options ) ) ;
97
119
} ) ;
98
120
}
99
121
100
- async function validateTable ( source : Table ) {
101
- const writer = RecordBatchFileWriter . writeAll ( source ) ;
122
+ async function validateTable ( source : Table , options ?: RecordBatchStreamWriterOptions ) {
123
+ const writer = RecordBatchFileWriter . writeAll ( source , options ) ;
102
124
const result = new Table ( RecordBatchReader . from ( await writer . toUint8Array ( ) ) ) ;
103
125
validateRecordBatchIterator ( 3 , source . batches ) ;
104
126
expect ( result ) . toEqualTable ( source ) ;
0 commit comments