Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Remove dead DBKVS code, fix Oracle hints (#7338)
Browse files Browse the repository at this point in the history
Oracle hints/query improved; dead DBKVS code removed
  • Loading branch information
CVdV-au authored Oct 28, 2024
1 parent 81ec4c6 commit 0dfdd21
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,18 @@ public interface DbQueryFactory {

FullQuery getLatestRowsQuery(Iterable<byte[]> rows, long ts, ColumnSelection columns, boolean includeValue);

FullQuery getLatestRowsQuery(
Collection<Map.Entry<byte[], Long>> rows, ColumnSelection columns, boolean includeValue);

FullQuery getAllRowQuery(byte[] row, long ts, ColumnSelection columns, boolean includeValue);

FullQuery getAllRowsQuery(Iterable<byte[]> rows, long ts, ColumnSelection columns, boolean includeValue);

FullQuery getAllRowsQuery(Collection<Map.Entry<byte[], Long>> rows, ColumnSelection columns, boolean includeValue);

FullQuery getLatestCellQuery(Cell cell, long ts, boolean includeValue);

FullQuery getLatestCellsQuery(Iterable<Cell> cells, long ts, boolean includeValue);

FullQuery getLatestCellsQuery(Collection<Map.Entry<Cell, Long>> cells, boolean includeValue);

FullQuery getAllCellQuery(Cell cell, long ts, boolean includeValue);

FullQuery getAllCellsQuery(Iterable<Cell> cells, long ts, boolean includeValue);

FullQuery getAllCellsQuery(Collection<Map.Entry<Cell, Long>> cells, boolean includeValue);

FullQuery getRangeQuery(RangeRequest range, long ts, int maxRows);

boolean hasOverflowValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public OracleQueryFactory(OracleDdlConfig config, String tableName, boolean hasO

@Override
public FullQuery getLatestRowQuery(byte[] row, long ts, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_LATEST_ONE_ROW_INNER (" + tableName + ") */ "
+ " SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) CARDINALITY(t 1) CARDINALITY(m 10) INDEX(m "
+ PrimaryKeyConstraintNames.get(tableName) + ") */ "
String query = "SELECT"
+ " m.row_name, m.col_name, max(m.ts) as ts "
+ " FROM " + tableName + " m "
+ " WHERE m.row_name = ? "
Expand All @@ -71,10 +68,8 @@ public FullQuery getLatestRowQuery(byte[] row, long ts, ColumnSelection columns,

@Override
public FullQuery getLatestRowsQuery(Iterable<byte[]> rows, long ts, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_LATEST_ROWS_SINGLE_BOUND_INNER (" + tableName + ") */ "
+ " SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) CARDINALITY(t 1) CARDINALITY(m 10) INDEX(m "
+ PrimaryKeyConstraintNames.get(tableName) + ") */ "
String query = "SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) */ "
+ " m.row_name, m.col_name, max(m.ts) as ts "
+ " FROM " + tableName + " m, TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE)) t "
+ " WHERE m.row_name = t.row_name "
Expand All @@ -95,39 +90,10 @@ public FullQuery getLatestRowsQuery(Iterable<byte[]> rows, long ts, ColumnSelect
: fullQuery.withArg(rowsToOracleArray(columns.getSelectedColumns()));
}

@Override
public FullQuery getLatestRowsQuery(
Collection<Map.Entry<byte[], Long>> rows, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_LATEST_ROWS_MANY_BOUNDS_INNER (" + tableName + ") */ "
+ " SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) CARDINALITY(t 1) CARDINALITY(m 10) INDEX(m "
+ PrimaryKeyConstraintNames.get(tableName) + ") */ "
+ " m.row_name, m.col_name, max(m.ts) as ts "
+ " FROM " + tableName + " m, TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE)) t "
+ " WHERE m.row_name = t.row_name "
+ " AND m.ts < t.max_ts "
+ (columns.allColumnsSelected()
? ""
: " AND EXISTS ("
+ "SELECT"
+ " /*+ NL_SJ */"
+ " 1"
+ " FROM TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE))"
+ " WHERE row_name = m.col_name) ")
+ " GROUP BY m.row_name, m.col_name";
query = wrapQueryWithIncludeValue("GET_LATEST_ROWS_MANY_BOUNDS", query, includeValue);
FullQuery fullQuery = new FullQuery(query).withArg(rowsAndTimestampsToOracleArray(rows));
return columns.allColumnsSelected()
? fullQuery
: fullQuery.withArg(rowsToOracleArray(columns.getSelectedColumns()));
}

@Override
public FullQuery getAllRowQuery(byte[] row, long ts, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_ALL_ONE_ROW (" + tableName + ") */ "
+ " SELECT"
+ " /*+ INDEX(m " + PrimaryKeyConstraintNames.get(tableName) + ") */ "
+ " m.row_name, m.col_name, m.ts" + getValueSubselect("m", includeValue)
+ " SELECT m.row_name, m.col_name, m.ts" + getValueSubselect("m", includeValue)
+ " FROM " + tableName + " m "
+ " WHERE m.row_name = ? "
+ " AND m.ts < ? "
Expand All @@ -149,8 +115,7 @@ public FullQuery getAllRowQuery(byte[] row, long ts, ColumnSelection columns, bo
public FullQuery getAllRowsQuery(Iterable<byte[]> rows, long ts, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_ALL_ROWS_SINGLE_BOUND (" + tableName + ") */ "
+ " SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) CARDINALITY(t 1) CARDINALITY(m 10) INDEX(m "
+ PrimaryKeyConstraintNames.get(tableName) + ") */ "
+ " /*+ USE_NL(t m) LEADING(t m) */"
+ " m.row_name, m.col_name, m.ts" + getValueSubselect("m", includeValue)
+ " FROM " + tableName + " m, TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE)) t "
+ " WHERE m.row_name = t.row_name "
Expand All @@ -169,37 +134,9 @@ public FullQuery getAllRowsQuery(Iterable<byte[]> rows, long ts, ColumnSelection
: fullQuery.withArg(rowsToOracleArray(columns.getSelectedColumns()));
}

@Override
public FullQuery getAllRowsQuery(
Collection<Map.Entry<byte[], Long>> rows, ColumnSelection columns, boolean includeValue) {
String query = " /* GET_ALL_ROWS_MANY_BOUNDS (" + tableName + ") */ "
+ " SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) CARDINALITY(t 1) CARDINALITY(m 10) INDEX(m "
+ PrimaryKeyConstraintNames.get(tableName) + ") */ "
+ " m.row_name, m.col_name, m.ts" + getValueSubselect("m", includeValue)
+ " FROM " + tableName + " m, TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE)) t "
+ " WHERE m.row_name = t.row_name "
+ " AND m.ts < t.max_ts "
+ (columns.allColumnsSelected()
? ""
: " AND EXISTS ("
+ "SELECT"
+ " /*+ NL_SJ */"
+ " 1"
+ " FROM TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE))"
+ " WHERE row_name = m.col_name) ");
FullQuery fullQuery = new FullQuery(query).withArg(rowsAndTimestampsToOracleArray(rows));
return columns.allColumnsSelected()
? fullQuery
: fullQuery.withArg(rowsToOracleArray(columns.getSelectedColumns()));
}

@Override
public FullQuery getLatestCellQuery(Cell cell, long ts, boolean includeValue) {
String query = " /* GET_LATEST_ONE_CELLS_INNER (" + tableName + ") */ "
+ " SELECT "
+ " /*+ INDEX(m " + PrimaryKeyConstraintNames.get(tableName) + ") */ "
+ " m.row_name, m.col_name, max(m.ts) as ts "
String query = "SELECT m.row_name, m.col_name, max(m.ts) as ts"
+ " FROM " + tableName + " m "
+ " WHERE m.row_name = ? "
+ " AND m.col_name = ? "
Expand All @@ -209,28 +146,10 @@ public FullQuery getLatestCellQuery(Cell cell, long ts, boolean includeValue) {
return new FullQuery(query).withArgs(cell.getRowName(), cell.getColumnName(), ts);
}

@Override
public FullQuery getLatestCellsQuery(Iterable<Cell> cells, long ts, boolean includeValue) {
String query = " /* GET_LATEST_CELLS_SINGLE_BOUND_INNER (" + tableName + ") */ "
+ " SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) CARDINALITY(t 1) CARDINALITY(m 10) INDEX(m "
+ PrimaryKeyConstraintNames.get(tableName) + ") */ "
+ " m.row_name, m.col_name, max(m.ts) as ts "
+ " FROM " + tableName + " m, TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE)) t "
+ " WHERE m.row_name = t.row_name "
+ " AND m.col_name = t.col_name "
+ " AND m.ts < ? "
+ " GROUP BY m.row_name, m.col_name";
query = wrapQueryWithIncludeValue("GET_LATEST_CELLS_SINGLE_BOUND", query, includeValue);
return new FullQuery(query).withArgs(cellsToOracleArray(cells), ts);
}

@Override
public FullQuery getLatestCellsQuery(Collection<Map.Entry<Cell, Long>> cells, boolean includeValue) {
String query = " /* GET_LATEST_CELLS_MANY_BOUNDS_INNER (" + tableName + ") */ "
+ " SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) CARDINALITY(t 1) CARDINALITY(m 10) INDEX(m "
+ PrimaryKeyConstraintNames.get(tableName) + ") */ "
String query = "SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) */ "
+ " m.row_name, m.col_name, max(m.ts) as ts "
+ " FROM " + tableName + " m, TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE)) t "
+ " WHERE m.row_name = t.row_name "
Expand All @@ -245,7 +164,6 @@ public FullQuery getLatestCellsQuery(Collection<Map.Entry<Cell, Long>> cells, bo
public FullQuery getAllCellQuery(Cell cell, long ts, boolean includeValue) {
String query = " /* GET_ALL_ONE_CELL (" + tableName + ") */ "
+ " SELECT"
+ " /*+ INDEX(m " + PrimaryKeyConstraintNames.get(tableName) + ") */ "
+ " m.row_name, m.col_name, m.ts" + getValueSubselect("m", includeValue)
+ " FROM " + tableName + " m "
+ " WHERE m.row_name = ? "
Expand All @@ -258,8 +176,7 @@ public FullQuery getAllCellQuery(Cell cell, long ts, boolean includeValue) {
public FullQuery getAllCellsQuery(Iterable<Cell> cells, long ts, boolean includeValue) {
String query = " /* GET_ALL_CELLS_SINGLE_BOUND (" + tableName + ") */ "
+ " SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) CARDINALITY(t 1) CARDINALITY(m 10) INDEX(m "
+ PrimaryKeyConstraintNames.get(tableName) + ") */ "
+ " /*+ USE_NL(t m) LEADING(t m) */ "
+ " m.row_name, m.col_name, m.ts" + getValueSubselect("m", includeValue)
+ " FROM " + tableName + " m, TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE)) t "
+ " WHERE m.row_name = t.row_name "
Expand All @@ -268,20 +185,6 @@ public FullQuery getAllCellsQuery(Iterable<Cell> cells, long ts, boolean include
return new FullQuery(query).withArgs(cellsToOracleArray(cells), ts);
}

@Override
public FullQuery getAllCellsQuery(Collection<Map.Entry<Cell, Long>> cells, boolean includeValue) {
String query = " /* GET_ALL_CELLS_MANY_BOUNDS (" + tableName + ") */ "
+ " SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) CARDINALITY(t 1) CARDINALITY(m 10) INDEX(m "
+ PrimaryKeyConstraintNames.get(tableName) + ") */ "
+ " m.row_name, m.col_name, m.ts" + getValueSubselect("m", includeValue)
+ " FROM " + tableName + " m, TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE)) t "
+ " WHERE m.row_name = t.row_name "
+ " AND m.col_name = t.col_name "
+ " AND m.ts < t.max_ts ";
return new FullQuery(query).withArg(cellsAndTimestampsToOracleArray(cells));
}

@Override
public FullQuery getRangeQuery(RangeRequest range, long ts, int maxRows) {
List<String> bounds = new ArrayList<>(2);
Expand All @@ -298,7 +201,7 @@ public FullQuery getRangeQuery(RangeRequest range, long ts, int maxRows) {
}
if (maxRows == 1) {
String query = " /* GET_RANGE_ONE_ROW (" + tableName + ") */ "
+ " SELECT /*+ INDEX(m " + PrimaryKeyConstraintNames.get(tableName) + ") */ "
+ " SELECT "
+ (range.isReverse() ? "max" : "min") + "(m.row_name) as row_name "
+ " FROM " + tableName + " m "
+ (bounds.isEmpty() ? "" : " WHERE " + Joiner.on(" AND ").join(bounds));
Expand All @@ -307,7 +210,7 @@ public FullQuery getRangeQuery(RangeRequest range, long ts, int maxRows) {

String query = " /* GET_RANGE_ROWS (" + tableName + ") */ "
+ " SELECT inner.row_name FROM "
+ " ( SELECT /*+ INDEX(m " + PrimaryKeyConstraintNames.get(tableName) + ") */ "
+ " ( SELECT"
+ " DISTINCT m.row_name "
+ " FROM " + tableName + " m "
+ (bounds.isEmpty() ? "" : " WHERE " + Joiner.on(" AND ").join(bounds))
Expand Down Expand Up @@ -373,15 +276,16 @@ protected FullQuery getRowsColumnRangeSubQuery(
@Override
protected FullQuery getRowsColumnRangeFullyLoadedRowsSubQuery(
List<byte[]> rows, long ts, ColumnRangeSelection columnRangeSelection) {
String query = " /* GET_ROWS_COLUMN_RANGE_FULLY_LOADED_ROWS (" + tableName + ") */ "
+ "SELECT * FROM ( SELECT m.row_name, m.col_name, max(m.ts) as ts"
+ " FROM " + tableName + " m, TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE)) t "
+ " WHERE m.row_name = t.row_name "
+ " AND m.ts < ? "
String query = "SELECT"
+ " /*+ USE_NL(t m) LEADING(t m) */"
+ " m.row_name, m.col_name, max(m.ts) as ts"
+ " FROM " + tableName + " m, TABLE(CAST(? AS " + structArrayPrefix() + "CELL_TS_TABLE)) t "
+ " WHERE m.row_name = t.row_name "
+ " AND m.ts < ? "
+ (columnRangeSelection.getStartCol().length > 0 ? " AND m.col_name >= ?" : "")
+ (columnRangeSelection.getEndCol().length > 0 ? " AND m.col_name < ?" : "")
+ " GROUP BY m.row_name, m.col_name"
+ " ORDER BY m.row_name ASC, m.col_name ASC )";
+ " ORDER BY m.row_name ASC, m.col_name ASC";
String wrappedQuery = wrapQueryWithIncludeValue("GET_ROWS_COLUMN_RANGE_FULLY_LOADED_ROWS", query, true);
FullQuery fullQuery = new FullQuery(wrappedQuery).withArgs(rowsToOracleArray(rows), ts);
if (columnRangeSelection.getStartCol().length > 0) {
Expand Down Expand Up @@ -436,16 +340,6 @@ private ArrayHandler cellsToOracleArray(Iterable<Cell> cells) {
structArrayPrefix() + "CELL_TS", "" + structArrayPrefix() + "CELL_TS_TABLE", oraRows);
}

private ArrayHandler rowsAndTimestampsToOracleArray(Collection<Map.Entry<byte[], Long>> rows) {
List<Object[]> oraRows = new ArrayList<>(rows.size());
for (Map.Entry<byte[], Long> entry : rows) {
oraRows.add(new Object[] {entry.getKey(), null, entry.getValue()});
}
return config.jdbcHandler()
.createStructArray(
structArrayPrefix() + "CELL_TS", "" + structArrayPrefix() + "CELL_TS_TABLE", oraRows);
}

private ArrayHandler cellsAndTimestampsToOracleArray(Collection<Map.Entry<Cell, Long>> cells) {
List<Object[]> oraRows = new ArrayList<>(cells.size());
for (Map.Entry<Cell, Long> entry : cells) {
Expand Down
Loading

0 comments on commit 0dfdd21

Please sign in to comment.