Skip to content

Commit 7baf2ca

Browse files
committed
fix: review comments on the format
Signed-off-by: MBWhite <[email protected]>
1 parent efe1ff2 commit 7baf2ca

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

examples/isthmus-api/README.md

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,50 @@ To run these you will need:
2828

2929
## Creating a Substrait Plan from SQL
3030

31-
To run [`FromSql.java`](./src/main/java/io/substrait/examples/FromSql.java) from the root of this repository. `subtrait.plan` is the name of file written.
31+
To run [`FromSql.java`](./src/main/java/io/substrait/examples/FromSql.java) from the root of this repository. `subtrait.plan` is the name of file written. It will output to stdout the text format of the protobuf for information; it's quite length so has been abbreviated here.
3232

3333
```bash
3434
./gradlew examples:isthmus-api:run --args "FromSql substrait.plan"
3535
> Task :examples:isthmus-api:run
36-
Plan{version=Version{major=0, minor=77, patch=0, producer=isthmus}, roots=[Root{input=Sort{input=Aggregate{input=Project{remap=Remap{indices=[15]}, input=Filter{input=Join{left=NamedScan{initialSchema=NamedStruct{struct=Struct{nullable=false, fields=[VarChar{nullable=true, length=15}, VarChar{nullable=true, length=40}, VarChar{nullable=true, length=40}, VarChar{nullable=true, length=15}, VarChar{nullable=true, length=15}, I32{nullable=true}, VarChar{nullable=true, length=15}]}, names=[vehicle_id, make, model, colour, fuel_type, cylinder_capacity, first_use_date]}, names=[vehicles]}, right=NamedScan{initialSchema=NamedStruct{struct=Struct{nullable=false, fields=[VarChar{nullable=true, length=15}, VarChar{nullable=true, length=15}, VarChar{nullable=true, length=20}, VarChar{nullable=true, length=20}, VarChar{nullable=true, length=20}, VarChar{nullable=true, length=15}, I32{nullable=true}, VarChar{nullable=true, length=15}]}, names=[test_id, vehicle_id, test_date, test_class, test_type, test_result, test_mileage, postcode_area]}, names=[tests]}, condition=ScalarFunctionInvocation{declaration=equal:any_any, arguments=[FieldReference{segments=[StructField{offset=0}], type=VarChar{nullable=true, length=15}}, FieldReference{segments=[StructField{offset=8}], type=VarChar{nullable=true, length=15}}], options=[], outputType=Bool{nullable=true}}, joinType=INNER}, condition=ScalarFunctionInvocation{declaration=equal:any_any, arguments=[FieldReference{segments=[StructField{offset=12}], type=VarChar{nullable=true, length=15}}, VarCharLiteral{nullable=false, value=P, length=15}], options=[], outputType=Bool{nullable=true}}}, expressions=[FieldReference{segments=[StructField{offset=3}], type=VarChar{nullable=true, length=15}}]}, groupings=[Grouping{expressions=[FieldReference{segments=[StructField{offset=0}], type=VarChar{nullable=true, length=15}}]}], measures=[Measure{function=AggregateFunctionInvocation{declaration=count:, arguments=[], options=[], aggregationPhase=INITIAL_TO_RESULT, sort=[], outputType=I64{nullable=false}, invocation=ALL}}]}, sortFields=[SortField{expr=FieldReference{segments=[StructField{offset=1}], type=Struct{nullable=false, fields=[VarChar{nullable=true, length=15}, I64{nullable=false}]}}, direction=ASC_NULLS_LAST}]}, names=[COLOUR, COLOURCOUNT]}], expectedTypeUrls=[]}
36+
extension_uris {
37+
extension_uri_anchor: 2
38+
uri: "/functions_aggregate_generic.yaml"
39+
}
40+
extension_uris {
41+
extension_uri_anchor: 1
42+
uri: "/functions_comparison.yaml"
43+
}
44+
extensions {
45+
extension_function {
46+
extension_uri_reference: 1
47+
function_anchor: 1
48+
name: "equal:any_any"
49+
extension_urn_reference: 1
50+
}
51+
}
52+
extensions {
53+
extension_function {
54+
extension_uri_reference: 2
55+
function_anchor: 2
56+
name: "count:"
57+
extension_urn_reference: 2
58+
}
59+
}
60+
relations {....}
61+
}
62+
version {
63+
minor_number: 77
64+
producer: "isthmus"
65+
}
66+
extension_urns {
67+
extension_urn_anchor: 1
68+
urn: "extension:io.substrait:functions_comparison"
69+
}
70+
extension_urns {
71+
extension_urn_anchor: 2
72+
urn: "extension:io.substrait:functions_aggregate_generic"
73+
}
74+
3775
File written to substrait.plan
3876
```
3977

@@ -49,13 +87,43 @@ Please see the code comments for details of how the conversion is done.
4987

5088
To run [`ToSql.java`](./src/main/java/io/substrait/examples/ToSql.java) from the root of this repository
5189
`subtrait.plan` is the name of file to be read - and probably will be the first created with `FromSql`.
52-
90+
Again the text format of the protobuf has been abbreviated.
5391
```bash
5492
./gradlew examples:isthmus-api:run --args "ToSql substrait.plan"
5593

5694
> Task :examples:isthmus-api:run
5795
Reading from substrait.plan
58-
Plan{version=Version{major=0, minor=77, patch=0, producer=isthmus}, roots=[Root{input=Sort{input=Aggregate{input=Project{remap=Remap{indices=[15]}, input=Filter{input=Join{left=NamedScan{initialSchema=NamedStruct{struct=Struct{nullable=false, fields=[VarChar{nullable=true, length=15}, VarChar{nullable=true, length=40}, VarChar{nullable=true, length=40}, VarChar{nullable=true, length=15}, VarChar{nullable=true, length=15}, I32{nullable=true}, VarChar{nullable=true, length=15}]}, names=[vehicle_id, make, model, colour, fuel_type, cylinder_capacity, first_use_date]}, names=[vehicles]}, right=NamedScan{initialSchema=NamedStruct{struct=Struct{nullable=false, fields=[VarChar{nullable=true, length=15}, VarChar{nullable=true, length=15}, VarChar{nullable=true, length=20}, VarChar{nullable=true, length=20}, VarChar{nullable=true, length=20}, VarChar{nullable=true, length=15}, I32{nullable=true}, VarChar{nullable=true, length=15}]}, names=[test_id, vehicle_id, test_date, test_class, test_type, test_result, test_mileage, postcode_area]}, names=[tests]}, condition=ScalarFunctionInvocation{declaration=equal:any_any, arguments=[FieldReference{segments=[StructField{offset=0}], type=VarChar{nullable=true, length=15}}, FieldReference{segments=[StructField{offset=8}], type=VarChar{nullable=true, length=15}}], options=[], outputType=Bool{nullable=true}}, joinType=INNER}, condition=ScalarFunctionInvocation{declaration=equal:any_any, arguments=[FieldReference{segments=[StructField{offset=12}], type=VarChar{nullable=true, length=15}}, VarCharLiteral{nullable=false, value=P, length=15}], options=[], outputType=Bool{nullable=true}}}, expressions=[FieldReference{segments=[StructField{offset=3}], type=VarChar{nullable=true, length=15}}]}, groupings=[Grouping{expressions=[FieldReference{segments=[StructField{offset=0}], type=VarChar{nullable=true, length=15}}]}], measures=[Measure{function=AggregateFunctionInvocation{declaration=count:, arguments=[], options=[], aggregationPhase=INITIAL_TO_RESULT, sort=[], outputType=I64{nullable=false}, invocation=ALL}}]}, sortFields=[SortField{expr=FieldReference{segments=[StructField{offset=1}], type=I64{nullable=false}}, direction=ASC_NULLS_LAST}]}, names=[COLOUR, COLOURCOUNT]}], expectedTypeUrls=[]}
96+
extension_uris {
97+
extension_uri_anchor: 2
98+
uri: "/functions_aggregate_generic.yaml"
99+
}
100+
extension_uris {
101+
extension_uri_anchor: 1
102+
uri: "/functions_comparison.yaml"
103+
}
104+
extensions {
105+
extension_function {
106+
extension_uri_reference: 1
107+
function_anchor: 1
108+
name: "equal:any_any"
109+
extension_urn_reference: 1
110+
}
111+
}
112+
extensions {....}
113+
relations {....}
114+
version {
115+
minor_number: 77
116+
producer: "isthmus"
117+
}
118+
extension_urns {
119+
extension_urn_anchor: 1
120+
urn: "extension:io.substrait:functions_comparison"
121+
}
122+
extension_urns {
123+
extension_urn_anchor: 2
124+
urn: "extension:io.substrait:functions_aggregate_generic"
125+
}
126+
59127

60128
SELECT `t2`.`colour0` AS `COLOUR`, `t2`.`$f1` AS `COLOURCOUNT`
61129
FROM (SELECT `vehicles`.`colour` AS `colour0`, COUNT(*) AS `$f1`

examples/isthmus-api/src/main/java/io/substrait/examples/FromSql.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,16 @@ GROUP BY vehicles.colour ORDER BY count(*)
8383
final SqlDialect dialect = SqlDialect.DatabaseProduct.DERBY.getDialect();
8484
final Plan substraitPlan = sqlToSubstrait.convert(query, catalogReader, dialect);
8585

86-
System.out.println(substraitPlan);
86+
// Create the proto plan to display to stdout - as it has a better format
87+
final PlanProtoConverter planToProto = new PlanProtoConverter();
88+
final io.substrait.proto.Plan protoPlan = planToProto.toProto(substraitPlan);
89+
System.out.println(protoPlan);
8790

8891
// write out to file if given a file name
8992
// convert to a protobuff byte array and write as binary file
9093
if (args.length == 1) {
91-
final PlanProtoConverter planToProto = new PlanProtoConverter();
92-
final byte[] buffer = planToProto.toProto(substraitPlan).toByteArray();
9394

95+
final byte[] buffer = protoPlan.toByteArray();
9496
final Path outputFile = Paths.get(args[0]);
9597
Files.write(outputFile, buffer);
9698
System.out.println("File written to " + outputFile);

examples/isthmus-api/src/main/java/io/substrait/examples/ToSql.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.substrait.isthmus.SubstraitTypeSystem;
88
import io.substrait.plan.Plan;
99
import io.substrait.plan.Plan.Root;
10+
import io.substrait.plan.PlanProtoConverter;
1011
import io.substrait.plan.ProtoPlanConverter;
1112
import java.io.IOException;
1213
import java.nio.file.Files;
@@ -46,8 +47,10 @@ public void run(String[] args) {
4647
final ProtoPlanConverter protoToPlan = new ProtoPlanConverter();
4748
final Plan substraitPlan = protoToPlan.from(proto);
4849

49-
// output the plan for information
50-
System.out.println(substraitPlan);
50+
// Create the proto plan to display to stdout - as it has a better format
51+
final PlanProtoConverter planToProto = new PlanProtoConverter();
52+
final io.substrait.proto.Plan protoPlan = planToProto.toProto(substraitPlan);
53+
System.out.println(protoPlan);
5154

5255
final SimpleExtension.ExtensionCollection extensions =
5356
DefaultExtensionCatalog.DEFAULT_COLLECTION;
@@ -62,6 +65,7 @@ public void run(String[] args) {
6265
final RelToSqlConverter relToSql = new RelToSqlConverter(sqlDialect);
6366
final List<String> sqlStrings = new ArrayList<>();
6467

68+
System.out.println("\n");
6569
// and get each root from the calcite plan
6670
for (final Root root : substraitPlan.getRoots()) {
6771
final RelNode calciteRelNode = converter.convert(root).project(true);

0 commit comments

Comments
 (0)