use of io.crate.metadata.doc.DocTableInfo in project crate by crate.
the class CreateSnapshotAnalyzer method analyze.
public CreateSnapshotAnalyzedStatement analyze(CreateSnapshot node, Analysis analysis) {
Optional<QualifiedName> repositoryName = node.name().getPrefix();
// validate repository
Preconditions.checkArgument(repositoryName.isPresent(), "Snapshot must be specified by \"<repository_name>\".\"<snapshot_name>\"");
Preconditions.checkArgument(repositoryName.get().getParts().size() == 1, String.format(Locale.ENGLISH, "Invalid repository name '%s'", repositoryName.get()));
repositoryService.failIfRepositoryDoesNotExist(repositoryName.get().toString());
// snapshot existence in repo is validated upon execution
String snapshotName = node.name().getSuffix();
SnapshotId snapshotId = new SnapshotId(repositoryName.get().toString(), snapshotName);
// validate and extract settings
Settings settings = GenericPropertiesConverter.settingsFromProperties(node.properties(), analysis.parameterContext(), SETTINGS).build();
boolean ignoreUnavailable = settings.getAsBoolean(IGNORE_UNAVAILABLE.name(), IGNORE_UNAVAILABLE.defaultValue());
// iterate tables
if (node.tableList().isPresent()) {
List<Table> tableList = node.tableList().get();
Set<String> snapshotIndices = new HashSet<>(tableList.size());
for (Table table : tableList) {
TableInfo tableInfo;
try {
tableInfo = schemas.getTableInfo(TableIdent.of(table, analysis.sessionContext().defaultSchema()));
} catch (ResourceUnknownException e) {
if (ignoreUnavailable) {
LOGGER.info("ignoring: {}", e.getMessage());
continue;
} else {
throw e;
}
}
if (!(tableInfo instanceof DocTableInfo)) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot create snapshot of tables in schema '%s'", tableInfo.ident().schema()));
}
Operation.blockedRaiseException(tableInfo, Operation.READ);
DocTableInfo docTableInfo = (DocTableInfo) tableInfo;
if (table.partitionProperties().isEmpty()) {
snapshotIndices.addAll(Arrays.asList(docTableInfo.concreteIndices()));
} else {
PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(docTableInfo, table.partitionProperties(), analysis.parameterContext().parameters());
if (!docTableInfo.partitions().contains(partitionName)) {
if (!ignoreUnavailable) {
throw new PartitionUnknownException(tableInfo.ident().fqn(), partitionName.ident());
} else {
LOGGER.info("ignoring unknown partition of table '{}' with ident '{}'", partitionName.tableIdent(), partitionName.ident());
}
} else {
snapshotIndices.add(partitionName.asIndexName());
}
}
}
/**
* For now, we always (in case there are indices to restore) include the globalMetaData,
* not only if one of the tables in the table list is partitioned.
* Previously we only included it in snapshots of full partitioned tables.
* However, to make restoring of shapshots of single partitions work
* we also need to include the global metadata (index templates).
*/
return CreateSnapshotAnalyzedStatement.forTables(snapshotId, settings, ImmutableList.copyOf(snapshotIndices), !snapshotIndices.isEmpty());
} else {
for (SchemaInfo schemaInfo : schemas) {
for (TableInfo tableInfo : schemaInfo) {
// only check for user generated tables
if (tableInfo instanceof DocTableInfo) {
Operation.blockedRaiseException(tableInfo, Operation.READ);
}
}
}
return CreateSnapshotAnalyzedStatement.all(snapshotId, settings);
}
}
use of io.crate.metadata.doc.DocTableInfo in project crate by crate.
the class DropTableAnalyzer method analyze.
public DropTableAnalyzedStatement analyze(DropTable node, @Nullable String defaultSchema) {
TableIdent tableIdent = TableIdent.of(node.table(), defaultSchema);
DocTableInfo tableInfo = null;
boolean isNoop = false;
try {
tableInfo = schemas.getDroppableTable(tableIdent);
} catch (ResourceUnknownException e) {
if (node.dropIfExists()) {
isNoop = true;
} else {
throw e;
}
}
if (!isNoop) {
Operation.blockedRaiseException(tableInfo, Operation.DROP);
}
return new DropTableAnalyzedStatement(tableInfo, isNoop, node.dropIfExists());
}
use of io.crate.metadata.doc.DocTableInfo in project crate by crate.
the class AlterTableOperation method executeAlterTable.
public CompletableFuture<Long> executeAlterTable(AlterTableAnalyzedStatement analysis) {
DocTableInfo table = analysis.table();
if (table.isAlias() && !table.isPartitioned()) {
return CompletableFutures.failedFuture(new AlterTableAliasException(table.ident().fqn()));
}
List<CompletableFuture<Long>> results = new ArrayList<>(3);
if (table.isPartitioned()) {
// create new filtered partition table settings
PartitionedTableParameterInfo tableSettingsInfo = (PartitionedTableParameterInfo) table.tableParameterInfo();
TableParameter parameterWithFilteredSettings = new TableParameter(analysis.tableParameter().settings(), tableSettingsInfo.partitionTableSettingsInfo().supportedInternalSettings());
Optional<PartitionName> partitionName = analysis.partitionName();
if (partitionName.isPresent()) {
String index = partitionName.get().asIndexName();
results.add(updateMapping(analysis.tableParameter().mappings(), index));
results.add(updateSettings(parameterWithFilteredSettings, index));
} else {
// template gets all changes unfiltered
results.add(updateTemplate(analysis.tableParameter(), table.ident()));
if (!analysis.excludePartitions()) {
String[] indices = table.concreteIndices();
results.add(updateMapping(analysis.tableParameter().mappings(), indices));
results.add(updateSettings(parameterWithFilteredSettings, indices));
}
}
} else {
results.add(updateMapping(analysis.tableParameter().mappings(), table.ident().indexName()));
results.add(updateSettings(analysis.tableParameter(), table.ident().indexName()));
}
final CompletableFuture<Long> result = new CompletableFuture<>();
applyMultiFutureCallback(result, results);
return result;
}
use of io.crate.metadata.doc.DocTableInfo in project crate by crate.
the class InsertFromSubQueryAnalyzer method analyze.
public AnalyzedStatement analyze(InsertFromSubquery node, Analysis analysis) {
DocTableInfo tableInfo = schemas.getWritableTable(TableIdent.of(node.table(), analysis.sessionContext().defaultSchema()));
Operation.blockedRaiseException(tableInfo, Operation.INSERT);
DocTableRelation tableRelation = new DocTableRelation(tableInfo);
FieldProvider fieldProvider = new NameFieldProvider(tableRelation);
QueriedRelation source = (QueriedRelation) relationAnalyzer.analyze(node.subQuery(), analysis);
List<Reference> targetColumns = new ArrayList<>(resolveTargetColumns(node.columns(), tableInfo, source.fields().size()));
validateColumnsAndAddCastsIfNecessary(targetColumns, source.querySpec());
Map<Reference, Symbol> onDuplicateKeyAssignments = null;
if (!node.onDuplicateKeyAssignments().isEmpty()) {
onDuplicateKeyAssignments = processUpdateAssignments(tableRelation, targetColumns, analysis.sessionContext(), analysis.parameterContext(), analysis.transactionContext(), fieldProvider, node.onDuplicateKeyAssignments());
}
return new InsertFromSubQueryAnalyzedStatement(source, tableInfo, targetColumns, onDuplicateKeyAssignments);
}
use of io.crate.metadata.doc.DocTableInfo in project crate by crate.
the class InsertFromValuesAnalyzer method analyze.
public AnalyzedStatement analyze(InsertFromValues node, Analysis analysis) {
DocTableInfo tableInfo = schemas.getWritableTable(TableIdent.of(node.table(), analysis.sessionContext().defaultSchema()));
Operation.blockedRaiseException(tableInfo, Operation.INSERT);
DocTableRelation tableRelation = new DocTableRelation(tableInfo);
FieldProvider fieldProvider = new NameFieldProvider(tableRelation);
Function<ParameterExpression, Symbol> convertParamFunction = analysis.parameterContext();
ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, fieldProvider, null);
ExpressionAnalysisContext expressionAnalysisContext = new ExpressionAnalysisContext();
expressionAnalyzer.setResolveFieldsOperation(Operation.INSERT);
ValuesResolver valuesResolver = new ValuesResolver(tableRelation);
ExpressionAnalyzer valuesAwareExpressionAnalyzer = new ValuesAwareExpressionAnalyzer(functions, analysis.sessionContext(), convertParamFunction, fieldProvider, valuesResolver);
InsertFromValuesAnalyzedStatement statement = new InsertFromValuesAnalyzedStatement(tableInfo, analysis.parameterContext().numBulkParams());
handleInsertColumns(node, node.maxValuesLength(), statement);
Set<Reference> allReferencedReferences = new HashSet<>();
for (GeneratedReference reference : tableInfo.generatedColumns()) {
allReferencedReferences.addAll(reference.referencedReferences());
}
ReferenceToLiteralConverter.Context referenceToLiteralContext = new ReferenceToLiteralConverter.Context(statement.columns(), allReferencedReferences);
ValueNormalizer valuesNormalizer = new ValueNormalizer(schemas);
EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.CLUSTER, ReplaceMode.COPY, null, tableRelation);
analyzeColumns(statement.tableInfo(), statement.columns());
for (ValuesList valuesList : node.valuesLists()) {
analyzeValues(tableRelation, valuesNormalizer, normalizer, expressionAnalyzer, expressionAnalysisContext, analysis.transactionContext(), valuesResolver, valuesAwareExpressionAnalyzer, valuesList, node.onDuplicateKeyAssignments(), statement, analysis.parameterContext(), referenceToLiteralContext);
}
return statement;
}
Aggregations