forked from hazelcast/hazelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableResolverImpl.java
357 lines (310 loc) · 14.4 KB
/
TableResolverImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright 2023 Hazelcast Inc.
*
* Licensed under the Hazelcast Community License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://hazelcast.com/hazelcast-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.jet.sql.impl.schema;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.LifecycleEvent;
import com.hazelcast.dataconnection.impl.InternalDataConnectionService;
import com.hazelcast.jet.function.QuadFunction;
import com.hazelcast.jet.sql.impl.connector.SqlConnector;
import com.hazelcast.jet.sql.impl.connector.SqlConnector.SqlExternalResource;
import com.hazelcast.jet.sql.impl.connector.SqlConnectorCache;
import com.hazelcast.jet.sql.impl.connector.infoschema.MappingColumnsTable;
import com.hazelcast.jet.sql.impl.connector.infoschema.MappingsTable;
import com.hazelcast.jet.sql.impl.connector.infoschema.TablesTable;
import com.hazelcast.jet.sql.impl.connector.infoschema.UDTAttributesTable;
import com.hazelcast.jet.sql.impl.connector.infoschema.UserDefinedTypesTable;
import com.hazelcast.jet.sql.impl.connector.infoschema.ViewsTable;
import com.hazelcast.jet.sql.impl.connector.virtual.ViewTable;
import com.hazelcast.spi.impl.NodeEngine;
import com.hazelcast.sql.impl.QueryException;
import com.hazelcast.sql.impl.schema.BadTable;
import com.hazelcast.sql.impl.schema.ConstantTableStatistics;
import com.hazelcast.sql.impl.schema.Mapping;
import com.hazelcast.sql.impl.schema.MappingField;
import com.hazelcast.sql.impl.schema.Table;
import com.hazelcast.sql.impl.schema.TableResolver;
import com.hazelcast.sql.impl.schema.dataconnection.DataConnectionCatalogEntry;
import com.hazelcast.sql.impl.schema.type.Type;
import com.hazelcast.sql.impl.schema.view.View;
import com.hazelcast.sql.impl.security.SqlSecurityContext;
import javax.annotation.Nonnull;
import java.security.Permission;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import static com.hazelcast.internal.util.Preconditions.checkNotNull;
import static com.hazelcast.sql.impl.QueryUtils.CATALOG;
import static com.hazelcast.sql.impl.QueryUtils.SCHEMA_NAME_INFORMATION_SCHEMA;
import static com.hazelcast.sql.impl.QueryUtils.SCHEMA_NAME_PUBLIC;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
/**
* A table resolver for DDL-created mappings and for the {@code
* information_schema}.
*/
public class TableResolverImpl implements TableResolver {
private static final List<List<String>> SEARCH_PATHS = singletonList(
asList(CATALOG, SCHEMA_NAME_PUBLIC)
);
private static final List<QuadFunction<List<Mapping>, List<View>, List<Type>, NodeEngine, Table>> ADDITIONAL_TABLE_PRODUCERS
= asList(
(m, v, t, hz) -> new TablesTable(CATALOG, SCHEMA_NAME_INFORMATION_SCHEMA, SCHEMA_NAME_PUBLIC, m, v),
(m, v, t, hz) -> new MappingsTable(CATALOG, SCHEMA_NAME_INFORMATION_SCHEMA, SCHEMA_NAME_PUBLIC, m,
hz.getDataConnectionService()::typeForDataConnection,
hz.getConfig().getSecurityConfig().isEnabled()),
(m, v, t, hz) -> new MappingColumnsTable(CATALOG, SCHEMA_NAME_INFORMATION_SCHEMA, SCHEMA_NAME_PUBLIC, m, v),
(m, v, t, hz) -> new ViewsTable(CATALOG, SCHEMA_NAME_INFORMATION_SCHEMA, SCHEMA_NAME_PUBLIC, v),
(m, v, t, hz) -> new UserDefinedTypesTable(CATALOG, SCHEMA_NAME_INFORMATION_SCHEMA, SCHEMA_NAME_PUBLIC, t),
(m, v, t, hz) -> new UDTAttributesTable(CATALOG, SCHEMA_NAME_INFORMATION_SCHEMA, SCHEMA_NAME_PUBLIC, t)
);
private final NodeEngine nodeEngine;
private final RelationsStorage relationsStorage;
private final SqlConnectorCache connectorCache;
private final List<TableListener> listeners;
// These fields should normally be volatile because we're accessing them from multiple threads. But we
// don't care if some thread doesn't see a newer value written by another thread. Each thread will write
// the same value (we assume that the number of mappings and views doesn't change much), so we
// shave a tiny bit of performance from not synchronizing :)
private int lastViewsSize;
private int lastMappingsSize;
private int lastTypesSize;
public TableResolverImpl(
NodeEngine nodeEngine,
RelationsStorage relationsStorage,
SqlConnectorCache connectorCache
) {
this.nodeEngine = nodeEngine;
this.relationsStorage = relationsStorage;
this.connectorCache = connectorCache;
this.listeners = new CopyOnWriteArrayList<>();
// because listeners are invoked asynchronously from the calling thread,
// local changes are handled in createMapping() & removeMapping(), thus
// we skip events originating from local member to avoid double processing
nodeEngine.getHazelcastInstance().getLifecycleService().addLifecycleListener(event -> {
if (event.getState() == LifecycleEvent.LifecycleState.STARTED) {
this.relationsStorage.initializeWithListener(new AbstractSchemaStorage.EntryListenerAdapter() {
@Override
public void entryUpdated(EntryEvent<String, Object> event) {
if (!event.getMember().localMember()) {
listeners.forEach(TableListener::onTableChanged);
}
}
@Override
public void entryRemoved(EntryEvent<String, Object> event) {
if (!event.getMember().localMember()) {
listeners.forEach(TableListener::onTableChanged);
}
}
});
}
});
}
// region mapping
public void createMapping(Mapping mapping, boolean replace, boolean ifNotExists, SqlSecurityContext securityContext) {
Mapping resolved = resolveMapping(mapping, securityContext);
String name = resolved.name();
if (ifNotExists) {
relationsStorage.putIfAbsent(name, resolved);
} else if (replace) {
relationsStorage.put(name, resolved);
listeners.forEach(TableListener::onTableChanged);
} else if (!relationsStorage.putIfAbsent(name, resolved)) {
throw QueryException.error("Mapping or view already exists: " + name);
}
}
private Mapping resolveMapping(Mapping mapping, SqlSecurityContext securityContext) {
Map<String, String> options = mapping.options();
String type = mapping.connectorType();
String dataConnection = mapping.dataConnection();
List<MappingField> resolvedFields;
SqlConnector connector;
if (type == null) {
connector = extractConnector(dataConnection);
} else {
connector = connectorCache.forType(type);
}
String objectType = mapping.objectType() == null
? connector.defaultObjectType()
: mapping.objectType();
checkNotNull(objectType, "objectType cannot be null");
SqlExternalResource externalResource = new SqlExternalResource(
mapping.externalName(),
mapping.dataConnection(),
connector.typeName(),
objectType,
options
);
List<Permission> permissions = connector.permissionsForResolve(externalResource, nodeEngine);
for (Permission permission : permissions) {
securityContext.checkPermission(permission);
}
resolvedFields = connector.resolveAndValidateFields(
nodeEngine,
externalResource,
mapping.fields()
);
return new Mapping(
mapping.name(),
mapping.externalName(),
mapping.dataConnection(), type,
objectType,
new ArrayList<>(resolvedFields),
new LinkedHashMap<>(options)
);
}
private SqlConnector extractConnector(@Nonnull String dataConnection) {
InternalDataConnectionService dataConnectionService = nodeEngine.getDataConnectionService();
// TODO atm data connection and connector types match, but that's
// not going to be universally true in the future
String type = dataConnectionService.typeForDataConnection(dataConnection);
return connectorCache.forType(type);
}
public void removeMapping(String name, boolean ifExists) {
if (relationsStorage.removeMapping(name) != null) {
listeners.forEach(TableListener::onTableChanged);
} else if (!ifExists) {
throw QueryException.error("Mapping does not exist: " + name);
}
}
@Nonnull
public Collection<String> getMappingNames() {
return relationsStorage.mappingNames();
}
// endregion
// region view
public void createView(View view, boolean replace, boolean ifNotExists) {
if (ifNotExists) {
relationsStorage.putIfAbsent(view.name(), view);
} else if (replace) {
relationsStorage.put(view.name(), view);
} else if (!relationsStorage.putIfAbsent(view.name(), view)) {
throw QueryException.error("Mapping or view already exists: " + view.name());
}
}
public void removeView(String name, boolean ifExists) {
if (relationsStorage.removeView(name) == null && !ifExists) {
throw QueryException.error("View does not exist: " + name);
}
}
@Nonnull
public Collection<String> getViewNames() {
return relationsStorage.viewNames();
}
// endregion
// region type
public Collection<String> getTypeNames() {
return relationsStorage.typeNames();
}
public Collection<Type> getTypes() {
return relationsStorage.getAllTypes();
}
public void createType(Type type, boolean replace, boolean ifNotExists) {
if (ifNotExists) {
relationsStorage.putIfAbsent(type.getName(), type);
} else if (replace) {
relationsStorage.put(type.getName(), type);
} else if (!relationsStorage.putIfAbsent(type.getName(), type)) {
throw QueryException.error("Type already exists: " + type.getName());
}
}
public void removeType(String name, boolean ifExists) {
if (relationsStorage.removeType(name) == null && !ifExists) {
throw QueryException.error("Type does not exist: " + name);
}
}
// endregion
@Nonnull
@Override
public List<List<String>> getDefaultSearchPaths() {
return SEARCH_PATHS;
}
@Nonnull
@Override
public List<Table> getTables() {
Collection<Object> objects = relationsStorage.allObjects();
List<Table> tables = new ArrayList<>(objects.size() + ADDITIONAL_TABLE_PRODUCERS.size());
int lastMappingsSize = this.lastMappingsSize;
int lastViewsSize = this.lastViewsSize;
int lastTypesSize = this.lastTypesSize;
// Trying to avoid list growing.
List<Mapping> mappings = lastMappingsSize == 0 ? new ArrayList<>() : new ArrayList<>(lastMappingsSize);
List<View> views = lastViewsSize == 0 ? new ArrayList<>() : new ArrayList<>(lastViewsSize);
List<Type> types = lastTypesSize == 0 ? new ArrayList<>() : new ArrayList<>(lastTypesSize);
for (Object o : objects) {
if (o instanceof Mapping) {
tables.add(toTable((Mapping) o));
mappings.add((Mapping) o);
} else if (o instanceof View) {
tables.add(toTable((View) o));
views.add((View) o);
} else if (o instanceof Type) {
types.add((Type) o);
} else if (o instanceof DataConnectionCatalogEntry) {
// Note: data connection is not a 'table' or 'relation',
// It's stored in a separate namespace.
continue;
} else {
throw new RuntimeException("Unexpected: " + o);
}
}
ADDITIONAL_TABLE_PRODUCERS.forEach(
producer -> tables.add(producer.apply(mappings, views, types, nodeEngine)));
this.lastViewsSize = views.size();
this.lastMappingsSize = mappings.size();
this.lastTypesSize = types.size();
return tables;
}
private Table toTable(Mapping mapping) {
try {
SqlConnector connector;
if (mapping.connectorType() == null) {
connector = extractConnector(mapping.dataConnection());
} else {
connector = connectorCache.forType((mapping.connectorType()));
}
assert connector != null;
return connector.createTable(
nodeEngine,
SCHEMA_NAME_PUBLIC,
mapping.name(),
sqlExternalResourceFrom(mapping, connector),
mapping.fields());
} catch (Throwable e) {
// will fail later if invalid table is actually used in a query
return new BadTable(SCHEMA_NAME_PUBLIC, mapping.name(), mapping.objectType(), e);
}
}
private static SqlExternalResource sqlExternalResourceFrom(Mapping internalMapping, SqlConnector connector) {
String internalObjType = internalMapping.objectType() == null
? connector.defaultObjectType()
: internalMapping.objectType();
checkNotNull(internalObjType, "objectType cannot be null");
String connectorType = connector.typeName();
return new SqlExternalResource(internalMapping.externalName(),
internalMapping.dataConnection(),
connectorType, internalObjType, internalMapping.options());
}
private Table toTable(View view) {
return new ViewTable(SCHEMA_NAME_PUBLIC, view.name(), view.query(), new ConstantTableStatistics(0L));
}
@Override
public void registerListener(TableListener listener) {
listeners.add(listener);
}
}