-
Notifications
You must be signed in to change notification settings - Fork 72
/
PlanEntryPoint.java
90 lines (76 loc) · 3.14 KB
/
PlanEntryPoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package io.substrait.isthmus;
import static picocli.CommandLine.Command;
import static picocli.CommandLine.Option;
import static picocli.CommandLine.Parameters;
import com.google.common.annotations.VisibleForTesting;
import com.google.protobuf.TextFormat;
import com.google.protobuf.util.JsonFormat;
import io.substrait.isthmus.SubstraitRelVisitor.CrossJoinPolicy;
import io.substrait.proto.Plan;
import java.util.List;
import java.util.concurrent.Callable;
import org.apache.calcite.sql.validate.SqlConformanceEnum;
import picocli.CommandLine;
@Command(
name = "isthmus",
version = "isthmus 0.1",
description = "Converts a SQL query to a Substrait Plan")
public class PlanEntryPoint implements Callable<Integer> {
static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(PlanEntryPoint.class);
@Parameters(index = "0", description = "The sql we should parse.")
private String sql;
@Option(
names = {"-c", "--create"},
description =
"One or multiple create table statements e.g. CREATE TABLE T1(foo int, bar bigint)")
private List<String> createStatements;
@Option(
names = {"-m", "--multistatement"},
description = "Allow multiple statements terminated with a semicolon")
private boolean allowMultiStatement;
@Option(
names = {"--outputformat"},
defaultValue = "PROTOJSON",
description = "Set the output format for the generated plan: ${COMPLETION-CANDIDATES}")
private OutputFormat outputFormat = OutputFormat.PROTOJSON;
enum OutputFormat {
PROTOJSON, // protobuf json format
PROTOTEXT, // protobuf text format
BINARY, // protobuf BINARY format
}
@Option(
names = {"--sqlconformancemode"},
description = "One of built-in Calcite SQL compatibility modes: ${COMPLETION-CANDIDATES}")
private SqlConformanceEnum sqlConformanceMode = SqlConformanceEnum.DEFAULT;
@Option(
names = {"--crossjoinpolicy"},
description = "One of built-in Calcite SQL compatibility modes: ${COMPLETION-CANDIDATES}")
private CrossJoinPolicy crossJoinPolicy = CrossJoinPolicy.KEEP_AS_CROSS_JOIN;
// this example implements Callable, so parsing, error handling and handling user
// requests for usage help or version help can be done with one line of code.
public static void main(String... args) {
int exitCode = new CommandLine(new PlanEntryPoint()).execute(args);
System.exit(exitCode);
}
@Override
public Integer call() throws Exception {
FeatureBoard featureBoard = buildFeatureBoard();
SqlToSubstrait converter = new SqlToSubstrait(featureBoard);
Plan plan = converter.execute(sql, createStatements);
switch (outputFormat) {
case PROTOJSON -> System.out.println(
JsonFormat.printer().includingDefaultValueFields().print(plan));
case PROTOTEXT -> TextFormat.printer().print(plan, System.out);
case BINARY -> plan.writeTo(System.out);
}
return 0;
}
@VisibleForTesting
FeatureBoard buildFeatureBoard() {
return ImmutableFeatureBoard.builder()
.allowsSqlBatch(allowMultiStatement)
.sqlConformanceMode(sqlConformanceMode)
.crossJoinPolicy(crossJoinPolicy)
.build();
}
}