arkflow-plugin 0.1.0

High-performance Rust flow processing engine
Documentation
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Arrow Processor Components
//!
//! A processor for converting between binary data and the Arrow format

use arkflow_core::processor::{register_processor_builder, Processor, ProcessorBuilder};
use arkflow_core::{Bytes, Content, Error, MessageBatch};
use async_trait::async_trait;
use datafusion::arrow;
use datafusion::arrow::array::{
    ArrayRef, BooleanArray, Float64Array, Int64Array, NullArray, StringArray, UInt64Array,
};
use datafusion::arrow::datatypes::{DataType, Field, Schema};
use datafusion::arrow::record_batch::RecordBatch;
use serde_json::Value;
use std::sync::Arc;

/// Arrow format conversion processor configuration

/// Arrow Format Conversion Processor

pub struct JsonToArrowProcessor;

#[async_trait]
impl Processor for JsonToArrowProcessor {
    async fn process(&self, msg_batch: MessageBatch) -> Result<Vec<MessageBatch>, Error> {
        match msg_batch.content {
            Content::Arrow(_) => Err(Error::Process("The input must be binary data".to_string())),
            Content::Binary(v) => {
                let mut batches = Vec::with_capacity(v.len());
                for x in v {
                    let record_batch = json_to_arrow(&x)?;
                    batches.push(record_batch)
                }
                if batches.is_empty() {
                    return Ok(vec![]);
                }

                let schema = batches[0].schema();
                let batch = arrow::compute::concat_batches(&schema, &batches)
                    .map_err(|e| Error::Process(format!("Merge batches failed: {}", e)))?;
                Ok(vec![MessageBatch::new_arrow(batch)])
            }
        }
    }

    async fn close(&self) -> Result<(), Error> {
        Ok(())
    }
}

pub struct ArrowToJsonProcessor;

#[async_trait]
impl Processor for ArrowToJsonProcessor {
    async fn process(&self, msg_batch: MessageBatch) -> Result<Vec<MessageBatch>, Error> {
        match msg_batch.content {
            Content::Arrow(v) => {
                let json_data = arrow_to_json(&v)?;
                Ok(vec![MessageBatch::new_binary(vec![json_data])])
            }
            Content::Binary(_) => Err(Error::Process(
                "The input must be in Arrow format".to_string(),
            )),
        }
    }

    async fn close(&self) -> Result<(), Error> {
        Ok(())
    }
}

fn json_to_arrow(content: &Bytes) -> Result<RecordBatch, Error> {
    // 解析JSON内容
    let json_value: Value = serde_json::from_slice(content)
        .map_err(|e| Error::Process(format!("JSON解析错误: {}", e)))?;

    match json_value {
        Value::Object(obj) => {
            // 单个对象转换为单行表
            let mut fields = Vec::new();
            let mut columns: Vec<ArrayRef> = Vec::new();

            // 提取所有字段和值
            for (key, value) in obj {
                match value {
                    Value::Null => {
                        fields.push(Field::new(&key, DataType::Null, true));
                        // 空值列处理
                        columns.push(Arc::new(NullArray::new(1)));
                    }
                    Value::Bool(v) => {
                        fields.push(Field::new(&key, DataType::Boolean, false));
                        columns.push(Arc::new(BooleanArray::from(vec![v])));
                    }
                    Value::Number(v) => {
                        if v.is_i64() {
                            fields.push(Field::new(&key, DataType::Int64, false));
                            columns.push(Arc::new(Int64Array::from(vec![v.as_i64().unwrap()])));
                        } else if v.is_u64() {
                            fields.push(Field::new(&key, DataType::UInt64, false));
                            columns.push(Arc::new(UInt64Array::from(vec![v.as_u64().unwrap()])));
                        } else {
                            fields.push(Field::new(&key, DataType::Float64, false));
                            columns.push(Arc::new(Float64Array::from(vec![v
                                .as_f64()
                                .unwrap_or(0.0)])));
                        }
                    }
                    Value::String(v) => {
                        fields.push(Field::new(&key, DataType::Utf8, false));
                        columns.push(Arc::new(StringArray::from(vec![v])));
                    }
                    Value::Array(v) => {
                        fields.push(Field::new(&key, DataType::Utf8, false));
                        if let Ok(x) = serde_json::to_string(&v) {
                            columns.push(Arc::new(StringArray::from(vec![x])));
                        } else {
                            columns.push(Arc::new(StringArray::from(vec!["[]".to_string()])));
                        }
                    }
                    Value::Object(v) => {
                        fields.push(Field::new(&key, DataType::Utf8, false));
                        if let Ok(x) = serde_json::to_string(&v) {
                            columns.push(Arc::new(StringArray::from(vec![x])));
                        } else {
                            columns.push(Arc::new(StringArray::from(vec!["{}".to_string()])));
                        }
                    }
                };
            }

            // 创建schema和记录批次
            let schema = Arc::new(Schema::new(fields));
            RecordBatch::try_new(schema, columns)
                .map_err(|e| Error::Process(format!("创建Arrow记录批次失败: {}", e)))
        }
        _ => Err(Error::Process("输入必须是JSON对象".to_string())),
    }
}

/// Convert Arrow format to JSON
fn arrow_to_json(batch: &RecordBatch) -> Result<Vec<u8>, Error> {
    // 使用Arrow的JSON序列化功能
    let mut buf = Vec::new();
    let mut writer = arrow::json::ArrayWriter::new(&mut buf);
    writer
        .write(batch)
        .map_err(|e| Error::Process(format!("Arrow JSON序列化错误: {}", e)))?;
    writer
        .finish()
        .map_err(|e| Error::Process(format!("Arrow JSON序列化完成错误: {}", e)))?;

    Ok(buf)
}

pub(crate) struct JsonToArrowProcessorBuilder;
impl ProcessorBuilder for JsonToArrowProcessorBuilder {
    fn build(&self, _: &Option<serde_json::Value>) -> Result<Arc<dyn Processor>, Error> {
        Ok(Arc::new(JsonToArrowProcessor))
    }
}
pub(crate) struct ArrowToJsonProcessorBuilder;
impl ProcessorBuilder for ArrowToJsonProcessorBuilder {
    fn build(&self, _: &Option<serde_json::Value>) -> Result<Arc<dyn Processor>, Error> {
        Ok(Arc::new(ArrowToJsonProcessor))
    }
}

pub fn init() {
    register_processor_builder("arrow_to_json", Arc::new(ArrowToJsonProcessorBuilder));
    register_processor_builder("json_to_arrow", Arc::new(JsonToArrowProcessorBuilder));
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    // Helper function to create a simple JSON object for testing
    fn create_test_json() -> Vec<u8> {
        // Create a JSON object with different data types
        let mut map = HashMap::new();
        map.insert("null_field", Value::Null);
        map.insert("bool_field", Value::Bool(true));
        map.insert("int_field", Value::Number(serde_json::Number::from(42)));
        map.insert(
            "uint_field",
            Value::Number(serde_json::Number::from(100u64)),
        );
        map.insert(
            "float_field",
            Value::Number(serde_json::Number::from_f64(3.14).unwrap()),
        );
        map.insert("string_field", Value::String("test".to_string()));
        map.insert(
            "array_field",
            Value::Array(vec![Value::Number(serde_json::Number::from(1))]),
        );
        map.insert(
            "object_field",
            Value::Object({
                let mut inner = serde_json::Map::new();
                inner.insert("key".to_string(), Value::String("value".to_string()));
                inner
            }),
        );

        // Serialize to JSON bytes
        serde_json::to_vec(&map).unwrap()
    }

    #[tokio::test]
    async fn test_json_to_arrow_processor_success() {
        // Test successful conversion from JSON to Arrow
        let processor = JsonToArrowProcessor;
        let json_data = create_test_json();

        // Create a message batch with binary content
        let msg_batch = MessageBatch::new_binary(vec![json_data]);

        // Process the message batch
        let result = processor.process(msg_batch).await.unwrap();

        // Verify the result
        assert_eq!(result.len(), 1, "Should return one message batch");
        match &result[0].content {
            Content::Arrow(batch) => {
                // Verify the schema and data
                assert_eq!(batch.num_rows(), 1, "Should have one row");
                assert_eq!(batch.num_columns(), 8, "Should have 8 columns");

                // Verify column names
                let schema = batch.schema();
                let field_names: Vec<&str> =
                    schema.fields().iter().map(|f| f.name().as_str()).collect();
                assert!(field_names.contains(&"null_field"));
                assert!(field_names.contains(&"bool_field"));
                assert!(field_names.contains(&"int_field"));
                assert!(field_names.contains(&"uint_field"));
                assert!(field_names.contains(&"float_field"));
                assert!(field_names.contains(&"string_field"));
                assert!(field_names.contains(&"array_field"));
                assert!(field_names.contains(&"object_field"));
            }
            _ => panic!("Expected Arrow content"),
        }
    }

    #[tokio::test]
    async fn test_json_to_arrow_processor_empty_input() {
        // Test with empty input
        let processor = JsonToArrowProcessor;
        let msg_batch = MessageBatch::new_binary(vec![]);

        // Process the message batch
        let result = processor.process(msg_batch).await.unwrap();

        // Verify the result
        assert!(
            result.is_empty(),
            "Should return empty result for empty input"
        );
    }

    #[tokio::test]
    async fn test_json_to_arrow_processor_invalid_input() {
        // Test with invalid JSON input
        let processor = JsonToArrowProcessor;
        let invalid_json = b"{invalid json";

        // Create a message batch with invalid JSON content
        let msg_batch = MessageBatch::new_binary(vec![invalid_json.to_vec()]);

        // Process the message batch should fail
        let result = processor.process(msg_batch).await;
        assert!(result.is_err(), "Should return error for invalid JSON");
    }

    #[tokio::test]
    async fn test_json_to_arrow_processor_non_object_input() {
        // Test with JSON that is not an object (e.g., array)
        let processor = JsonToArrowProcessor;
        let array_json = serde_json::to_vec(&[1, 2, 3]).unwrap();

        // Create a message batch with array JSON content
        let msg_batch = MessageBatch::new_binary(vec![array_json]);

        // Process the message batch should fail
        let result = processor.process(msg_batch).await;
        assert!(result.is_err(), "Should return error for non-object JSON");
    }

    #[tokio::test]
    async fn test_json_to_arrow_processor_wrong_content_type() {
        // Test with Arrow content instead of Binary
        let processor = JsonToArrowProcessor;

        // Create a simple Arrow record batch
        let schema = Arc::new(Schema::new(vec![Field::new("test", DataType::Utf8, false)]));
        let columns: Vec<ArrayRef> = vec![Arc::new(StringArray::from(vec!["test"]))];
        let record_batch = RecordBatch::try_new(schema, columns).unwrap();

        // Create a message batch with Arrow content
        let msg_batch = MessageBatch::new_arrow(record_batch);

        // Process the message batch should fail
        let result = processor.process(msg_batch).await;
        assert!(result.is_err(), "Should return error for Arrow content");
    }

    #[tokio::test]
    async fn test_arrow_to_json_processor_success() {
        // Test successful conversion from Arrow to JSON
        let processor = ArrowToJsonProcessor;

        // Create a simple Arrow record batch
        let schema = Arc::new(Schema::new(vec![
            Field::new("string_field", DataType::Utf8, false),
            Field::new("int_field", DataType::Int64, false),
            Field::new("bool_field", DataType::Boolean, false),
        ]));

        let columns: Vec<ArrayRef> = vec![
            Arc::new(StringArray::from(vec!["test"])),
            Arc::new(Int64Array::from(vec![42])),
            Arc::new(BooleanArray::from(vec![true])),
        ];

        let record_batch = RecordBatch::try_new(schema, columns).unwrap();

        // Create a message batch with Arrow content
        let msg_batch = MessageBatch::new_arrow(record_batch);

        // Process the message batch
        let result = processor.process(msg_batch).await.unwrap();

        // Verify the result
        assert_eq!(result.len(), 1, "Should return one message batch");
        match &result[0].content {
            Content::Binary(v) => {
                assert_eq!(v.len(), 1, "Should have one binary item");

                // Parse the JSON to verify content
                let json_str = String::from_utf8_lossy(&v[0]);
                let json_value: serde_json::Value = serde_json::from_str(&json_str).unwrap();

                // Verify it's a valid JSON array with one object
                assert!(json_value.is_array(), "Should be a JSON array");
                let array = json_value.as_array().unwrap();
                assert_eq!(array.len(), 1, "Should have one object in array");

                let obj = &array[0];
                assert!(obj.is_object(), "Should be a JSON object");
                let obj_map = obj.as_object().unwrap();

                // Verify fields
                assert_eq!(obj_map["string_field"], "test");
                assert_eq!(obj_map["int_field"], 42);
                assert_eq!(obj_map["bool_field"], true);
            }
            _ => panic!("Expected Binary content"),
        }
    }

    #[tokio::test]
    async fn test_arrow_to_json_processor_wrong_content_type() {
        // Test with Binary content instead of Arrow
        let processor = ArrowToJsonProcessor;
        let binary_data = vec![1, 2, 3];

        // Create a message batch with Binary content
        let msg_batch = MessageBatch::new_binary(vec![binary_data]);

        // Process the message batch should fail
        let result = processor.process(msg_batch).await;
        assert!(result.is_err(), "Should return error for Binary content");
    }

    #[tokio::test]
    async fn test_json_to_arrow_function() {
        // Test the json_to_arrow function directly
        let json_data = create_test_json();
        let result = json_to_arrow(&json_data).unwrap();

        // Verify the result
        assert_eq!(result.num_rows(), 1, "Should have one row");
        assert_eq!(result.num_columns(), 8, "Should have 8 columns");

        // Verify specific values
        let schema = result.schema();
        for (i, field) in schema.fields().iter().enumerate() {
            match field.name().as_str() {
                "bool_field" => {
                    let array = result
                        .column(i)
                        .as_any()
                        .downcast_ref::<BooleanArray>()
                        .unwrap();
                    assert_eq!(array.value(0), true);
                }
                "int_field" => {
                    let array = result
                        .column(i)
                        .as_any()
                        .downcast_ref::<Int64Array>()
                        .unwrap();
                    assert_eq!(array.value(0), 42);
                }
                "string_field" => {
                    let array = result
                        .column(i)
                        .as_any()
                        .downcast_ref::<StringArray>()
                        .unwrap();
                    assert_eq!(array.value(0), "test");
                }
                _ => {}
            }
        }
    }

    #[tokio::test]
    async fn test_arrow_to_json_function() {
        // Test the arrow_to_json function directly
        // Create a simple Arrow record batch
        let schema = Arc::new(Schema::new(vec![Field::new(
            "test_field",
            DataType::Utf8,
            false,
        )]));

        let columns: Vec<ArrayRef> = vec![Arc::new(StringArray::from(vec!["test_value"]))];

        let record_batch = RecordBatch::try_new(schema, columns).unwrap();

        // Convert to JSON
        let json_bytes = arrow_to_json(&record_batch).unwrap();

        // Verify the result
        let json_str = String::from_utf8_lossy(&json_bytes);
        let json_value: serde_json::Value = serde_json::from_str(&json_str).unwrap();

        // Verify it's a valid JSON array with one object
        assert!(json_value.is_array(), "Should be a JSON array");
        let array = json_value.as_array().unwrap();
        assert_eq!(array.len(), 1, "Should have one object in array");

        let obj = &array[0];
        assert!(obj.is_object(), "Should be a JSON object");
        let obj_map = obj.as_object().unwrap();

        // Verify field
        assert_eq!(obj_map["test_field"], "test_value");
    }
}