Search in sources :

Example 11 with CollectExpression

use of io.crate.execution.engine.collect.CollectExpression in project crate by crate.

the class ScalarTestCase method assertEvaluate.

/**
 * asserts that the given functionExpression matches the given matcher.
 * If the functionExpression contains references the inputs will be used in the order the references appear.
 * <p>
 * E.g.
 * <code>
 * assertEvaluate("foo(name, age)", anyOf("expectedValue1", "expectedValue2"), inputForName, inputForAge)
 * </code>
 */
@SuppressWarnings("unchecked")
public <T> void assertEvaluate(String functionExpression, Matcher<T> expectedValue, Literal<?>... literals) {
    if (expectedValue == null) {
        expectedValue = (Matcher<T>) nullValue();
    }
    sqlExpressions.context().allowEagerNormalize(true);
    Symbol functionSymbol = sqlExpressions.asSymbol(functionExpression);
    functionSymbol = sqlExpressions.normalize(functionSymbol);
    if (functionSymbol instanceof Literal) {
        Object value = ((Literal) functionSymbol).value();
        assertThat((T) value, expectedValue);
        return;
    }
    LinkedList<Literal<?>> unusedLiterals = new LinkedList<>(Arrays.asList(literals));
    Function function = (Function) RefReplacer.replaceRefs(functionSymbol, r -> {
        if (unusedLiterals.isEmpty()) {
            throw new IllegalArgumentException("No value literal for reference=" + r + ", please add more literals");
        }
        // Can be null.
        Literal<?> literal = unusedLiterals.pollFirst();
        return literal;
    });
    if (unusedLiterals.size() == literals.length) {
        // Currently it's supposed that literals will be either references or parameters.
        // One of replaceRefs and bindParameters does nothing and doesn't consume unusedLiterals.
        function = (Function) ParameterBinder.bindParameters(function, p -> {
            if (unusedLiterals.isEmpty()) {
                throw new IllegalArgumentException("No value literal for parameter=" + p + ", please add more literals");
            }
            // Can be null.
            Literal<?> literal = unusedLiterals.pollFirst();
            return literal;
        });
    }
    Scalar scalar = (Scalar) sqlExpressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
    assertThat("Function implementation not found using full qualified lookup", scalar, Matchers.notNullValue());
    AssertMax1ValueCallInput[] arguments = new AssertMax1ValueCallInput[function.arguments().size()];
    InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(txnCtx);
    for (int i = 0; i < function.arguments().size(); i++) {
        Symbol arg = function.arguments().get(i);
        Input<?> input = ctx.add(arg);
        arguments[i] = new AssertMax1ValueCallInput(input);
    }
    Object actualValue = scalar.compile(function.arguments()).evaluate(txnCtx, sqlExpressions.nodeCtx, (Input[]) arguments);
    assertThat((T) actualValue, expectedValue);
    // Reset calls
    for (AssertMax1ValueCallInput argument : arguments) {
        argument.calls = 0;
    }
    actualValue = scalar.evaluate(txnCtx, sqlExpressions.nodeCtx, arguments);
    assertThat((T) actualValue, expectedValue);
}
Also used : Input(io.crate.data.Input) TransactionContext(io.crate.metadata.TransactionContext) SqlExpressions(io.crate.testing.SqlExpressions) InputColumn(io.crate.expression.symbol.InputColumn) Arrays(java.util.Arrays) ParameterBinder(io.crate.expression.symbol.ParameterBinder) RelationName(io.crate.metadata.RelationName) SessionSettings(io.crate.metadata.settings.SessionSettings) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Matchers.not(org.hamcrest.Matchers.not) SearchPath(io.crate.metadata.SearchPath) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RefReplacer(io.crate.expression.symbol.RefReplacer) Locale(java.util.Locale) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Is.is(org.hamcrest.core.Is.is) SQLExecutor(io.crate.testing.SQLExecutor) LinkedList(java.util.LinkedList) Before(org.junit.Before) DocTableInfo(io.crate.metadata.doc.DocTableInfo) DataType(io.crate.types.DataType) Matchers(org.hamcrest.Matchers) Function(io.crate.expression.symbol.Function) Lists2(io.crate.common.collections.Lists2) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Row(io.crate.data.Row) Literal(io.crate.expression.symbol.Literal) Symbol(io.crate.expression.symbol.Symbol) FunctionImplementation(io.crate.metadata.FunctionImplementation) DocSchemaInfo(io.crate.metadata.doc.DocSchemaInfo) Matcher(org.hamcrest.Matcher) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) InputFactory(io.crate.expression.InputFactory) Scalar(io.crate.metadata.Scalar) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) InputFactory(io.crate.expression.InputFactory) Symbol(io.crate.expression.symbol.Symbol) CollectExpression(io.crate.execution.engine.collect.CollectExpression) LinkedList(java.util.LinkedList) Scalar(io.crate.metadata.Scalar) Function(io.crate.expression.symbol.Function) Input(io.crate.data.Input) Literal(io.crate.expression.symbol.Literal)

Example 12 with CollectExpression

use of io.crate.execution.engine.collect.CollectExpression in project crate by crate.

the class ProjectionToProjectorVisitor method visitOrderedTopN.

@Override
public Projector visitOrderedTopN(OrderedTopNProjection projection, Context context) {
    /* OrderBy symbols are added to the rows to enable sorting on them post-collect. E.g.:
         *
         * outputs: [x]
         * orderBy: [y]
         *
         * topLevelInputs: [x, y]
         *                    /
         * orderByIndices: [1]
         */
    InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(context.txnCtx);
    ctx.add(projection.outputs());
    ctx.add(projection.orderBy());
    int numOutputs = projection.outputs().size();
    List<Input<?>> inputs = ctx.topLevelInputs();
    int[] orderByIndices = new int[inputs.size() - numOutputs];
    int idx = 0;
    for (int i = numOutputs; i < inputs.size(); i++) {
        orderByIndices[idx++] = i;
    }
    // priority queues implementation are backed by an arrayList
    int rowMemoryOverhead = 32;
    RowCellsAccountingWithEstimators rowAccounting = new RowCellsAccountingWithEstimators(Symbols.typeView(Lists2.concat(projection.outputs(), projection.orderBy())), context.ramAccounting, rowMemoryOverhead);
    if (projection.limit() > TopN.NO_LIMIT) {
        return new SortingTopNProjector(rowAccounting, inputs, ctx.expressions(), numOutputs, OrderingByPosition.arrayOrdering(orderByIndices, projection.reverseFlags(), projection.nullsFirst()), projection.limit(), projection.offset(), UNBOUNDED_COLLECTOR_THRESHOLD);
    }
    return new SortingProjector(rowAccounting, inputs, ctx.expressions(), numOutputs, OrderingByPosition.arrayOrdering(orderByIndices, projection.reverseFlags(), projection.nullsFirst()), projection.offset());
}
Also used : SortingTopNProjector(io.crate.execution.engine.sort.SortingTopNProjector) InputFactory(io.crate.expression.InputFactory) RowCellsAccountingWithEstimators(io.crate.breaker.RowCellsAccountingWithEstimators) Input(io.crate.data.Input) SortingProjector(io.crate.execution.engine.sort.SortingProjector) NestableCollectExpression(io.crate.execution.engine.collect.NestableCollectExpression) CollectExpression(io.crate.execution.engine.collect.CollectExpression)

Example 13 with CollectExpression

use of io.crate.execution.engine.collect.CollectExpression in project crate by crate.

the class ProjectionToProjectorVisitor method visitWriterProjection.

@Override
public Projector visitWriterProjection(WriterProjection projection, Context context) {
    InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(context.txnCtx);
    List<Input<?>> inputs = null;
    if (!projection.inputs().isEmpty()) {
        ctx.add(projection.inputs());
        inputs = ctx.topLevelInputs();
    }
    projection = projection.normalize(normalizer, context.txnCtx);
    String uri = DataTypes.STRING.sanitizeValue(SymbolEvaluator.evaluate(context.txnCtx, nodeCtx, projection.uri(), Row.EMPTY, SubQueryResults.EMPTY));
    assert uri != null : "URI must not be null";
    StringBuilder sb = new StringBuilder(uri);
    Symbol resolvedFileName = normalizer.normalize(WriterProjection.DIRECTORY_TO_FILENAME, context.txnCtx);
    assert resolvedFileName instanceof Literal : "resolvedFileName must be a Literal, but is: " + resolvedFileName;
    assert resolvedFileName.valueType().id() == StringType.ID : "resolvedFileName.valueType() must be " + StringType.INSTANCE;
    String fileName = (String) ((Literal) resolvedFileName).value();
    if (!uri.endsWith("/")) {
        sb.append("/");
    }
    sb.append(fileName);
    if (projection.compressionType() == WriterProjection.CompressionType.GZIP) {
        sb.append(".gz");
    }
    uri = sb.toString();
    Map<ColumnIdent, Object> overwrites = symbolMapToObject(projection.overwrites(), ctx, context.txnCtx);
    return new FileWriterProjector(threadPool.generic(), uri, projection.compressionType(), inputs, ctx.expressions(), overwrites, projection.outputNames(), projection.outputFormat(), fileOutputFactoryMap, projection.withClauseOptions());
}
Also used : InputFactory(io.crate.expression.InputFactory) Symbol(io.crate.expression.symbol.Symbol) NestableCollectExpression(io.crate.execution.engine.collect.NestableCollectExpression) CollectExpression(io.crate.execution.engine.collect.CollectExpression) ColumnIdent(io.crate.metadata.ColumnIdent) Input(io.crate.data.Input) FileWriterProjector(io.crate.execution.engine.export.FileWriterProjector) Literal(io.crate.expression.symbol.Literal)

Example 14 with CollectExpression

use of io.crate.execution.engine.collect.CollectExpression in project crate by crate.

the class ProjectionToProjectorVisitor method visitProjectSet.

@Override
public Projector visitProjectSet(ProjectSetProjection projectSet, Context context) {
    ArrayList<Input<Iterable<Row>>> tableFunctions = new ArrayList<>(projectSet.tableFunctions().size());
    InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(context.txnCtx);
    for (int i = 0; i < projectSet.tableFunctions().size(); i++) {
        Symbol tableFunction = projectSet.tableFunctions().get(i);
        Input<Iterable<Row>> implementation = (Input<Iterable<Row>>) ctx.add(tableFunction);
        tableFunctions.add(implementation);
    }
    ctx.add(projectSet.standalone());
    TableFunctionApplier tableFunctionApplier = new TableFunctionApplier(tableFunctions, ctx.topLevelInputs(), ctx.expressions());
    return new FlatMapProjector(tableFunctionApplier);
}
Also used : InputFactory(io.crate.expression.InputFactory) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) NestableCollectExpression(io.crate.execution.engine.collect.NestableCollectExpression) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Input(io.crate.data.Input) Row(io.crate.data.Row)

Example 15 with CollectExpression

use of io.crate.execution.engine.collect.CollectExpression in project crate by crate.

the class ProjectionToProjectorVisitor method visitColumnIndexWriterProjection.

@Override
public Projector visitColumnIndexWriterProjection(ColumnIndexWriterProjection projection, Context context) {
    InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(context.txnCtx);
    List<Input<?>> partitionedByInputs = new ArrayList<>(projection.partitionedBySymbols().size());
    for (Symbol partitionedBySymbol : projection.partitionedBySymbols()) {
        partitionedByInputs.add(ctx.add(partitionedBySymbol));
    }
    List<Input<?>> insertInputs = new ArrayList<>(projection.columnSymbolsExclPartition().size());
    for (Symbol symbol : projection.columnSymbolsExclPartition()) {
        insertInputs.add(ctx.add(symbol));
    }
    ClusterState state = clusterService.state();
    Settings tableSettings = TableSettingsResolver.get(state.getMetadata(), projection.tableIdent(), !projection.partitionedBySymbols().isEmpty());
    int targetTableNumShards = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.get(tableSettings);
    int targetTableNumReplicas = NumberOfReplicas.fromSettings(tableSettings, state.getNodes().getSize());
    return new ColumnIndexWriterProjector(clusterService, nodeJobsCounter, circuitBreakerService.getBreaker(HierarchyCircuitBreakerService.QUERY), context.ramAccounting, threadPool.scheduler(), threadPool.executor(ThreadPool.Names.SEARCH), context.txnCtx, nodeCtx, state.metadata().settings(), targetTableNumShards, targetTableNumReplicas, IndexNameResolver.create(projection.tableIdent(), projection.partitionIdent(), partitionedByInputs), transportActionProvider, projection.primaryKeys(), projection.ids(), projection.clusteredBy(), projection.clusteredByIdent(), projection.columnReferencesExclPartition(), insertInputs, ctx.expressions(), projection.isIgnoreDuplicateKeys(), projection.onDuplicateKeyAssignments(), projection.bulkActions(), projection.autoCreateIndices(), projection.returnValues(), context.jobId);
}
Also used : InputFactory(io.crate.expression.InputFactory) ClusterState(org.elasticsearch.cluster.ClusterState) Input(io.crate.data.Input) ColumnIndexWriterProjector(io.crate.execution.engine.indexing.ColumnIndexWriterProjector) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) NestableCollectExpression(io.crate.execution.engine.collect.NestableCollectExpression) CollectExpression(io.crate.execution.engine.collect.CollectExpression) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

CollectExpression (io.crate.execution.engine.collect.CollectExpression)21 Input (io.crate.data.Input)15 InputFactory (io.crate.expression.InputFactory)12 Symbol (io.crate.expression.symbol.Symbol)12 ArrayList (java.util.ArrayList)11 Row (io.crate.data.Row)9 InputColumn (io.crate.expression.symbol.InputColumn)8 RowN (io.crate.data.RowN)7 Reference (io.crate.metadata.Reference)7 List (java.util.List)6 ClusterState (org.elasticsearch.cluster.ClusterState)6 InputCollectExpression (io.crate.execution.engine.collect.InputCollectExpression)5 NestableCollectExpression (io.crate.execution.engine.collect.NestableCollectExpression)5 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)5 OrderBy (io.crate.analyze.OrderBy)4 RamAccounting (io.crate.breaker.RamAccounting)4 InMemoryBatchIterator (io.crate.data.InMemoryBatchIterator)4 Function (io.crate.expression.symbol.Function)4 DataType (io.crate.types.DataType)4 Test (org.junit.Test)4