|
1 | 1 | package com.linkedin.hoptimator.planner;
|
2 | 2 |
|
| 3 | +import org.apache.calcite.adapter.jdbc.JdbcSchema; |
| 4 | +import org.apache.calcite.adapter.jdbc.JdbcCatalogSchema; |
3 | 5 | import org.apache.calcite.config.CalciteConnectionConfig;
|
4 | 6 | import org.apache.calcite.config.CalciteConnectionConfigImpl;
|
5 | 7 | import org.apache.calcite.jdbc.Driver;
|
6 | 8 | import org.apache.calcite.jdbc.CalciteConnection;
|
| 9 | +import org.apache.calcite.model.ModelHandler; |
7 | 10 | import org.apache.calcite.rel.RelCollationTraitDef;
|
8 | 11 | import org.apache.calcite.rel.RelNode;
|
9 | 12 | import org.apache.calcite.rel.rules.CoreRules;
|
|
16 | 19 | import org.apache.calcite.sql.SqlNode;
|
17 | 20 | import org.apache.calcite.schema.Schema;
|
18 | 21 | import org.apache.calcite.schema.SchemaPlus;
|
19 |
| -import org.apache.calcite.model.ModelHandler; |
20 | 22 | import org.apache.calcite.tools.RuleSet;
|
21 | 23 | import org.apache.calcite.tools.RuleSets;
|
22 | 24 | import org.apache.calcite.tools.Frameworks;
|
|
26 | 28 | import com.linkedin.hoptimator.catalog.Database;
|
27 | 29 | import com.linkedin.hoptimator.catalog.DatabaseSchema;
|
28 | 30 |
|
| 31 | +import java.io.IOException; |
29 | 32 | import java.util.Arrays;
|
30 | 33 | import java.util.ArrayList;
|
31 | 34 | import java.util.List;
|
32 | 35 | import java.util.Properties;
|
33 | 36 | import java.util.NoSuchElementException;
|
| 37 | +import java.sql.SQLException; |
| 38 | +import javax.sql.DataSource; |
34 | 39 |
|
35 | 40 | /** A one-shot stateful object, which creates Pipelines from SQL. */
|
36 | 41 | public class HoptimatorPlanner {
|
@@ -67,8 +72,20 @@ public class HoptimatorPlanner {
|
67 | 72 | public interface Factory {
|
68 | 73 | HoptimatorPlanner makePlanner() throws Exception;
|
69 | 74 |
|
70 |
| - static Factory fromModelFile(String filePath, Properties properties) { |
71 |
| - return () -> HoptimatorPlanner.fromModelFile(filePath, properties); |
| 75 | + static Factory fromSchema(String catalog, Schema schema) { |
| 76 | + return () -> HoptimatorPlanner.fromSchema(catalog, schema); |
| 77 | + } |
| 78 | + |
| 79 | + static Factory fromDataSource(String catalog, DataSource dataSource) { |
| 80 | + return () -> HoptimatorPlanner.fromDataSource(catalog, dataSource); |
| 81 | + } |
| 82 | + |
| 83 | + static Factory fromJdbc(String url, String catalog, String username, String password) { |
| 84 | + return () -> HoptimatorPlanner.fromJdbc(url, catalog, username, password); |
| 85 | + } |
| 86 | + |
| 87 | + static Factory fromJdbc(String url, Properties properties) { |
| 88 | + return () -> HoptimatorPlanner.fromJdbc(url, properties); |
72 | 89 | }
|
73 | 90 | }
|
74 | 91 |
|
@@ -131,23 +148,38 @@ public Database database(String name) {
|
131 | 148 | return ((DatabaseSchema) subSchema).database();
|
132 | 149 | }
|
133 | 150 |
|
134 |
| - public static HoptimatorPlanner fromModelFile(String filePath, Properties properties) throws Exception { |
135 |
| - String uri = filePath; |
136 |
| - if (uri.startsWith("jdbc:calcite:model=")) { |
137 |
| - uri = uri.substring("jdbc:calcite:model=".length()); |
138 |
| - } |
| 151 | + public static HoptimatorPlanner fromSchema(String name, Schema schema) { |
| 152 | + SchemaPlus rootSchema = Frameworks.createRootSchema(true); |
| 153 | + rootSchema.add(name == null ? "ROOT" : name, schema); |
| 154 | + return new HoptimatorPlanner(rootSchema); |
| 155 | + } |
| 156 | + |
| 157 | + public static HoptimatorPlanner fromDataSource(String catalog, DataSource dataSource) { |
| 158 | + Schema schema = JdbcCatalogSchema.create(null, catalog, dataSource, catalog); |
| 159 | + return fromSchema(catalog, schema); |
| 160 | + } |
| 161 | + |
| 162 | + public static HoptimatorPlanner fromModelFile(String filePath, Properties properties) |
| 163 | + throws SQLException, IOException { |
139 | 164 | Driver driver = new Driver();
|
140 | 165 | CalciteConnectionConfig connectionConfig = new CalciteConnectionConfigImpl(properties);
|
141 | 166 | CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", properties);
|
142 | 167 | SchemaPlus schema = connection.getRootSchema();
|
143 |
| - ModelHandler modelHandler = new ModelHandler(connection, uri); // side-effect: modifies connection |
| 168 | + ModelHandler modelHandler = new ModelHandler(connection, filePath); // side-effect: modifies connection |
144 | 169 | return new HoptimatorPlanner(schema);
|
145 | 170 | }
|
146 | 171 |
|
147 |
| - // for testing purposes |
148 |
| - static HoptimatorPlanner fromSchema(String name, Schema schema) { |
149 |
| - SchemaPlus rootSchema = Frameworks.createRootSchema(true); |
150 |
| - rootSchema.add(name, schema); |
151 |
| - return new HoptimatorPlanner(rootSchema); |
| 172 | + public static HoptimatorPlanner fromJdbc(String url, String catalog, String username, String password) { |
| 173 | + DataSource dataSource = JdbcSchema.dataSource(url, null, username, password); |
| 174 | + return fromDataSource(catalog, dataSource); |
| 175 | + } |
| 176 | + |
| 177 | + public static HoptimatorPlanner fromJdbc(String url, Properties properties) throws SQLException, IOException { |
| 178 | + if (url.startsWith("jdbc:calcite:model=")) { |
| 179 | + return fromModelFile(url.substring("jdbc:calcite:model=".length()), properties); |
| 180 | + } else { |
| 181 | + return fromJdbc(url, properties.getProperty("catalog"), properties.getProperty("username"), |
| 182 | + properties.getProperty("password")); |
| 183 | + } |
152 | 184 | }
|
153 | 185 | }
|
0 commit comments