Skip to main content

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