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);
}
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());
}
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());
}
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);
}
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);
}
Aggregations