datafusion_flight_sql_server/
state.rs

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
use std::fmt::Display;

use arrow_flight::{
    error::FlightError,
    sql::{self, Any, Command},
};
use prost::{bytes::Bytes, Message};

pub type Result<T, E = FlightError> = std::result::Result<T, E>;

#[derive(Debug, PartialEq, Clone)]
pub struct CommandTicket {
    pub command: sql::Command,
}

impl CommandTicket {
    pub fn new(cmd: sql::Command) -> Self {
        Self { command: cmd }
    }

    pub fn try_decode(msg: Bytes) -> Result<Self> {
        let msg = CommandTicketMessage::decode(msg).map_err(decode_error_flight_error)?;

        Self::try_decode_command(msg.command)
    }

    pub fn try_decode_command(cmd: Bytes) -> Result<Self> {
        let content_msg = Any::decode(cmd).map_err(decode_error_flight_error)?;
        let command = Command::try_from(content_msg).map_err(FlightError::Arrow)?;

        Ok(Self { command })
    }

    pub fn try_encode(self) -> Result<Bytes> {
        let content_msg = self.command.into_any().encode_to_vec();

        let msg = CommandTicketMessage {
            command: content_msg.into(),
        };

        Ok(msg.encode_to_vec().into())
    }
}

#[derive(Clone, PartialEq, Message)]
struct CommandTicketMessage {
    #[prost(bytes = "bytes", tag = "2")]
    command: Bytes,
}

fn decode_error_flight_error(err: prost::DecodeError) -> FlightError {
    FlightError::DecodeError(format!("{err:?}"))
}

/// Represents a query handle for use in prepared statements.
/// All state required to run the prepared statement is passed
/// back and forth to the client, so any service instance can run it
#[derive(Debug, Clone)]
pub struct QueryHandle {
    /// The raw SQL query text
    query: String,
    parameters: Option<Bytes>,
}

impl QueryHandle {
    pub fn new(query: String, parameters: Option<Bytes>) -> Self {
        Self { query, parameters }
    }

    pub fn query(&self) -> &str {
        self.query.as_ref()
    }

    pub fn parameters(&self) -> Option<&[u8]> {
        self.parameters.as_deref()
    }

    pub fn set_parameters(&mut self, parameters: Option<Bytes>) {
        self.parameters = parameters;
    }

    pub fn try_decode(msg: Bytes) -> Result<Self> {
        let msg = QueryHandleMessage::decode(msg).map_err(decode_error_flight_error)?;

        Ok(Self {
            query: msg.query,
            parameters: msg.parameters,
        })
    }

    pub fn encode(self) -> Bytes {
        let msg = QueryHandleMessage {
            query: self.query,
            parameters: self.parameters,
        };

        msg.encode_to_vec().into()
    }
}

impl From<QueryHandle> for Bytes {
    fn from(value: QueryHandle) -> Self {
        value.encode()
    }
}

impl Display for QueryHandle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Query({})", self.query)
    }
}

#[derive(Clone, PartialEq, Message)]
pub struct QueryHandleMessage {
    /// The raw SQL query text
    #[prost(string, tag = "1")]
    query: String,
    #[prost(bytes = "bytes", optional, tag = "2")]
    parameters: Option<Bytes>,
}