Search in sources :

Example 11 with FunctionHandle

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

the class AbstractTestAggregationFunction method getFunction.

protected final InternalAggregationFunction getFunction() {
    List<TypeSignatureProvider> parameterTypes = fromTypeSignatures(Lists.transform(getFunctionParameterTypes(), TypeSignature::parseTypeSignature));
    FunctionHandle functionHandle = functionAndTypeManager.resolveFunction(Optional.empty(), session.getTransactionId(), qualifyObjectName(QualifiedName.of(getFunctionName())), parameterTypes);
    return functionAndTypeManager.getAggregateFunctionImplementation(functionHandle);
}
Also used : TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle)

Example 12 with FunctionHandle

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

the class PlanPrinter method castToVarchar.

private static String castToVarchar(Type type, Object value, FunctionAndTypeManager functionAndTypeManager, Session session) {
    if (value == null) {
        return "NULL";
    }
    try {
        FunctionHandle cast = functionAndTypeManager.lookupCast(CAST, type.getTypeSignature(), VARCHAR.getTypeSignature());
        Slice coerced = (Slice) new InterpretedFunctionInvoker(functionAndTypeManager).invoke(cast, session.getSqlFunctionProperties(), value);
        return "\"" + coerced.toStringUtf8().replace("\"", "\\\"") + "\"";
    } catch (OperatorNotFoundException e) {
        return "<UNREPRESENTABLE VALUE>";
    }
}
Also used : InterpretedFunctionInvoker(com.facebook.presto.sql.InterpretedFunctionInvoker) OperatorNotFoundException(com.facebook.presto.metadata.OperatorNotFoundException) Slice(io.airlift.slice.Slice) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle)

Example 13 with FunctionHandle

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

the class StatisticAggregations method split.

private Parts split(PlanVariableAllocator variableAllocator, FunctionAndTypeManager functionAndTypeManager, boolean intermediate) {
    ImmutableMap.Builder<VariableReferenceExpression, Aggregation> finalOrIntermediateAggregations = ImmutableMap.builder();
    ImmutableMap.Builder<VariableReferenceExpression, Aggregation> partialAggregations = ImmutableMap.builder();
    for (Map.Entry<VariableReferenceExpression, Aggregation> entry : aggregations.entrySet()) {
        Aggregation originalAggregation = entry.getValue();
        FunctionHandle functionHandle = originalAggregation.getFunctionHandle();
        InternalAggregationFunction function = functionAndTypeManager.getAggregateFunctionImplementation(functionHandle);
        // create partial aggregation
        VariableReferenceExpression partialVariable = variableAllocator.newVariable(entry.getValue().getCall().getSourceLocation(), functionAndTypeManager.getFunctionMetadata(functionHandle).getName().getObjectName(), function.getIntermediateType());
        partialAggregations.put(partialVariable, new Aggregation(new CallExpression(originalAggregation.getCall().getSourceLocation(), originalAggregation.getCall().getDisplayName(), functionHandle, function.getIntermediateType(), originalAggregation.getArguments()), originalAggregation.getFilter(), originalAggregation.getOrderBy(), originalAggregation.isDistinct(), originalAggregation.getMask()));
        // create final aggregation
        finalOrIntermediateAggregations.put(entry.getKey(), new Aggregation(new CallExpression(originalAggregation.getCall().getSourceLocation(), originalAggregation.getCall().getDisplayName(), functionHandle, intermediate ? function.getIntermediateType() : function.getFinalType(), ImmutableList.of(partialVariable)), Optional.empty(), Optional.empty(), false, Optional.empty()));
    }
    StatisticAggregations finalOrIntermediateAggregation = new StatisticAggregations(finalOrIntermediateAggregations.build(), groupingVariables);
    return new Parts(intermediate ? Optional.empty() : Optional.of(finalOrIntermediateAggregation), intermediate ? Optional.of(finalOrIntermediateAggregation) : Optional.empty(), new StatisticAggregations(partialAggregations.build(), groupingVariables));
}
Also used : Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) InternalAggregationFunction(com.facebook.presto.operator.aggregation.InternalAggregationFunction) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) CallExpression(com.facebook.presto.spi.relation.CallExpression) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 14 with FunctionHandle

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

the class TestPageProcessorCompiler method testSanityFilterOnDictionary.

@Test
public void testSanityFilterOnDictionary() {
    FunctionAndTypeManager functionAndTypeManager = createTestMetadataManager().getFunctionAndTypeManager();
    CallExpression lengthVarchar = new CallExpression("length", functionAndTypeManager.lookupFunction("length", fromTypes(VARCHAR)), BIGINT, ImmutableList.of(field(0, VARCHAR)));
    FunctionHandle lessThan = functionAndTypeManager.resolveOperator(LESS_THAN, fromTypes(BIGINT, BIGINT));
    CallExpression filter = new CallExpression(LESS_THAN.name(), lessThan, BOOLEAN, ImmutableList.of(lengthVarchar, constant(10L, BIGINT)));
    PageProcessor processor = compiler.compilePageProcessor(TEST_SESSION.getSqlFunctionProperties(), Optional.of(filter), ImmutableList.of(field(0, VARCHAR)), false, MAX_BATCH_SIZE).get();
    Page page = new Page(createDictionaryBlock(createExpectedValues(10), 100));
    Page outputPage = getOnlyElement(processor.process(null, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)).orElseThrow(() -> new AssertionError("page is not present"));
    assertEquals(outputPage.getPositionCount(), 100);
    assertTrue(outputPage.getBlock(0) instanceof DictionaryBlock);
    DictionaryBlock dictionaryBlock = (DictionaryBlock) outputPage.getBlock(0);
    assertEquals(dictionaryBlock.getDictionary().getPositionCount(), 10);
    // test filter caching
    Page outputPage2 = getOnlyElement(processor.process(null, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)).orElseThrow(() -> new AssertionError("page is not present"));
    assertEquals(outputPage2.getPositionCount(), 100);
    assertTrue(outputPage2.getBlock(0) instanceof DictionaryBlock);
    DictionaryBlock dictionaryBlock2 = (DictionaryBlock) outputPage2.getBlock(0);
    // both output pages must have the same dictionary
    assertEquals(dictionaryBlock2.getDictionary(), dictionaryBlock.getDictionary());
}
Also used : PageProcessor(com.facebook.presto.operator.project.PageProcessor) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) BlockAssertions.createLongDictionaryBlock(com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock) DriverYieldSignal(com.facebook.presto.operator.DriverYieldSignal) Page(com.facebook.presto.common.Page) CallExpression(com.facebook.presto.spi.relation.CallExpression) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Test(org.testng.annotations.Test)

Example 15 with FunctionHandle

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

the class TestMySqlFunctionNamespaceManager method getLatestFunctionHandle.

private FunctionHandle getLatestFunctionHandle(SqlFunctionId functionId) {
    FunctionNamespaceTransactionHandle transactionHandle = functionNamespaceManager.beginTransaction();
    Optional<SqlInvokedFunction> function = functionNamespaceManager.getFunctions(Optional.of(transactionHandle), functionId.getFunctionName()).stream().filter(candidate -> candidate.getFunctionId().equals(functionId)).max(comparing(SqlInvokedFunction::getRequiredVersion));
    assertTrue(function.isPresent());
    functionNamespaceManager.commit(transactionHandle);
    return function.get().getRequiredFunctionHandle();
}
Also used : FunctionNamespaceTransactionHandle(com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle) Parameter(com.facebook.presto.spi.function.Parameter) Arrays(java.util.Arrays) TestingMySqlServer(com.facebook.presto.testing.mysql.TestingMySqlServer) Throwables.throwIfUnchecked(com.google.common.base.Throwables.throwIfUnchecked) Test(org.testng.annotations.Test) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) AfterMethod(org.testng.annotations.AfterMethod) TypeSignature(com.facebook.presto.common.type.TypeSignature) POWER_TOWER(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.POWER_TOWER) DriftNettyClientModule(com.facebook.drift.transport.netty.client.DriftNettyClientModule) Handle(org.jdbi.v3.core.Handle) Map(java.util.Map) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) FUNCTION_POWER_TOWER_DOUBLE_UPDATED(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.FUNCTION_POWER_TOWER_DOUBLE_UPDATED) FunctionVersion.withVersion(com.facebook.presto.spi.function.FunctionVersion.withVersion) SqlFunctionHandle(com.facebook.presto.spi.function.SqlFunctionHandle) Assert.assertFalse(org.testng.Assert.assertFalse) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.nCopies(java.util.Collections.nCopies) BeforeClass(org.testng.annotations.BeforeClass) BeforeMethod(org.testng.annotations.BeforeMethod) DETERMINISTIC(com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.DETERMINISTIC) TANGENT(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.TANGENT) SCALAR(com.facebook.presto.spi.function.FunctionKind.SCALAR) String.format(java.lang.String.format) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) List(java.util.List) RETURNS_NULL_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.RETURNS_NULL_ON_NULL_INPUT) Optional(java.util.Optional) ErrorCodeSupplier(com.facebook.presto.spi.ErrorCodeSupplier) FUNCTION_POWER_TOWER_INT(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.FUNCTION_POWER_TOWER_INT) AlterRoutineCharacteristics(com.facebook.presto.spi.function.AlterRoutineCharacteristics) Joiner(com.google.common.base.Joiner) Bootstrap(com.facebook.airlift.bootstrap.Bootstrap) Assert.assertEquals(org.testng.Assert.assertEquals) LifeCycleManager(com.facebook.airlift.bootstrap.LifeCycleManager) FUNCTION_POWER_TOWER_DOUBLE(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.FUNCTION_POWER_TOWER_DOUBLE) PrestoException(com.facebook.presto.spi.PrestoException) ImmutableList(com.google.common.collect.ImmutableList) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) TEST_SCHEMA(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.TEST_SCHEMA) Comparator.comparing(java.util.Comparator.comparing) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) CALLED_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.CALLED_ON_NULL_INPUT) Jdbi(org.jdbi.v3.core.Jdbi) AfterClass(org.testng.annotations.AfterClass) TEST_CATALOG(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.TEST_CATALOG) Assert.fail(org.testng.Assert.fail) Closeables.closeQuietly(com.facebook.airlift.testing.Closeables.closeQuietly) Injector(com.google.inject.Injector) DOUBLE(com.facebook.presto.common.type.StandardTypes.DOUBLE) FUNCTION_TANGENT(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.FUNCTION_TANGENT) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) FunctionVersion.notVersioned(com.facebook.presto.spi.function.FunctionVersion.notVersioned) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Assert.assertTrue(org.testng.Assert.assertTrue) SimpleAddressSqlFunctionExecutorsModule(com.facebook.presto.functionNamespace.execution.SimpleAddressSqlFunctionExecutorsModule) FunctionNamespaceTransactionHandle(com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction)

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