use of io.crate.metadata.SearchPath 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.SearchPath in project crate by crate.
the class RelationAnalyzer method visitTable.
@Override
protected AnalyzedRelation visitTable(Table<?> node, StatementAnalysisContext context) {
QualifiedName tableQualifiedName = node.getName();
SearchPath searchPath = context.sessionContext().searchPath();
AnalyzedRelation relation;
TableInfo tableInfo;
try {
tableInfo = schemas.resolveTableInfo(tableQualifiedName, context.currentOperation(), context.sessionContext().sessionUser(), searchPath);
if (tableInfo instanceof DocTableInfo) {
// Dispatching of doc relations is based on the returned class of the schema information.
relation = new DocTableRelation((DocTableInfo) tableInfo);
} else {
relation = new TableRelation(tableInfo);
}
} catch (RelationUnknown e) {
Tuple<ViewMetadata, RelationName> viewMetadata;
try {
viewMetadata = schemas.resolveView(tableQualifiedName, searchPath);
} catch (RelationUnknown e1) {
// don't shadow original exception, as looking for the view is just a fallback
throw e;
}
ViewMetadata view = viewMetadata.v1();
AnalyzedRelation resolvedView = SqlParser.createStatement(view.stmt()).accept(this, context);
relation = new AnalyzedView(viewMetadata.v2(), view.owner(), resolvedView);
}
context.currentRelationContext().addSourceRelation(relation);
return relation;
}
use of io.crate.metadata.SearchPath in project crate by crate.
the class SwapTableAnalyzer method analyze.
public AnalyzedSwapTable analyze(SwapTable<Expression> swapTable, CoordinatorTxnCtx txnCtx, ParamTypeHints typeHints) {
HashMap<String, Expression> properties = new HashMap<>(swapTable.properties().properties());
Expression dropSourceExpr = properties.remove(DROP_SOURCE);
if (!properties.isEmpty()) {
throw new IllegalArgumentException("Invalid options for ALTER CLUSTER SWAP TABLE: " + String.join(", ", properties.keySet()));
}
Symbol dropSource;
if (dropSourceExpr == null) {
dropSource = Literal.BOOLEAN_FALSE;
} else {
ExpressionAnalyzer exprAnalyzer = new ExpressionAnalyzer(txnCtx, nodeCtx, typeHints, FieldProvider.UNSUPPORTED, null);
dropSource = exprAnalyzer.convert(dropSourceExpr, new ExpressionAnalysisContext(txnCtx.sessionContext()));
}
SearchPath searchPath = txnCtx.sessionContext().searchPath();
User user = txnCtx.sessionContext().sessionUser();
return new AnalyzedSwapTable((DocTableInfo) schemas.resolveTableInfo(swapTable.source(), Operation.ALTER, user, searchPath), (DocTableInfo) schemas.resolveTableInfo(swapTable.target(), Operation.ALTER, user, searchPath), dropSource);
}
use of io.crate.metadata.SearchPath in project crate by crate.
the class SQLIntegrationTestCase method assertFunctionIsCreatedOnAll.
public void assertFunctionIsCreatedOnAll(String schema, String name, List<DataType<?>> argTypes) throws Exception {
SearchPath searchPath = SearchPath.pathWithPGCatalogAndDoc();
assertBusy(() -> {
Iterable<Functions> functions = internalCluster().getInstances(Functions.class);
for (Functions function : functions) {
FunctionImplementation func = function.get(schema, name, Lists2.map(argTypes, t -> Literal.of(t, null)), searchPath);
assertThat(func, is(not(nullValue())));
assertThat(func.info().ident().argumentTypes(), is(equalTo(argTypes)));
}
}, 20L, TimeUnit.SECONDS);
}
Aggregations