Search in sources :

Example 11 with VisibleForTesting

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

the class AlterTableAddColumnPlan method bind.

@VisibleForTesting
public static BoundAddColumn bind(AnalyzedAlterTableAddColumn alterTable, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row params, SubQueryResults subQueryResults, FulltextAnalyzerResolver fulltextAnalyzerResolver) {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, params, subQueryResults);
    DocTableInfo tableInfo = alterTable.tableInfo();
    AnalyzedTableElements<Object> tableElements = alterTable.analyzedTableElements().map(eval);
    for (AnalyzedColumnDefinition<Object> column : tableElements.columns()) {
        ensureColumnLeafsAreNew(column, tableInfo);
    }
    addExistingPrimaryKeys(tableInfo, tableElements);
    ensureNoIndexDefinitions(tableElements.columns());
    addExistingCheckConstraints(tableInfo, tableElements);
    // validate table elements
    AnalyzedTableElements<Symbol> tableElementsUnboundWithExpressions = alterTable.analyzedTableElementsWithExpressions();
    Settings tableSettings = AnalyzedTableElements.validateAndBuildSettings(tableElements, fulltextAnalyzerResolver);
    Map<String, Object> mapping = AnalyzedTableElements.finalizeAndValidate(tableInfo.ident(), tableElementsUnboundWithExpressions, tableElements);
    int numCurrentPks = tableInfo.primaryKey().size();
    if (tableInfo.primaryKey().contains(DocSysColumns.ID)) {
        numCurrentPks -= 1;
    }
    boolean hasNewPrimaryKeys = AnalyzedTableElements.primaryKeys(tableElements).size() > numCurrentPks;
    boolean hasGeneratedColumns = tableElementsUnboundWithExpressions.hasGeneratedColumns();
    return new BoundAddColumn(tableInfo, tableElements, tableSettings, mapping, hasNewPrimaryKeys, hasGeneratedColumns);
}
Also used : AnalyzedColumnDefinition(io.crate.analyze.AnalyzedColumnDefinition) HashMap(java.util.HashMap) ObjectType(io.crate.types.ObjectType) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DependencyCarrier(io.crate.planner.DependencyCarrier) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Settings(org.elasticsearch.common.settings.Settings) Locale(java.util.Locale) Map(java.util.Map) OneRowActionListener(io.crate.execution.support.OneRowActionListener) AnalyzedAlterTableAddColumn(io.crate.analyze.AnalyzedAlterTableAddColumn) FulltextAnalyzerResolver(io.crate.metadata.FulltextAnalyzerResolver) LinkedHashSet(java.util.LinkedHashSet) DocSysColumns(io.crate.metadata.doc.DocSysColumns) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) BoundAddColumn(io.crate.analyze.BoundAddColumn) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) CheckConstraint(io.crate.sql.tree.CheckConstraint) RowConsumer(io.crate.data.RowConsumer) Objects(java.util.Objects) AnalyzedTableElements(io.crate.analyze.AnalyzedTableElements) List(java.util.List) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) DocTableInfo(io.crate.metadata.doc.DocTableInfo) Symbol(io.crate.expression.symbol.Symbol) CheckConstraint(io.crate.sql.tree.CheckConstraint) BoundAddColumn(io.crate.analyze.BoundAddColumn) Settings(org.elasticsearch.common.settings.Settings) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 12 with VisibleForTesting

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

the class UpdateSettingsPlan method buildSettingsFrom.

@VisibleForTesting
static Settings buildSettingsFrom(Collection<Assignment<Symbol>> assignments, Function<? super Symbol, Object> eval) {
    Settings.Builder settingsBuilder = Settings.builder();
    for (Assignment<Symbol> entry : assignments) {
        String settingsName = eval.apply(entry.columnName()).toString();
        if (CrateSettings.isValidSetting(settingsName) == false) {
            throw new IllegalArgumentException("Setting '" + settingsName + "' is not supported");
        }
        Symbol expression = Lists2.getOnlyElement(entry.expressions());
        Object value = eval.apply(expression);
        CrateSettings.flattenSettings(settingsBuilder, settingsName, value);
    }
    Settings settings = settingsBuilder.build();
    for (String checkForRuntime : settings.keySet()) {
        CrateSettings.checkIfRuntimeSetting(checkForRuntime);
    }
    return settings;
}
Also used : Symbol(io.crate.expression.symbol.Symbol) CrateSettings(io.crate.metadata.settings.CrateSettings) Settings(org.elasticsearch.common.settings.Settings) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 13 with VisibleForTesting

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

the class Session method singleExec.

@VisibleForTesting
CompletableFuture<?> singleExec(Portal portal, ResultReceiver<?> resultReceiver, int maxRows) {
    var activeConsumer = portal.activeConsumer();
    if (activeConsumer != null && activeConsumer.suspended()) {
        activeConsumer.replaceResultReceiver(resultReceiver, maxRows);
        activeConsumer.resume();
        return resultReceiver.completionFuture();
    }
    var jobId = UUIDs.dirtyUUID();
    var routingProvider = new RoutingProvider(Randomness.get().nextInt(), planner.getAwarenessAttributes());
    var clusterState = executor.clusterService().state();
    var txnCtx = new CoordinatorTxnCtx(sessionContext);
    var nodeCtx = executor.nodeContext();
    var params = new RowN(portal.params().toArray());
    var plannerContext = new PlannerContext(clusterState, routingProvider, jobId, txnCtx, nodeCtx, maxRows, params);
    var analyzedStmt = portal.analyzedStatement();
    String rawStatement = portal.preparedStmt().rawStatement();
    if (analyzedStmt == null) {
        String errorMsg = "Statement must have been analyzed: " + rawStatement;
        jobsLogs.logPreExecutionFailure(jobId, rawStatement, errorMsg, sessionContext.sessionUser());
        throw new IllegalStateException(errorMsg);
    }
    Plan plan;
    try {
        plan = planner.plan(analyzedStmt, plannerContext);
    } catch (Throwable t) {
        jobsLogs.logPreExecutionFailure(jobId, rawStatement, SQLExceptions.messageOf(t), sessionContext.sessionUser());
        throw t;
    }
    if (!analyzedStmt.isWriteOperation()) {
        resultReceiver = new RetryOnFailureResultReceiver(executor.clusterService(), clusterState, indexName -> executor.clusterService().state().metadata().hasIndex(indexName), resultReceiver, jobId, (newJobId, resultRec) -> retryQuery(newJobId, analyzedStmt, routingProvider, new RowConsumerToResultReceiver(resultRec, maxRows, new JobsLogsUpdateListener(newJobId, jobsLogs)), params, txnCtx, nodeCtx));
    }
    jobsLogs.logExecutionStart(jobId, rawStatement, sessionContext.sessionUser(), StatementClassifier.classify(plan));
    RowConsumerToResultReceiver consumer = new RowConsumerToResultReceiver(resultReceiver, maxRows, new JobsLogsUpdateListener(jobId, jobsLogs));
    portal.setActiveConsumer(consumer);
    plan.execute(executor, plannerContext, consumer, params, SubQueryResults.EMPTY);
    return resultReceiver.completionFuture();
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) RetryOnFailureResultReceiver(io.crate.protocols.postgres.RetryOnFailureResultReceiver) Analyzer(io.crate.analyze.Analyzer) DependencyCarrier(io.crate.planner.DependencyCarrier) ClusterState(org.elasticsearch.cluster.ClusterState) Relations(io.crate.analyze.Relations) RowN(io.crate.data.RowN) TransactionState(io.crate.protocols.postgres.TransactionState) Map(java.util.Map) JobsLogsUpdateListener(io.crate.protocols.postgres.JobsLogsUpdateListener) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) AnalyzedStatement(io.crate.analyze.AnalyzedStatement) UUIDs(org.elasticsearch.common.UUIDs) UUID(java.util.UUID) Lists2(io.crate.common.collections.Lists2) List(java.util.List) Logger(org.apache.logging.log4j.Logger) AnalyzedDiscard(io.crate.analyze.AnalyzedDiscard) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) AnalyzedBegin(io.crate.analyze.AnalyzedBegin) SubQueryResults(io.crate.planner.operators.SubQueryResults) Statement(io.crate.sql.tree.Statement) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) AnalyzedCommit(io.crate.analyze.AnalyzedCommit) AccessControl(io.crate.auth.AccessControl) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) JobsLogs(io.crate.execution.engine.collect.stats.JobsLogs) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Portal(io.crate.protocols.postgres.Portal) Symbols(io.crate.expression.symbol.Symbols) FormatCodes(io.crate.protocols.postgres.FormatCodes) SqlParser(io.crate.sql.parser.SqlParser) Nullable(javax.annotation.Nullable) Iterator(java.util.Iterator) RelationInfo(io.crate.metadata.RelationInfo) Target(io.crate.sql.tree.DiscardStatement.Target) DataType(io.crate.types.DataType) Planner(io.crate.planner.Planner) RoutingProvider(io.crate.metadata.RoutingProvider) RowConsumer(io.crate.data.RowConsumer) StatementClassifier(io.crate.planner.operators.StatementClassifier) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) ReadOnlyException(io.crate.exceptions.ReadOnlyException) SQLExceptions(io.crate.exceptions.SQLExceptions) LogManager(org.apache.logging.log4j.LogManager) Randomness(org.elasticsearch.common.Randomness) AnalyzedDeallocate(io.crate.analyze.AnalyzedDeallocate) RoutingProvider(io.crate.metadata.RoutingProvider) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) Plan(io.crate.planner.Plan) RowN(io.crate.data.RowN) PlannerContext(io.crate.planner.PlannerContext) JobsLogsUpdateListener(io.crate.protocols.postgres.JobsLogsUpdateListener) RetryOnFailureResultReceiver(io.crate.protocols.postgres.RetryOnFailureResultReceiver) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 14 with VisibleForTesting

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

the class FileWriterCountCollector method createJsonBuilder.

@VisibleForTesting
static XContentBuilder createJsonBuilder(OutputStream outputStream) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder(outputStream);
    builder.generator().configure(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM, false);
    return builder;
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 15 with VisibleForTesting

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

the class TransportAnalyzeAction method createTableStats.

@VisibleForTesting
static Stats createTableStats(Samples samples, List<Reference> primitiveColumns) {
    List<Row> records = samples.records;
    List<Object> columnValues = new ArrayList<>(records.size());
    Map<ColumnIdent, ColumnStats> statsByColumn = new HashMap<>(primitiveColumns.size());
    for (int i = 0; i < primitiveColumns.size(); i++) {
        Reference primitiveColumn = primitiveColumns.get(i);
        columnValues.clear();
        int nullCount = 0;
        for (Row record : records) {
            Object value = record.get(i);
            if (value == null) {
                nullCount++;
            } else {
                columnValues.add(value);
            }
        }
        @SuppressWarnings("unchecked") DataType<Object> dataType = (DataType<Object>) primitiveColumn.valueType();
        columnValues.sort(dataType::compare);
        ColumnStats<?> columnStats = ColumnStats.fromSortedValues(columnValues, dataType, nullCount, samples.numTotalDocs);
        statsByColumn.put(primitiveColumn.column(), columnStats);
    }
    return new Stats(samples.numTotalDocs, samples.numTotalSizeInBytes, statsByColumn);
}
Also used : HashMap(java.util.HashMap) Reference(io.crate.metadata.Reference) ArrayList(java.util.ArrayList) ColumnIdent(io.crate.metadata.ColumnIdent) DataType(io.crate.types.DataType) Row(io.crate.data.Row) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

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