Skip to main content

client/
flight.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use arrow_flight::FlightData;
16use common_grpc::flight::{FlightDecoder, FlightMessage};
17use snafu::ResultExt;
18
19use crate::Result;
20use crate::error::{ConvertFlightDataSnafu, Error};
21
22pub(crate) fn decode_flight_data(
23    decoder: &mut FlightDecoder,
24    flight_data: std::result::Result<FlightData, tonic::Status>,
25) -> Option<Result<FlightMessage>> {
26    flight_data
27        .map_err(Error::from)
28        .and_then(|data| decoder.try_decode(&data).context(ConvertFlightDataSnafu))
29        .transpose()
30}
31
32#[cfg(test)]
33mod tests {
34    use std::sync::Arc;
35
36    use common_grpc::flight::FlightEncoder;
37    use datatypes::arrow::array::{DictionaryArray, StringArray, UInt32Array};
38    use datatypes::arrow::datatypes::{DataType, Field, Schema, UInt32Type};
39    use datatypes::arrow::record_batch::RecordBatch;
40
41    use super::*;
42
43    #[test]
44    fn test_decode_flight_data_skips_dictionary_batches() {
45        let schema = Arc::new(Schema::new(vec![Field::new_dictionary(
46            "host",
47            DataType::UInt32,
48            DataType::Utf8,
49            false,
50        )]));
51        let batch = RecordBatch::try_new(
52            schema.clone(),
53            vec![Arc::new(DictionaryArray::<UInt32Type>::new(
54                UInt32Array::from(vec![0, 1, 0]),
55                Arc::new(StringArray::from(vec!["host-a", "host-b"])),
56            ))],
57        )
58        .unwrap();
59
60        let mut encoder = FlightEncoder::default();
61        let mut flight_data = Vec::new();
62        flight_data.extend(encoder.encode(FlightMessage::Schema(schema.clone())));
63        let encoded_batch = encoder.encode(FlightMessage::RecordBatch(batch.clone()));
64        assert_eq!(2, encoded_batch.len());
65        flight_data.extend(encoded_batch);
66
67        let mut decoder = FlightDecoder::default();
68        let messages = flight_data
69            .into_iter()
70            .filter_map(|data| decode_flight_data(&mut decoder, Ok(data)))
71            .collect::<Result<Vec<_>>>()
72            .unwrap();
73
74        assert_eq!(2, messages.len());
75        assert!(matches!(&messages[0], FlightMessage::Schema(actual) if actual == &schema));
76        assert!(matches!(&messages[1], FlightMessage::RecordBatch(actual) if actual == &batch));
77    }
78}