| 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.command.query; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.HashMap; |
| 10 |
import org.h2.expression.ExpressionVisitor; |
| 11 |
import org.h2.table.Column; |
| 12 |
import org.h2.table.Table; |
| 13 |
import org.h2.table.TableFilter; |
| 14 |
|
| 15 |
/** |
| 16 |
* This information is expensive to compute for large queries, so do so |
| 17 |
* on-demand. Also store the information pre-mapped by table to avoid expensive |
| 18 |
* traversal. |
| 19 |
*/ |
| 20 |
public class AllColumnsForPlan { |
| 21 |
|
| 22 |
private final TableFilter[] filters; |
| 23 |
private HashMap<Table, ArrayList<Column>> map; |
| 24 |
|
| 25 |
public AllColumnsForPlan(TableFilter[] filters) { |
| 26 |
this.filters = filters; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Called by ExpressionVisitor. |
| 31 |
* |
| 32 |
* @param newCol new column to be added. |
| 33 |
*/ |
| 34 |
public void add(Column newCol) { |
| 35 |
ArrayList<Column> cols = map.get(newCol.getTable()); |
| 36 |
if (cols == null) { |
| 37 |
cols = new ArrayList<>(); |
| 38 |
map.put(newCol.getTable(), cols); |
| 39 |
} |
| 40 |
if (!cols.contains(newCol)) |
| 41 |
cols.add(newCol); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Used by index to calculate the cost of a scan. |
| 46 |
* |
| 47 |
* @param table the table. |
| 48 |
* @return all table's referenced columns. |
| 49 |
*/ |
| 50 |
public ArrayList<Column> get(Table table) { |
| 51 |
if (map == null) { |
| 52 |
map = new HashMap<>(); |
| 53 |
ExpressionVisitor.allColumnsForTableFilters(filters, this); |
| 54 |
} |
| 55 |
return map.get(table); |
| 56 |
} |
| 57 |
|
| 58 |
} |