use of io.prestosql.spi.function.FunctionMetadata in project hetu-core by openlookeng.
the class RowExpressionVerifier method visitArithmeticBinary.
@Override
protected Boolean visitArithmeticBinary(ArithmeticBinaryExpression expected, RowExpression actual) {
if (actual instanceof CallExpression) {
FunctionMetadata functionMetadata = metadata.getFunctionAndTypeManager().getFunctionMetadata(((CallExpression) actual).getFunctionHandle());
if (!functionMetadata.getOperatorType().isPresent() || !functionMetadata.getOperatorType().get().isArithmeticOperator()) {
return false;
}
OperatorType actualOperatorType = functionMetadata.getOperatorType().get();
OperatorType expectedOperatorType = getOperatorType(expected.getOperator());
if (expectedOperatorType.equals(actualOperatorType)) {
return process(expected.getLeft(), ((CallExpression) actual).getArguments().get(0)) && process(expected.getRight(), ((CallExpression) actual).getArguments().get(1));
}
}
return false;
}
use of io.prestosql.spi.function.FunctionMetadata in project hetu-core by openlookeng.
the class FunctionNamespaceTestCommon method testGetFunction.
/**
* test get function
*/
public void testGetFunction(AbstractSqlInvokedFunctionNamespaceManager functionNamespaceManager) {
QualifiedObjectName powerTower = QualifiedObjectName.valueOf(new CatalogSchemaName(this.catalogName, funcNsSchemaName), functionName);
List<Parameter> parameters = ImmutableList.of(new Parameter("x", parseTypeSignature(DOUBLE)));
List<Parameter> parameters1 = ImmutableList.of(new Parameter("y", parseTypeSignature(INTEGER)), new Parameter("x", parseTypeSignature(DOUBLE)));
String funcDesc = "power tower test";
String funcBody = "RETURN pow(x, x)";
Map<String, String> funcProperties = ImmutableMap.of("executor", "mysql");
SqlInvokedFunction function = new SqlInvokedFunction(powerTower, parameters, parseTypeSignature(DOUBLE), funcDesc, RoutineCharacteristics.builder().setDeterminism(DETERMINISTIC).setLanguage(JDBC).build(), funcBody, funcProperties, Optional.empty());
SqlInvokedFunction function1 = new SqlInvokedFunction(powerTower, parameters1, parseTypeSignature(DOUBLE), funcDesc, RoutineCharacteristics.builder().setDeterminism(DETERMINISTIC).setLanguage(JDBC).build(), funcBody, funcProperties, Optional.empty());
try {
functionNamespaceManager.createFunction(function, false);
functionNamespaceManager.createFunction(function1, false);
} catch (PrestoException e) {
fail(format("Create function(id=%s) failed.", function.getFunctionId().toString()));
}
if ((functionNamespaceManager instanceof InMemoryFunctionNamespaceManager)) {
// trans to InMemoryFunctionNamespaceManager obj for sub classes method test
InMemoryFunctionNamespaceManager manager = (InMemoryFunctionNamespaceManager) functionNamespaceManager;
List<SqlInvokedFunction> storedFunc = manager.listFunctions();
assertTrue(storedFunc.size() == 2);
storedFunc = manager.fetchFunctionsDirect(powerTower);
assertTrue(storedFunc.size() == 2);
Optional<SqlInvokedFunction> singleFunc = manager.fetchFunctionsDirect(powerTower, parameters.stream().map(x -> x.getType()).collect(Collectors.toList()));
assertTrue(singleFunc.isPresent());
FunctionMetadata funcMeta = manager.fetchFunctionMetadataDirect(singleFunc.get().getFunctionHandle().get());
assertEquals(funcMeta.getName(), powerTower);
}
}
use of io.prestosql.spi.function.FunctionMetadata in project hetu-core by openlookeng.
the class TestInMemoryManager method testCreateAndGetFunction.
/**
* test get function
*/
@Test
public void testCreateAndGetFunction() {
QualifiedObjectName powerTower = QualifiedObjectName.valueOf(new CatalogSchemaName(this.catalogName, funcNsSchemaName), functionName);
List<Parameter> parameters = ImmutableList.of(new Parameter("x", parseTypeSignature(DOUBLE)));
List<Parameter> parameters1 = ImmutableList.of(new Parameter("y", parseTypeSignature(INTEGER)), new Parameter("x", parseTypeSignature(DOUBLE)));
testProc.createFunction(functionNamespaceManager, functionName, parameters);
testProc.createFunction(functionNamespaceManager, functionName, parameters1);
List<SqlInvokedFunction> storedFunc = functionNamespaceManager.listFunctions();
assertTrue(storedFunc.size() == 2);
storedFunc = functionNamespaceManager.fetchFunctionsDirect(powerTower);
assertTrue(storedFunc.size() == 2);
Optional<SqlInvokedFunction> singleFunc = functionNamespaceManager.fetchFunctionsDirect(powerTower, parameters.stream().map(x -> x.getType()).collect(Collectors.toList()));
assertTrue(singleFunc.isPresent());
FunctionMetadata funcMeta = functionNamespaceManager.fetchFunctionMetadataDirect(singleFunc.get().getFunctionHandle().get());
assertEquals(funcMeta.getName(), powerTower);
}
use of io.prestosql.spi.function.FunctionMetadata in project hetu-core by openlookeng.
the class NullIfCodeGenerator method generateExpression.
@Override
public BytecodeNode generateExpression(FunctionHandle functionHandle, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments) {
Scope scope = generatorContext.getScope();
RowExpression first = arguments.get(0);
RowExpression second = arguments.get(1);
LabelNode notMatch = new LabelNode("notMatch");
// push first arg on the stack
Variable firstValue = scope.createTempVariable(first.getType().getJavaType());
BytecodeBlock block = new BytecodeBlock().comment("check if first arg is null").append(generatorContext.generate(first)).append(ifWasNullPopAndGoto(scope, notMatch, void.class)).dup(first.getType().getJavaType()).putVariable(firstValue);
Type firstType = first.getType();
Type secondType = second.getType();
// if (equal(cast(first as <common type>), cast(second as <common type>))
FunctionAndTypeManager functionAndTypeManager = generatorContext.getFunctionManager();
FunctionHandle equalFunction = functionAndTypeManager.resolveOperatorFunctionHandle(EQUAL, TypeSignatureProvider.fromTypes(firstType, secondType));
FunctionMetadata equalFunctionMetadata = functionAndTypeManager.getFunctionMetadata(equalFunction);
BuiltInScalarFunctionImplementation equalsFunction = generatorContext.getFunctionManager().getBuiltInScalarFunctionImplementation(equalFunction);
BytecodeNode equalsCall = generatorContext.generateCall(EQUAL.name(), equalsFunction, ImmutableList.of(cast(generatorContext, firstValue, firstType, equalFunctionMetadata.getArgumentTypes().get(0)), cast(generatorContext, generatorContext.generate(second, Optional.empty()), secondType, equalFunctionMetadata.getArgumentTypes().get(1))));
BytecodeBlock conditionBlock = new BytecodeBlock().append(equalsCall).append(BytecodeUtils.ifWasNullClearPopAndGoto(scope, notMatch, void.class, boolean.class));
// if first and second are equal, return null
BytecodeBlock trueBlock = new BytecodeBlock().append(generatorContext.wasNull().set(constantTrue())).pop(first.getType().getJavaType()).pushJavaDefault(first.getType().getJavaType());
// else return first (which is still on the stack
block.append(new IfStatement().condition(conditionBlock).ifTrue(trueBlock).ifFalse(notMatch));
return block;
}
use of io.prestosql.spi.function.FunctionMetadata in project hetu-core by openlookeng.
the class ExtractSpatialJoins method tryCreateSpatialJoin.
private static Result tryCreateSpatialJoin(Context context, JoinNode joinNode, RowExpression filter, PlanNodeId nodeId, List<Symbol> outputSymbols, CallExpression spatialComparison, Metadata metadata, SplitManager splitManager, PageSourceManager pageSourceManager, TypeAnalyzer typeAnalyzer) {
FunctionMetadata spatialComparisonMetadata = metadata.getFunctionAndTypeManager().getFunctionMetadata(spatialComparison.getFunctionHandle());
checkArgument(spatialComparison.getArguments().size() == 2 && spatialComparisonMetadata.getOperatorType().isPresent());
PlanNode leftNode = joinNode.getLeft();
PlanNode rightNode = joinNode.getRight();
List<Symbol> leftSymbols = leftNode.getOutputSymbols();
List<Symbol> rightSymbols = rightNode.getOutputSymbols();
RowExpression radius;
Optional<Symbol> newRadiusSymbol;
CallExpression newComparison;
OperatorType operatorType = spatialComparisonMetadata.getOperatorType().get();
if (operatorType.equals(OperatorType.LESS_THAN) || operatorType.equals(OperatorType.LESS_THAN_OR_EQUAL)) {
// ST_Distance(a, b) <= r
radius = spatialComparison.getArguments().get(1);
Set<Symbol> radiusSymbols = extractUnique(radius);
if (radiusSymbols.isEmpty() || (rightSymbols.containsAll(radiusSymbols) && containsNone(leftSymbols, radiusSymbols))) {
newRadiusSymbol = newRadiusSymbol(context, radius);
newComparison = new CallExpression(spatialComparison.getDisplayName(), spatialComparison.getFunctionHandle(), spatialComparison.getType(), ImmutableList.of(spatialComparison.getArguments().get(0), mapToExpression(newRadiusSymbol, radius, context)), Optional.empty());
} else {
return Result.empty();
}
} else {
// r >= ST_Distance(a, b)
radius = spatialComparison.getArguments().get(0);
Set<Symbol> radiusSymbols = extractUnique(radius);
if (radiusSymbols.isEmpty() || (rightSymbols.containsAll(radiusSymbols) && containsNone(leftSymbols, radiusSymbols))) {
OperatorType newOperatorType = SpatialJoinUtils.flip(operatorType);
FunctionHandle flippedHandle = getFlippedFunctionHandle(spatialComparison, metadata.getFunctionAndTypeManager());
newRadiusSymbol = newRadiusSymbol(context, radius);
newComparison = new CallExpression(newOperatorType.name(), flippedHandle, spatialComparison.getType(), ImmutableList.of(spatialComparison.getArguments().get(1), mapToExpression(newRadiusSymbol, radius, context)), Optional.empty());
} else {
return Result.empty();
}
}
RowExpression newFilter = replaceExpression(filter, ImmutableMap.of(spatialComparison, newComparison));
PlanNode newRightNode = newRadiusSymbol.map(symbol -> addProjection(context, rightNode, symbol, radius)).orElse(rightNode);
JoinNode newJoinNode = new JoinNode(joinNode.getId(), joinNode.getType(), leftNode, newRightNode, joinNode.getCriteria(), joinNode.getOutputSymbols(), Optional.of(newFilter), joinNode.getLeftHashSymbol(), joinNode.getRightHashSymbol(), joinNode.getDistributionType(), joinNode.isSpillable(), joinNode.getDynamicFilters());
return tryCreateSpatialJoin(context, newJoinNode, newFilter, nodeId, outputSymbols, (CallExpression) newComparison.getArguments().get(0), Optional.of(newComparison.getArguments().get(1)), metadata, splitManager, pageSourceManager, typeAnalyzer);
}
Aggregations