Debian Codegraph @

src/main/org/h2/expression/function/TableInfoFunction.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.function;
7
8 import java.util.ArrayList;
9
10 import org.h2.command.Parser;
11 import org.h2.engine.SessionLocal;
12 import org.h2.expression.Expression;
13 import org.h2.expression.ExpressionVisitor;
14 import org.h2.index.Index;
15 import org.h2.message.DbException;
16 import org.h2.mvstore.db.MVSpatialIndex;
17 import org.h2.table.Column;
18 import org.h2.table.Table;
19 import org.h2.value.TypeInfo;
20 import org.h2.value.Value;
21 import org.h2.value.ValueBigint;
22 import org.h2.value.ValueNull;
23
24 /**
25 * A table information function.
26 */
27 public final class TableInfoFunction extends Function1_2 {
28
29 /**
30 * DISK_SPACE_USED() (non-standard).
31 */
32 public static final int DISK_SPACE_USED = 0;
33
34 /**
35 * ESTIMATED_ENVELOPE().
36 */
37 public static final int ESTIMATED_ENVELOPE = DISK_SPACE_USED + 1;
38
39 private static final String[] NAMES = { //
40 "DISK_SPACE_USED", "ESTIMATED_ENVELOPE" //
41 };
42
43 private final int function;
44
45 public TableInfoFunction(Expression arg1, Expression arg2, int function) {
46 super(arg1, arg2);
47 this.function = function;
48 }
49
50 @Override
51 public Value getValue(SessionLocal session, Value v1, Value v2) {
52 Table table = new Parser(session).parseTableName(v1.getString());
53 l: switch (function) {
54 case DISK_SPACE_USED:
55 v1 = ValueBigint.get(table.getDiskSpaceUsed());
56 break;
57 case ESTIMATED_ENVELOPE: {
58 Column column = table.getColumn(v2.getString());
59 ArrayList<Index> indexes = table.getIndexes();
60 if (indexes != null) {
61 for (int i = 1, size = indexes.size(); i < size; i++) {
62 Index index = indexes.get(i);
63 if (index instanceof MVSpatialIndex && index.isFirstColumn(column)) {
64 v1 = ((MVSpatialIndex) index).getEstimatedBounds(session);
65 break l;
66 }
67 }
68 }
69 v1 = ValueNull.INSTANCE;
70 break;
71 }
72 default:
73 throw DbException.getInternalError("function=" + function);
74 }
75 return v1;
76 }
77
78 @Override
79 public Expression optimize(SessionLocal session) {
80 left = left.optimize(session);
81 if (right != null) {
82 right = right.optimize(session);
83 }
84 switch (function) {
85 case DISK_SPACE_USED:
86 type = TypeInfo.TYPE_BIGINT;
87 break;
88 case ESTIMATED_ENVELOPE:
89 type = TypeInfo.TYPE_GEOMETRY;
90 break;
91 default:
92 throw DbException.getInternalError("function=" + function);
93 }
94 return this;
95 }
96
97 @Override
98 public boolean isEverything(ExpressionVisitor visitor) {
99 switch (visitor.getType()) {
100 case ExpressionVisitor.DETERMINISTIC:
101 return false;
102 }
103 return super.isEverything(visitor);
104 }
105
106 @Override
107 public String getName() {
108 return NAMES[function];
109 }
110
111 }