3333import java .sql .SQLException ;
3434import java .util .List ;
3535
36-
3736public class GenericJdbcTableSource extends JdbcTableSource implements GenericJdbcExecutionOperator {
3837
38+ /**
39+ * Name of the JDBC configuration to use.
40+ */
41+ public String jdbcName ;
42+
3943 /**
4044 * Creates a new instance.
4145 *
42- * @see TableSource#TableSource(String, String...)
43- * @param jdbcName on which table resides
44- *
45- *
46+ * @param tableName the table to read from
47+ * @param jdbcName the JDBC configuration name
48+ * @param columnNames the columns to read
4649 */
47-
48- public String jdbcName ;
49- public GenericJdbcTableSource (String jdbcName , String tableName , String ... columnNames ) {
50+ public GenericJdbcTableSource (String tableName , String ... columnNames ) {
5051 super (tableName , columnNames );
51- this .jdbcName = jdbcName ;
52+ this .jdbcName = "genericjdbc" ;
5253 }
5354
5455 /**
5556 * Copies an instance (exclusive of broadcasts).
5657 *
57- * @param that that should be copied
58+ * @param that the instance that should be copied
5859 */
5960 public GenericJdbcTableSource (GenericJdbcTableSource that ) {
6061 super (that );
@@ -66,42 +67,45 @@ public List<ChannelDescriptor> getSupportedInputChannels(int index) {
6667 throw new UnsupportedOperationException ("This operator has no input channels." );
6768 }
6869
70+ @ Override
6971 public CardinalityEstimator getCardinalityEstimator (int outputIndex ) {
7072 assert outputIndex == 0 ;
7173 return new CardinalityEstimator () {
7274 @ Override
7375 public CardinalityEstimate estimate (OptimizationContext optimizationContext , CardinalityEstimate ... inputEstimates ) {
74- // see Job for StopWatch measurements
76+
7577 final TimeMeasurement timeMeasurement = optimizationContext .getJob ().getStopWatch ().start (
7678 "Optimization" , "Cardinality&Load Estimation" , "Push Estimation" , "Estimate source cardinalities"
7779 );
7880
79- // Establish a DB connection.
8081 try (Connection connection = GenericJdbcPlatform .getInstance ()
81- .createDatabaseDescriptor (optimizationContext .getConfiguration (),jdbcName )
82+ .createDatabaseDescriptor (optimizationContext .getConfiguration (), jdbcName )
8283 .createJdbcConnection ()) {
8384
84- // Query the table cardinality.
85- final String sql = String .format ("SELECT count(*) FROM %s;" , GenericJdbcTableSource .this .getTableName ());
85+ final String sql = String .format (
86+ "SELECT count(*) FROM %s;" , GenericJdbcTableSource .this .getTableName ()
87+ );
88+
8689 final ResultSet resultSet = connection .createStatement ().executeQuery (sql );
90+
8791 if (!resultSet .next ()) {
8892 throw new SQLException ("No query result for \" " + sql + "\" ." );
8993 }
9094 long cardinality = resultSet .getLong (1 );
9195 return new CardinalityEstimate (cardinality , cardinality , 1d );
9296
9397 } catch (Exception e ) {
98+
9499 LogManager .getLogger (this .getClass ()).error (
95100 "Could not estimate cardinality for {}." , GenericJdbcTableSource .this , e
96101 );
97102
98- // If we could not load the cardinality, let's use a very conservative estimate.
99103 return new CardinalityEstimate (10 , 10000000 , 0.9 );
104+
100105 } finally {
101106 timeMeasurement .stop ();
102107 }
103108 }
104109 };
105110 }
106- }
107-
111+ }
0 commit comments