Debian Codegraph @

src/main/org/h2/expression/ExpressionVisitor.java

raw
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.expression;
7
8 import java.util.HashSet;
9 import org.h2.command.query.AllColumnsForPlan;
10 import org.h2.engine.DbObject;
11 import org.h2.table.Column;
12 import org.h2.table.ColumnResolver;
13 import org.h2.table.Table;
14 import org.h2.table.TableFilter;
15
16 /**
17 * The visitor pattern is used to iterate through all expressions of a query
18 * to optimize a statement.
19 */
20 public final class ExpressionVisitor {
21
22 /**
23 * Is the value independent on unset parameters or on columns of a higher
24 * level query, or sequence values (that means can it be evaluated right
25 * now)?
26 */
27 public static final int INDEPENDENT = 0;
28
29 /**
30 * The visitor singleton for the type INDEPENDENT.
31 */
32 public static final ExpressionVisitor INDEPENDENT_VISITOR =
33 new ExpressionVisitor(INDEPENDENT);
34
35 /**
36 * Are all aggregates MIN(column), MAX(column), COUNT(*), MEDIAN(column),
37 * ENVELOPE(count) for the given table (getTable)?
38 */
39 public static final int OPTIMIZABLE_AGGREGATE = 1;
40
41 /**
42 * Does the expression return the same results for the same parameters?
43 */
44 public static final int DETERMINISTIC = 2;
45
46 /**
47 * The visitor singleton for the type DETERMINISTIC.
48 */
49 public static final ExpressionVisitor DETERMINISTIC_VISITOR =
50 new ExpressionVisitor(DETERMINISTIC);
51
52 /**
53 * Can the expression be evaluated, that means are all columns set to
54 * 'evaluatable'?
55 */
56 public static final int EVALUATABLE = 3;
57
58 /**
59 * The visitor singleton for the type EVALUATABLE.
60 */
61 public static final ExpressionVisitor EVALUATABLE_VISITOR =
62 new ExpressionVisitor(EVALUATABLE);
63
64 /**
65 * Count of cached INDEPENDENT and EVALUATABLE visitors with different query
66 * level.
67 */
68 private static final int CACHED = 8;
69
70 /**
71 * INDEPENDENT listeners with query level 0, 1, ...
72 */
73 private static final ExpressionVisitor[] INDEPENDENT_VISITORS;
74
75 /**
76 * EVALUATABLE listeners with query level 0, 1, ...
77 */
78 private static final ExpressionVisitor[] EVALUATABLE_VISITORS;
79
80 static {
81 ExpressionVisitor[] a = new ExpressionVisitor[CACHED];
82 a[0] = INDEPENDENT_VISITOR;
83 for (int i = 1; i < CACHED; i++) {
84 a[i] = new ExpressionVisitor(INDEPENDENT, i);
85 }
86 INDEPENDENT_VISITORS = a;
87 a = new ExpressionVisitor[CACHED];
88 a[0] = EVALUATABLE_VISITOR;
89 for (int i = 1; i < CACHED; i++) {
90 a[i] = new ExpressionVisitor(EVALUATABLE, i);
91 }
92 EVALUATABLE_VISITORS = a;
93 }
94
95 /**
96 * Request to set the latest modification id (addDataModificationId).
97 */
98 public static final int SET_MAX_DATA_MODIFICATION_ID = 4;
99
100 /**
101 * Does the expression have no side effects (change the data)?
102 */
103 public static final int READONLY = 5;
104
105 /**
106 * The visitor singleton for the type EVALUATABLE.
107 */
108 public static final ExpressionVisitor READONLY_VISITOR =
109 new ExpressionVisitor(READONLY);
110
111 /**
112 * Does an expression have no relation to the given table filter
113 * (getResolver)?
114 */
115 public static final int NOT_FROM_RESOLVER = 6;
116
117 /**
118 * Request to get the set of dependencies (addDependency).
119 */
120 public static final int GET_DEPENDENCIES = 7;
121
122 /**
123 * Can the expression be added to a condition of an outer query. Example:
124 * ROWNUM() can't be added as a condition to the inner query of select id
125 * from (select t.*, rownum as r from test t) where r between 2 and 3; Also
126 * a sequence expression must not be used.
127 */
128 public static final int QUERY_COMPARABLE = 8;
129
130 /**
131 * Get all referenced columns for the optimiser.
132 */
133 public static final int GET_COLUMNS1 = 9;
134
135 /**
136 * Get all referenced columns.
137 */
138 public static final int GET_COLUMNS2 = 10;
139
140 /**
141 * Decrement query level of all expression columns.
142 */
143 public static final int DECREMENT_QUERY_LEVEL = 11;
144
145 /**
146 * The visitor singleton for the type QUERY_COMPARABLE.
147 */
148 public static final ExpressionVisitor QUERY_COMPARABLE_VISITOR =
149 new ExpressionVisitor(QUERY_COMPARABLE);
150
151 private final int type;
152 private final int queryLevel;
153 private final HashSet<?> set;
154 private final AllColumnsForPlan columns1;
155 private final Table table;
156 private final long[] maxDataModificationId;
157 private final ColumnResolver resolver;
158
159 private ExpressionVisitor(int type,
160 int queryLevel,
161 HashSet<?> set,
162 AllColumnsForPlan columns1, Table table, ColumnResolver resolver,
163 long[] maxDataModificationId) {
164 this.type = type;
165 this.queryLevel = queryLevel;
166 this.set = set;
167 this.columns1 = columns1;
168 this.table = table;
169 this.resolver = resolver;
170 this.maxDataModificationId = maxDataModificationId;
171 }
172
173 private ExpressionVisitor(int type) {
174 this.type = type;
175 this.queryLevel = 0;
176 this.set = null;
177 this.columns1 = null;
178 this.table = null;
179 this.resolver = null;
180 this.maxDataModificationId = null;
181 }
182
183 private ExpressionVisitor(int type, int queryLevel) {
184 this.type = type;
185 this.queryLevel = queryLevel;
186 this.set = null;
187 this.columns1 = null;
188 this.table = null;
189 this.resolver = null;
190 this.maxDataModificationId = null;
191 }
192
193 /**
194 * Create a new visitor object to collect dependencies.
195 *
196 * @param dependencies the dependencies set
197 * @return the new visitor
198 */
199 public static ExpressionVisitor getDependenciesVisitor(
200 HashSet<DbObject> dependencies) {
201 return new ExpressionVisitor(GET_DEPENDENCIES, 0, dependencies, null,
202 null, null, null);
203 }
204
205 /**
206 * Create a new visitor to check if all aggregates are for the given table.
207 *
208 * @param table the table
209 * @return the new visitor
210 */
211 public static ExpressionVisitor getOptimizableVisitor(Table table) {
212 return new ExpressionVisitor(OPTIMIZABLE_AGGREGATE, 0, null,
213 null, table, null, null);
214 }
215
216 /**
217 * Create a new visitor to check if no expression depends on the given
218 * resolver.
219 *
220 * @param resolver the resolver
221 * @return the new visitor
222 */
223 public static ExpressionVisitor getNotFromResolverVisitor(ColumnResolver resolver) {
224 return new ExpressionVisitor(NOT_FROM_RESOLVER, 0, null, null, null,
225 resolver, null);
226 }
227
228 /**
229 * Create a new visitor to get all referenced columns.
230 *
231 * @param columns the columns map
232 * @return the new visitor
233 */
234 public static ExpressionVisitor getColumnsVisitor(AllColumnsForPlan columns) {
235 return new ExpressionVisitor(GET_COLUMNS1, 0, null, columns, null, null, null);
236 }
237
238 /**
239 * Create a new visitor to get all referenced columns.
240 *
241 * @param columns the columns map
242 * @param table table to gather columns from, or {@code null} to gather all columns
243 * @return the new visitor
244 */
245 public static ExpressionVisitor getColumnsVisitor(HashSet<Column> columns, Table table) {
246 return new ExpressionVisitor(GET_COLUMNS2, 0, columns, null, table, null, null);
247 }
248
249 public static ExpressionVisitor getMaxModificationIdVisitor() {
250 return new ExpressionVisitor(SET_MAX_DATA_MODIFICATION_ID, 0, null,
251 null, null, null, new long[1]);
252 }
253
254 /**
255 * Create a new visitor to decrement query level in columns with the
256 * specified resolvers.
257 *
258 * @param columnResolvers
259 * column resolvers
260 * @param queryDecrement
261 * 0 to check whether operation is allowed, 1 to actually perform
262 * the decrement
263 * @return the new visitor
264 */
265 public static ExpressionVisitor getDecrementQueryLevelVisitor(HashSet<ColumnResolver> columnResolvers,
266 int queryDecrement) {
267 return new ExpressionVisitor(DECREMENT_QUERY_LEVEL, queryDecrement, columnResolvers, null, null, null, null);
268 }
269
270 /**
271 * Add a new dependency to the set of dependencies.
272 * This is used for GET_DEPENDENCIES visitors.
273 *
274 * @param obj the additional dependency.
275 */
276 @SuppressWarnings("unchecked")
277 public void addDependency(DbObject obj) {
278 ((HashSet<DbObject>) set).add(obj);
279 }
280
281 /**
282 * Add a new column to the set of columns.
283 * This is used for GET_COLUMNS visitors.
284 *
285 * @param column the additional column.
286 */
287 void addColumn1(Column column) {
288 columns1.add(column);
289 }
290
291 /**
292 * Add a new column to the set of columns.
293 * This is used for GET_COLUMNS2 visitors.
294 *
295 * @param column the additional column.
296 */
297 @SuppressWarnings("unchecked")
298 void addColumn2(Column column) {
299 if (table == null || table == column.getTable()) {
300 ((HashSet<Column>) set).add(column);
301 }
302 }
303
304 /**
305 * Get the dependency set.
306 * This is used for GET_DEPENDENCIES visitors.
307 *
308 * @return the set
309 */
310 @SuppressWarnings("unchecked")
311 public HashSet<DbObject> getDependencies() {
312 return (HashSet<DbObject>) set;
313 }
314
315 /**
316 * Increment or decrement the query level.
317 *
318 * @param offset 1 to increment, -1 to decrement
319 * @return this visitor or its clone with the changed query level
320 */
321 public ExpressionVisitor incrementQueryLevel(int offset) {
322 if (type == INDEPENDENT) {
323 offset += queryLevel;
324 return offset < CACHED ? INDEPENDENT_VISITORS[offset] : new ExpressionVisitor(INDEPENDENT, offset);
325 } else if (type == EVALUATABLE) {
326 offset += queryLevel;
327 return offset < CACHED ? EVALUATABLE_VISITORS[offset] : new ExpressionVisitor(EVALUATABLE, offset);
328 } else {
329 return this;
330 }
331 }
332
333 /**
334 * Get the column resolver.
335 * This is used for NOT_FROM_RESOLVER visitors.
336 *
337 * @return the column resolver
338 */
339 public ColumnResolver getResolver() {
340 return resolver;
341 }
342
343 /**
344 * Get the set of column resolvers.
345 * This is used for {@link #DECREMENT_QUERY_LEVEL} visitors.
346 *
347 * @return the set
348 */
349 @SuppressWarnings("unchecked")
350 public HashSet<ColumnResolver> getColumnResolvers() {
351 return (HashSet<ColumnResolver>) set;
352 }
353
354 /**
355 * Update the field maxDataModificationId if this value is higher
356 * than the current value.
357 * This is used for SET_MAX_DATA_MODIFICATION_ID visitors.
358 *
359 * @param value the data modification id
360 */
361 public void addDataModificationId(long value) {
362 long m = maxDataModificationId[0];
363 if (value > m) {
364 maxDataModificationId[0] = value;
365 }
366 }
367
368 /**
369 * Get the last data modification.
370 * This is used for SET_MAX_DATA_MODIFICATION_ID visitors.
371 *
372 * @return the maximum modification id
373 */
374 public long getMaxDataModificationId() {
375 return maxDataModificationId[0];
376 }
377
378 int getQueryLevel() {
379 assert type == INDEPENDENT || type == EVALUATABLE || type == DECREMENT_QUERY_LEVEL;
380 return queryLevel;
381 }
382
383 /**
384 * Get the table.
385 * This is used for OPTIMIZABLE_MIN_MAX_COUNT_ALL visitors.
386 *
387 * @return the table
388 */
389 public Table getTable() {
390 return table;
391 }
392
393 /**
394 * Get the visitor type.
395 *
396 * @return the type
397 */
398 public int getType() {
399 return type;
400 }
401
402 /**
403 * Get the set of columns of all tables.
404 *
405 * @param filters the filters
406 * @param allColumnsSet the on-demand all-columns set
407 */
408 public static void allColumnsForTableFilters(TableFilter[] filters, AllColumnsForPlan allColumnsSet) {
409 for (TableFilter filter : filters) {
410 if (filter.getSelect() != null) {
411 filter.getSelect().isEverything(ExpressionVisitor.getColumnsVisitor(allColumnsSet));
412 }
413 }
414 }
415
416 }