MCPcopy
hub / github.com/pathwaycom/pathway / unpack_col_dict

Function unpack_col_dict

python/pathway/stdlib/utils/col.py:97–188  ·  view source on GitHub ↗

Unpacks columns from a json object Input: - column: Column expression of column containing some pw.Json with an object - schema: Schema for columns to extract Output: - Table with columns given by the schema Example: >>> import pathway as pw >>> t = pw.debug.table_

(
    column: pw.ColumnReference,
    schema: type[pw.Schema],
)

Source from the content-addressed store, hash-verified

95@check_arg_types
96@trace_user_frame
97def unpack_col_dict(
98 column: pw.ColumnReference,
99 schema: type[pw.Schema],
100) -> pw.Table:
101 """Unpacks columns from a json object
102
103 Input:
104 - column: Column expression of column containing some pw.Json with an object
105 - schema: Schema for columns to extract
106
107 Output:
108 - Table with columns given by the schema
109
110 Example:
111 >>> import pathway as pw
112 >>> t = pw.debug.table_from_rows(
113 ... schema=pw.schema_from_types(data=pw.Json),
114 ... rows=[
115 ... ({"field_a": 13, "field_b": "foo", "field_c": False},),
116 ... ({"field_a": 17, "field_c": True, "field_d": 3.4},)
117 ... ]
118 ... )
119 >>> class DataSchema(pw.Schema):
120 ... field_a: int
121 ... field_b: str | None
122 ... field_c: bool
123 ... field_d: float | None
124 >>> t2 = pw.utils.col.unpack_col_dict(t.data, schema=DataSchema)
125 >>> pw.debug.compute_and_print(t2, include_id=False)
126 field_a | field_b | field_c | field_d
127 13 | foo | False |
128 17 | | True | 3.4
129 """
130 typehints = schema._dtypes()
131
132 def _convert_from_json(name: str, col: pw.ColumnExpression):
133 _type = dt.unoptionalize(typehints[name])
134 is_optional = isinstance(typehints[name], dt.Optional)
135 result: pw.ColumnExpression
136
137 def _optional(
138 col: pw.ColumnExpression,
139 op: Callable[[pw.ColumnExpression], pw.ColumnExpression],
140 ) -> pw.ColumnExpression:
141 if is_optional:
142 return pw.if_else(col == pw.Json.NULL, None, op(col))
143 else:
144 return op(col)
145
146 match _type:
147 case dt.JSON:
148 result = col
149 case dt.BOOL:
150 result = col.as_bool()
151 case dt.FLOAT:
152 result = col.as_float()
153 case dt.INT:
154 result = col.as_int()

Callers

nothing calls this directly

Calls 6

_convert_from_jsonFunction · 0.85
_dtypesMethod · 0.80
update_typesMethod · 0.80
column_namesMethod · 0.45
getMethod · 0.45
selectMethod · 0.45

Tested by

no test coverage detected