Skip to main content

risingwave_expr_impl/scalar/
variant.rs

1// Copyright 2026 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use risingwave_common::types::{ScalarRefImpl, VariantRef, VariantVal};
16use risingwave_expr::expr::Context;
17use risingwave_expr::{ExprError, Result, function};
18use thiserror_ext::AsReport;
19
20#[function("to_variant(any) -> variant")]
21fn to_variant(input: Option<ScalarRefImpl<'_>>, ctx: &Context) -> Result<VariantVal> {
22    VariantVal::try_from_scalar_ref(input, &ctx.arg_types[0]).map_err(|e| ExprError::InvalidParam {
23        name: "to_variant",
24        reason: e.to_report_string().into(),
25    })
26}
27
28#[function("variant_get(variant, varchar) -> variant")]
29fn variant_get(value: VariantRef<'_>, path: &str) -> Result<Option<VariantVal>> {
30    value
31        .access_path_strict(path)
32        .map_err(|e| ExprError::InvalidParam {
33            name: "variant_get",
34            reason: e.to_report_string().into(),
35        })
36}
37
38#[function("try_variant_get(variant, varchar) -> variant")]
39fn try_variant_get(value: VariantRef<'_>, path: &str) -> Option<VariantVal> {
40    value.access_path(path)
41}
42
43#[function("variant_typeof(variant) -> varchar")]
44fn variant_typeof(value: VariantRef<'_>, writer: &mut impl std::fmt::Write) {
45    writer.write_str(value.type_name()).unwrap();
46}