databendlabs/databend · error

fail to serialize Scalar

Error message

fail to serialize Scalar

What it means

When constructing the HTTP query request state, each session variable (`Scalar`) is serialized to a JSON string with `serde_json::to_string(&v).expect("fail to serialize Scalar")`. `Scalar`'s JSON serialization is expected to be infallible; a `Err` means the scalar value (e.g., a struct/variant payload) could not be represented as JSON — typically a serde serializer error from an unsupported value or a serialization bug in `Scalar`'s `Serialize` impl. The panic aborts HTTP query request creation.

Solutions

  1. Identify which session variable fails (iterate `variables` and test `serde_json::to_string` per value); unset or change it before issuing the HTTP query.
  2. If a custom/new `Scalar` variant is involved, implement or fix its `Serialize` impl so JSON serialization always succeeds.
  3. Upgrade Databend if this occurs with standard variables — it indicates a fixed serialization bug.
  4. Replace the expect with `map_err(...)` into a server error so the HTTP API returns 4xx/5xx instead of panicking.

Example fix

// before
serde_json::to_string(&v).expect("fail to serialize Scalar"),

// after
serde_json::to_string(&v).map_err(|e| {
    ErrorCode::Internal(format!("fail to serialize Scalar '{}': {}", k, e))
})?,
Defensive patterns

Strategy: try-catch

Validate before calling

// Before issuing the HTTP query, sanity-check session variables are JSON-serializable
for (k, v) in &variables {
    serde_json::to_string(v).unwrap_or_else(|e| panic!("session var '{}' not serializable: {}", k, e));
}

Try / catch

// Library-side hardening
serde_json::to_string(&v).map_err(|e| {
    ErrorCode::Internal(format!("fail to serialize Scalar '{}': {}", k, e))
})?

Prevention

When it happens

Trigger: `new(variables, ...)` on the HTTP query request struct receiving a session variable whose `Scalar` serialization fails — e.g., a newly introduced Scalar variant without a JSON `Serialize` implementation, or a nested value exceeding serde limits.

Common situations: Setting exotic session variables (large structs, new data types) through `SET` before running a query over the HTTP API; development builds where a new Scalar type was added without updating serde support.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of databendlabs/databend@288d84d76e (2026-09-11). Data as JSON: /api/errors/35817d90d813731e. Report an issue: GitHub.

Appendix: source

Thrown at src/query/service/src/servers/http/v1/query/http_query.rs:266

    #[serde(default)]
    #[serde(skip_serializing_if = "is_default")]
    pub last_query_ids: Vec<String>,
}

impl HttpSessionStateInternal {
    fn new(
        variables: &HashMap<String, Scalar>,
        last_query_result_cache_key: String,
        has_temp_table: bool,
        last_node_id: Option<String>,
        last_query_ids: Vec<String>,
    ) -> Self {
        let variables = variables
            .iter()
            .map(|(k, v)| {
                (
                    k.clone(),
                    serde_json::to_string(&v).expect("fail to serialize Scalar"),
                )
            })
            .collect();
        Self {
            variables,
            last_query_result_cache_key,
            has_temp_table,
            last_node_id,
            last_query_ids,
        }
    }

    pub fn get_variables(&self) -> Result<HashMap<String, Scalar>> {
        let mut vars = HashMap::with_capacity(self.variables.len());
        for (k, v) in self.variables.iter() {
            match serde_json::from_str::<Scalar>(v) {
                Ok(s) => {
                    vars.insert(k.to_string(), s);

View on GitHub (pinned to 288d84d76e)