Skip to content

Commit 9dc8f32

Browse files
committed
fix: properly encode metadata field values when creating actions
Fixes #3174 Signed-off-by: R. Tyler Croy <[email protected]>
1 parent 5113aea commit 9dc8f32

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

python/src/schema.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,18 +437,18 @@ impl Field {
437437
inner = inner.with_metadata(metadata.iter().map(|(k, v)| {
438438
(
439439
k,
440-
if let serde_json::Value::Number(n) = v {
441-
n.as_i64().map_or_else(
440+
match v {
441+
serde_json::Value::Number(n) => n.as_i64().map_or_else(
442442
|| MetadataValue::String(v.to_string()),
443443
|i| {
444444
i32::try_from(i)
445445
.ok()
446446
.map(MetadataValue::Number)
447447
.unwrap_or_else(|| MetadataValue::String(v.to_string()))
448448
},
449-
)
450-
} else {
451-
MetadataValue::String(v.to_string())
449+
),
450+
serde_json::Value::String(s) => MetadataValue::String(s.to_string()),
451+
other => MetadataValue::String(other.to_string()),
452452
},
453453
)
454454
}));

python/tests/test_generated_columns.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def gc_schema() -> Schema:
1414
Field(
1515
name="gc",
1616
type=PrimitiveType("integer"),
17-
metadata={"delta.generationExpression": "'5'"},
17+
metadata={"delta.generationExpression": "5"},
1818
),
1919
]
2020
)
@@ -118,7 +118,7 @@ def test_write_with_invalid_gc_to_table(table_with_gc, invalid_gc_data):
118118
with pytest.raises(
119119
DeltaError,
120120
match=re.escape(
121-
"Invariant violations: [\"Check or Invariant (gc = '5' OR (gc IS NULL AND '5' IS NULL)) violated by value in row: [10]\"]"
121+
'Invariant violations: ["Check or Invariant (gc = 5 OR (gc IS NULL AND 5 IS NULL)) violated by value in row: [10]"]'
122122
),
123123
):
124124
write_deltalake(table_with_gc, mode="append", data=invalid_gc_data)
@@ -177,7 +177,7 @@ def test_raise_when_gc_passed_during_adding_new_columns(tmp_path, data_without_g
177177
Field(
178178
name="gc",
179179
type=PrimitiveType("integer"),
180-
metadata={"delta.generationExpression": "'5'"},
180+
metadata={"delta.generationExpression": "5"},
181181
)
182182
]
183183
)
@@ -257,7 +257,7 @@ def test_merge_with_gc_invalid(table_with_gc: DeltaTable, invalid_gc_data):
257257
with pytest.raises(
258258
DeltaError,
259259
match=re.escape(
260-
"Invariant violations: [\"Check or Invariant (gc = '5' OR (gc IS NULL AND '5' IS NULL)) violated by value in row: [10]\"]"
260+
'Invariant violations: ["Check or Invariant (gc = 5 OR (gc IS NULL AND 5 IS NULL)) violated by value in row: [10]"]'
261261
),
262262
):
263263
(

python/tests/test_schema.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,12 @@ def test_schema_conversions(schema, expected_schema, conversion_mode):
553553
result_schema = _convert_pa_schema_to_delta(schema, conversion_mode)
554554

555555
assert result_schema == expected_schema
556+
557+
558+
# <https://github.com/delta-io/delta-rs/issues/3174>
559+
def test_field_serialization():
560+
from deltalake import Field
561+
562+
f = Field("fieldname", "binary", metadata={"key": "value"})
563+
assert f.name == "fieldname"
564+
assert f.metadata == {"key": "value"}

0 commit comments

Comments
 (0)