| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.index; |
| 7 |
|
| 8 |
import org.h2.command.query.AllColumnsForPlan; |
| 9 |
import org.h2.engine.SessionLocal; |
| 10 |
import org.h2.message.DbException; |
| 11 |
import org.h2.result.SearchRow; |
| 12 |
import org.h2.result.SortOrder; |
| 13 |
import org.h2.table.FunctionTable; |
| 14 |
import org.h2.table.IndexColumn; |
| 15 |
import org.h2.table.TableFilter; |
| 16 |
import org.h2.table.VirtualConstructedTable; |
| 17 |
|
| 18 |
/** |
| 19 |
* An index for a virtual table that returns a result set. Search in this index |
| 20 |
* performs scan over all rows and should be avoided. |
| 21 |
*/ |
| 22 |
public class VirtualConstructedTableIndex extends VirtualTableIndex { |
| 23 |
|
| 24 |
private final VirtualConstructedTable table; |
| 25 |
|
| 26 |
public VirtualConstructedTableIndex(VirtualConstructedTable table, IndexColumn[] columns) { |
| 27 |
super(table, null, columns); |
| 28 |
this.table = table; |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public boolean isFindUsingFullTableScan() { |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public Cursor find(SessionLocal session, SearchRow first, SearchRow last) { |
| 38 |
return new VirtualTableCursor(this, first, last, table.getResult(session)); |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
public double getCost(SessionLocal session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, |
| 43 |
AllColumnsForPlan allColumnsSet) { |
| 44 |
if (masks != null) { |
| 45 |
throw DbException.getUnsupportedException("Virtual table"); |
| 46 |
} |
| 47 |
long expectedRows; |
| 48 |
if (table.canGetRowCount(session)) { |
| 49 |
expectedRows = table.getRowCountApproximation(session); |
| 50 |
} else { |
| 51 |
expectedRows = database.getSettings().estimatedFunctionTableRows; |
| 52 |
} |
| 53 |
return expectedRows * 10; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public String getPlanSQL() { |
| 58 |
return table instanceof FunctionTable ? "function" : "table scan"; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public boolean canScan() { |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
} |