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