Search in sources :

Example 1 with DataCenterColumnHandle

use of io.hetu.core.plugin.datacenter.DataCenterColumnHandle in project hetu-core by openlookeng.

the class DataCenterClient method getTableStatistics.

/**
 * Get remote table statistics.
 *
 * @param tableFullName the fully qualified table name
 * @param columnHandles data center column handles
 * @return the table statistics
 */
public TableStatistics getTableStatistics(String tableFullName, Map<String, ColumnHandle> columnHandles) {
    String query = "SHOW STATS FOR " + tableFullName;
    Iterable<List<Object>> data;
    try {
        data = getResults(clientSession, query);
    } catch (SQLException ex) {
        throw new PrestoTransportException(REMOTE_TASK_ERROR, HostAddress.fromUri(this.serverUri.uri()), "could not connect to the remote data center");
    }
    TableStatistics.Builder builder = TableStatistics.builder();
    List<Object> lastRow = null;
    for (List<Object> row : data) {
        ColumnStatistics.Builder columnStatisticBuilder = new ColumnStatistics.Builder();
        lastRow = row;
        if (row.get(0) == null) {
            // Only the last row can have the first column (column name) null
            continue;
        }
        // row[0] is column_name
        DataCenterColumnHandle columnHandle = (DataCenterColumnHandle) columnHandles.get(row.get(0).toString());
        if (columnHandle == null) {
            // Unknown column found
            continue;
        }
        // row[1] is data_size
        if (row.get(1) != null) {
            columnStatisticBuilder.setDataSize(Estimate.of(Double.parseDouble(row.get(1).toString())));
        }
        // row[2] is distinct_values_count
        if (row.get(2) != null) {
            columnStatisticBuilder.setDistinctValuesCount(Estimate.of(Double.parseDouble(row.get(2).toString())));
        }
        // row[3] is nulls_fraction
        if (row.get(3) != null) {
            columnStatisticBuilder.setNullsFraction(Estimate.of(Double.parseDouble(row.get(3).toString())));
        }
        // row[5] is low_value and row[6] is high_value
        if (row.get(5) != null && row.get(6) != null) {
            String minStr = row.get(5).toString();
            String maxStr = row.get(6).toString();
            Type columnType = columnHandle.getColumnType();
            if (columnType.equals(DATE)) {
                LocalDate minDate = LocalDate.parse(minStr, DATE_FORMATTER);
                LocalDate maxDate = LocalDate.parse(maxStr, DATE_FORMATTER);
                columnStatisticBuilder.setRange(new DoubleRange(minDate.toEpochDay(), maxDate.toEpochDay()));
            } else {
                columnStatisticBuilder.setRange(new DoubleRange(Double.parseDouble(minStr), Double.parseDouble(maxStr)));
            }
        }
        builder.setColumnStatistics(columnHandle, columnStatisticBuilder.build());
    }
    // Get row_count from the last row
    if (lastRow != null && lastRow.get(4) != null) {
        builder.setRowCount(Estimate.of(Double.parseDouble(lastRow.get(4).toString())));
    }
    return builder.build();
}
Also used : ColumnStatistics(io.prestosql.spi.statistics.ColumnStatistics) SQLException(java.sql.SQLException) LocalDate(java.time.LocalDate) DoubleRange(io.prestosql.spi.statistics.DoubleRange) Type(io.prestosql.spi.type.Type) TypeUtil.parseType(io.prestosql.client.util.TypeUtil.parseType) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) TableStatistics(io.prestosql.spi.statistics.TableStatistics) PrestoTransportException(io.prestosql.spi.PrestoTransportException) DataCenterColumnHandle(io.hetu.core.plugin.datacenter.DataCenterColumnHandle)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 DataCenterColumnHandle (io.hetu.core.plugin.datacenter.DataCenterColumnHandle)1 TypeUtil.parseType (io.prestosql.client.util.TypeUtil.parseType)1 PrestoTransportException (io.prestosql.spi.PrestoTransportException)1 ColumnStatistics (io.prestosql.spi.statistics.ColumnStatistics)1 DoubleRange (io.prestosql.spi.statistics.DoubleRange)1 TableStatistics (io.prestosql.spi.statistics.TableStatistics)1 Type (io.prestosql.spi.type.Type)1 SQLException (java.sql.SQLException)1 LocalDate (java.time.LocalDate)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1