Skip to content

Commit 32e0524

Browse files
authored
chore: restrict the cond of decoding key to basic types for mutations (#820)
Only when a basic type / any type is annotated, we decode the primary key columns to a single value instead of a struct. This means they can always explicitly create a struct type (namedtuple or dataclass) if they want even when there's only a single primary key column. This will allow users to handle multiple-primary-key-columns in a consistent way.
1 parent 0783cdf commit 32e0524

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

python/cocoindex/op.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
resolve_forward_ref,
2929
analyze_type_info,
3030
AnalyzedAnyType,
31+
AnalyzedBasicType,
3132
AnalyzedDictType,
3233
)
3334

@@ -507,15 +508,21 @@ def create_export_context(
507508
else (None, None)
508509
)
509510

510-
if len(key_fields_schema) == 1:
511+
key_type_info = analyze_type_info(key_annotation)
512+
if (
513+
len(key_fields_schema) == 1
514+
and key_fields_schema[0]["type"]["kind"] != "Struct"
515+
and isinstance(key_type_info.variant, (AnalyzedAnyType, AnalyzedBasicType))
516+
):
517+
# Special case for ease of use: single key column can be mapped to a basic type without the wrapper struct.
511518
key_decoder = make_engine_value_decoder(
512519
["(key)"],
513520
key_fields_schema[0]["type"],
514-
analyze_type_info(key_annotation),
521+
key_type_info,
515522
)
516523
else:
517524
key_decoder = make_engine_struct_decoder(
518-
["(key)"], key_fields_schema, analyze_type_info(key_annotation)
525+
["(key)"], key_fields_schema, key_type_info
519526
)
520527

521528
value_decoder = make_engine_struct_decoder(

0 commit comments

Comments
 (0)