use of io.crate.expression.symbol.Function in project crate by crate.
the class InputColumns method visitFunction.
@Override
public Symbol visitFunction(Function symbol, final SourceSymbols sourceSymbols) {
Symbol replacement = getFunctionReplacementOrNull(symbol, sourceSymbols);
if (replacement != null) {
return replacement;
}
ArrayList<Symbol> replacedFunctionArgs = getProcessedArgs(symbol.arguments(), sourceSymbols);
return new Function(symbol.signature(), replacedFunctionArgs, symbol.valueType());
}
use of io.crate.expression.symbol.Function in project crate by crate.
the class ProjectionBuilder method getAggregations.
private ArrayList<Aggregation> getAggregations(Collection<Function> functions, AggregateMode mode, InputColumns.SourceSymbols sourceSymbols, SearchPath searchPath, java.util.function.Function<Symbol, Symbol> subQueryAndParamBinder) {
ArrayList<Aggregation> aggregations = new ArrayList<>(functions.size());
for (Function function : functions) {
assert function.type() == FunctionType.AGGREGATE : "function type must be " + FunctionType.AGGREGATE;
List<Symbol> aggregationInputs;
Symbol filterInput;
switch(mode) {
case ITER_FINAL:
case ITER_PARTIAL:
// ITER means that there is no aggregation part upfront, therefore the input
// symbols need to be in arguments
aggregationInputs = InputColumns.create(function.arguments(), sourceSymbols);
Symbol filter = function.filter();
if (filter != null) {
filterInput = InputColumns.create(filter, sourceSymbols);
} else {
filterInput = Literal.BOOLEAN_TRUE;
}
break;
case PARTIAL_FINAL:
aggregationInputs = List.of(sourceSymbols.getICForSource(function));
filterInput = Literal.BOOLEAN_TRUE;
break;
default:
throw new AssertionError("Invalid mode: " + mode.name());
}
AggregationFunction<?, ?> aggregationFunction = (AggregationFunction<?, ?>) nodeCtx.functions().getQualified(function, searchPath);
assert aggregationFunction != null : "Aggregation function implementation not found using full qualified lookup: " + function;
var valueType = mode.returnType(aggregationFunction);
var functionInfo = FunctionInfo.of(aggregationFunction.signature(), aggregationFunction.boundSignature().getArgumentDataTypes(), valueType);
Aggregation aggregation = new Aggregation(aggregationFunction.signature(), functionInfo, aggregationFunction.boundSignature().getReturnType().createType(), valueType, Lists2.map(aggregationInputs, subQueryAndParamBinder), subQueryAndParamBinder.apply(filterInput));
aggregations.add(aggregation);
}
return aggregations;
}
use of io.crate.expression.symbol.Function in project crate by crate.
the class FilterProjectionTest method testStreaming.
@Test
public void testStreaming() throws Exception {
var eqFunction = new Function(EqOperator.SIGNATURE, List.of(new InputColumn(0, DataTypes.INTEGER), new InputColumn(1, DataTypes.INTEGER)), EqOperator.RETURN_TYPE);
FilterProjection p = new FilterProjection(eqFunction, Collections.singletonList(new InputColumn(0)));
p.requiredGranularity(RowGranularity.SHARD);
BytesStreamOutput out = new BytesStreamOutput();
Projection.toStream(p, out);
StreamInput in = out.bytes().streamInput();
FilterProjection p2 = (FilterProjection) Projection.fromStream(in);
assertEquals(p, p2);
}
use of io.crate.expression.symbol.Function in project crate by crate.
the class ScalarTestCase method assertNormalize.
public void assertNormalize(String functionExpression, Matcher<? super Symbol> expectedSymbol, boolean evaluate) {
// Explicit normalization happens further below
sqlExpressions.context().allowEagerNormalize(false);
Symbol functionSymbol = sqlExpressions.asSymbol(functionExpression);
if (functionSymbol instanceof Literal) {
assertThat(functionSymbol, expectedSymbol);
return;
}
Function function = (Function) functionSymbol;
FunctionImplementation impl = sqlExpressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
assertThat("Function implementation not found using full qualified lookup", impl, Matchers.notNullValue());
Symbol normalized = sqlExpressions.normalize(function);
assertThat(String.format(Locale.ENGLISH, "expected <%s> to normalize to %s", functionExpression, expectedSymbol), normalized, expectedSymbol);
if (evaluate && normalized instanceof Input && allArgsAreInputs(function.arguments())) {
Input[] inputs = new Input[function.arguments().size()];
for (int i = 0; i < inputs.length; i++) {
inputs[i] = ((Input) function.arguments().get(i));
}
Object expectedValue = ((Input) normalized).value();
assertThat(((Scalar) impl).evaluate(txnCtx, null, inputs), is(expectedValue));
assertThat(((Scalar) impl).compile(function.arguments()).evaluate(txnCtx, sqlExpressions.nodeCtx, inputs), is(expectedValue));
}
}
use of io.crate.expression.symbol.Function in project crate by crate.
the class ScalarTestCase method assertCompile.
public void assertCompile(String functionExpression, java.util.function.Function<Scalar, Matcher<Scalar>> matcher) {
Symbol functionSymbol = sqlExpressions.asSymbol(functionExpression);
functionSymbol = sqlExpressions.normalize(functionSymbol);
assertThat("function expression was normalized, compile would not be hit", functionSymbol, not(instanceOf(Literal.class)));
Function function = (Function) functionSymbol;
Scalar scalar = (Scalar) sqlExpressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
assertThat("Function implementation not found using full qualified lookup", scalar, Matchers.notNullValue());
Scalar compiled = scalar.compile(function.arguments());
assertThat(compiled, matcher.apply(scalar));
}
Aggregations