|
1 | 1 | import substrait.gen.proto.algebra_pb2 as stalg |
2 | 2 | import substrait.gen.proto.type_pb2 as stt |
3 | | -from substrait.type_inference import infer_rel_schema |
4 | | - |
| 3 | +from substrait.type_inference import ( |
| 4 | + infer_expression_type, |
| 5 | + infer_nested_type, |
| 6 | + infer_rel_schema, |
| 7 | +) |
5 | 8 |
|
6 | 9 | struct = stt.Type.Struct( |
7 | 10 | types=[ |
@@ -312,3 +315,146 @@ def test_inference_join_left_mark(): |
312 | 315 | ) |
313 | 316 |
|
314 | 317 | assert infer_rel_schema(rel) == expected |
| 318 | + |
| 319 | + |
| 320 | +def test_infer_expression_type_literal(): |
| 321 | + """Test infer_expression_type with a literal expression.""" |
| 322 | + expr = stalg.Expression(literal=stalg.Expression.Literal(i64=42, nullable=False)) |
| 323 | + |
| 324 | + result = infer_expression_type(expr, struct) |
| 325 | + |
| 326 | + expected = stt.Type(i64=stt.Type.I64(nullability=stt.Type.NULLABILITY_REQUIRED)) |
| 327 | + assert result == expected |
| 328 | + |
| 329 | + |
| 330 | +def test_infer_expression_type_selection(): |
| 331 | + """Test infer_expression_type with a field selection expression.""" |
| 332 | + expr = stalg.Expression( |
| 333 | + selection=stalg.Expression.FieldReference( |
| 334 | + root_reference=stalg.Expression.FieldReference.RootReference(), |
| 335 | + direct_reference=stalg.Expression.ReferenceSegment( |
| 336 | + struct_field=stalg.Expression.ReferenceSegment.StructField(field=0), |
| 337 | + ), |
| 338 | + ) |
| 339 | + ) |
| 340 | + |
| 341 | + result = infer_expression_type(expr, struct) |
| 342 | + |
| 343 | + # Should return the type of field 0 from the struct (i64) |
| 344 | + expected = stt.Type(i64=stt.Type.I64(nullability=stt.Type.NULLABILITY_REQUIRED)) |
| 345 | + assert result == expected |
| 346 | + |
| 347 | + |
| 348 | +def test_infer_expression_type_window_function(): |
| 349 | + """Test infer_expression_type with a window function expression.""" |
| 350 | + expr = stalg.Expression( |
| 351 | + window_function=stalg.Expression.WindowFunction( |
| 352 | + function_reference=0, |
| 353 | + output_type=stt.Type( |
| 354 | + i64=stt.Type.I64(nullability=stt.Type.NULLABILITY_NULLABLE) |
| 355 | + ), |
| 356 | + ) |
| 357 | + ) |
| 358 | + |
| 359 | + result = infer_expression_type(expr, struct) |
| 360 | + |
| 361 | + expected = stt.Type(i64=stt.Type.I64(nullability=stt.Type.NULLABILITY_NULLABLE)) |
| 362 | + assert result == expected |
| 363 | + |
| 364 | + |
| 365 | +def test_infer_nested_type_struct(): |
| 366 | + """Test infer_nested_type with a struct nested expression.""" |
| 367 | + expr = stalg.Expression( |
| 368 | + nested=stalg.Expression.Nested( |
| 369 | + struct=stalg.Expression.Nested.Struct( |
| 370 | + fields=[ |
| 371 | + stalg.Expression( |
| 372 | + literal=stalg.Expression.Literal(i32=1, nullable=False) |
| 373 | + ), |
| 374 | + stalg.Expression( |
| 375 | + literal=stalg.Expression.Literal(string="test", nullable=True) |
| 376 | + ), |
| 377 | + ] |
| 378 | + ), |
| 379 | + nullable=False, |
| 380 | + ) |
| 381 | + ) |
| 382 | + |
| 383 | + result = infer_nested_type(expr.nested, struct) |
| 384 | + |
| 385 | + expected = stt.Type( |
| 386 | + struct=stt.Type.Struct( |
| 387 | + types=[ |
| 388 | + stt.Type(i32=stt.Type.I32(nullability=stt.Type.NULLABILITY_REQUIRED)), |
| 389 | + stt.Type( |
| 390 | + string=stt.Type.String(nullability=stt.Type.NULLABILITY_NULLABLE) |
| 391 | + ), |
| 392 | + ], |
| 393 | + nullability=stt.Type.NULLABILITY_REQUIRED, |
| 394 | + ) |
| 395 | + ) |
| 396 | + assert result == expected |
| 397 | + |
| 398 | + |
| 399 | +def test_infer_nested_type_list(): |
| 400 | + """Test infer_nested_type with a list nested expression.""" |
| 401 | + expr = stalg.Expression( |
| 402 | + nested=stalg.Expression.Nested( |
| 403 | + list=stalg.Expression.Nested.List( |
| 404 | + values=[ |
| 405 | + stalg.Expression( |
| 406 | + literal=stalg.Expression.Literal(fp32=3.14, nullable=False) |
| 407 | + ), |
| 408 | + ] |
| 409 | + ), |
| 410 | + nullable=False, |
| 411 | + ) |
| 412 | + ) |
| 413 | + |
| 414 | + result = infer_nested_type(expr.nested, struct) |
| 415 | + |
| 416 | + expected = stt.Type( |
| 417 | + list=stt.Type.List( |
| 418 | + type=stt.Type( |
| 419 | + fp32=stt.Type.FP32(nullability=stt.Type.NULLABILITY_REQUIRED) |
| 420 | + ), |
| 421 | + nullability=stt.Type.NULLABILITY_REQUIRED, |
| 422 | + ) |
| 423 | + ) |
| 424 | + assert result == expected |
| 425 | + |
| 426 | + |
| 427 | +def test_infer_nested_type_map(): |
| 428 | + """Test infer_nested_type with a map nested expression.""" |
| 429 | + expr = stalg.Expression( |
| 430 | + nested=stalg.Expression.Nested( |
| 431 | + map=stalg.Expression.Nested.Map( |
| 432 | + key_values=[ |
| 433 | + stalg.Expression.Nested.Map.KeyValue( |
| 434 | + key=stalg.Expression( |
| 435 | + literal=stalg.Expression.Literal( |
| 436 | + string="key", nullable=False |
| 437 | + ) |
| 438 | + ), |
| 439 | + value=stalg.Expression( |
| 440 | + literal=stalg.Expression.Literal(i32=42, nullable=False) |
| 441 | + ), |
| 442 | + ), |
| 443 | + ] |
| 444 | + ), |
| 445 | + nullable=False, |
| 446 | + ) |
| 447 | + ) |
| 448 | + |
| 449 | + result = infer_nested_type(expr.nested, struct) |
| 450 | + |
| 451 | + expected = stt.Type( |
| 452 | + map=stt.Type.Map( |
| 453 | + key=stt.Type( |
| 454 | + string=stt.Type.String(nullability=stt.Type.NULLABILITY_REQUIRED) |
| 455 | + ), |
| 456 | + value=stt.Type(i32=stt.Type.I32(nullability=stt.Type.NULLABILITY_REQUIRED)), |
| 457 | + nullability=stt.Type.NULLABILITY_REQUIRED, |
| 458 | + ) |
| 459 | + ) |
| 460 | + assert result == expected |
0 commit comments