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
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display, Formatter, Result};
use utoipa::ToSchema;

/// URL-encoded `format` argument to the `/query` endpoint.
#[derive(Debug, Deserialize, PartialEq, Clone, Copy, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum AdHocResultFormat {
    /// Serialize results as a human-readable text table.
    Text,
    /// Serialize results as new-line delimited JSON records.
    Json,
    /// Download results in a parquet file.
    Parquet,
}

impl Display for AdHocResultFormat {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            AdHocResultFormat::Text => write!(f, "text"),
            AdHocResultFormat::Json => write!(f, "json"),
            AdHocResultFormat::Parquet => write!(f, "parquet"),
        }
    }
}

impl Default for AdHocResultFormat {
    fn default() -> Self {
        Self::Text
    }
}

fn default_format() -> AdHocResultFormat {
    AdHocResultFormat::default()
}

/// URL-encoded arguments to the `/query` endpoint.
#[derive(Clone, Debug, PartialEq, Deserialize, ToSchema)]
pub struct AdhocQueryArgs {
    /// The SQL query to run.
    pub sql: String,
    /// In what format the data is sent to the client.
    #[serde(default = "default_format")]
    pub format: AdHocResultFormat,
}

/// A query over an output stream.
///
/// We currently do not support ad hoc queries.  Instead the client can use
/// three pre-defined queries to inspect the contents of a table or view.
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, PartialOrd, ToSchema, Ord)]
pub enum OutputQuery {
    /// Query the entire contents of the table (similar to `SELECT * FROM`).
    #[serde(rename = "table")]
    Table,
    /// Neighborhood query (see `Stream::neighborhood` in dbsp)
    #[serde(rename = "neighborhood")]
    Neighborhood,
    /// Quantiles query (see `Stream::stream_key_quantiles` in dbsp).
    #[serde(rename = "quantiles")]
    Quantiles,
}

impl Default for OutputQuery {
    fn default() -> Self {
        Self::Table
    }
}

impl Display for OutputQuery {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OutputQuery::Table => write!(f, "table"),
            OutputQuery::Neighborhood => write!(f, "neighborhood"),
            OutputQuery::Quantiles => write!(f, "quantiles"),
        }
    }
}

// This is only here so we can derive `ToSchema` for it without adding
// a `utoipa` dependency to the `dbsp` crate to derive ToSchema for
// `NeighborhoodDescr`.
/// A request to output a specific neighborhood of a table or view.
/// The neighborhood is defined in terms of its central point (`anchor`)
/// and the number of rows preceding and following the anchor to output.
#[derive(Clone, Serialize, Deserialize, ToSchema)]
pub struct NeighborhoodQuery {
    pub anchor: Option<utoipa::openapi::Object>,
    pub before: u32,
    pub after: u32,
}

impl Debug for NeighborhoodQuery {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "NeighborhoodQuery")
    }
}