|
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.statistics.ColumnStatistics; |
| 34 | +import com.facebook.presto.spi.statistics.DoubleRange; |
| 35 | +import com.facebook.presto.spi.statistics.Estimate; |
| 36 | +import com.facebook.presto.spi.statistics.TableStatistics; |
| 37 | +import com.google.common.collect.Maps; |
28 | 38 | import jakarta.inject.Inject; |
29 | 39 |
|
30 | 40 | import java.sql.Connection; |
31 | 41 | import java.sql.DatabaseMetaData; |
| 42 | +import java.sql.Date; |
32 | 43 | import java.sql.PreparedStatement; |
33 | 44 | import java.sql.ResultSet; |
34 | 45 | import java.sql.SQLException; |
35 | 46 | import java.sql.Types; |
| 47 | +import java.util.HashMap; |
| 48 | +import java.util.List; |
| 49 | +import java.util.Map; |
36 | 50 | import java.util.Optional; |
37 | 51 |
|
38 | 52 | import static com.facebook.presto.common.type.DecimalType.createDecimalType; |
|
47 | 61 | import static com.facebook.presto.plugin.jdbc.mapping.StandardColumnMappings.varbinaryReadMapping; |
48 | 62 | import static com.facebook.presto.plugin.jdbc.mapping.StandardColumnMappings.varcharReadMapping; |
49 | 63 | import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED; |
| 64 | +import static java.lang.Double.NaN; |
50 | 65 | import static java.lang.String.format; |
51 | 66 | import static java.util.Locale.ENGLISH; |
52 | 67 | import static java.util.Objects.requireNonNull; |
53 | 68 |
|
54 | 69 | public class OracleClient |
55 | 70 | extends BaseJdbcClient |
56 | 71 | { |
| 72 | + private static final Logger LOG = Logger.get(OracleClient.class); |
57 | 73 | private static final int FETCH_SIZE = 1000; |
58 | 74 |
|
59 | 75 | private final boolean synonymsEnabled; |
@@ -93,6 +109,7 @@ protected ResultSet getTables(Connection connection, Optional<String> schemaName |
93 | 109 | escapeNamePattern(tableName, Optional.of(escape)).orElse(null), |
94 | 110 | getTableTypes()); |
95 | 111 | } |
| 112 | + |
96 | 113 | @Override |
97 | 114 | public PreparedStatement getPreparedStatement(ConnectorSession session, Connection connection, String sql) |
98 | 115 | throws SQLException |
@@ -137,6 +154,99 @@ protected void renameTable(JdbcIdentity identity, String catalogName, SchemaTabl |
137 | 154 | } |
138 | 155 | } |
139 | 156 |
|
| 157 | + @Override |
| 158 | + public TableStatistics getTableStatistics(ConnectorSession session, JdbcTableHandle handle, List<JdbcColumnHandle> columnHandles, TupleDomain<ColumnHandle> tupleDomain) |
| 159 | + { |
| 160 | + try { |
| 161 | + requireNonNull(handle.getSchemaName(), "schema name is null"); |
| 162 | + requireNonNull(handle.getTableName(), "table name is null"); |
| 163 | + String sql = format( |
| 164 | + "SELECT NUM_ROWS, AVG_ROW_LEN, LAST_ANALYZED\n" + |
| 165 | + "FROM DBA_TAB_STATISTICS\n" + |
| 166 | + "WHERE OWNER='%s'\n" + |
| 167 | + "AND TABLE_NAME='%s'", |
| 168 | + handle.getSchemaName().toUpperCase(), handle.getTableName().toUpperCase()); |
| 169 | + try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session)); |
| 170 | + PreparedStatement preparedStatement = getPreparedStatement(session, connection, sql); |
| 171 | + PreparedStatement preparedStatementCol = getPreparedStatement(session, connection, getColumnStaticsSql(handle)); |
| 172 | + ResultSet resultSet = preparedStatement.executeQuery(); |
| 173 | + ResultSet resultSetColumnStats = preparedStatementCol.executeQuery()) { |
| 174 | + if (!resultSet.next()) { |
| 175 | + LOG.debug("Stats not found for table : %s.%s", handle.getSchemaName(), handle.getTableName()); |
| 176 | + return TableStatistics.empty(); |
| 177 | + } |
| 178 | + double numRows = resultSet.getDouble("NUM_ROWS"); |
| 179 | + // double avgRowLen = resultSet.getDouble("AVG_ROW_LEN"); |
| 180 | + Date lastAnalyzed = resultSet.getDate("LAST_ANALYZED"); |
| 181 | + |
| 182 | + Map<ColumnHandle, ColumnStatistics> columnStatisticsMap = new HashMap<>(); |
| 183 | + Map<String, JdbcColumnHandle> columnHandleMap = Maps.uniqueIndex(columnHandles, JdbcColumnHandle::getColumnName); |
| 184 | + while (resultSetColumnStats.next() && numRows > 0) { |
| 185 | + String columnName = resultSetColumnStats.getString("COLUMN_NAME"); |
| 186 | + double nullsCount = resultSetColumnStats.getDouble("NUM_NULLS"); |
| 187 | + double ndv = resultSetColumnStats.getDouble("NUM_DISTINCT"); |
| 188 | + // 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 |
| 189 | + // So we use column type as a clue and parse to double by converting as string first. |
| 190 | + double lowValue = toDouble(resultSetColumnStats.getString("LOW_VALUE")); |
| 191 | + double highValue = toDouble(resultSetColumnStats.getString("HIGH_VALUE")); |
| 192 | + ColumnStatistics.Builder columnStatisticsBuilder = ColumnStatistics.builder() |
| 193 | + .setDataSize(Estimate.estimateFromDouble(resultSet.getDouble("DATA_LENGTH"))) |
| 194 | + .setNullsFraction(Estimate.estimateFromDouble(nullsCount / numRows)) |
| 195 | + .setDistinctValuesCount(Estimate.estimateFromDouble(ndv)); |
| 196 | + ColumnStatistics columnStatistics = columnStatisticsBuilder.build(); |
| 197 | + if (Double.isFinite(lowValue) && Double.isFinite(highValue)) { |
| 198 | + columnStatistics = columnStatisticsBuilder.setRange(new DoubleRange(lowValue, highValue)).build(); |
| 199 | + } |
| 200 | + columnStatisticsMap.put(columnHandleMap.get(columnName), columnStatistics); |
| 201 | + } |
| 202 | + LOG.info("getTableStatics for table: %s.%s.%s with last analyzed: %s", |
| 203 | + handle.getCatalogName(), handle.getSchemaName(), handle.getTableName(), lastAnalyzed); |
| 204 | + return TableStatistics.builder() |
| 205 | + .setColumnStatistics(columnStatisticsMap) |
| 206 | + .setRowCount(Estimate.estimateFromDouble(numRows)).build(); |
| 207 | + } |
| 208 | + } |
| 209 | + catch (SQLException | RuntimeException e) { |
| 210 | + throw new PrestoException(JDBC_ERROR, "Failed fetching statistics for table: " + handle, e); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + private String getColumnStaticsSql(JdbcTableHandle handle) |
| 215 | + { |
| 216 | + // UTL_RAW.CAST_TO_BINARY_X does not render correctly so those types are not supported. |
| 217 | + return format( |
| 218 | + "SELECT COLUMN_NAME,\n" + |
| 219 | + "DATA_TYPE,\n" + |
| 220 | + "DATA_LENGTH,\n" + |
| 221 | + "NUM_NULLS,\n" + |
| 222 | + "NUM_DISTINCT,\n" + |
| 223 | + "DENSITY,\n" + |
| 224 | + "CASE DATA_TYPE\n" + |
| 225 | + " WHEN 'NUMBER' THEN TO_CHAR(UTL_RAW.CAST_TO_NUMBER(LOW_VALUE))\n" + |
| 226 | + " ELSE NULL\n" + |
| 227 | + "END AS LOW_VALUE,\n" + |
| 228 | + "CASE DATA_TYPE\n" + |
| 229 | + " WHEN 'NUMBER' THEN TO_CHAR(UTL_RAW.CAST_TO_NUMBER(HIGH_VALUE))\n" + |
| 230 | + " ELSE NULL\n" + |
| 231 | + "END AS HIGH_VALUE\n" + |
| 232 | + "FROM ALL_TAB_COLUMNS\n" + |
| 233 | + "WHERE OWNER = '%s'\n" + |
| 234 | + " AND TABLE_NAME = '%s'", handle.getSchemaName().toUpperCase(), handle.getTableName().toUpperCase()); |
| 235 | + } |
| 236 | + |
| 237 | + private double toDouble(String number) |
| 238 | + { |
| 239 | + try { |
| 240 | + return Double.parseDouble(number); |
| 241 | + } |
| 242 | + catch (Exception e) { |
| 243 | + // a string represented by number, may not even be a parseable number this is expected. e.g. if column type is |
| 244 | + // varchar. |
| 245 | + LOG.debug(e, "error while decoding : %s", number); |
| 246 | + } |
| 247 | + return NaN; |
| 248 | + } |
| 249 | + |
140 | 250 | @Override |
141 | 251 | public Optional<ReadMapping> toPrestoType(ConnectorSession session, JdbcTypeHandle typeHandle) |
142 | 252 | { |
|
0 commit comments