Debian Codegraph @

src/main/org/h2/expression/function/table/ArrayTableFunction.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.table;
7
8 import java.util.ArrayList;
9
10 import org.h2.api.ErrorCode;
11 import org.h2.engine.Database;
12 import org.h2.engine.SessionLocal;
13 import org.h2.expression.Expression;
14 import org.h2.expression.ExpressionColumn;
15 import org.h2.message.DbException;
16 import org.h2.result.LocalResult;
17 import org.h2.result.ResultInterface;
18 import org.h2.table.Column;
19 import org.h2.util.json.JSONArray;
20 import org.h2.util.json.JSONValue;
21 import org.h2.value.Value;
22 import org.h2.value.ValueCollectionBase;
23 import org.h2.value.ValueInteger;
24 import org.h2.value.ValueJson;
25 import org.h2.value.ValueNull;
26
27 /**
28 * A table value function.
29 */
30 public final class ArrayTableFunction extends TableFunction {
31
32 /**
33 * UNNEST().
34 */
35 public static final int UNNEST = 0;
36
37 /**
38 * TABLE() (non-standard).
39 */
40 public static final int TABLE = UNNEST + 1;
41
42 /**
43 * TABLE_DISTINCT() (non-standard).
44 */
45 public static final int TABLE_DISTINCT = TABLE + 1;
46
47 private Column[] columns;
48
49 private static final String[] NAMES = { //
50 "UNNEST", "TABLE", "TABLE_DISTINCT" //
51 };
52
53 private final int function;
54
55 public ArrayTableFunction(int function) {
56 super(new Expression[1]);
57 this.function = function;
58 }
59
60 @Override
61 public ResultInterface getValue(SessionLocal session) {
62 return getTable(session, false);
63 }
64
65 @Override
66 public void optimize(SessionLocal session) {
67 super.optimize(session);
68 if (args.length < 1) {
69 throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, getName(), ">0");
70 }
71 }
72
73 @Override
74 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
75 if (function == UNNEST) {
76 super.getSQL(builder, sqlFlags);
77 if (args.length < columns.length) {
78 builder.append(" WITH ORDINALITY");
79 }
80 } else {
81 builder.append(getName()).append('(');
82 for (int i = 0; i < args.length; i++) {
83 if (i > 0) {
84 builder.append(", ");
85 }
86 builder.append(columns[i].getCreateSQL()).append('=');
87 args[i].getUnenclosedSQL(builder, sqlFlags);
88 }
89 builder.append(')');
90 }
91 return builder;
92 }
93
94 @Override
95 public ResultInterface getValueTemplate(SessionLocal session) {
96 return getTable(session, true);
97 }
98
99 public void setColumns(ArrayList<Column> columns) {
100 this.columns = columns.toArray(new Column[0]);
101 }
102
103 private ResultInterface getTable(SessionLocal session, boolean onlyColumnList) {
104 int totalColumns = columns.length;
105 Expression[] header = new Expression[totalColumns];
106 Database db = session.getDatabase();
107 for (int i = 0; i < totalColumns; i++) {
108 Column c = columns[i];
109 ExpressionColumn col = new ExpressionColumn(db, c);
110 header[i] = col;
111 }
112 LocalResult result = new LocalResult(session, header, totalColumns, totalColumns);
113 if (!onlyColumnList && function == TABLE_DISTINCT) {
114 result.setDistinct();
115 }
116 if (!onlyColumnList) {
117 int len = totalColumns;
118 boolean unnest = function == UNNEST, addNumber = false;
119 if (unnest) {
120 len = args.length;
121 if (len < totalColumns) {
122 addNumber = true;
123 }
124 }
125 Value[][] list = new Value[len][];
126 int rows = 0;
127 for (int i = 0; i < len; i++) {
128 Value v = args[i].getValue(session);
129 if (v == ValueNull.INSTANCE) {
130 list[i] = Value.EMPTY_VALUES;
131 } else {
132 Value[] l;
133 switch (v.getValueType()) {
134 case Value.JSON: {
135 JSONValue value = v.convertToAnyJson().getDecomposition();
136 if (value instanceof JSONArray) {
137 l = ((JSONArray) value).getArray(Value.class, ValueJson::fromJson);
138 } else {
139 l = Value.EMPTY_VALUES;
140 }
141 break;
142 }
143 case Value.ARRAY:
144 case Value.ROW: {
145 l = ((ValueCollectionBase) v).getList();
146 break;
147 }
148 default:
149 l = new Value[] { v };
150 }
151 list[i] = l;
152 rows = Math.max(rows, l.length);
153 }
154 }
155 for (int row = 0; row < rows; row++) {
156 Value[] r = new Value[totalColumns];
157 for (int j = 0; j < len; j++) {
158 Value[] l = list[j];
159 Value v;
160 if (l.length <= row) {
161 v = ValueNull.INSTANCE;
162 } else {
163 Column c = columns[j];
164 v = l[row];
165 if (!unnest) {
166 v = v.convertForAssignTo(c.getType(), session, c);
167 }
168 }
169 r[j] = v;
170 }
171 if (addNumber) {
172 r[len] = ValueInteger.get(row + 1);
173 }
174 result.addRow(r);
175 }
176 }
177 result.done();
178 return result;
179 }
180
181 @Override
182 public String getName() {
183 return NAMES[function];
184 }
185
186 @Override
187 public boolean isDeterministic() {
188 return true;
189 }
190
191 public int getFunctionType() {
192 return function;
193 }
194
195 }