Search in sources :

Example 46 with CallExpression

use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.

the class TestInCodeGenerator method testInteger.

@Test
public void testInteger() {
    FunctionAndTypeManager functionAndTypeManager = createTestMetadataManager().getFunctionAndTypeManager();
    List<RowExpression> values = new ArrayList<>();
    values.add(constant((long) Integer.MIN_VALUE, INTEGER));
    values.add(constant((long) Integer.MAX_VALUE, INTEGER));
    values.add(constant(3L, INTEGER));
    assertEquals(checkSwitchGenerationCase(INTEGER, values), DIRECT_SWITCH);
    values.add(constant(null, INTEGER));
    assertEquals(checkSwitchGenerationCase(INTEGER, values), DIRECT_SWITCH);
    values.add(new CallExpression(CAST.name(), functionAndTypeManager.lookupCast(CAST, DOUBLE.getTypeSignature(), INTEGER.getTypeSignature()), INTEGER, Collections.singletonList(constant(12345678901234.0, DOUBLE))));
    assertEquals(checkSwitchGenerationCase(INTEGER, values), DIRECT_SWITCH);
    for (int i = 6; i <= 32; ++i) {
        values.add(constant((long) i, INTEGER));
    }
    assertEquals(checkSwitchGenerationCase(INTEGER, values), DIRECT_SWITCH);
    values.add(constant(33L, INTEGER));
    assertEquals(checkSwitchGenerationCase(INTEGER, values), SET_CONTAINS);
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) ArrayList(java.util.ArrayList) RowExpression(com.facebook.presto.spi.relation.RowExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 47 with CallExpression

use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.

the class DruidAggregationProjectConverter method handleDateTruncationViaDateTruncation.

private DruidExpression handleDateTruncationViaDateTruncation(CallExpression function, Map<VariableReferenceExpression, DruidQueryGeneratorContext.Selection> context) {
    RowExpression timeInputParameter = function.getArguments().get(1);
    String inputColumn;
    String inputTimeZone;
    String inputFormat;
    CallExpression timeConversion = getExpressionAsFunction(timeInputParameter, timeInputParameter);
    if (!timeConversion.getDisplayName().toLowerCase(ENGLISH).equals(FROM_UNIXTIME)) {
        throw new PrestoException(DRUID_PUSHDOWN_UNSUPPORTED_EXPRESSION, "Unsupported time function: " + timeConversion.getDisplayName() + " to pushdown for Druid connector.");
    }
    inputColumn = timeConversion.getArguments().get(0).accept(this, context).getDefinition();
    inputTimeZone = timeConversion.getArguments().size() > 1 ? getStringFromConstant(timeConversion.getArguments().get(1)) : DateTimeZone.UTC.getID();
    inputFormat = "seconds";
    RowExpression intervalParameter = function.getArguments().get(0);
    if (!(intervalParameter instanceof ConstantExpression)) {
        throw new PrestoException(DRUID_PUSHDOWN_UNSUPPORTED_EXPRESSION, "Unsupported interval unit: " + intervalParameter + " to pushdown for Druid connector.");
    }
    return derived("dateTrunc(" + inputColumn + "," + inputFormat + ", " + inputTimeZone + ", " + getStringFromConstant(intervalParameter) + ")");
}
Also used : ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) PrestoException(com.facebook.presto.spi.PrestoException) DruidPushdownUtils.getLiteralAsString(com.facebook.presto.druid.DruidPushdownUtils.getLiteralAsString) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 48 with CallExpression

use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.

the class DruidPushdownUtils method handlePushDownSingleDistinctCount.

/**
 * Try to push down query like: `SELECT count(distinct $COLUMN) FROM myTable` to Druid as `SELECT distinctCount($COLUMN) FROM myTable`.
 * This function only handles the case of an AggregationNode (COUNT on $COLUMN) on top of an AggregationNode(of non-aggregate on $COLUMN).
 *
 * @return true if push down successfully otherwise false.
 */
private static boolean handlePushDownSingleDistinctCount(ImmutableList.Builder<DruidAggregationColumnNode> nodeBuilder, AggregationNode aggregationNode, VariableReferenceExpression outputColumn, AggregationNode.Aggregation aggregation) {
    if (!aggregation.getCall().getDisplayName().equalsIgnoreCase(COUNT_FUNCTION_NAME)) {
        return false;
    }
    List<RowExpression> arguments = aggregation.getCall().getArguments();
    if (arguments.size() != 1) {
        return false;
    }
    RowExpression aggregationArgument = arguments.get(0);
    // Handle the case of Count Aggregation on top of a Non-Agg GroupBy Aggregation.
    if (!(aggregationNode.getSource() instanceof AggregationNode)) {
        return false;
    }
    AggregationNode sourceAggregationNode = (AggregationNode) aggregationNode.getSource();
    Set<String> sourceAggregationGroupSet = getGroupKeys(sourceAggregationNode.getGroupingKeys());
    Set<String> aggregationGroupSet = getGroupKeys(aggregationNode.getGroupingKeys());
    aggregationGroupSet.add(aggregationArgument.toString());
    if (!sourceAggregationGroupSet.containsAll(aggregationGroupSet) && aggregationGroupSet.containsAll(sourceAggregationGroupSet)) {
        return false;
    }
    nodeBuilder.add(new AggregationFunctionColumnNode(outputColumn, new CallExpression(aggregation.getCall().getSourceLocation(), DRUID_COUNT_DISTINCT_FUNCTION_NAME, aggregation.getFunctionHandle(), aggregation.getCall().getType(), ImmutableList.of(aggregationArgument))));
    return true;
}
Also used : RowExpression(com.facebook.presto.spi.relation.RowExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) AggregationFunctionColumnNode(com.facebook.presto.druid.DruidAggregationColumnNode.AggregationFunctionColumnNode) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 49 with CallExpression

use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.

the class PageFunctionCompiler method compileProjectionInternal.

private Supplier<PageProjection> compileProjectionInternal(SqlFunctionProperties sqlFunctionProperties, Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions, List<RowExpression> projections, boolean isOptimizeCommonSubExpression, Optional<String> classNameSuffix) {
    requireNonNull(projections, "projections is null");
    checkArgument(!projections.isEmpty() && projections.stream().allMatch(projection -> projection instanceof CallExpression || projection instanceof SpecialFormExpression));
    PageFieldsToInputParametersRewriter.Result result = rewritePageFieldsToInputParameters(projections);
    List<RowExpression> rewrittenExpression = result.getRewrittenExpressions();
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    // generate Work
    ClassDefinition pageProjectionWorkDefinition = definePageProjectWorkClass(sqlFunctionProperties, sessionFunctions, rewrittenExpression, callSiteBinder, isOptimizeCommonSubExpression, classNameSuffix);
    Class<? extends Work> pageProjectionWorkClass;
    try {
        pageProjectionWorkClass = defineClass(pageProjectionWorkDefinition, Work.class, callSiteBinder.getBindings(), getClass().getClassLoader());
    } catch (PrestoException prestoException) {
        throw prestoException;
    } catch (Exception e) {
        throw new PrestoException(COMPILER_ERROR, e);
    }
    return () -> new GeneratedPageProjection(rewrittenExpression, rewrittenExpression.stream().allMatch(determinismEvaluator::isDeterministic), result.getInputChannels(), constructorMethodHandle(pageProjectionWorkClass, List.class, SqlFunctionProperties.class, Page.class, SelectedPositions.class));
}
Also used : SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) RowExpression(com.facebook.presto.spi.relation.RowExpression) PrestoException(com.facebook.presto.spi.PrestoException) GeneratedPageProjection(com.facebook.presto.operator.project.GeneratedPageProjection) Page(com.facebook.presto.common.Page) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) PrestoException(com.facebook.presto.spi.PrestoException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) PageFieldsToInputParametersRewriter(com.facebook.presto.operator.project.PageFieldsToInputParametersRewriter) CallSiteBinder(com.facebook.presto.bytecode.CallSiteBinder) SelectedPositions(com.facebook.presto.operator.project.SelectedPositions) Work(com.facebook.presto.operator.Work) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CallExpression(com.facebook.presto.spi.relation.CallExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression)

Example 50 with CallExpression

use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.

the class LambdaBytecodeGenerator method variableReferenceCompiler.

private static RowExpressionVisitor<BytecodeNode, Scope> variableReferenceCompiler(Map<String, ParameterAndType> parameterMap) {
    return new RowExpressionVisitor<BytecodeNode, Scope>() {

        @Override
        public BytecodeNode visitInputReference(InputReferenceExpression node, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitCall(CallExpression call, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitConstant(ConstantExpression literal, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitLambda(LambdaDefinitionExpression lambda, Scope context) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitVariableReference(VariableReferenceExpression reference, Scope context) {
            ParameterAndType parameterAndType = parameterMap.get(reference.getName());
            Parameter parameter = parameterAndType.getParameter();
            Class<?> type = parameterAndType.getType();
            return new BytecodeBlock().append(parameter).append(unboxPrimitiveIfNecessary(context, type));
        }

        @Override
        public BytecodeNode visitSpecialForm(SpecialFormExpression specialForm, Scope context) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Scope(com.facebook.presto.bytecode.Scope) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowExpressionVisitor(com.facebook.presto.spi.relation.RowExpressionVisitor) Parameter(com.facebook.presto.bytecode.Parameter) CallExpression(com.facebook.presto.spi.relation.CallExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression)

Aggregations

CallExpression (com.facebook.presto.spi.relation.CallExpression)64 RowExpression (com.facebook.presto.spi.relation.RowExpression)33 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)33 Test (org.testng.annotations.Test)22 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)20 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)19 ImmutableList (com.google.common.collect.ImmutableList)18 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)16 Type (com.facebook.presto.common.type.Type)14 Map (java.util.Map)14 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)13 ImmutableMap (com.google.common.collect.ImmutableMap)13 Optional (java.util.Optional)12 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)11 OperatorType (com.facebook.presto.common.function.OperatorType)10 Aggregation (com.facebook.presto.spi.plan.AggregationNode.Aggregation)10 PlanNode (com.facebook.presto.spi.plan.PlanNode)10 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)10 SpecialFormExpression (com.facebook.presto.spi.relation.SpecialFormExpression)10 Page (com.facebook.presto.common.Page)8