Skip to content

Commit 20017a5

Browse files
authored
Add source/sink configs to resolve output (#153)
1 parent c9230b0 commit 20017a5

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

hoptimator-cli/src/main/java/sqlline/HoptimatorAppConfig.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,12 @@ public void execute(String line, DispatchCallback dispatchCallback) {
170170
HoptimatorConnection conn = (HoptimatorConnection) sqlline.getConnection();
171171
try {
172172
ResolvedTable resolved = conn.resolve(tablePath, Collections.emptyMap());
173-
sqlline.output("Avro schema:\n");
174-
sqlline.output(resolved.avroSchemaString());
173+
sqlline.output("Avro schema:");
174+
sqlline.output(resolved.avroSchemaString() + "\n");
175+
sqlline.output("Source configs:");
176+
sqlline.output(resolved.sourceConnectorConfigs().toString() + "\n");
177+
sqlline.output("Sink configs:");
178+
sqlline.output(resolved.sinkConnectorConfigs().toString() + "\n");
175179
} catch (SQLException e) {
176180
sqlline.error(e);
177181
dispatchCallback.setToFailure();

hoptimator-jdbc/src/main/java/com/linkedin/hoptimator/jdbc/HoptimatorConnection.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
package com.linkedin.hoptimator.jdbc;
22

3+
import com.linkedin.hoptimator.Database;
4+
import com.linkedin.hoptimator.Sink;
5+
import com.linkedin.hoptimator.Source;
6+
import com.linkedin.hoptimator.util.ConnectionService;
37
import java.sql.PreparedStatement;
48
import java.sql.SQLException;
59
import java.sql.Statement;
610
import java.util.ArrayList;
7-
import java.util.Collections;
811
import java.util.List;
912
import java.util.Locale;
1013
import java.util.Map;
14+
import java.util.Objects;
1115
import java.util.Properties;
1216
import java.util.stream.Collectors;
1317

18+
import org.apache.avro.Schema;
1419
import org.apache.calcite.jdbc.CalciteConnection;
1520
import org.apache.calcite.jdbc.CalcitePrepare;
21+
import org.apache.calcite.jdbc.CalciteSchema;
1622
import org.apache.calcite.plan.RelOptMaterialization;
1723
import org.apache.calcite.rel.RelNode;
1824

1925
import com.linkedin.hoptimator.avro.AvroConverter;
2026
import com.linkedin.hoptimator.util.DelegatingConnection;
27+
import org.apache.calcite.util.Util;
2128

2229

2330
public class HoptimatorConnection extends DelegatingConnection {
@@ -44,9 +51,12 @@ public ResolvedTable resolve(List<String> tablePath, Map<String, String> hints)
4451
.limit(tablePath.size() - 1)
4552
.collect(Collectors.joining("."));
4653
String schemaName = tablePath.get(tablePath.size() - 1).toLowerCase(Locale.ROOT);
47-
org.apache.avro.Schema avroSchema = AvroConverter.avro(namespace, schemaName, tableRel.getRowType());
48-
// TODO: generate source and sink configs via ConnectionService
49-
return new ResolvedTable(tablePath, avroSchema, Collections.emptyMap(), Collections.emptyMap());
54+
Schema avroSchema = AvroConverter.avro(namespace, schemaName, tableRel.getRowType());
55+
String database = databaseName(this.createPrepareContext(), tablePath);
56+
Source source = new Source(database, tablePath, hints);
57+
Sink sink = new Sink(database, tablePath, hints);
58+
return new ResolvedTable(tablePath, avroSchema, ConnectionService.configure(source, this),
59+
ConnectionService.configure(sink, this));
5060
} catch (Exception e) {
5161
throw new SQLException("Failed to resolve " + String.join(".", tablePath) + ": " + e.getMessage(), e);
5262
}
@@ -92,4 +102,16 @@ private void registerMaterialization(List<String> viewPath, RelNode tableRel, Re
92102
public List<RelOptMaterialization> materializations() {
93103
return materializations;
94104
}
105+
106+
private static String databaseName(CalcitePrepare.Context context, List<String> tablePath) throws SQLException {
107+
final List<String> path = Util.skipLast(tablePath);
108+
CalciteSchema schema = context.getRootSchema();
109+
for (String p : path) {
110+
schema = Objects.requireNonNull(schema).getSubSchema(p, true);
111+
}
112+
if (schema == null || !(schema.schema instanceof Database)) {
113+
throw new SQLException(tablePath + " is not a physical database.");
114+
}
115+
return ((Database) schema.schema).databaseName();
116+
}
95117
}

0 commit comments

Comments
 (0)