2
2
import json
3
3
from dataclasses import dataclass , fields
4
4
from datetime import datetime
5
+ from functools import cached_property
5
6
from typing import (
6
7
Any ,
7
8
NewType ,
11
12
)
12
13
from urllib .parse import urlparse
13
14
15
+ import orjson
16
+
14
17
from datachain .error import DatasetVersionNotFoundError
15
18
from datachain .sql .types import NAME_TYPES_MAPPING , SQLType
16
19
@@ -178,7 +181,7 @@ class DatasetVersion:
178
181
schema : dict [str , Union [SQLType , type [SQLType ]]]
179
182
num_objects : Optional [int ]
180
183
size : Optional [int ]
181
- preview : Optional [list [dict ]]
184
+ _preview_data : Optional [Union [ str , list [dict ] ]]
182
185
sources : str = ""
183
186
query_script : str = ""
184
187
job_id : Optional [str ] = None
@@ -199,7 +202,7 @@ def parse( # noqa: PLR0913
199
202
script_output : str ,
200
203
num_objects : Optional [int ],
201
204
size : Optional [int ],
202
- preview : Optional [str ],
205
+ preview : Optional [Union [ str , list [ dict ]] ],
203
206
schema : dict [str , Union [SQLType , type [SQLType ]]],
204
207
sources : str = "" ,
205
208
query_script : str = "" ,
@@ -220,7 +223,7 @@ def parse( # noqa: PLR0913
220
223
schema ,
221
224
num_objects ,
222
225
size ,
223
- json . loads ( preview ) if preview else None ,
226
+ preview ,
224
227
sources ,
225
228
query_script ,
226
229
job_id ,
@@ -260,9 +263,17 @@ def serialized_schema(self) -> dict[str, Any]:
260
263
for c_name , c_type in self .schema .items ()
261
264
}
262
265
266
+ @cached_property
267
+ def preview (self ) -> Optional [list [dict ]]:
268
+ if isinstance (self ._preview_data , str ):
269
+ return orjson .loads (self ._preview_data )
270
+ return self ._preview_data if self ._preview_data else None
271
+
263
272
@classmethod
264
273
def from_dict (cls , d : dict [str , Any ]) -> "DatasetVersion" :
265
274
kwargs = {f .name : d [f .name ] for f in fields (cls ) if f .name in d }
275
+ if not hasattr (kwargs , "_preview_data" ):
276
+ kwargs ["_preview_data" ] = d .get ("preview" )
266
277
return cls (** kwargs )
267
278
268
279
0 commit comments