Search in sources :

Example 1 with DocTableInfo

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);
    }
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) Table(io.crate.sql.tree.Table) QualifiedName(io.crate.sql.tree.QualifiedName) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) PartitionName(io.crate.metadata.PartitionName) SnapshotId(org.elasticsearch.cluster.metadata.SnapshotId) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Settings(org.elasticsearch.common.settings.Settings) SchemaInfo(io.crate.metadata.table.SchemaInfo)

Example 2 with DocTableInfo

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());
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableIdent(io.crate.metadata.TableIdent) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException)

Example 3 with DocTableInfo

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;
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableParameter(io.crate.analyze.TableParameter) PartitionedTableParameterInfo(io.crate.analyze.PartitionedTableParameterInfo) PartitionName(io.crate.metadata.PartitionName) CompletableFuture(java.util.concurrent.CompletableFuture) AlterTableAliasException(io.crate.exceptions.AlterTableAliasException)

Example 4 with DocTableInfo

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);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) DynamicReference(io.crate.analyze.symbol.DynamicReference) Symbol(io.crate.analyze.symbol.Symbol)

Example 5 with DocTableInfo

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;
}
Also used : ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) DocTableInfo(io.crate.metadata.doc.DocTableInfo) ValueNormalizer(io.crate.analyze.expressions.ValueNormalizer) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Symbol(io.crate.analyze.symbol.Symbol) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) FieldProvider(io.crate.analyze.relations.FieldProvider) NameFieldProvider(io.crate.analyze.relations.NameFieldProvider) NameFieldProvider(io.crate.analyze.relations.NameFieldProvider) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Aggregations

DocTableInfo (io.crate.metadata.doc.DocTableInfo)127 Test (org.junit.Test)56 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)40 Symbol (io.crate.expression.symbol.Symbol)27 Reference (io.crate.metadata.Reference)27 SQLExecutor (io.crate.testing.SQLExecutor)25 RelationName (io.crate.metadata.RelationName)24 DocTableRelation (io.crate.analyze.relations.DocTableRelation)20 ColumnIdent (io.crate.metadata.ColumnIdent)20 TableInfo (io.crate.metadata.table.TableInfo)18 Assignments (io.crate.expression.symbol.Assignments)16 Row (io.crate.data.Row)14 PlannerContext (io.crate.planner.PlannerContext)13 Before (org.junit.Before)13 RowConsumer (io.crate.data.RowConsumer)12 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)12 PartitionName (io.crate.metadata.PartitionName)12 DependencyCarrier (io.crate.planner.DependencyCarrier)12 ArrayList (java.util.ArrayList)12 Map (java.util.Map)12