|
| 1 | +import substrait.gen.proto.type_pb2 as stt |
| 2 | +import substrait.gen.proto.plan_pb2 as stp |
| 3 | +import substrait.gen.proto.algebra_pb2 as stalg |
| 4 | +from substrait.builders.type import boolean, i64 |
| 5 | +from substrait.builders.plan import read_named_table |
| 6 | + |
| 7 | +struct = stt.Type.Struct(types=[i64(nullable=False), boolean()]) |
| 8 | + |
| 9 | +named_struct = stt.NamedStruct(names=["id", "is_applicable"], struct=struct) |
| 10 | + |
| 11 | + |
| 12 | +def test_write_rel(): |
| 13 | + actual = read_named_table("example_table", named_struct)(None) |
| 14 | + |
| 15 | + # write example table test |
| 16 | + stp.Plan( |
| 17 | + relations=[ |
| 18 | + stp.PlanRel( |
| 19 | + root=stalg.RelRoot( |
| 20 | + input=stalg.Rel( |
| 21 | + write=stalg.WriteRel( |
| 22 | + input=stalg.Rel( |
| 23 | + read=stalg.ReadRel( |
| 24 | + common=stalg.RelCommon(direct=stalg.RelCommon.Direct()), |
| 25 | + base_schema=named_struct, |
| 26 | + named_table=stalg.ReadRel.NamedTable( |
| 27 | + names=["example_table"] |
| 28 | + ), |
| 29 | + ) |
| 30 | + ), |
| 31 | + common=stalg.RelCommon(direct=stalg.RelCommon.Direct()), |
| 32 | + table_schema=named_struct, |
| 33 | + create_mode=stalg.WriteRel.CreateMode.CREATE_MODE_REPLACE_IF_EXISTS, |
| 34 | + named_table=stalg.NamedTable( |
| 35 | + names=["example_table_write_test"] |
| 36 | + ), |
| 37 | + ) |
| 38 | + ), |
| 39 | + names=["id", "is_applicable"], |
| 40 | + ) |
| 41 | + ) |
| 42 | + ] |
| 43 | + ) |
| 44 | + |
| 45 | + # read back the table |
| 46 | + expected = stp.Plan( |
| 47 | + relations=[ |
| 48 | + stp.PlanRel( |
| 49 | + root=stalg.RelRoot( |
| 50 | + input=stalg.Rel( |
| 51 | + read=stalg.ReadRel( |
| 52 | + common=stalg.RelCommon(direct=stalg.RelCommon.Direct()), |
| 53 | + base_schema=named_struct, |
| 54 | + named_table=stalg.ReadRel.NamedTable( |
| 55 | + names=["example_table_write_test"] |
| 56 | + ), |
| 57 | + ) |
| 58 | + ), |
| 59 | + names=["id", "is_applicable"], |
| 60 | + ) |
| 61 | + ) |
| 62 | + ] |
| 63 | + ) |
| 64 | + |
| 65 | + assert actual == expected |
| 66 | + |
| 67 | + |
| 68 | +def test_write_rel_db(): |
| 69 | + actual = read_named_table(["example_db", "example_table"], named_struct)(None) |
| 70 | + |
| 71 | + expected = stp.Plan( |
| 72 | + relations=[ |
| 73 | + stp.PlanRel( |
| 74 | + root=stalg.RelRoot( |
| 75 | + input=stalg.Rel( |
| 76 | + read=stalg.ReadRel( |
| 77 | + common=stalg.RelCommon(direct=stalg.RelCommon.Direct()), |
| 78 | + base_schema=named_struct, |
| 79 | + named_table=stalg.ReadRel.NamedTable( |
| 80 | + names=["example_db", "example_table"] |
| 81 | + ), |
| 82 | + ) |
| 83 | + ), |
| 84 | + names=["id", "is_applicable"], |
| 85 | + ) |
| 86 | + ) |
| 87 | + ] |
| 88 | + ) |
| 89 | + |
| 90 | + assert actual == expected |
0 commit comments