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
use std::io;
use std::marker::PhantomData;
use std::process::Stdio;
use std::time::Duration;

use async_trait::async_trait;
use bytes::{Buf, BytesMut};
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use sqllogictest::{AsyncDB, DBOutput, DefaultColumnType};
use thiserror::Error;
use tokio::io::AsyncWriteExt;
use tokio::process::{Child, ChildStdin, ChildStdout, Command};
use tokio_util::codec::{Decoder, FramedRead};

/// Communicates with a subprocess via its stdin/stdout.
///
/// # Protocol
///
/// Sends JSON stream:
/// ```json
/// {"sql":"SELECT 1,2"}
/// ```
///
/// Receives JSON stream:
///
/// If the query succeeds:
/// ```json
/// {"result":[["1","2"]]}
/// ```
///
/// If the query fails:
/// ```json
/// {"err":"..."}
/// ```
pub struct ExternalDriver {
    child: Child,
    stdin: ChildStdin,
    stdout: FramedRead<ChildStdout, JsonDecoder<Output>>,
}

#[derive(Serialize)]
struct Input {
    sql: String,
}

#[derive(Deserialize)]
#[serde(untagged)]
enum Output {
    Success { result: Vec<Vec<String>> },
    Failed { err: String },
}

#[derive(Debug, Error)]
pub enum ExternalDriverError {
    #[error("ser/de failed")]
    Json(#[from] serde_json::Error),
    #[error("io failed")]
    Io(#[from] io::Error),
    #[error("sql failed {0}")]
    Sql(String),
}

type Result<T> = std::result::Result<T, ExternalDriverError>;

impl ExternalDriver {
    /// Spawn and pipe into the subprocess with the given `cmd`.
    pub async fn connect(mut cmd: Command) -> Result<Self> {
        let cmd = cmd.stdin(Stdio::piped()).stdout(Stdio::piped());

        let mut child = cmd.spawn()?;

        let stdin = child.stdin.take().unwrap();
        let stdout = child.stdout.take().unwrap();
        let stdout = FramedRead::new(stdout, JsonDecoder::default());

        Ok(Self {
            child,
            stdin,
            stdout,
        })
    }
}

impl Drop for ExternalDriver {
    fn drop(&mut self) {
        let _ = self.child.start_kill();
    }
}

#[async_trait]
impl AsyncDB for ExternalDriver {
    type Error = ExternalDriverError;
    type ColumnType = DefaultColumnType;

    async fn run(&mut self, sql: &str) -> Result<DBOutput<Self::ColumnType>> {
        let input = Input {
            sql: sql.to_string(),
        };
        let input = serde_json::to_string(&input)?;
        self.stdin.write_all(input.as_bytes()).await?;
        let output = match self.stdout.next().await {
            Some(Ok(output)) => output,
            Some(Err(e)) => return Err(e),
            None => return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into()),
        };
        match output {
            Output::Success { result } => Ok(DBOutput::Rows {
                types: vec![], /* FIXME: Fix it after https://github.com/risinglightdb/sqllogictest-rs/issues/36 is resolved. */
                rows: result,
            }),
            Output::Failed { err } => Err(ExternalDriverError::Sql(err)),
        }
    }

    fn engine_name(&self) -> &str {
        "external"
    }

    async fn sleep(dur: Duration) {
        tokio::time::sleep(dur).await
    }

    async fn run_command(command: std::process::Command) -> std::io::Result<std::process::Output> {
        Command::from(command).output().await
    }
}

struct JsonDecoder<T>(PhantomData<T>);

impl<T> Default for JsonDecoder<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Decoder for JsonDecoder<T>
where
    T: for<'de> serde::de::Deserialize<'de>,
{
    type Item = T;
    type Error = ExternalDriverError;

    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
        let mut inner = serde_json::Deserializer::from_slice(src.as_ref()).into_iter::<T>();
        match inner.next() {
            None => Ok(None),
            Some(Err(e)) if e.is_eof() => Ok(None),
            Some(Err(e)) => Err(e.into()),
            Some(Ok(v)) => {
                let len = inner.byte_offset();
                src.advance(len);
                Ok(Some(v))
            }
        }
    }
}