// Find all the ProcParameter correlations that map to the target column
// in the Procedure
// ParameterCorrelations correlations = info.getCorrelations();
// assert(correlations != null);
ProcParameter default_param = catalog_proc.getParameters().get(0);
ObjectHistogram<Column> col_access_histogram = this.proc_column_histogram.get(catalog_proc);
if (col_access_histogram == null) {
if (debug.val)
LOG.warn("No column access histogram for " + catalog_proc + ". Setting to default");
return (default_param);
}
if (debug.val)
LOG.debug(catalog_proc + " Column Histogram:\n" + col_access_histogram);
// Loop through each Table and check whether its partitioning column is
// referenced in this procedure
Map<ProcParameter, List<Double>> param_weights = new HashMap<ProcParameter, List<Double>>();
for (Table catalog_tbl : info.catalogContext.database.getTables()) {
if (catalog_tbl.getIsreplicated())
continue;
Column catalog_col = catalog_tbl.getPartitioncolumn();
if (!col_access_histogram.contains(catalog_col))
continue;
long col_access_cnt = col_access_histogram.get(catalog_col);
if (debug.val)
LOG.debug(CatalogUtil.getDisplayName(catalog_col));
// Now loop through the ProcParameters and figure out which ones are
// correlated to the Column
for (ProcParameter catalog_proc_param : catalog_proc.getParameters()) {
// Skip if this is an array
if (hints.enable_array_procparameter_candidates == false && catalog_proc_param.getIsarray())
continue;
if (!param_weights.containsKey(catalog_proc_param)) {
param_weights.put(catalog_proc_param, new ArrayList<Double>());
}
List<Double> weights_list = param_weights.get(catalog_proc_param);
Collection<ParameterMapping> pms = mappings.get(catalog_proc_param, catalog_col);
if (pms != null) {
for (ParameterMapping c : pms) {
weights_list.add(c.getCoefficient() * col_access_cnt);
} // FOR
}
if (debug.val)
LOG.debug(" " + catalog_proc_param + ": " + weights_list);
} // FOR
if (debug.val)
LOG.debug("");
} // FOR (Table)
final Map<ProcParameter, Double> final_param_weights = new HashMap<ProcParameter, Double>();
for (Entry<ProcParameter, List<Double>> e : param_weights.entrySet()) {
// The weights for each ProcParameter will be the geometric mean of
// the correlation coefficients
if (!e.getValue().isEmpty()) {
double weights[] = new double[e.getValue().size()];
for (int i = 0; i < weights.length; i++)
weights[i] = e.getValue().get(i);
final_param_weights.put(e.getKey(), MathUtil.geometricMean(weights, MathUtil.GEOMETRIC_MEAN_ZERO));
}
} // FOR
if (final_param_weights.isEmpty()) {
if (debug.val)
LOG.warn("Failed to find any ProcParameters for " + catalog_proc.getName() + " that map to partition columns");
return (default_param);
}
Map<ProcParameter, Double> sorted = CollectionUtil.sortByValues(final_param_weights, true);
assert (sorted != null);
ProcParameter best_param = CollectionUtil.first(sorted.keySet());
if (debug.val)
LOG.debug("Best Param: " + best_param + " " + sorted);
return (best_param);
}