use of io.crate.metadata.RelationName in project crate by crate.
the class FetchTask method start.
@Override
public void start() {
synchronized (jobId) {
if (killed != null) {
result.completeExceptionally(killed);
return;
}
HashMap<String, RelationName> index2TableIdent = new HashMap<>();
for (Map.Entry<RelationName, Collection<String>> entry : phase.tableIndices().entrySet()) {
for (String indexName : entry.getValue()) {
index2TableIdent.put(indexName, entry.getKey());
}
}
Set<RelationName> tablesWithFetchRefs = new HashSet<>();
for (Reference reference : phase.fetchRefs()) {
tablesWithFetchRefs.add(reference.ident().tableIdent());
}
String source = "fetch-task: " + jobId.toString() + '-' + phase.phaseId() + '-' + phase.name();
for (Routing routing : routingIterable) {
Map<String, Map<String, IntIndexedContainer>> locations = routing.locations();
Map<String, IntIndexedContainer> indexShards = locations.get(localNodeId);
for (Map.Entry<String, IntIndexedContainer> indexShardsEntry : indexShards.entrySet()) {
String indexName = indexShardsEntry.getKey();
Integer base = phase.bases().get(indexName);
if (base == null) {
continue;
}
IndexMetadata indexMetadata = metadata.index(indexName);
if (indexMetadata == null) {
if (IndexParts.isPartitioned(indexName)) {
continue;
}
throw new IndexNotFoundException(indexName);
}
Index index = indexMetadata.getIndex();
RelationName ident = index2TableIdent.get(indexName);
assert ident != null : "no relationName found for index " + indexName;
tableIdents.put(base, ident);
toFetch.put(ident, new ArrayList<>());
for (IntCursor shard : indexShardsEntry.getValue()) {
ShardId shardId = new ShardId(index, shard.value);
int readerId = base + shardId.id();
SharedShardContext shardContext = shardContexts.get(readerId);
if (shardContext == null) {
try {
shardContext = sharedShardContexts.createContext(shardId, readerId);
shardContexts.put(readerId, shardContext);
if (tablesWithFetchRefs.contains(ident)) {
searchers.put(readerId, shardContext.acquireSearcher(source));
}
} catch (IndexNotFoundException e) {
if (!IndexParts.isPartitioned(indexName)) {
throw e;
}
}
}
}
}
}
for (Reference reference : phase.fetchRefs()) {
Collection<Reference> references = toFetch.get(reference.ident().tableIdent());
if (references != null) {
references.add(reference);
}
}
}
if (searchers.isEmpty() || phase.fetchRefs().isEmpty()) {
// no fetch references means there will be no fetch requests
// this context is only here to allow the collectors to generate docids with the right bases
// the bases are fetched in the prepare phase therefore this context can be closed
close();
}
}
use of io.crate.metadata.RelationName in project crate by crate.
the class ViewAnalyzer method analyze.
public AnalyzedDropView analyze(DropView dropView, CoordinatorTxnCtx txnCtx) {
// No exists check to avoid stale clusterState race conditions
ArrayList<RelationName> views = new ArrayList<>(dropView.names().size());
ArrayList<RelationName> missing = new ArrayList<>();
for (QualifiedName qualifiedName : dropView.names()) {
try {
views.add(schemas.resolveView(qualifiedName, txnCtx.sessionContext().searchPath()).v2());
} catch (RelationUnknown e) {
if (!dropView.ifExists()) {
missing.add(RelationName.of(qualifiedName, txnCtx.sessionContext().searchPath().currentSchema()));
}
}
}
if (!missing.isEmpty()) {
throw new RelationsUnknown(missing);
}
return new AnalyzedDropView(views, dropView.ifExists());
}
use of io.crate.metadata.RelationName in project crate by crate.
the class ViewAnalyzer method analyze.
public CreateViewStmt analyze(CreateView createView, CoordinatorTxnCtx txnCtx) {
RelationName name = RelationName.of(createView.name(), txnCtx.sessionContext().searchPath().currentSchema());
name.ensureValidForRelationCreation();
if (BlobSchemaInfo.NAME.equals(name.schema())) {
throw new UnsupportedOperationException("Creating a view in the \"blob\" schema is not supported");
}
AnalyzedRelation query;
String formattedQuery;
try {
formattedQuery = SqlFormatter.formatSql(createView.query());
} catch (Exception e) {
throw new UnsupportedOperationException("Invalid query used in CREATE VIEW. Query: " + createView.query());
}
try {
// Analyze the formatted Query to make sure the formatting didn't mess it up in any way.
query = relationAnalyzer.analyze((Query) SqlParser.createStatement(formattedQuery), txnCtx, ParamTypeHints.EMPTY);
} catch (Exception e) {
throw new UnsupportedOperationException("Invalid query used in CREATE VIEW. " + e.getMessage() + ". Query: " + formattedQuery);
}
if (query.outputs().stream().map(f -> Symbols.pathFromSymbol(f).sqlFqn()).distinct().count() != query.outputs().size()) {
throw new IllegalArgumentException("Query in CREATE VIEW must not have duplicate column names");
}
return new CreateViewStmt(name, query, createView.query(), createView.replaceExisting(), txnCtx.sessionContext().sessionUser());
}
use of io.crate.metadata.RelationName in project crate by crate.
the class TransportAnalyzeAction method publishTableStats.
private CompletableFuture<AcknowledgedResponse> publishTableStats(Map<RelationName, Stats> newTableStats) {
List<DiscoveryNode> nodesOn41OrAfter = StreamSupport.stream(clusterService.state().nodes().spliterator(), false).filter(x -> x.getVersion().onOrAfter(Version.V_4_1_0)).collect(Collectors.toList());
var listener = new FutureActionListener<AcknowledgedResponse, AcknowledgedResponse>(x -> x);
var multiListener = new MultiActionListener<>(nodesOn41OrAfter.size(), Collectors.reducing(new AcknowledgedResponse(true), (resp1, resp2) -> new AcknowledgedResponse(resp1.isAcknowledged() && resp2.isAcknowledged())), listener);
var responseHandler = new ActionListenerResponseHandler<>(multiListener, AcknowledgedResponse::new, ThreadPool.Names.SAME);
PublishTableStatsRequest request = new PublishTableStatsRequest(newTableStats);
for (DiscoveryNode node : nodesOn41OrAfter) {
transportService.sendRequest(node, RECEIVE_TABLE_STATS, request, responseHandler);
}
return listener;
}
use of io.crate.metadata.RelationName in project crate by crate.
the class TransportAnalyzeAction method fetchSamples.
private CompletableFuture<Samples> fetchSamples(RelationName relationName, List<Reference> columns) {
FutureActionListener<FetchSampleResponse, Samples> listener = new FutureActionListener<>(FetchSampleResponse::samples);
List<DiscoveryNode> nodesOn41OrAfter = StreamSupport.stream(clusterService.state().nodes().spliterator(), false).filter(x -> x.getVersion().onOrAfter(Version.V_4_1_0)).collect(Collectors.toList());
MultiActionListener<FetchSampleResponse, ?, FetchSampleResponse> multiListener = new MultiActionListener<>(nodesOn41OrAfter.size(), Collectors.reducing(new FetchSampleResponse(Samples.EMPTY), (FetchSampleResponse s1, FetchSampleResponse s2) -> FetchSampleResponse.merge(TransportAnalyzeAction.NUM_SAMPLES, s1, s2)), listener);
List<Streamer> streamers = Arrays.asList(Symbols.streamerArray(columns));
ActionListenerResponseHandler<FetchSampleResponse> responseHandler = new ActionListenerResponseHandler<>(multiListener, in -> new FetchSampleResponse(streamers, in), ThreadPool.Names.SAME);
for (DiscoveryNode node : nodesOn41OrAfter) {
transportService.sendRequest(node, FETCH_SAMPLES, new FetchSampleRequest(relationName, columns, TransportAnalyzeAction.NUM_SAMPLES), responseHandler);
}
return listener;
}
Aggregations