{"record":{"id":"06fd6e87c31637c8","repo":"risingwavelabs/risingwave","slug":"cannot-convert-as-ty-to-variant","errorCode":null,"errorMessage":"cannot convert {} as {ty} to variant","messagePattern":"cannot convert (.+?) as (.+?) to variant","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/common/src/types/variant.rs","lineNumber":719,"sourceCode":"        (ScalarRefImpl::Map(v), DataType::Map(map_type)) => {\n            let mut object = builder\n                .try_new_object()\n                .context(\"failed to create variant map object\")?;\n            let entries = v\n                .iter()\n                .map(|(key, value)| {\n                    let field = key.to_text_with_type(map_type.key());\n                    (field, value)\n                })\n                .sorted_by(|a, b| a.0.cmp(&b.0))\n                .collect_vec();\n            for (field, value) in entries {\n                let mut field_builder = ObjectFieldBuilder::new(field.as_str(), &mut object);\n                append_datum_value(value, map_type.value(), &mut field_builder)?;\n            }\n            object.finish();\n        }\n        (value, ty) => bail!(\"cannot convert {} as {ty} to variant\", value.get_ident()),\n    }\n    Ok(())\n}\n\n/// Variant objects require unique field names, but RisingWave struct types allow duplicates.\n/// Callers iterate fields in name order, so comparing against the previous name suffices.\nfn reject_duplicate_field(previous: Option<&str>, field_name: &str) -> anyhow::Result<()> {\n    if previous == Some(field_name) {\n        bail!(\"variant object cannot have duplicate field name `{field_name}`\");\n    }\n    Ok(())\n}\n\nfn append_struct(\n    value: super::StructRef<'_>,\n    struct_type: &StructType,\n    builder: &mut impl VariantBuilderExt,\n) -> anyhow::Result<()> {","sourceCodeStart":701,"sourceCodeEnd":737,"githubUrl":"https://github.com/risingwavelabs/risingwave/blob/6469eb736d691e8e9b8a419a57edd6429ca77417/src/common/src/types/variant.rs#L701-L737","documentation":"append_datum_value converts a RisingWave scalar Datum into a Variant value. The match covers all supported scalar/type combinations; any (value, type) pair outside the supported set (e.g. appending unsupported types like interval or serial as variant) hits this catch-all bail. `value.get_ident()` gives a human-readable value description and `ty` the type.","triggerScenarios":"Calling try_from_scalar_ref or append_datum_value with a type/value combination not implemented in the conversion match (e.g. converting an interval or serial datum into a Variant).","commonSituations":"Building variant columns from tables containing types without a Variant representation, or copy/compute jobs that cast data to Variant without first filtering unsupported types.","solutions":["Cast or exclude the unsupported type before conversion (e.g. cast interval to varchar first)","Add an arm in append_datum_value for the missing type if you own the code","Filter rows/types in SQL to supported variant types"],"exampleFix":"// before\nVariant::try_from_scalar_ref(&interval_datum)?; // bails\n// after\nVariant::try_from_scalar_ref(&varchar_datum)?; // cast interval to varchar first","handlingStrategy":"try-catch","validationCode":"fn variant_supported(ty: &DataType) -> bool {\n    !matches!(ty, DataType::Interval | DataType::Serial)\n}","typeGuard":null,"tryCatchPattern":"match append_datum_value(&datum, &ty, &mut builder) {\n    Err(e) if e.to_string().contains(\"to variant\") => {\n        // fall back: convert value to its string form\n        append_datum_value(&datum.cast_to(&DataType::Varchar).unwrap(), &DataType::Varchar, &mut builder)?;\n    }\n    r => r?,\n}","preventionTips":["Cast unsupported types (interval, serial) to varchar before variant conversion","Keep a list of types supported by append_datum_value and validate schemas against it","Cover new types with an arm in the conversion match when adding them"],"tags":["rust","variant","type-conversion"],"backgroundTag":"incompatible-source-type","analyzedSha":"6469eb736d691e8e9b8a419a57edd6429ca77417","analyzedAt":"2026-09-11T21:06:21.487Z","contentChangedAt":"2026-09-11T21:06:21.487Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}