|
1 | 1 | package io.substrait.isthmus; |
2 | 2 |
|
3 | | -import com.google.common.annotations.VisibleForTesting; |
4 | | -import io.substrait.isthmus.expression.DdlRelBuilder; |
5 | | -import io.substrait.isthmus.sql.SubstraitSqlValidator; |
| 3 | +import io.substrait.isthmus.operation.CalciteOperation; |
| 4 | +import io.substrait.isthmus.sql.SubstraitSqlToCalcite; |
6 | 5 | import io.substrait.plan.ImmutablePlan.Builder; |
7 | 6 | import io.substrait.plan.Plan; |
8 | 7 | import io.substrait.plan.Plan.Version; |
9 | 8 | import io.substrait.plan.PlanProtoConverter; |
10 | 9 | import java.util.List; |
11 | | -import org.apache.calcite.plan.hep.HepPlanner; |
12 | | -import org.apache.calcite.plan.hep.HepProgram; |
13 | 10 | import org.apache.calcite.prepare.Prepare; |
14 | | -import org.apache.calcite.rel.RelRoot; |
15 | | -import org.apache.calcite.rel.rules.CoreRules; |
16 | | -import org.apache.calcite.sql.SqlNode; |
17 | | -import org.apache.calcite.sql.SqlNodeList; |
18 | 11 | import org.apache.calcite.sql.parser.SqlParseException; |
19 | | -import org.apache.calcite.sql.parser.SqlParser; |
20 | | -import org.apache.calcite.sql.validate.SqlValidator; |
21 | | -import org.apache.calcite.sql2rel.SqlToRelConverter; |
22 | | -import org.apache.calcite.sql2rel.StandardConvertletTable; |
23 | 12 |
|
24 | 13 | /** Take a SQL statement and a set of table definitions and return a substrait plan. */ |
25 | 14 | public class SqlToSubstrait extends SqlConverterBase { |
@@ -66,126 +55,14 @@ public Plan convert(String sqlStatements, Prepare.CatalogReader catalogReader) |
66 | 55 | builder.version(Version.builder().from(Version.DEFAULT_VERSION).producer("isthmus").build()); |
67 | 56 |
|
68 | 57 | // TODO: consider case in which one sql passes conversion while others don't |
69 | | - sqlToRelNode(sqlStatements, catalogReader).stream() |
70 | | - .map(root -> SubstraitRelVisitor.convert(root, EXTENSION_COLLECTION, featureBoard)) |
71 | | - .forEach(root -> builder.addRoots(root)); |
72 | | - |
73 | | - return builder.build(); |
74 | | - } |
75 | | - |
76 | | - /** |
77 | | - * Converts one or more SQL statements into a Substrait {@link Plan}. |
78 | | - * |
79 | | - * @param sqlStatements a string containing one more SQL statements |
80 | | - * @param catalogReader the {@link Prepare.CatalogReader} for finding tables/views referenced in |
81 | | - * the SQL statements |
82 | | - * @return the Substrait {@link Plan} |
83 | | - * @throws SqlParseException if there is an error while parsing the SQL statements |
84 | | - */ |
85 | | - public Plan convertNew(String sqlStatements, Prepare.CatalogReader catalogReader) |
86 | | - throws SqlParseException { |
87 | | - Builder builder = io.substrait.plan.Plan.builder(); |
88 | | - builder.version(Version.builder().from(Version.DEFAULT_VERSION).producer("isthmus").build()); |
89 | | - |
90 | | - // TODO: consider case in which one sql passes conversion while others don't |
91 | | - SubstraitSqlToCalcite.convertQueries(sqlStatements, catalogReader).stream() |
92 | | - .map(root -> SubstraitRelVisitor.convert(root, EXTENSION_COLLECTION, featureBoard)) |
93 | | - .forEach(root -> builder.addRoots(root)); |
94 | | - |
95 | | - return builder.build(); |
96 | | - } |
97 | | - |
98 | | - @VisibleForTesting |
99 | | - List<RelRoot> sqlToRelNode(String sql, Prepare.CatalogReader catalogReader) |
100 | | - throws SqlParseException { |
101 | | - SqlValidator validator = new SubstraitSqlValidator(catalogReader); |
102 | | - SqlParser parser = SqlParser.create(sql, parserConfig); |
103 | | - SqlNodeList parsedList = parser.parseStmtList(); |
104 | | - SqlToRelConverter converter = createSqlToRelConverter(validator, catalogReader); |
105 | | - List<RelRoot> roots = |
106 | | - parsedList.stream() |
107 | | - .map(parsed -> getBestExpRelRoot(converter, parsed)) |
108 | | - .collect(java.util.stream.Collectors.toList()); |
109 | | - return roots; |
110 | | - } |
111 | | - |
112 | | - void sqlToPlanRoots( |
113 | | - String sql, SqlValidator validator, Prepare.CatalogReader catalogReader, Builder builder) |
114 | | - throws SqlParseException { |
115 | | - List<CalciteOperation> calciteOperations = sqlToCalciteOperation(sql, validator, catalogReader); |
| 58 | + List<CalciteOperation> calciteOperations = |
| 59 | + SubstraitSqlToCalcite.convertQueries(sqlStatements, catalogReader); |
116 | 60 | CalciteOperationToSubstrait calciteOperationToSubstrait = |
117 | 61 | new CalciteOperationToSubstrait(EXTENSION_COLLECTION, featureBoard); |
118 | 62 | calciteOperations.stream() |
119 | 63 | .map(operation -> operation.accept(calciteOperationToSubstrait)) |
120 | 64 | .forEach(builder::addRoots); |
121 | | - } |
122 | | - |
123 | | - void sqlToPlanRoots1( |
124 | | - String sql, SqlValidator validator, Prepare.CatalogReader catalogReader, Builder builder) |
125 | | - throws SqlParseException { |
126 | | - |
127 | | - SqlParser parser = SqlParser.create(sql, parserConfig); |
128 | | - SqlNodeList parsedList = parser.parseStmtList(); |
129 | | - if (parsedList.isEmpty()) { |
130 | | - return; |
131 | | - } |
132 | | - |
133 | | - SqlToRelConverter converter = createSqlToRelConverter(validator, catalogReader); |
134 | | - DdlRelBuilder ddlRelBuilder = |
135 | | - new DdlRelBuilder( |
136 | | - converter, SqlToSubstrait::getBestExpRelRoot, EXTENSION_COLLECTION, featureBoard); |
137 | | - |
138 | | - List<SqlNode> nonDdlNodes = new ArrayList<>(); |
139 | 65 |
|
140 | | - for (SqlNode sqlNode : parsedList) { |
141 | | - final io.substrait.plan.Plan.Root ddlRoot = sqlNode.accept(ddlRelBuilder); |
142 | | - if (ddlRoot != null) { |
143 | | - builder.addRoots(ddlRoot); |
144 | | - } else { |
145 | | - nonDdlNodes.add(sqlNode); |
146 | | - } |
147 | | - } |
148 | | - |
149 | | - if (!nonDdlNodes.isEmpty()) { |
150 | | - SqlNodeList dmlNodes = new SqlNodeList(nonDdlNodes, parsedList.getParserPosition()); |
151 | | - |
152 | | - List<RelRoot> relRoots = sqlNodesToRelNode(dmlNodes, converter); |
153 | | - relRoots.stream() |
154 | | - .map(root -> SubstraitRelVisitor.convert(root, EXTENSION_COLLECTION, featureBoard)) |
155 | | - .forEach(builder::addRoots); |
156 | | - } |
157 | | - } |
158 | | - |
159 | | - private List<RelRoot> sqlNodesToRelNode( |
160 | | - final SqlNodeList parsedList, final SqlToRelConverter converter) { |
161 | | - return parsedList.stream() |
162 | | - .map(parsed -> getBestExpRelRoot(converter, parsed)) |
163 | | - .collect(java.util.stream.Collectors.toList()); |
164 | | - } |
165 | | - |
166 | | - protected SqlToRelConverter createSqlToRelConverter( |
167 | | - SqlValidator validator, Prepare.CatalogReader catalogReader) { |
168 | | - SqlToRelConverter converter = |
169 | | - new SqlToRelConverter( |
170 | | - null, |
171 | | - validator, |
172 | | - catalogReader, |
173 | | - relOptCluster, |
174 | | - StandardConvertletTable.INSTANCE, |
175 | | - converterConfig); |
176 | | - return converter; |
177 | | - } |
178 | | - |
179 | | - protected static RelRoot getBestExpRelRoot(SqlToRelConverter converter, SqlNode parsed) { |
180 | | - RelRoot root = converter.convertQuery(parsed, true, true); |
181 | | - { |
182 | | - // RelBuilder seems to implicitly use the rule below, |
183 | | - // need to add to avoid discrepancies in assertFullRoundTrip |
184 | | - HepProgram program = HepProgram.builder().addRuleInstance(CoreRules.PROJECT_REMOVE).build(); |
185 | | - HepPlanner hepPlanner = new HepPlanner(program); |
186 | | - hepPlanner.setRoot(root.rel); |
187 | | - root = root.withRel(hepPlanner.findBestExp()); |
188 | | - } |
189 | | - return root; |
| 66 | + return builder.build(); |
190 | 67 | } |
191 | 68 | } |
0 commit comments