| 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.table; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import org.h2.api.ErrorCode; |
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.expression.Expression; |
| 12 |
import org.h2.index.Index; |
| 13 |
import org.h2.index.RangeIndex; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.schema.Schema; |
| 16 |
import org.h2.value.TypeInfo; |
| 17 |
|
| 18 |
/** |
| 19 |
* The table SYSTEM_RANGE is a virtual table that generates incrementing numbers |
| 20 |
* with a given start end point. |
| 21 |
*/ |
| 22 |
public class RangeTable extends VirtualTable { |
| 23 |
|
| 24 |
/** |
| 25 |
* The name of the range table. |
| 26 |
*/ |
| 27 |
public static final String NAME = "SYSTEM_RANGE"; |
| 28 |
|
| 29 |
/** |
| 30 |
* The PostgreSQL alias for the range table. |
| 31 |
*/ |
| 32 |
public static final String ALIAS = "GENERATE_SERIES"; |
| 33 |
|
| 34 |
private Expression min, max, step; |
| 35 |
private boolean optimized; |
| 36 |
|
| 37 |
private final RangeIndex index; |
| 38 |
|
| 39 |
/** |
| 40 |
* Create a new range with the given start and end expressions. |
| 41 |
* |
| 42 |
* @param schema the schema (always the main schema) |
| 43 |
* @param min the start expression |
| 44 |
* @param max the end expression |
| 45 |
*/ |
| 46 |
public RangeTable(Schema schema, Expression min, Expression max) { |
| 47 |
super(schema, 0, NAME); |
| 48 |
this.min = min; |
| 49 |
this.max = max; |
| 50 |
Column[] columns = new Column[] { new Column("X", TypeInfo.TYPE_BIGINT) }; |
| 51 |
setColumns(columns); |
| 52 |
index = new RangeIndex(this, IndexColumn.wrap(columns)); |
| 53 |
} |
| 54 |
|
| 55 |
public RangeTable(Schema schema, Expression min, Expression max, Expression step) { |
| 56 |
this(schema, min, max); |
| 57 |
this.step = step; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 62 |
builder.append(NAME).append('('); |
| 63 |
min.getUnenclosedSQL(builder, sqlFlags).append(", "); |
| 64 |
max.getUnenclosedSQL(builder, sqlFlags); |
| 65 |
if (step != null) { |
| 66 |
step.getUnenclosedSQL(builder.append(", "), sqlFlags); |
| 67 |
} |
| 68 |
return builder.append(')'); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public boolean canGetRowCount(SessionLocal session) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public long getRowCount(SessionLocal session) { |
| 78 |
long step = getStep(session); |
| 79 |
if (step == 0L) { |
| 80 |
throw DbException.get(ErrorCode.STEP_SIZE_MUST_NOT_BE_ZERO); |
| 81 |
} |
| 82 |
long delta = getMax(session) - getMin(session); |
| 83 |
if (step > 0) { |
| 84 |
if (delta < 0) { |
| 85 |
return 0; |
| 86 |
} |
| 87 |
} else if (delta > 0) { |
| 88 |
return 0; |
| 89 |
} |
| 90 |
return delta / step + 1; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public TableType getTableType() { |
| 95 |
return TableType.SYSTEM_TABLE; |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public Index getScanIndex(SessionLocal session) { |
| 100 |
return index; |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public ArrayList<Index> getIndexes() { |
| 105 |
ArrayList<Index> list = new ArrayList<>(2); |
| 106 |
// Scan index (ignored by MIN/MAX optimization) |
| 107 |
list.add(index); |
| 108 |
// Normal index |
| 109 |
list.add(index); |
| 110 |
return list; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Calculate and get the start value of this range. |
| 115 |
* |
| 116 |
* @param session the session |
| 117 |
* @return the start value |
| 118 |
*/ |
| 119 |
public long getMin(SessionLocal session) { |
| 120 |
optimize(session); |
| 121 |
return min.getValue(session).getLong(); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Calculate and get the end value of this range. |
| 126 |
* |
| 127 |
* @param session the session |
| 128 |
* @return the end value |
| 129 |
*/ |
| 130 |
public long getMax(SessionLocal session) { |
| 131 |
optimize(session); |
| 132 |
return max.getValue(session).getLong(); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get the increment. |
| 137 |
* |
| 138 |
* @param session the session |
| 139 |
* @return the increment (1 by default) |
| 140 |
*/ |
| 141 |
public long getStep(SessionLocal session) { |
| 142 |
optimize(session); |
| 143 |
if (step == null) { |
| 144 |
return 1; |
| 145 |
} |
| 146 |
return step.getValue(session).getLong(); |
| 147 |
} |
| 148 |
|
| 149 |
private void optimize(SessionLocal s) { |
| 150 |
if (!optimized) { |
| 151 |
min = min.optimize(s); |
| 152 |
max = max.optimize(s); |
| 153 |
if (step != null) { |
| 154 |
step = step.optimize(s); |
| 155 |
} |
| 156 |
optimized = true; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public long getMaxDataModificationId() { |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public long getRowCountApproximation(SessionLocal session) { |
| 167 |
return 100; |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public boolean isDeterministic() { |
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
} |