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 ///
50 /// The hash identifies a result set within one Feldera release. It builds
51 /// on DataFusion's row hash, so an upgrade can change it for unchanged
52 /// data; compare only hashes taken from the same release.
53 Hash,
54}
55
56impl Display for AdHocResultFormat {
57 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
58 match self {
59 AdHocResultFormat::Text => write!(f, "text"),
60 AdHocResultFormat::Json => write!(f, "json"),
61 AdHocResultFormat::Parquet => write!(f, "parquet"),
62 AdHocResultFormat::ArrowIpc => write!(f, "arrow_ipc"),
63 AdHocResultFormat::Hash => write!(f, "hash"),
64 }
65 }
66}
67
68fn default_format() -> AdHocResultFormat {
69 AdHocResultFormat::default()
70}
71
72/// Arguments to the `/query` endpoint.
73///
74/// The arguments can be provided in two ways:
75///
76/// - In case a normal HTTP connection is established to the endpoint,
77/// these arguments are passed as URL-encoded parameters.
78/// Note: this mode is deprecated and will be removed in the future.
79///
80/// - If a Websocket connection is opened to `/query`, the arguments are passed
81/// to the server over the websocket as a JSON encoded string.
82#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ToSchema)]
83pub struct AdhocQueryArgs {
84 /// The SQL query to run.
85 #[serde(default)]
86 pub sql: String,
87 /// In what format the data is sent to the client.
88 #[serde(default = "default_format")]
89 pub format: AdHocResultFormat,
90}