Skip to main content

vortex_json/
dtype.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Extension dtype definition for the JSON type
5
6use vortex_array::EmptyMetadata;
7use vortex_array::dtype::extension::ExtDType;
8use vortex_array::dtype::extension::ExtId;
9use vortex_array::dtype::extension::ExtVTable;
10use vortex_array::scalar::ScalarValue;
11use vortex_error::VortexResult;
12use vortex_error::vortex_bail;
13use vortex_error::vortex_ensure;
14use vortex_session::registry::CachedId;
15
16/// JSON logical type backed by UTF-8 string storage.
17#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
18pub struct Json;
19
20impl ExtVTable for Json {
21    type Metadata = EmptyMetadata;
22    type NativeValue<'a> = &'a str;
23
24    fn id(&self) -> ExtId {
25        static ID: CachedId = CachedId::new("vortex.json");
26        *ID
27    }
28
29    fn serialize_metadata(&self, _metadata: &Self::Metadata) -> VortexResult<Vec<u8>> {
30        Ok(vec![])
31    }
32
33    fn deserialize_metadata(&self, metadata: &[u8]) -> VortexResult<Self::Metadata> {
34        vortex_ensure!(metadata.is_empty(), "JSON metadata must be empty");
35        Ok(EmptyMetadata)
36    }
37
38    fn validate_dtype(ext_dtype: &ExtDType<Self>) -> VortexResult<()> {
39        vortex_ensure!(
40            ext_dtype.storage_dtype().is_utf8(),
41            "JSON storage dtype must be utf8, got {}",
42            ext_dtype.storage_dtype()
43        );
44        Ok(())
45    }
46
47    fn unpack_native<'a>(
48        _ext_dtype: &'a ExtDType<Self>,
49        storage_value: &'a ScalarValue,
50    ) -> VortexResult<Self::NativeValue<'a>> {
51        let ScalarValue::Utf8(value) = storage_value else {
52            vortex_bail!("JSON storage scalar must be utf8, got {storage_value}");
53        };
54        Ok(value.as_str())
55    }
56}