Skip to content

Commit 1933735

Browse files
committed
grammar and template updates
1 parent fcefae5 commit 1933735

6 files changed

Lines changed: 64 additions & 5 deletions

File tree

meta/src/meta/codegen_go.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,13 @@ def unwrap_if_option(field_expr, field_value: str) -> tuple[str, str]:
592592
lines.append(f"{indent}}}")
593593
return (field_value, tmp)
594594
else:
595-
# Ptr-wrapped scalar: use deref()
595+
# Ptr-wrapped scalar: usually deref for plain struct fields (float64, int64, …).
596+
# Proto `optional string` fields are *string in Go — pass *string through.
597+
inner_go = self.gen_type(field_expr.type.element_type)
598+
if inner_go == "string":
599+
opt_go = self.gen_option_type(inner_go)
600+
if opt_go.startswith("*"):
601+
return (field_value, field_value)
596602
return (field_value, f"deref({field_value}, {zero})")
597603
return (field_value, field_value)
598604

@@ -905,6 +911,21 @@ def _generate_Assign(self, expr, lines: list[str], indent: str) -> str:
905911
self.mark_declared(var_name)
906912
return self.gen_none()
907913

914+
# Integer literal with explicit Int32/Int64/... annotation: avoid untyped `:= 0` (int)
915+
# so later assignments from int64 (e.g. _extract_value_int64) type-check.
916+
if (
917+
isinstance(expr.expr, Lit)
918+
and type(expr.expr.value) is int
919+
and expr.var.type is not None
920+
):
921+
var_type = self.gen_type(expr.var.type)
922+
if var_type in ("int64", "int32", "uint32", "uint64"):
923+
lines.append(
924+
f"{indent}{var_name} := {var_type}({expr.expr.value})"
925+
)
926+
self.mark_declared(var_name)
927+
return self.gen_none()
928+
908929
# Regular assignment
909930
expr_code = self.generate_lines(expr.expr, lines, indent)
910931
assert expr_code is not None, (

meta/src/meta/codegen_templates.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class BuiltinTemplate:
3333
"none": BuiltinTemplate("None"),
3434
"make_empty_bytes": BuiltinTemplate('b""'),
3535
"dict_from_list": BuiltinTemplate("dict({0})"),
36+
"string_map_from_pairs": BuiltinTemplate("dict({0})"),
3637
"dict_get": BuiltinTemplate("{0}.get({1})"),
3738
"dict_to_pairs": BuiltinTemplate("sorted({0}.items())"),
3839
"has_proto_field": BuiltinTemplate("{0}.HasField({1})"),
@@ -144,6 +145,7 @@ class BuiltinTemplate:
144145
"none": BuiltinTemplate("nothing"),
145146
"make_empty_bytes": BuiltinTemplate("UInt8[]"),
146147
"dict_from_list": BuiltinTemplate("Dict({0})"),
148+
"string_map_from_pairs": BuiltinTemplate("Dict({0})"),
147149
"dict_get": BuiltinTemplate("get({0}, {1}, nothing)"),
148150
"dict_to_pairs": BuiltinTemplate("sort([(k, v) for (k, v) in {0}])"),
149151
"has_proto_field": BuiltinTemplate("_has_proto_field({0}, Symbol({1}))"),
@@ -260,6 +262,7 @@ class BuiltinTemplate:
260262
"none": BuiltinTemplate("nil"),
261263
"make_empty_bytes": BuiltinTemplate("[]byte{}"),
262264
"dict_from_list": BuiltinTemplate("dictFromList({0})"),
265+
"string_map_from_pairs": BuiltinTemplate("stringMapFromPairs({0})"),
263266
"dict_get": BuiltinTemplate("dictGetValue({0}, {1})"),
264267
"dict_to_pairs": BuiltinTemplate("dictToPairs({0})"),
265268
"has_proto_field": BuiltinTemplate("hasProtoField({0}, {1})"),

meta/src/meta/grammar.y

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ def _extract_value_int32(value: Optional[logic.Value], default: int) -> Int32:
13281328
return builtin.int64_to_int32(default)
13291329

13301330

1331-
def _extract_value_int64(value: Optional[logic.Value], default: int) -> int:
1331+
def _extract_value_int64(value: Optional[logic.Value], default: int) -> Int64:
13321332
if value is not None and builtin.has_proto_field(builtin.unwrap_option(value), 'int_value'):
13331333
return builtin.unwrap_option(value).int_value
13341334
return default
@@ -1641,8 +1641,8 @@ def construct_iceberg_config(
16411641
property_pairs: Sequence[Tuple[String, String]],
16421642
auth_property_pairs: Sequence[Tuple[String, String]],
16431643
) -> logic.IcebergConfig:
1644-
props: Dict[String, String] = builtin.dict_from_list(property_pairs)
1645-
auth_props: Dict[String, String] = builtin.dict_from_list(auth_property_pairs)
1644+
props: Dict[String, String] = builtin.string_map_from_pairs(property_pairs)
1645+
auth_props: Dict[String, String] = builtin.string_map_from_pairs(auth_property_pairs)
16461646
scope_pb: Optional[String] = iceberg_optional_string_field(scope_opt)
16471647
return logic.IcebergConfig(
16481648
catalog_uri=catalog_uri,
@@ -1677,7 +1677,7 @@ def construct_export_iceberg_config_full(
16771677
config_dict: Optional[Sequence[Tuple[String, logic.Value]]],
16781678
) -> transactions.ExportIcebergConfig:
16791679
prefix: String = ""
1680-
target_file_size_bytes: int = 0
1680+
target_file_size_bytes: Int64 = 0
16811681
compression: String = ""
16821682
if config_dict is not None:
16831683
cfg: Dict[String, logic.Value] = builtin.dict_from_list(builtin.unwrap_option(config_dict))

meta/src/meta/target_builtins.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ def is_builtin(name: str) -> bool:
217217

218218
# === Dict operations ===
219219
register_builtin("dict_from_list", [SequenceType(TupleType([K, V]))], DictType(K, V))
220+
register_builtin(
221+
"string_map_from_pairs",
222+
[SequenceType(TupleType([STRING, STRING]))],
223+
DictType(STRING, STRING),
224+
)
220225
register_builtin("dict_get", [DictType(K, V), K], OptionType(V))
221226
register_builtin("dict_to_pairs", [DictType(K, V)], ListType(TupleType([K, V])))
222227

meta/src/meta/templates/parser.go.template

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,19 @@ func dictFromList(pairs [][]interface{{}}) map[string]interface{{}} {{
543543
return result
544544
}}
545545

546+
// stringMapFromPairs builds map[string]string from (prop key value) pair rows.
547+
func stringMapFromPairs(pairs [][]interface{{}}) map[string]string {{
548+
out := make(map[string]string)
549+
for _, pair := range pairs {{
550+
if len(pair) >= 2 {{
551+
k, _ := pair[0].(string)
552+
v, _ := pair[1].(string)
553+
out[k] = v
554+
}}
555+
}}
556+
return out
557+
}}
558+
546559
// dictGetValue retrieves a Value from the config dict with type assertion
547560
func dictGetValue(m map[string]interface{{}}, key string) *pb.Value {{
548561
if v, ok := m[key]; ok {{

meta/src/meta/templates/pretty_printer.go.template

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,23 @@ func listSort(pairs [][]interface{{}}) [][]interface{{}} {{
261261
return pairs
262262
}}
263263

264+
// dictToPairs converts map[string]string to sorted key/value rows for pretty printing.
265+
func dictToPairs(m map[string]string) [][]interface{{}} {{
266+
if len(m) == 0 {{
267+
return nil
268+
}}
269+
keys := make([]string, 0, len(m))
270+
for k := range m {{
271+
keys = append(keys, k)
272+
}}
273+
sort.Strings(keys)
274+
out := make([][]interface{{}}, 0, len(keys))
275+
for _, k := range keys {{
276+
out = append(out, []interface{{}}{{k, m[k]}})
277+
}}
278+
return out
279+
}}
280+
264281
// --- Free functions ---
265282

266283
func uint128ToString(low, high uint64) string {{

0 commit comments

Comments
 (0)