Search in sources :

Example 21 with VisibleForTesting

use of io.crate.common.annotations.VisibleForTesting in project crate by crate.

the class RestoreSnapshotPlan method bind.

@VisibleForTesting
public static BoundRestoreSnapshot bind(AnalyzedRestoreSnapshot restoreSnapshot, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row parameters, SubQueryResults subQueryResults, Schemas schemas) {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, parameters, subQueryResults);
    Settings settings = GenericPropertiesConverter.genericPropertiesToSettings(restoreSnapshot.properties().map(eval), SnapshotSettings.SETTINGS);
    HashSet<BoundRestoreSnapshot.RestoreTableInfo> restoreTables = new HashSet<>(restoreSnapshot.tables().size());
    for (Table<Symbol> table : restoreSnapshot.tables()) {
        var relationName = RelationName.of(table.getName(), txnCtx.sessionContext().searchPath().currentSchema());
        try {
            DocTableInfo docTableInfo = schemas.getTableInfo(relationName, Operation.RESTORE_SNAPSHOT);
            if (table.partitionProperties().isEmpty()) {
                throw new RelationAlreadyExists(relationName);
            }
            var partitionName = toPartitionName(docTableInfo, Lists2.map(table.partitionProperties(), x -> x.map(eval)));
            if (docTableInfo.partitions().contains(partitionName)) {
                throw new PartitionAlreadyExistsException(partitionName);
            }
            restoreTables.add(new BoundRestoreSnapshot.RestoreTableInfo(relationName, partitionName));
        } catch (RelationUnknown | SchemaUnknownException e) {
            if (table.partitionProperties().isEmpty()) {
                restoreTables.add(new BoundRestoreSnapshot.RestoreTableInfo(relationName, null));
            } else {
                var partitionName = toPartitionName(relationName, Lists2.map(table.partitionProperties(), x -> x.map(eval)));
                restoreTables.add(new BoundRestoreSnapshot.RestoreTableInfo(relationName, partitionName));
            }
        }
    }
    return new BoundRestoreSnapshot(restoreSnapshot.repository(), restoreSnapshot.snapshot(), restoreTables, restoreSnapshot.includeTables(), restoreSnapshot.includeCustomMetadata(), restoreSnapshot.customMetadataTypes(), restoreSnapshot.includeGlobalSettings(), restoreSnapshot.globalSettings(), settings);
}
Also used : PartitionAlreadyExistsException(io.crate.exceptions.PartitionAlreadyExistsException) IndexParts(io.crate.metadata.IndexParts) RelationName(io.crate.metadata.RelationName) CompletableFuture(java.util.concurrent.CompletableFuture) Operation(io.crate.metadata.table.Operation) GetSnapshotsRequest(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) Function(java.util.function.Function) PartitionName(io.crate.metadata.PartitionName) ArrayList(java.util.ArrayList) DependencyCarrier(io.crate.planner.DependencyCarrier) HashSet(java.util.HashSet) WAIT_FOR_COMPLETION(io.crate.analyze.SnapshotSettings.WAIT_FOR_COMPLETION) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Settings(org.elasticsearch.common.settings.Settings) RestoreSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest) SchemaUnknownException(io.crate.exceptions.SchemaUnknownException) BoundRestoreSnapshot(io.crate.analyze.BoundRestoreSnapshot) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) GenericPropertiesConverter(io.crate.analyze.GenericPropertiesConverter) OneRowActionListener(io.crate.execution.support.OneRowActionListener) FutureActionListener(io.crate.action.FutureActionListener) PartitionPropertiesAnalyzer.toPartitionName(io.crate.analyze.PartitionPropertiesAnalyzer.toPartitionName) GetSnapshotsResponse(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) DocTableInfo(io.crate.metadata.doc.DocTableInfo) AnalyzedRestoreSnapshot(io.crate.analyze.AnalyzedRestoreSnapshot) NodeContext(io.crate.metadata.NodeContext) Table(io.crate.sql.tree.Table) Set(java.util.Set) Lists2(io.crate.common.collections.Lists2) SnapshotSettings(io.crate.analyze.SnapshotSettings) RowConsumer(io.crate.data.RowConsumer) List(java.util.List) Row(io.crate.data.Row) RelationAlreadyExists(io.crate.exceptions.RelationAlreadyExists) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) IGNORE_UNAVAILABLE(io.crate.analyze.SnapshotSettings.IGNORE_UNAVAILABLE) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) Schemas(io.crate.metadata.Schemas) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) RelationUnknown(io.crate.exceptions.RelationUnknown) TransportGetSnapshotsAction(org.elasticsearch.action.admin.cluster.snapshots.get.TransportGetSnapshotsAction) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) BoundRestoreSnapshot(io.crate.analyze.BoundRestoreSnapshot) DocTableInfo(io.crate.metadata.doc.DocTableInfo) RelationUnknown(io.crate.exceptions.RelationUnknown) Symbol(io.crate.expression.symbol.Symbol) SchemaUnknownException(io.crate.exceptions.SchemaUnknownException) RelationAlreadyExists(io.crate.exceptions.RelationAlreadyExists) Settings(org.elasticsearch.common.settings.Settings) SnapshotSettings(io.crate.analyze.SnapshotSettings) PartitionAlreadyExistsException(io.crate.exceptions.PartitionAlreadyExistsException) HashSet(java.util.HashSet) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 22 with VisibleForTesting

use of io.crate.common.annotations.VisibleForTesting in project crate by crate.

the class RestoreSnapshotPlan method resolveTableFromSnapshots.

@VisibleForTesting
static void resolveTableFromSnapshots(BoundRestoreSnapshot.RestoreTableInfo table, List<SnapshotInfo> snapshots, ResolveIndicesAndTemplatesContext context) throws RelationUnknown {
    String name = table.tableIdent().indexNameOrAlias();
    for (SnapshotInfo snapshot : snapshots) {
        for (String index : snapshot.indices()) {
            if (name.equals(index)) {
                context.addIndex(index);
                return;
            } else if (isIndexPartitionOfTable(index, table.tableIdent())) {
                String templateName = table.partitionTemplate();
                // add a partitions wildcard
                // to match all partitions if a partitioned table was meant
                context.addIndex(templateName + "*");
                context.addTemplate(templateName);
                return;
            }
        }
    }
    context.addTemplate(table.partitionTemplate());
}
Also used : SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 23 with VisibleForTesting

use of io.crate.common.annotations.VisibleForTesting in project crate by crate.

the class RestoreSnapshotPlan method resolveIndexNames.

@VisibleForTesting
static CompletableFuture<ResolveIndicesAndTemplatesContext> resolveIndexNames(String repositoryName, Set<BoundRestoreSnapshot.RestoreTableInfo> restoreTables, boolean ignoreUnavailable, TransportGetSnapshotsAction transportGetSnapshotsAction) {
    ResolveIndicesAndTemplatesContext context = new ResolveIndicesAndTemplatesContext();
    ArrayList<BoundRestoreSnapshot.RestoreTableInfo> toResolveFromSnapshot = new ArrayList<>();
    for (var table : restoreTables) {
        if (table.hasPartitionInfo()) {
            context.addIndex(table.partitionName().asIndexName());
            context.addTemplate(table.partitionTemplate());
        } else if (ignoreUnavailable) {
            // If ignoreUnavailable is true, it's cheaper to simply
            // return indexName and the partitioned wildcard instead
            // checking if it's a partitioned table or not
            context.addIndex(table.tableIdent().indexNameOrAlias());
            // For the case its a partitioned table we restore all partitions and the templates
            String templateName = table.partitionTemplate();
            context.addIndex(templateName + "*");
            context.addTemplate(templateName);
        } else {
            // index name needs to be resolved from snapshot
            toResolveFromSnapshot.add(table);
        }
    }
    if (toResolveFromSnapshot.isEmpty()) {
        return CompletableFuture.completedFuture(context);
    } else {
        FutureActionListener<GetSnapshotsResponse, ResolveIndicesAndTemplatesContext> listener = new FutureActionListener<>(response -> {
            resolveTablesFromSnapshots(toResolveFromSnapshot, response.getSnapshots(), context);
            return context;
        });
        transportGetSnapshotsAction.execute(new GetSnapshotsRequest(repositoryName), listener);
        return listener;
    }
}
Also used : GetSnapshotsResponse(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) GetSnapshotsRequest(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest) ArrayList(java.util.ArrayList) FutureActionListener(io.crate.action.FutureActionListener) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 24 with VisibleForTesting

use of io.crate.common.annotations.VisibleForTesting in project crate by crate.

the class WindowAgg method create.

@VisibleForTesting
public static LogicalPlan create(LogicalPlan source, List<WindowFunction> windowFunctions) {
    if (windowFunctions.isEmpty()) {
        return source;
    }
    LinkedHashMap<WindowDefinition, ArrayList<WindowFunction>> groupedFunctions = new LinkedHashMap<>();
    for (WindowFunction windowFunction : windowFunctions) {
        WindowDefinition windowDefinition = windowFunction.windowDefinition();
        ArrayList<WindowFunction> functions = groupedFunctions.computeIfAbsent(windowDefinition, w -> new ArrayList<>());
        functions.add(windowFunction);
    }
    LogicalPlan lastWindowAgg = source;
    for (Map.Entry<WindowDefinition, ArrayList<WindowFunction>> entry : groupedFunctions.entrySet()) {
        /*
             * Pass along the source outputs as standalone symbols as they might be required in cases like:
             *      select x, avg(x) OVER() from t;
             */
        ArrayList<WindowFunction> functions = entry.getValue();
        WindowDefinition windowDefinition = entry.getKey();
        OrderBy orderBy = windowDefinition.orderBy();
        if (orderBy == null || lastWindowAgg.outputs().containsAll(orderBy.orderBySymbols())) {
            lastWindowAgg = new WindowAgg(lastWindowAgg, windowDefinition, functions, lastWindowAgg.outputs());
        } else {
            // ``WindowProjector.createUpdateProbeValueFunction` expects that all OrderBY symbols are `InputColumn`
            // Here we have a case where there is a function or something in the orderBy expression that is *not*
            // already provided by the source.
            // -> Inject `eval` so that the `orderBy` of the window-function will turn into a `InputColumn`
            Eval eval = new Eval(lastWindowAgg, Lists2.concatUnique(lastWindowAgg.outputs(), orderBy.orderBySymbols()));
            lastWindowAgg = new WindowAgg(eval, windowDefinition, functions, eval.outputs());
        }
    }
    return lastWindowAgg;
}
Also used : OrderBy(io.crate.analyze.OrderBy) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) WindowFunction(io.crate.expression.symbol.WindowFunction) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) WindowDefinition(io.crate.analyze.WindowDefinition) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 25 with VisibleForTesting

use of io.crate.common.annotations.VisibleForTesting in project crate by crate.

the class S3FileInput method toPreGlobUri.

@VisibleForTesting
@Nullable
static S3URI toPreGlobUri(S3URI uri) {
    Matcher hasGlobMatcher = HAS_GLOBS_PATTERN.matcher(uri.toString());
    S3URI preGlobUri = null;
    if (hasGlobMatcher.matches()) {
        preGlobUri = S3URI.toS3URI(URI.create(hasGlobMatcher.group(1)));
    }
    return preGlobUri;
}
Also used : Matcher(java.util.regex.Matcher) S3URI(io.crate.copy.s3.common.S3URI) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Aggregations

VisibleForTesting (io.crate.common.annotations.VisibleForTesting)36 Row (io.crate.data.Row)12 Symbol (io.crate.expression.symbol.Symbol)12 SubQueryResults (io.crate.planner.operators.SubQueryResults)11 Settings (org.elasticsearch.common.settings.Settings)11 RowConsumer (io.crate.data.RowConsumer)10 NodeContext (io.crate.metadata.NodeContext)10 DependencyCarrier (io.crate.planner.DependencyCarrier)10 Plan (io.crate.planner.Plan)10 PlannerContext (io.crate.planner.PlannerContext)10 Function (java.util.function.Function)10 SymbolEvaluator (io.crate.analyze.SymbolEvaluator)9 Row1 (io.crate.data.Row1)9 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)9 ArrayList (java.util.ArrayList)8 OneRowActionListener (io.crate.execution.support.OneRowActionListener)7 Map (java.util.Map)7 ColumnIdent (io.crate.metadata.ColumnIdent)6 DocTableInfo (io.crate.metadata.doc.DocTableInfo)5 Nullable (javax.annotation.Nullable)5