use of io.crate.expression.symbol.Function in project crate by crate.
the class HandlerSideLevelCollectTest method testInformationSchemaTables.
@Test
public void testInformationSchemaTables() throws Exception {
InformationSchemaInfo schemaInfo = internalCluster().getInstance(InformationSchemaInfo.class);
TableInfo tablesTableInfo = schemaInfo.getTableInfo("tables");
Routing routing = tablesTableInfo.getRouting(clusterService().state(), routingProvider, WhereClause.MATCH_ALL, RoutingProvider.ShardSelection.ANY, SessionContext.systemSessionContext());
List<Symbol> toCollect = new ArrayList<>();
for (Reference reference : tablesTableInfo.columns()) {
toCollect.add(reference);
}
Symbol tableNameRef = toCollect.get(12);
List<Symbol> arguments = Arrays.asList(tableNameRef, Literal.of("shards"));
FunctionImplementation eqImpl = functions.get(null, EqOperator.NAME, arguments, SearchPath.pathWithPGCatalogAndDoc());
Function whereClause = new Function(eqImpl.signature(), arguments, EqOperator.RETURN_TYPE);
RoutedCollectPhase collectNode = collectNode(routing, toCollect, RowGranularity.DOC, new WhereClause(whereClause));
Bucket result = collect(collectNode);
assertThat(TestingHelpers.printedTable(result), is("NULL| NULL| NULL| strict| NULL| NULL| NULL| SYSTEM GENERATED| NULL| NULL| NULL| sys| shards| sys| BASE TABLE| NULL\n"));
}
use of io.crate.expression.symbol.Function in project crate by crate.
the class SimplifyEqualsOperationOnIdenticalReferencesTest method testSimplifyRefEqRefWhenRefIsNotNullable.
@Test
public void testSimplifyRefEqRefWhenRefIsNotNullable() {
Function functionToOptimize = new Function(EqOperator.SIGNATURE, List.of(NOT_NULLABLE_REF, NOT_NULLABLE_REF), DataTypes.BOOLEAN);
Match<Function> match = RULE.pattern().accept(functionToOptimize, Captures.empty());
assertTrue(match.isPresent());
// function to optimize has no parent
Symbol optimizedFunction = RULE.apply(match.value(), match.captures(), NODE_CONTEXT, null);
assertThat(optimizedFunction, is(Literal.BOOLEAN_TRUE));
// function to optimize has a parent
Function dummyParent = new Function(NotPredicate.SIGNATURE, List.of(functionToOptimize), DataTypes.BOOLEAN);
optimizedFunction = RULE.apply(match.value(), match.captures(), NODE_CONTEXT, dummyParent);
assertThat(optimizedFunction, is(Literal.BOOLEAN_TRUE));
}
use of io.crate.expression.symbol.Function in project crate by crate.
the class SimplifyEqualsOperationOnIdenticalReferencesTest method testSimplifyRefEqRefWhenParentIsIgnore3vlFunction.
@Test
public void testSimplifyRefEqRefWhenParentIsIgnore3vlFunction() {
Function func = new Function(Ignore3vlFunction.SIGNATURE, List.of(new Function(EqOperator.SIGNATURE, List.of(NULLABLE_REF, NULLABLE_REF), DataTypes.BOOLEAN)), DataTypes.BOOLEAN);
Match<Function> match = RULE.pattern().accept(func.arguments().get(0), Captures.empty());
assertTrue(match.isPresent());
Symbol optimizedFunction = RULE.apply(match.value(), match.captures(), NODE_CONTEXT, func);
assertThat(optimizedFunction, is(Literal.BOOLEAN_TRUE));
}
use of io.crate.expression.symbol.Function 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.expression.symbol.Function in project crate by crate.
the class ValuesFunctionTest method test_function_return_type_of_the_next_nested_item.
@Test
public void test_function_return_type_of_the_next_nested_item() {
Function function = (Function) sqlExpressions.asSymbol("_values([['a', 'b']])");
var funcImplementation = (TableFunctionImplementation<?>) sqlExpressions.nodeCtx.functions().getQualified(function, txnCtx.sessionSettings().searchPath());
assertThat(funcImplementation.returnType(), instanceOf(RowType.class));
assertThat(funcImplementation.returnType().fieldTypes(), contains(DataTypes.STRING_ARRAY));
}
Aggregations