|
13 | 13 | */ |
14 | 14 | package com.facebook.presto.plugin.oracle; |
15 | 15 |
|
| 16 | +import com.facebook.airlift.log.Logger; |
| 17 | +import com.facebook.presto.common.predicate.TupleDomain; |
16 | 18 | import com.facebook.presto.common.type.Decimals; |
17 | 19 | import com.facebook.presto.common.type.VarcharType; |
18 | 20 | import com.facebook.presto.plugin.jdbc.BaseJdbcClient; |
19 | 21 | import com.facebook.presto.plugin.jdbc.BaseJdbcConfig; |
20 | 22 | import com.facebook.presto.plugin.jdbc.ConnectionFactory; |
| 23 | +import com.facebook.presto.plugin.jdbc.JdbcColumnHandle; |
21 | 24 | import com.facebook.presto.plugin.jdbc.JdbcConnectorId; |
22 | 25 | import com.facebook.presto.plugin.jdbc.JdbcIdentity; |
| 26 | +import com.facebook.presto.plugin.jdbc.JdbcTableHandle; |
23 | 27 | import com.facebook.presto.plugin.jdbc.JdbcTypeHandle; |
24 | 28 | import com.facebook.presto.plugin.jdbc.mapping.ReadMapping; |
| 29 | +import com.facebook.presto.spi.ColumnHandle; |
25 | 30 | import com.facebook.presto.spi.ConnectorSession; |
26 | 31 | import com.facebook.presto.spi.PrestoException; |
27 | 32 | import com.facebook.presto.spi.SchemaTableName; |
| 33 | +import com.facebook.presto.spi.function.table.Preconditions; |
| 34 | +import com.facebook.presto.spi.statistics.ColumnStatistics; |
| 35 | +import com.facebook.presto.spi.statistics.DoubleRange; |
| 36 | +import com.facebook.presto.spi.statistics.Estimate; |
| 37 | +import com.facebook.presto.spi.statistics.TableStatistics; |
| 38 | +import com.google.common.collect.Maps; |
28 | 39 | import jakarta.inject.Inject; |
29 | 40 |
|
30 | 41 | import java.sql.Connection; |
31 | 42 | import java.sql.DatabaseMetaData; |
| 43 | +import java.sql.Date; |
32 | 44 | import java.sql.PreparedStatement; |
33 | 45 | import java.sql.ResultSet; |
34 | 46 | import java.sql.SQLException; |
35 | 47 | import java.sql.Types; |
| 48 | +import java.util.HashMap; |
| 49 | +import java.util.List; |
| 50 | +import java.util.Map; |
36 | 51 | import java.util.Optional; |
37 | 52 |
|
38 | 53 | import static com.facebook.presto.common.type.DecimalType.createDecimalType; |
|
46 | 61 | import static com.facebook.presto.plugin.jdbc.mapping.StandardColumnMappings.smallintReadMapping; |
47 | 62 | import static com.facebook.presto.plugin.jdbc.mapping.StandardColumnMappings.varcharReadMapping; |
48 | 63 | import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED; |
| 64 | +import static com.google.common.base.Verify.verify; |
| 65 | +import static java.lang.Double.NaN; |
49 | 66 | import static java.lang.String.format; |
50 | 67 | import static java.util.Locale.ENGLISH; |
51 | 68 | import static java.util.Objects.requireNonNull; |
52 | 69 |
|
53 | 70 | public class OracleClient |
54 | 71 | extends BaseJdbcClient |
55 | 72 | { |
| 73 | + private static final Logger LOG = Logger.get(OracleClient.class); |
56 | 74 | private static final int FETCH_SIZE = 1000; |
57 | 75 |
|
58 | 76 | private final boolean synonymsEnabled; |
@@ -92,6 +110,7 @@ protected ResultSet getTables(Connection connection, Optional<String> schemaName |
92 | 110 | escapeNamePattern(tableName, Optional.of(escape)).orElse(null), |
93 | 111 | getTableTypes()); |
94 | 112 | } |
| 113 | + |
95 | 114 | @Override |
96 | 115 | public PreparedStatement getPreparedStatement(ConnectorSession session, Connection connection, String sql) |
97 | 116 | throws SQLException |
@@ -129,6 +148,101 @@ protected void renameTable(JdbcIdentity identity, String catalogName, SchemaTabl |
129 | 148 | } |
130 | 149 | } |
131 | 150 |
|
| 151 | + @Override |
| 152 | + public TableStatistics getTableStatistics(ConnectorSession session, JdbcTableHandle handle, List<JdbcColumnHandle> columnHandles, TupleDomain<ColumnHandle> tupleDomain) |
| 153 | + { |
| 154 | + try { |
| 155 | + Preconditions.checkNotNullOrEmpty(handle.getSchemaName(), "schema name"); |
| 156 | + Preconditions.checkNotNullOrEmpty(handle.getTableName(), "table name"); |
| 157 | + String sql = format( |
| 158 | + "SELECT NUM_ROWS, AVG_ROW_LEN, LAST_ANALYZED\n" + |
| 159 | + "FROM DBA_TAB_STATISTICS\n" + |
| 160 | + "WHERE OWNER='%s'\n" + |
| 161 | + "AND TABLE_NAME='%s'", |
| 162 | + handle.getSchemaName().toUpperCase(), handle.getTableName().toUpperCase()); |
| 163 | + try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session))) { |
| 164 | + PreparedStatement preparedStatement = getPreparedStatement(session, connection, sql); |
| 165 | + ResultSet resultSet = preparedStatement.executeQuery(); |
| 166 | + verify(resultSet.next(), |
| 167 | + format("Stats not found for table : %s.%s", handle.getSchemaName(), handle.getTableName())); |
| 168 | + double numRows = resultSet.getDouble("NUM_ROWS"); |
| 169 | + double avgRowLen = resultSet.getDouble("AVG_ROW_LEN"); |
| 170 | + Date lastAnalyzed = resultSet.getDate("LAST_ANALYZED"); |
| 171 | + String sqlCol = getColumnStaticsSql(handle, columnHandles); |
| 172 | + PreparedStatement preparedStatementCol = getPreparedStatement(session, connection, sqlCol); |
| 173 | + resultSet = preparedStatementCol.executeQuery(); |
| 174 | + Map<ColumnHandle, ColumnStatistics> columnStatisticsMap = new HashMap<>(); |
| 175 | + Map<String, JdbcColumnHandle> columnHandleMap = Maps.uniqueIndex(columnHandles, JdbcColumnHandle::getColumnName); |
| 176 | + while (resultSet.next()) { |
| 177 | + String columnName = resultSet.getString("COLUMN_NAME"); |
| 178 | + double nullsCount = resultSet.getDouble("NUM_NULLS"); |
| 179 | + double ndv = resultSet.getDouble("NUM_DISTINCT"); |
| 180 | + // Oracle stores low and high values as RAW(1000) i.e. a byte array. No way to unwrap it, without a clue about the underlying type |
| 181 | + // So we use column type as a clue and parse double by converting to string. |
| 182 | + double lowValue = toDouble(resultSet.getString("LOW_VALUE")); |
| 183 | + double highValue = toDouble(resultSet.getString("HIGH_VALUE")); |
| 184 | + ColumnStatistics.Builder columnStatisticsBuilder = ColumnStatistics.builder() |
| 185 | + .setDataSize(Estimate.estimateFromDouble(resultSet.getDouble("DATA_LENGTH"))) |
| 186 | + .setNullsFraction(Estimate.estimateFromDouble(nullsCount / numRows)) |
| 187 | + .setDistinctValuesCount(Estimate.estimateFromDouble(ndv)); |
| 188 | + ColumnStatistics columnStatistics = columnStatisticsBuilder.build(); |
| 189 | + if (Double.isFinite(lowValue) && Double.isFinite(highValue)) { |
| 190 | + columnStatistics = columnStatisticsBuilder.setRange(new DoubleRange(lowValue, highValue)).build(); |
| 191 | + } |
| 192 | + columnStatisticsMap.put(columnHandleMap.get(columnName), columnStatistics); |
| 193 | + } |
| 194 | + LOG.info("getTableStatics for table: %s.%s.%s with last analyzed: %s", |
| 195 | + handle.getCatalogName(), handle.getSchemaName(), handle.getTableName(), lastAnalyzed); |
| 196 | + return TableStatistics.builder() |
| 197 | + .setColumnStatistics(columnStatisticsMap) |
| 198 | + .setRowCount(Estimate.estimateFromDouble(numRows)) |
| 199 | + .setTotalSize(Estimate.estimateFromDouble(avgRowLen * numRows)).build(); |
| 200 | + } |
| 201 | + } |
| 202 | + catch (SQLException | RuntimeException e) { |
| 203 | + throw new PrestoException(JDBC_ERROR, "Failed fetching statistics for table: " + handle, e); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private static String getColumnStaticsSql(JdbcTableHandle handle, List<JdbcColumnHandle> columnHandles) |
| 208 | + { |
| 209 | + String columnStatsSqlTemplate = ( |
| 210 | + "(SELECT COLUMN_NAME,\n" + |
| 211 | + " DATA_TYPE,\n" + |
| 212 | + " DATA_LENGTH,\n" + |
| 213 | + " NUM_NULLS,\n" + |
| 214 | + " NUM_DISTINCT,\n" + |
| 215 | + " to_char(UTL_RAW.CAST_TO_%s(LOW_VALUE)) LOW_VALUE,\n" + |
| 216 | + " to_char(UTL_RAW.CAST_TO_%s(HIGH_VALUE)) HIGH_VALUE\n" + |
| 217 | + "FROM ALL_TAB_COLUMNS\n" + |
| 218 | + "WHERE OWNER = '%s'\n" + |
| 219 | + " AND TABLE_NAME = '%s' AND COLUMN_NAME = '%s') "); |
| 220 | + StringBuffer sqlQueryBuffer = new StringBuffer(); |
| 221 | + for (JdbcColumnHandle columnHandle : columnHandles) { |
| 222 | + if (sqlQueryBuffer.length() != 0) { |
| 223 | + sqlQueryBuffer.append(" UNION "); |
| 224 | + } |
| 225 | + String targetOracleType = columnHandle.getJdbcTypeHandle().getJdbcTypeName(); |
| 226 | + String columnStatsSql = format(columnStatsSqlTemplate, targetOracleType, targetOracleType, handle.getSchemaName(), |
| 227 | + handle.getTableName(), columnHandle.getColumnName().toUpperCase()); |
| 228 | + sqlQueryBuffer.append(columnStatsSql); |
| 229 | + } |
| 230 | + return sqlQueryBuffer.toString(); |
| 231 | + } |
| 232 | + |
| 233 | + private double toDouble(String number) |
| 234 | + { |
| 235 | + try { |
| 236 | + return Double.parseDouble(number); |
| 237 | + } |
| 238 | + catch (Exception e) { |
| 239 | + // a string represented by number, may not even be a parseable number this is expected. e.g. if column type is |
| 240 | + // varchar. |
| 241 | + LOG.debug(e, "error while decoding : %s", number); |
| 242 | + } |
| 243 | + return NaN; |
| 244 | + } |
| 245 | + |
132 | 246 | @Override |
133 | 247 | public Optional<ReadMapping> toPrestoType(ConnectorSession session, JdbcTypeHandle typeHandle) |
134 | 248 | { |
|
0 commit comments