Search in sources :

Example 36 with FunctionHandle

use of com.facebook.presto.spi.function.FunctionHandle in project presto by prestodb.

the class StatsUtil method toStatsRepresentation.

static OptionalDouble toStatsRepresentation(FunctionAndTypeManager functionAndTypeManager, ConnectorSession session, Type type, Object value) {
    requireNonNull(value, "value is null");
    if (convertibleToDoubleWithCast(type)) {
        InterpretedFunctionInvoker functionInvoker = new InterpretedFunctionInvoker(functionAndTypeManager);
        FunctionHandle cast = functionAndTypeManager.lookupCast(CAST, type.getTypeSignature(), DoubleType.DOUBLE.getTypeSignature());
        return OptionalDouble.of((double) functionInvoker.invoke(cast, session.getSqlFunctionProperties(), singletonList(value)));
    }
    if (DateType.DATE.equals(type)) {
        return OptionalDouble.of(((Long) value).doubleValue());
    }
    return OptionalDouble.empty();
}
Also used : InterpretedFunctionInvoker(com.facebook.presto.sql.InterpretedFunctionInvoker) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle)

Example 37 with FunctionHandle

use of com.facebook.presto.spi.function.FunctionHandle in project presto by prestodb.

the class TestFunctionAndTypeManager method testIdentityCast.

@Test
public void testIdentityCast() {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    FunctionHandle exactOperator = functionAndTypeManager.lookupCast(CastType.CAST, HYPER_LOG_LOG.getTypeSignature(), HYPER_LOG_LOG.getTypeSignature());
    assertEquals(exactOperator, new BuiltInFunctionHandle(new Signature(CAST.getFunctionName(), SCALAR, HYPER_LOG_LOG.getTypeSignature(), HYPER_LOG_LOG.getTypeSignature())));
}
Also used : FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TypeSignature(com.facebook.presto.common.type.TypeSignature) LiteralEncoder.getMagicLiteralFunctionSignature(com.facebook.presto.sql.planner.LiteralEncoder.getMagicLiteralFunctionSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Test(org.testng.annotations.Test)

Example 38 with FunctionHandle

use of com.facebook.presto.spi.function.FunctionHandle in project presto by prestodb.

the class TestRowExpressionDomainTranslator method testExpressionConstantFolding.

@Test
public void testExpressionConstantFolding() {
    FunctionHandle hex = metadata.getFunctionAndTypeManager().lookupFunction("from_hex", fromTypes(VARCHAR));
    RowExpression originalExpression = greaterThan(C_VARBINARY, call("from_hex", hex, VARBINARY, stringLiteral("123456")));
    ExtractionResult result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_CONSTANT);
    Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456"));
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_VARBINARY, Domain.create(ValueSet.ofRanges(Range.greaterThan(VARBINARY, value)), false))));
    RowExpression expression = toPredicate(result.getTupleDomain());
    assertEquals(expression, greaterThan(C_VARBINARY, varbinaryLiteral(value)));
}
Also used : Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) LiteralEncoder.toRowExpression(com.facebook.presto.sql.planner.LiteralEncoder.toRowExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) ExtractionResult(com.facebook.presto.spi.relation.DomainTranslator.ExtractionResult) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Test(org.testng.annotations.Test)

Example 39 with FunctionHandle

use of com.facebook.presto.spi.function.FunctionHandle in project presto by prestodb.

the class TestTypeValidator method testInvalidWindowFunctionCall.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "type of variable 'sum(_[0-9]+)?' is expected to be double, but the actual type is bigint")
public void testInvalidWindowFunctionCall() {
    VariableReferenceExpression windowVariable = variableAllocator.newVariable("sum", DOUBLE);
    FunctionHandle functionHandle = FUNCTION_MANAGER.lookupFunction("sum", fromTypes(DOUBLE));
    WindowNode.Frame frame = new WindowNode.Frame(RANGE, UNBOUNDED_PRECEDING, Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty());
    WindowNode.Function function = new WindowNode.Function(call("sum", functionHandle, BIGINT, variableA), frame, false);
    WindowNode.Specification specification = new WindowNode.Specification(ImmutableList.of(), Optional.empty());
    PlanNode node = new WindowNode(Optional.empty(), newId(), baseTableScan, specification, ImmutableMap.of(windowVariable, function), Optional.empty(), ImmutableSet.of(), 0);
    assertTypesValid(node);
}
Also used : WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Test(org.testng.annotations.Test)

Example 40 with FunctionHandle

use of com.facebook.presto.spi.function.FunctionHandle in project presto by prestodb.

the class TestTypeValidator method testValidWindow.

@Test
public void testValidWindow() {
    VariableReferenceExpression windowVariable = variableAllocator.newVariable("sum", DOUBLE);
    FunctionHandle functionHandle = FUNCTION_MANAGER.lookupFunction("sum", fromTypes(DOUBLE));
    WindowNode.Frame frame = new WindowNode.Frame(RANGE, UNBOUNDED_PRECEDING, Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty());
    WindowNode.Function function = new WindowNode.Function(call("sum", functionHandle, DOUBLE, variableC), frame, false);
    WindowNode.Specification specification = new WindowNode.Specification(ImmutableList.of(), Optional.empty());
    PlanNode node = new WindowNode(Optional.empty(), newId(), baseTableScan, specification, ImmutableMap.of(windowVariable, function), Optional.empty(), ImmutableSet.of(), 0);
    assertTypesValid(node);
}
Also used : WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Test(org.testng.annotations.Test)

Aggregations

FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)44 Test (org.testng.annotations.Test)20 CallExpression (com.facebook.presto.spi.relation.CallExpression)16 Type (com.facebook.presto.common.type.Type)14 RowExpression (com.facebook.presto.spi.relation.RowExpression)13 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)9 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)9 ImmutableList (com.google.common.collect.ImmutableList)8 Variable (com.facebook.presto.bytecode.Variable)7 JavaScalarFunctionImplementation (com.facebook.presto.spi.function.JavaScalarFunctionImplementation)7 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)6 IfStatement (com.facebook.presto.bytecode.control.IfStatement)6 Page (com.facebook.presto.common.Page)6 ArrayType (com.facebook.presto.common.type.ArrayType)6 Map (java.util.Map)6 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)5 Scope (com.facebook.presto.bytecode.Scope)5 FunctionMetadata (com.facebook.presto.spi.function.FunctionMetadata)5 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)4 MapType (com.facebook.presto.common.type.MapType)4