use of io.crate.metadata.FunctionName in project crate by crate.
the class Function method printFunctionWithParenthesis.
private void printFunctionWithParenthesis(StringBuilder builder, Style style) {
FunctionName functionName = info.ident().fqnName();
builder.append(functionName.displayName());
builder.append("(");
for (int i = 0; i < arguments.size(); i++) {
Symbol argument = arguments.get(i);
builder.append(argument.toString(style));
if (i + 1 < arguments.size()) {
builder.append(", ");
}
}
builder.append(")");
printFilter(builder, style);
}
use of io.crate.metadata.FunctionName in project crate by crate.
the class UserDefinedFunctionService method validateFunctionIsNotInUseByGeneratedColumn.
void validateFunctionIsNotInUseByGeneratedColumn(String schema, String functionName, UserDefinedFunctionsMetadata functionsMetadata, ClusterState currentState) {
// The iteration of schemas/tables must happen on the node context WITHOUT the UDF already removed.
// Otherwise the lazy table factories will already fail while evaluating generated functionsMetadata.
// To avoid that, a copy of the node context with the removed UDF function is used on concrete expression evaluation.
var nodeCtxWithRemovedFunction = new NodeContext(nodeCtx.functions().copyOf());
updateImplementations(schema, functionsMetadata.functionsMetadata().stream(), nodeCtxWithRemovedFunction);
var metadata = currentState.metadata();
var indices = Stream.of(metadata.getConcreteAllIndices()).filter(NO_BLOB_NOR_DANGLING).map(IndexParts::new).filter(indexParts -> !indexParts.isPartitioned()).collect(Collectors.toList());
var templates = metadata.getTemplates().keysIt();
while (templates.hasNext()) {
var indexParts = new IndexParts(templates.next());
if (indexParts.isPartitioned()) {
indices.add(indexParts);
}
}
var indexNameExpressionResolver = new IndexNameExpressionResolver();
for (var indexParts : indices) {
var tableInfo = new DocTableInfoBuilder(nodeCtx, indexParts.toRelationName(), currentState, indexNameExpressionResolver).build();
TableReferenceResolver tableReferenceResolver = new TableReferenceResolver(tableInfo.columns(), tableInfo.ident());
CoordinatorTxnCtx coordinatorTxnCtx = CoordinatorTxnCtx.systemTransactionContext();
ExpressionAnalyzer exprAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, nodeCtxWithRemovedFunction, ParamTypeHints.EMPTY, tableReferenceResolver, null);
for (var ref : tableInfo.columns()) {
if (ref instanceof GeneratedReference) {
var genRef = (GeneratedReference) ref;
Expression expression = SqlParser.createExpression(genRef.formattedGeneratedExpression());
try {
exprAnalyzer.convert(expression, new ExpressionAnalysisContext(coordinatorTxnCtx.sessionContext()));
} catch (UnsupportedOperationException e) {
throw new IllegalArgumentException("Cannot drop function '" + functionName + "', it is still in use by '" + tableInfo + "." + genRef + "'");
}
}
}
}
}
use of io.crate.metadata.FunctionName in project crate by crate.
the class UserDefinedFunctionService method updateImplementations.
public void updateImplementations(String schema, Stream<UserDefinedFunctionMetadata> userDefinedFunctions, NodeContext nodeCtx) {
final Map<FunctionName, List<FunctionProvider>> implementations = new HashMap<>();
Iterator<UserDefinedFunctionMetadata> it = userDefinedFunctions.iterator();
while (it.hasNext()) {
UserDefinedFunctionMetadata udf = it.next();
FunctionProvider resolver = buildFunctionResolver(udf);
if (resolver == null) {
continue;
}
var functionName = new FunctionName(udf.schema(), udf.name());
var resolvers = implementations.computeIfAbsent(functionName, k -> new ArrayList<>());
resolvers.add(resolver);
}
nodeCtx.functions().registerUdfFunctionImplementationsForSchema(schema, implementations);
}
use of io.crate.metadata.FunctionName in project crate by crate.
the class UserDefinedFunctionsIntegrationTest method test_pg_function_is_visible_when_oid_is_retrieved_from_column.
@Test
public void test_pg_function_is_visible_when_oid_is_retrieved_from_column() throws Exception {
Signature signature = Signature.builder().kind(FunctionType.SCALAR).name(new FunctionName(null, CurrentTimeFunction.NAME)).argumentTypes().returnType(DataTypes.TIMETZ.getTypeSignature()).build();
int functionOid = OidHash.functionOid(signature);
execute("create table oid_test(oid integer)");
execute("insert into oid_test values(" + functionOid + ")");
execute("refresh table oid_test");
execute("select pg_function_is_visible(t.oid) from oid_test t");
assertThat(response.rows()[0][0], is(true));
execute("drop table oid_test");
}
use of io.crate.metadata.FunctionName in project crate by crate.
the class UserDefinedFunctionsIntegrationTest method test_pg_get_function_result.
@Test
public void test_pg_get_function_result() throws Exception {
TypeSignature returnTypeSig = TypeSignature.parseTypeSignature("array(array(integer))");
String returnType = returnTypeSig.toString();
Signature signature = Signature.builder().kind(FunctionType.SCALAR).name(new FunctionName(Schemas.DOC_SCHEMA_NAME, "make_2d_array")).argumentTypes(DataTypes.INTEGER.getTypeSignature()).returnType(returnTypeSig).build();
int functionOid = OidHash.functionOid(signature);
execute("select pg_get_function_result(?)", new Object[] { functionOid });
assertThat(response.rows()[0][0], nullValue());
execute("create function doc.make_2d_array(integer) returns array(array(integer)) language dummy_lang as ?", new Object[] { returnType });
execute("select pg_get_function_result(" + functionOid + ")");
assertThat(response.rows()[0][0], is(returnType));
execute("drop function doc.make_2d_array(integer)");
execute("select pg_get_function_result(" + functionOid + ")");
assertThat(response.rows()[0][0], nullValue());
}
Aggregations