feldera_types/
query.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::{Debug, Display, Formatter, Result};
3use utoipa::ToSchema;
4
5/// The maximum size of a WebSocket frames we're sending in bytes.
6pub const MAX_WS_FRAME_SIZE: usize = 1024 * 1024 * 2;
7
8/// URL-encoded `format` argument to the `/query` endpoint.
9#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy, ToSchema)]
10#[serde(rename_all = "snake_case")]
11pub enum AdHocResultFormat {
12    /// Serialize results as a human-readable text table.
13    Text,
14    /// Serialize results as new-line delimited JSON records.
15    ///
16    /// # Deprecation Notice
17    /// This format is deprecated and will be removed in the future.
18    /// Users are encouraged to use the `arrow_ipc` format instead,
19    /// See <https://github.com/feldera/feldera/issues/4219> for more details.
20    Json,
21    /// Download results in a parquet file.
22    Parquet,
23    /// Stream data in the arrow IPC format.
24    ArrowIpc,
25    /// Returns a hash of the results instead of the actual data.
26    ///
27    /// The output in this case is a single string/line containing a
28    /// SHA256 hash (or a JSON formatted error message in case the query
29    /// failed to execute).
30    ///
31    /// This is useful for verifying the integrity of the data
32    /// without transferring the entire dataset.
33    ///
34    /// Note that supplying a query with this format will implicitly
35    /// add an `ORDER BY` clause for all fields of the result to the query
36    /// to ensure consistent ordering of results.
37    ///
38    /// e.g., a query like `select * from materialized_view` will be rewritten as
39    /// `select * from materialized_view order by col1, col2, ..., colN`
40    Hash,
41}
42
43impl Display for AdHocResultFormat {
44    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
45        match self {
46            AdHocResultFormat::Text => write!(f, "text"),
47            AdHocResultFormat::Json => write!(f, "json"),
48            AdHocResultFormat::Parquet => write!(f, "parquet"),
49            AdHocResultFormat::ArrowIpc => write!(f, "arrow_ipc"),
50            AdHocResultFormat::Hash => write!(f, "hash"),
51        }
52    }
53}
54
55impl Default for AdHocResultFormat {
56    fn default() -> Self {
57        Self::Text
58    }
59}
60
61fn default_format() -> AdHocResultFormat {
62    AdHocResultFormat::default()
63}
64
65/// Arguments to the `/query` endpoint.
66///
67/// The arguments can be provided in two ways:
68///
69/// - In case a normal HTTP connection is established to the endpoint,
70///   these arguments are passed as URL-encoded parameters.
71///   Note: this mode is deprecated and will be removed in the future.
72///
73/// - If a Websocket connection is opened to `/query`, the arguments are passed
74///   to the server over the websocket as a JSON encoded string.
75#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ToSchema)]
76pub struct AdhocQueryArgs {
77    /// The SQL query to run.
78    #[serde(default)]
79    pub sql: String,
80    /// In what format the data is sent to the client.
81    #[serde(default = "default_format")]
82    pub format: AdHocResultFormat,
83}