use of io.crate.metadata.Scalar in project crate by crate.
the class CurrentSchemaFunctionTest method testEvaluateCurrentSchemaNotSupported.
public void testEvaluateCurrentSchemaNotSupported() throws Exception {
expectedException.expect(UnsupportedOperationException.class);
expectedException.expectMessage("Cannot evaluate CURRENT_SCHEMA function.");
Function function = (Function) sqlExpressions.asSymbol("current_schema()");
Scalar impl = (Scalar) functions.get(function.info().ident());
impl.evaluate();
}
use of io.crate.metadata.Scalar in project crate by crate.
the class BaseImplementationSymbolVisitor method visitFunction.
@Override
public Input<?> visitFunction(Function function, C context) {
final FunctionImplementation functionImplementation = functions.get(function.info().ident());
if (functionImplementation instanceof Scalar<?, ?>) {
List<Symbol> arguments = function.arguments();
Scalar<?, ?> scalarImpl = ((Scalar) functionImplementation).compile(arguments);
Input[] argumentInputs = new Input[arguments.size()];
int i = 0;
for (Symbol argument : function.arguments()) {
argumentInputs[i++] = process(argument, context);
}
return new FunctionExpression<>(scalarImpl, argumentInputs);
} else {
throw new IllegalArgumentException(SymbolFormatter.format("Cannot find implementation for function %s", function));
}
}
use of io.crate.metadata.Scalar 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.metadata.Scalar in project crate by crate.
the class SimpleTopNProjectorTest method testFunctionExpression.
@Test
public void testFunctionExpression() throws Throwable {
Scalar floor = (Scalar) TestingHelpers.getFunctions().get(new FunctionIdent("floor", Collections.<DataType>singletonList(DataTypes.DOUBLE)));
FunctionExpression<Number, ?> funcExpr = new FunctionExpression<>(floor, new Input[] { input });
Projector projector = new SimpleTopNProjector(ImmutableList.<Input<?>>of(funcExpr), COLLECT_EXPRESSIONS, 10, TopN.NO_OFFSET);
Iterable<Row> rows = RowGenerator.fromSingleColValues(() -> IntStream.range(0, 12).mapToDouble(i -> 42.3d).iterator());
BatchIterator batchIterator = projector.apply(RowsBatchIterator.newInstance(rows, 1));
consumer.accept(batchIterator, null);
Bucket result = consumer.getBucket();
assertThat(result.size(), is(10));
assertThat(result.iterator().next(), isRow(42L));
}
use of io.crate.metadata.Scalar in project crate by crate.
the class AbstractTableFunctionsTest 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