1
1
package com .linkedin .hoptimator .jdbc ;
2
2
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 ;
3
7
import java .sql .PreparedStatement ;
4
8
import java .sql .SQLException ;
5
9
import java .sql .Statement ;
6
10
import java .util .ArrayList ;
7
- import java .util .Collections ;
8
11
import java .util .List ;
9
12
import java .util .Locale ;
10
13
import java .util .Map ;
14
+ import java .util .Objects ;
11
15
import java .util .Properties ;
12
16
import java .util .stream .Collectors ;
13
17
18
+ import org .apache .avro .Schema ;
14
19
import org .apache .calcite .jdbc .CalciteConnection ;
15
20
import org .apache .calcite .jdbc .CalcitePrepare ;
21
+ import org .apache .calcite .jdbc .CalciteSchema ;
16
22
import org .apache .calcite .plan .RelOptMaterialization ;
17
23
import org .apache .calcite .rel .RelNode ;
18
24
19
25
import com .linkedin .hoptimator .avro .AvroConverter ;
20
26
import com .linkedin .hoptimator .util .DelegatingConnection ;
27
+ import org .apache .calcite .util .Util ;
21
28
22
29
23
30
public class HoptimatorConnection extends DelegatingConnection {
@@ -44,9 +51,12 @@ public ResolvedTable resolve(List<String> tablePath, Map<String, String> hints)
44
51
.limit (tablePath .size () - 1 )
45
52
.collect (Collectors .joining ("." ));
46
53
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 ));
50
60
} catch (Exception e ) {
51
61
throw new SQLException ("Failed to resolve " + String .join ("." , tablePath ) + ": " + e .getMessage (), e );
52
62
}
@@ -92,4 +102,16 @@ private void registerMaterialization(List<String> viewPath, RelNode tableRel, Re
92
102
public List <RelOptMaterialization > materializations () {
93
103
return materializations ;
94
104
}
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
+ }
95
117
}
0 commit comments