Search in sources :

Example 26 with ConstantExpression

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

the class SubfieldExtractor method toSubfield.

private static Optional<Subfield> toSubfield(RowExpression expression, StandardFunctionResolution functionResolution, ExpressionOptimizer expressionOptimizer, ConnectorSession connectorSession) {
    List<Subfield.PathElement> elements = new ArrayList<>();
    while (true) {
        if (expression instanceof VariableReferenceExpression) {
            Collections.reverse(elements);
            return Optional.of(new Subfield(((VariableReferenceExpression) expression).getName(), unmodifiableList(elements)));
        }
        if (expression instanceof SpecialFormExpression && ((SpecialFormExpression) expression).getForm() == DEREFERENCE) {
            SpecialFormExpression dereferenceExpression = (SpecialFormExpression) expression;
            RowExpression base = dereferenceExpression.getArguments().get(0);
            RowType baseType = (RowType) base.getType();
            RowExpression indexExpression = expressionOptimizer.optimize(dereferenceExpression.getArguments().get(1), ExpressionOptimizer.Level.OPTIMIZED, connectorSession);
            if (indexExpression instanceof ConstantExpression) {
                Object index = ((ConstantExpression) indexExpression).getValue();
                if (index instanceof Number) {
                    Optional<String> fieldName = baseType.getFields().get(((Number) index).intValue()).getName();
                    if (fieldName.isPresent()) {
                        elements.add(new Subfield.NestedField(fieldName.get()));
                        expression = base;
                        continue;
                    }
                }
            }
            return Optional.empty();
        }
        if (expression instanceof CallExpression && functionResolution.isSubscriptFunction(((CallExpression) expression).getFunctionHandle())) {
            List<RowExpression> arguments = ((CallExpression) expression).getArguments();
            RowExpression indexExpression = expressionOptimizer.optimize(arguments.get(1), ExpressionOptimizer.Level.OPTIMIZED, connectorSession);
            if (indexExpression instanceof ConstantExpression) {
                Object index = ((ConstantExpression) indexExpression).getValue();
                if (index instanceof Number) {
                    elements.add(new Subfield.LongSubscript(((Number) index).longValue()));
                    expression = arguments.get(0);
                    continue;
                }
                if (isVarcharType(indexExpression.getType())) {
                    elements.add(new Subfield.StringSubscript(((Slice) index).toStringUtf8()));
                    expression = arguments.get(0);
                    continue;
                }
            }
            return Optional.empty();
        }
        return Optional.empty();
    }
}
Also used : ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) ArrayList(java.util.ArrayList) RowExpression(com.facebook.presto.spi.relation.RowExpression) RowType(com.facebook.presto.common.type.RowType) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Slice(io.airlift.slice.Slice) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) Subfield(com.facebook.presto.common.Subfield)

Example 27 with ConstantExpression

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

the class RowExpressionVerifier method visitCast.

@Override
protected Boolean visitCast(Cast expected, RowExpression actual) {
    // TODO: clean up cast path
    if (actual instanceof ConstantExpression && expected.getExpression() instanceof Literal && expected.getType().equals(actual.getType().toString())) {
        Literal literal = (Literal) expected.getExpression();
        if (literal instanceof StringLiteral) {
            Object value = LiteralInterpreter.evaluate(TEST_SESSION.toConnectorSession(), (ConstantExpression) actual);
            String actualString = value instanceof Slice ? ((Slice) value).toStringUtf8() : String.valueOf(value);
            return ((StringLiteral) literal).getValue().equals(actualString);
        }
        return getValueFromLiteral(literal).equals(String.valueOf(LiteralInterpreter.evaluate(TEST_SESSION.toConnectorSession(), (ConstantExpression) actual)));
    }
    if (actual instanceof VariableReferenceExpression && expected.getExpression() instanceof SymbolReference && expected.getType().equals(actual.getType().toString())) {
        return visitSymbolReference((SymbolReference) expected.getExpression(), actual);
    }
    if (!(actual instanceof CallExpression) || !functionResolution.isCastFunction(((CallExpression) actual).getFunctionHandle())) {
        return false;
    }
    if (!expected.getType().equalsIgnoreCase(actual.getType().toString()) && !(expected.getType().toLowerCase(ENGLISH).equals(VARCHAR) && actual.getType().getTypeSignature().getBase().equals(VARCHAR))) {
        return false;
    }
    return process(expected.getExpression(), ((CallExpression) actual).getArguments().get(0));
}
Also used : StringLiteral(com.facebook.presto.sql.tree.StringLiteral) Slice(io.airlift.slice.Slice) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) Literal(com.facebook.presto.sql.tree.Literal) DecimalLiteral(com.facebook.presto.sql.tree.DecimalLiteral) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 28 with ConstantExpression

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

the class TestRowExpressionSerde method testArrayLiteral.

@Test
public void testArrayLiteral() {
    RowExpression rowExpression = getRoundTrip("ARRAY [1, 2, 3]", true);
    assertTrue(rowExpression instanceof ConstantExpression);
    Object value = ((ConstantExpression) rowExpression).getValue();
    assertTrue(value instanceof IntArrayBlock);
    IntArrayBlock block = (IntArrayBlock) value;
    assertEquals(block.getPositionCount(), 3);
    assertEquals(block.getInt(0), 1);
    assertEquals(block.getInt(1), 2);
    assertEquals(block.getInt(2), 3);
}
Also used : IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) Test(org.testng.annotations.Test)

Example 29 with ConstantExpression

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

the class LocalExecutionPlanner method createOutputPartitioning.

private Optional<OutputPartitioning> createOutputPartitioning(TaskContext taskContext, PartitioningScheme partitioningScheme) {
    if (partitioningScheme.getPartitioning().getHandle().equals(FIXED_BROADCAST_DISTRIBUTION) || partitioningScheme.getPartitioning().getHandle().equals(FIXED_ARBITRARY_DISTRIBUTION) || partitioningScheme.getPartitioning().getHandle().equals(SCALED_WRITER_DISTRIBUTION) || partitioningScheme.getPartitioning().getHandle().equals(SINGLE_DISTRIBUTION) || partitioningScheme.getPartitioning().getHandle().equals(COORDINATOR_DISTRIBUTION)) {
        return Optional.empty();
    }
    List<VariableReferenceExpression> outputLayout = partitioningScheme.getOutputLayout();
    // We can convert the variables directly into channels, because the root must be a sink and therefore the layout is fixed
    List<Integer> partitionChannels;
    List<Optional<ConstantExpression>> partitionConstants;
    List<Type> partitionChannelTypes;
    if (partitioningScheme.getHashColumn().isPresent()) {
        partitionChannels = ImmutableList.of(outputLayout.indexOf(partitioningScheme.getHashColumn().get()));
        partitionConstants = ImmutableList.of(Optional.empty());
        partitionChannelTypes = ImmutableList.of(BIGINT);
    } else {
        checkArgument(partitioningScheme.getPartitioning().getArguments().stream().allMatch(argument -> argument instanceof ConstantExpression || argument instanceof VariableReferenceExpression), format("Expect all partitioning arguments to be either ConstantExpression or VariableReferenceExpression, but get %s", partitioningScheme.getPartitioning().getArguments()));
        partitionChannels = partitioningScheme.getPartitioning().getArguments().stream().map(argument -> {
            if (argument instanceof ConstantExpression) {
                return -1;
            }
            return outputLayout.indexOf(argument);
        }).collect(toImmutableList());
        partitionConstants = partitioningScheme.getPartitioning().getArguments().stream().map(argument -> {
            if (argument instanceof ConstantExpression) {
                return Optional.of((ConstantExpression) argument);
            }
            return Optional.<ConstantExpression>empty();
        }).collect(toImmutableList());
        partitionChannelTypes = partitioningScheme.getPartitioning().getArguments().stream().map(RowExpression::getType).collect(toImmutableList());
    }
    PartitionFunction partitionFunction = nodePartitioningManager.getPartitionFunction(taskContext.getSession(), partitioningScheme, partitionChannelTypes);
    OptionalInt nullChannel = OptionalInt.empty();
    Set<VariableReferenceExpression> partitioningColumns = partitioningScheme.getPartitioning().getVariableReferences();
    // partitioningColumns expected to have one column in the normal case, and zero columns when partitioning on a constant
    checkArgument(!partitioningScheme.isReplicateNullsAndAny() || partitioningColumns.size() <= 1);
    if (partitioningScheme.isReplicateNullsAndAny() && partitioningColumns.size() == 1) {
        nullChannel = OptionalInt.of(outputLayout.indexOf(getOnlyElement(partitioningColumns)));
    }
    return Optional.of(new OutputPartitioning(partitionFunction, partitionChannels, partitionConstants, partitioningScheme.isReplicateNullsAndAny(), nullChannel));
}
Also used : GENERIC_INTERNAL_ERROR(com.facebook.presto.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) RowNumberOperator(com.facebook.presto.operator.RowNumberOperator) GROUPED_EXECUTION(com.facebook.presto.operator.PipelineExecutionStrategy.GROUPED_EXECUTION) SystemSessionProperties.isOrderBySpillEnabled(com.facebook.presto.SystemSessionProperties.isOrderBySpillEnabled) StatisticAggregationsDescriptor(com.facebook.presto.sql.planner.plan.StatisticAggregationsDescriptor) IndexJoinOptimizer(com.facebook.presto.sql.planner.optimizations.IndexJoinOptimizer) DistinctLimitOperatorFactory(com.facebook.presto.operator.DistinctLimitOperator.DistinctLimitOperatorFactory) StageExecutionDescriptor(com.facebook.presto.operator.StageExecutionDescriptor) CursorProcessor(com.facebook.presto.operator.project.CursorProcessor) Map(java.util.Map) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) IndexJoinNode(com.facebook.presto.sql.planner.plan.IndexJoinNode) SystemSessionProperties.getDynamicFilteringMaxPerDriverSize(com.facebook.presto.SystemSessionProperties.getDynamicFilteringMaxPerDriverSize) NO_COMMIT(com.facebook.presto.operator.PageSinkCommitStrategy.NO_COMMIT) FragmentResultCacheManager(com.facebook.presto.operator.FragmentResultCacheManager) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) PageFunctionCompiler(com.facebook.presto.sql.gen.PageFunctionCompiler) DynamicFilterPlaceholder(com.facebook.presto.expressions.DynamicFilters.DynamicFilterPlaceholder) PARTIAL(com.facebook.presto.spi.plan.AggregationNode.Step.PARTIAL) LookupOuterOperatorFactory(com.facebook.presto.operator.LookupOuterOperator.LookupOuterOperatorFactory) PageSinkManager(com.facebook.presto.split.PageSinkManager) SortOrder(com.facebook.presto.common.block.SortOrder) RowNumberNode(com.facebook.presto.sql.planner.plan.RowNumberNode) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) PageProcessor(com.facebook.presto.operator.project.PageProcessor) StandaloneSpillerFactory(com.facebook.presto.spiller.StandaloneSpillerFactory) InternalAggregationFunction(com.facebook.presto.operator.aggregation.InternalAggregationFunction) LimitOperatorFactory(com.facebook.presto.operator.LimitOperator.LimitOperatorFactory) LimitNode(com.facebook.presto.spi.plan.LimitNode) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) LIFESPAN_COMMIT(com.facebook.presto.operator.PageSinkCommitStrategy.LIFESPAN_COMMIT) LocalPlannerAware(com.facebook.presto.operator.LocalPlannerAware) REPLICATED(com.facebook.presto.sql.planner.plan.JoinNode.DistributionType.REPLICATED) LocalMergeSourceOperatorFactory(com.facebook.presto.operator.exchange.LocalMergeSourceOperator.LocalMergeSourceOperatorFactory) SystemSessionProperties(com.facebook.presto.SystemSessionProperties) JoinFilterFunctionFactory(com.facebook.presto.sql.gen.JoinFilterFunctionCompiler.JoinFilterFunctionFactory) CreateHandle(com.facebook.presto.execution.scheduler.ExecutionWriterTarget.CreateHandle) JoinOperatorFactory(com.facebook.presto.operator.JoinOperatorFactory) SystemSessionProperties.isExchangeCompressionEnabled(com.facebook.presto.SystemSessionProperties.isExchangeCompressionEnabled) UnnestOperatorFactory(com.facebook.presto.operator.unnest.UnnestOperator.UnnestOperatorFactory) Supplier(java.util.function.Supplier) LookupJoinOperators(com.facebook.presto.operator.LookupJoinOperators) LinkedHashMap(java.util.LinkedHashMap) ScanFilterAndProjectOperatorFactory(com.facebook.presto.operator.ScanFilterAndProjectOperator.ScanFilterAndProjectOperatorFactory) UNGROUPED_EXECUTION(com.facebook.presto.operator.PipelineExecutionStrategy.UNGROUPED_EXECUTION) Lists(com.google.common.collect.Lists) LogicalRowExpressions(com.facebook.presto.expressions.LogicalRowExpressions) ConnectorIndex(com.facebook.presto.spi.ConnectorIndex) InternalPlanVisitor(com.facebook.presto.sql.planner.plan.InternalPlanVisitor) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) IndexJoinLookupStats(com.facebook.presto.operator.index.IndexJoinLookupStats) MetadataDeleteOperatorFactory(com.facebook.presto.operator.MetadataDeleteOperator.MetadataDeleteOperatorFactory) SystemSessionProperties.getTaskPartitionedWriterCount(com.facebook.presto.SystemSessionProperties.getTaskPartitionedWriterCount) TableCommitContext(com.facebook.presto.operator.TableCommitContext) RefreshMaterializedViewHandle(com.facebook.presto.execution.scheduler.ExecutionWriterTarget.RefreshMaterializedViewHandle) Session(com.facebook.presto.Session) PageSinkCommitter(com.facebook.presto.operator.TableFinishOperator.PageSinkCommitter) TASK_COMMIT(com.facebook.presto.operator.PageSinkCommitStrategy.TASK_COMMIT) SCALED_WRITER_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.SCALED_WRITER_DISTRIBUTION) Frame(com.facebook.presto.sql.planner.plan.WindowNode.Frame) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) NestedLoopJoinBridge(com.facebook.presto.operator.NestedLoopJoinBridge) TableScanOperatorFactory(com.facebook.presto.operator.TableScanOperator.TableScanOperatorFactory) SphericalGeographyUtils.sphericalDistance(com.facebook.presto.geospatial.SphericalGeographyUtils.sphericalDistance) RecordSet(com.facebook.presto.spi.RecordSet) AnalyzeTableHandle(com.facebook.presto.metadata.AnalyzeTableHandle) RIGHT(com.facebook.presto.sql.planner.plan.JoinNode.Type.RIGHT) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) REMOTE(com.facebook.presto.spi.plan.ProjectNode.Locality.REMOTE) TaskMetadataContext(com.facebook.presto.execution.TaskMetadataContext) RowExpressionInterpreter.rowExpressionInterpreter(com.facebook.presto.sql.planner.RowExpressionInterpreter.rowExpressionInterpreter) SystemSessionProperties.getFilterAndProjectMinOutputPageRowCount(com.facebook.presto.SystemSessionProperties.getFilterAndProjectMinOutputPageRowCount) SpillerFactory(com.facebook.presto.spiller.SpillerFactory) OuterOperatorFactoryResult(com.facebook.presto.operator.JoinOperatorFactory.OuterOperatorFactoryResult) MappedRecordSet(com.facebook.presto.split.MappedRecordSet) SystemSessionProperties.isWindowSpillEnabled(com.facebook.presto.SystemSessionProperties.isWindowSpillEnabled) Metadata(com.facebook.presto.metadata.Metadata) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) COORDINATOR_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.COORDINATOR_DISTRIBUTION) JsonCodec(com.facebook.airlift.json.JsonCodec) PartitionedLookupSourceFactory(com.facebook.presto.operator.PartitionedLookupSourceFactory) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) ExplainAnalyzeNode(com.facebook.presto.sql.planner.plan.ExplainAnalyzeNode) DiscreteDomain.integers(com.google.common.collect.DiscreteDomain.integers) IntStream.range(java.util.stream.IntStream.range) RemoteSourceNode(com.facebook.presto.sql.planner.plan.RemoteSourceNode) FRAGMENT_CHANNEL(com.facebook.presto.operator.TableWriterUtils.FRAGMENT_CHANNEL) MetadataDeleteNode(com.facebook.presto.sql.planner.plan.MetadataDeleteNode) TaskManagerConfig(com.facebook.presto.execution.TaskManagerConfig) Expressions.constant(com.facebook.presto.sql.relational.Expressions.constant) SystemSessionProperties.isAggregationSpillEnabled(com.facebook.presto.SystemSessionProperties.isAggregationSpillEnabled) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) HashMultimap(com.google.common.collect.HashMultimap) AccumulatorFactory(com.facebook.presto.operator.aggregation.AccumulatorFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LookupSourceFactory(com.facebook.presto.operator.LookupSourceFactory) TopNRowNumberNode(com.facebook.presto.sql.planner.plan.TopNRowNumberNode) IndexLookupSourceFactory(com.facebook.presto.operator.index.IndexLookupSourceFactory) IndexSourceNode(com.facebook.presto.sql.planner.plan.IndexSourceNode) DynamicFilterSourceOperator(com.facebook.presto.operator.DynamicFilterSourceOperator) CallExpression(com.facebook.presto.spi.relation.CallExpression) FIXED_BROADCAST_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.FIXED_BROADCAST_DISTRIBUTION) VariableToChannelTranslator(com.facebook.presto.sql.relational.VariableToChannelTranslator) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) OutputNode(com.facebook.presto.sql.planner.plan.OutputNode) HashBuilderOperatorFactory(com.facebook.presto.operator.HashBuilderOperator.HashBuilderOperatorFactory) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) SourceOperatorFactory(com.facebook.presto.operator.SourceOperatorFactory) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression) Collectors(java.util.stream.Collectors) MarkDistinctOperatorFactory(com.facebook.presto.operator.MarkDistinctOperator.MarkDistinctOperatorFactory) SystemSessionProperties.isExchangeChecksumEnabled(com.facebook.presto.SystemSessionProperties.isExchangeChecksumEnabled) SpatialJoinUtils.extractSupportedSpatialFunctions(com.facebook.presto.util.SpatialJoinUtils.extractSupportedSpatialFunctions) MoreFutures.addSuccessCallback(com.facebook.airlift.concurrent.MoreFutures.addSuccessCallback) DynamicTupleFilterFactory(com.facebook.presto.operator.index.DynamicTupleFilterFactory) Range.closedOpen(com.google.common.collect.Range.closedOpen) StreamingAggregationOperatorFactory(com.facebook.presto.operator.StreamingAggregationOperator.StreamingAggregationOperatorFactory) WindowFunctionSupplier(com.facebook.presto.operator.window.WindowFunctionSupplier) InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) SystemSessionProperties.isOptimizeCommonSubExpressions(com.facebook.presto.SystemSessionProperties.isOptimizeCommonSubExpressions) SpatialPredicate(com.facebook.presto.operator.SpatialIndexBuilderOperator.SpatialPredicate) ST_INTERSECTS(com.facebook.presto.util.SpatialJoinUtils.ST_INTERSECTS) AssignUniqueIdOperator(com.facebook.presto.operator.AssignUniqueIdOperator) ExpressionCompiler(com.facebook.presto.sql.gen.ExpressionCompiler) IntStream(java.util.stream.IntStream) ST_DISTANCE(com.facebook.presto.util.SpatialJoinUtils.ST_DISTANCE) DeterminismEvaluator(com.facebook.presto.spi.relation.DeterminismEvaluator) ROW_COUNT_CHANNEL(com.facebook.presto.operator.TableWriterUtils.ROW_COUNT_CHANNEL) ExplainAnalyzeOperatorFactory(com.facebook.presto.operator.ExplainAnalyzeOperator.ExplainAnalyzeOperatorFactory) FrameInfo(com.facebook.presto.operator.window.FrameInfo) PagesSerdeFactory(com.facebook.presto.execution.buffer.PagesSerdeFactory) UnionNode(com.facebook.presto.spi.plan.UnionNode) OperatorFactory(com.facebook.presto.operator.OperatorFactory) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) BlockEncodingSerde(com.facebook.presto.common.block.BlockEncodingSerde) Inject(javax.inject.Inject) DriverFactory(com.facebook.presto.operator.DriverFactory) ImmutableList(com.google.common.collect.ImmutableList) PagesSpatialIndexFactory(com.facebook.presto.operator.PagesSpatialIndexFactory) SystemSessionProperties.isJoinSpillingEnabled(com.facebook.presto.SystemSessionProperties.isJoinSpillingEnabled) ExecutionWriterTarget(com.facebook.presto.execution.scheduler.ExecutionWriterTarget) IndexManager(com.facebook.presto.index.IndexManager) SystemSessionProperties.getDynamicFilteringMaxPerDriverRowCount(com.facebook.presto.SystemSessionProperties.getDynamicFilteringMaxPerDriverRowCount) DynamicFilters(com.facebook.presto.expressions.DynamicFilters) SingleStreamSpillerFactory(com.facebook.presto.spiller.SingleStreamSpillerFactory) Type(com.facebook.presto.common.type.Type) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) SystemSessionProperties.isOptimizedRepartitioningEnabled(com.facebook.presto.SystemSessionProperties.isOptimizedRepartitioningEnabled) TypeUtils.writeNativeValue(com.facebook.presto.common.type.TypeUtils.writeNativeValue) OutputFactory(com.facebook.presto.operator.OutputFactory) JoinFilterFunctionCompiler(com.facebook.presto.sql.gen.JoinFilterFunctionCompiler) Reflection.constructorMethodHandle(com.facebook.presto.util.Reflection.constructorMethodHandle) StatisticsWriterOperatorFactory(com.facebook.presto.operator.StatisticsWriterOperator.StatisticsWriterOperatorFactory) SystemSessionProperties.getTaskWriterCount(com.facebook.presto.SystemSessionProperties.getTaskWriterCount) PlanNode(com.facebook.presto.spi.plan.PlanNode) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) DeleteScanInfo(com.facebook.presto.execution.scheduler.TableWriteInfo.DeleteScanInfo) SystemSessionProperties.getAggregationOperatorUnspillMemoryLimit(com.facebook.presto.SystemSessionProperties.getAggregationOperatorUnspillMemoryLimit) SystemSessionProperties.isEnableDynamicFiltering(com.facebook.presto.SystemSessionProperties.isEnableDynamicFiltering) RowExpressionNodeInliner.replaceExpression(com.facebook.presto.expressions.RowExpressionNodeInliner.replaceExpression) Page(com.facebook.presto.common.Page) Arrays(java.util.Arrays) PageSinkCommitStrategy(com.facebook.presto.operator.PageSinkCommitStrategy) STATS_START_CHANNEL(com.facebook.presto.operator.TableWriterUtils.STATS_START_CHANNEL) TableWriterOperatorFactory(com.facebook.presto.operator.TableWriterOperator.TableWriterOperatorFactory) PageSourceProvider(com.facebook.presto.split.PageSourceProvider) ST_EQUALS(com.facebook.presto.util.SpatialJoinUtils.ST_EQUALS) OutputBuffer(com.facebook.presto.execution.buffer.OutputBuffer) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PageChannelSelector(com.facebook.presto.operator.exchange.PageChannelSelector) ST_OVERLAPS(com.facebook.presto.util.SpatialJoinUtils.ST_OVERLAPS) TypeSignature(com.facebook.presto.common.type.TypeSignature) FULL(com.facebook.presto.sql.planner.plan.JoinNode.Type.FULL) PageBuilder(com.facebook.presto.common.PageBuilder) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) TableWriteInfo(com.facebook.presto.execution.scheduler.TableWriteInfo) WindowFunctionDefinition.window(com.facebook.presto.operator.WindowFunctionDefinition.window) TableWriterMergeOperatorFactory(com.facebook.presto.operator.TableWriterMergeOperator.TableWriterMergeOperatorFactory) NestedLoopBuildOperatorFactory(com.facebook.presto.operator.NestedLoopBuildOperator.NestedLoopBuildOperatorFactory) PipelineExecutionStrategy(com.facebook.presto.operator.PipelineExecutionStrategy) LocalExchangeSinkOperatorFactory(com.facebook.presto.operator.exchange.LocalExchangeSinkOperator.LocalExchangeSinkOperatorFactory) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) GroupIdOperator(com.facebook.presto.operator.GroupIdOperator) ST_TOUCHES(com.facebook.presto.util.SpatialJoinUtils.ST_TOUCHES) InsertHandle(com.facebook.presto.execution.scheduler.ExecutionWriterTarget.InsertHandle) EnforceSingleRowOperator(com.facebook.presto.operator.EnforceSingleRowOperator) SystemSessionProperties.isSpillEnabled(com.facebook.presto.SystemSessionProperties.isSpillEnabled) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) StageExecutionId(com.facebook.presto.execution.StageExecutionId) AssignUniqueId(com.facebook.presto.sql.planner.plan.AssignUniqueId) LocalExchangeSourceOperatorFactory(com.facebook.presto.operator.exchange.LocalExchangeSourceOperator.LocalExchangeSourceOperatorFactory) FilterAndProjectOperatorFactory(com.facebook.presto.operator.FilterAndProjectOperator.FilterAndProjectOperatorFactory) NestedLoopJoinOperatorFactory(com.facebook.presto.operator.NestedLoopJoinOperator.NestedLoopJoinOperatorFactory) AbstractJoinNode(com.facebook.presto.sql.planner.plan.AbstractJoinNode) FieldSetFilteringRecordSet(com.facebook.presto.operator.index.FieldSetFilteringRecordSet) FragmentResultCacheContext.createFragmentResultCacheContext(com.facebook.presto.execution.FragmentResultCacheContext.createFragmentResultCacheContext) Iterables(com.google.common.collect.Iterables) FIXED_ARBITRARY_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.FIXED_ARBITRARY_DISTRIBUTION) SINGLE_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.SINGLE_DISTRIBUTION) ContiguousSet(com.google.common.collect.ContiguousSet) SystemSessionProperties.getFilterAndProjectMinOutputPageSize(com.facebook.presto.SystemSessionProperties.getFilterAndProjectMinOutputPageSize) GroupIdNode(com.facebook.presto.sql.planner.plan.GroupIdNode) Assignments(com.facebook.presto.spi.plan.Assignments) DistinctLimitNode(com.facebook.presto.spi.plan.DistinctLimitNode) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) TopNOperatorFactory(com.facebook.presto.operator.TopNOperator.TopNOperatorFactory) ValuesOperatorFactory(com.facebook.presto.operator.ValuesOperator.ValuesOperatorFactory) Builder(com.google.common.collect.ImmutableMap.Builder) ArrayList(java.util.ArrayList) OptimizedPartitionedOutputFactory(com.facebook.presto.operator.repartition.OptimizedPartitionedOutputOperator.OptimizedPartitionedOutputFactory) TableWriterMergeNode(com.facebook.presto.sql.planner.plan.TableWriterMergeNode) AggregationOperatorFactory(com.facebook.presto.operator.AggregationOperator.AggregationOperatorFactory) SystemSessionProperties.isDistinctAggregationSpillEnabled(com.facebook.presto.SystemSessionProperties.isDistinctAggregationSpillEnabled) SpatialIndexBuilderOperatorFactory(com.facebook.presto.operator.SpatialIndexBuilderOperator.SpatialIndexBuilderOperatorFactory) TableHandle(com.facebook.presto.spi.TableHandle) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) TaskContext(com.facebook.presto.operator.TaskContext) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) SystemSessionProperties.getDynamicFilteringRangeRowLimitPerDriver(com.facebook.presto.SystemSessionProperties.getDynamicFilteringRangeRowLimitPerDriver) TableFinisher(com.facebook.presto.operator.TableFinishOperator.TableFinisher) OperatorType(com.facebook.presto.common.function.OperatorType) LocalExchangeFactory(com.facebook.presto.operator.exchange.LocalExchange.LocalExchangeFactory) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) SemiJoinNode(com.facebook.presto.sql.planner.plan.SemiJoinNode) TopNNode(com.facebook.presto.spi.plan.TopNNode) Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) AssignmentUtils.identityAssignments(com.facebook.presto.sql.planner.plan.AssignmentUtils.identityAssignments) PartitioningSpillerFactory(com.facebook.presto.spiller.PartitioningSpillerFactory) LambdaProvider(com.facebook.presto.operator.aggregation.LambdaProvider) SpatialJoinNode(com.facebook.presto.sql.planner.plan.SpatialJoinNode) SortNode(com.facebook.presto.sql.planner.plan.SortNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TableWriterNode(com.facebook.presto.sql.planner.plan.TableWriterNode) TableFinishOperatorFactory(com.facebook.presto.operator.TableFinishOperator.TableFinishOperatorFactory) INTERMEDIATE(com.facebook.presto.spi.plan.AggregationNode.Step.INTERMEDIATE) PagesIndex(com.facebook.presto.operator.PagesIndex) IndexSourceOperator(com.facebook.presto.operator.index.IndexSourceOperator) TaskOutputFactory(com.facebook.presto.operator.TaskOutputOperator.TaskOutputFactory) OrderByOperatorFactory(com.facebook.presto.operator.OrderByOperator.OrderByOperatorFactory) SpatialJoinOperatorFactory(com.facebook.presto.operator.SpatialJoinOperator.SpatialJoinOperatorFactory) TopNRowNumberOperator(com.facebook.presto.operator.TopNRowNumberOperator) LOCAL(com.facebook.presto.spi.plan.ProjectNode.Locality.LOCAL) ST_WITHIN(com.facebook.presto.util.SpatialJoinUtils.ST_WITHIN) HashAggregationOperatorFactory(com.facebook.presto.operator.HashAggregationOperator.HashAggregationOperatorFactory) ConnectorMetadataUpdaterManager(com.facebook.presto.metadata.ConnectorMetadataUpdaterManager) SystemSessionProperties.getTaskConcurrency(com.facebook.presto.SystemSessionProperties.getTaskConcurrency) DeleteHandle(com.facebook.presto.execution.scheduler.ExecutionWriterTarget.DeleteHandle) LambdaBytecodeGenerator.compileLambdaProvider(com.facebook.presto.sql.gen.LambdaBytecodeGenerator.compileLambdaProvider) SetBuilderOperatorFactory(com.facebook.presto.operator.SetBuilderOperator.SetBuilderOperatorFactory) Step(com.facebook.presto.spi.plan.AggregationNode.Step) NestedLoopJoinPagesSupplier(com.facebook.presto.operator.NestedLoopJoinPagesSupplier) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) JoinBridgeManager(com.facebook.presto.operator.JoinBridgeManager) CONTEXT_CHANNEL(com.facebook.presto.operator.TableWriterUtils.CONTEXT_CHANNEL) IndexBuildDriverFactoryProvider(com.facebook.presto.operator.index.IndexBuildDriverFactoryProvider) TRUE_CONSTANT(com.facebook.presto.expressions.LogicalRowExpressions.TRUE_CONSTANT) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) FragmentResultCacheContext(com.facebook.presto.execution.FragmentResultCacheContext) DataSize(io.airlift.units.DataSize) List(java.util.List) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) SampleNode(com.facebook.presto.sql.planner.plan.SampleNode) Optional(java.util.Optional) ST_CONTAINS(com.facebook.presto.util.SpatialJoinUtils.ST_CONTAINS) MarkDistinctNode(com.facebook.presto.spi.plan.MarkDistinctNode) INNER(com.facebook.presto.sql.planner.plan.JoinNode.Type.INNER) DynamicFilterExtractResult(com.facebook.presto.expressions.DynamicFilters.DynamicFilterExtractResult) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) Locality(com.facebook.presto.spi.plan.ProjectNode.Locality) HashMap(java.util.HashMap) SystemSessionProperties.getIndexLoaderTimeout(com.facebook.presto.SystemSessionProperties.getIndexLoaderTimeout) PrestoException(com.facebook.presto.spi.PrestoException) Multimap(com.google.common.collect.Multimap) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) FilterNode(com.facebook.presto.spi.plan.FilterNode) ST_CROSSES(com.facebook.presto.util.SpatialJoinUtils.ST_CROSSES) DevNullOperatorFactory(com.facebook.presto.operator.DevNullOperator.DevNullOperatorFactory) OrderingCompiler(com.facebook.presto.sql.gen.OrderingCompiler) Verify.verify(com.google.common.base.Verify.verify) PartitionedOutputFactory(com.facebook.presto.operator.repartition.PartitionedOutputOperator.PartitionedOutputFactory) FINAL(com.facebook.presto.spi.plan.AggregationNode.Step.FINAL) Objects.requireNonNull(java.util.Objects.requireNonNull) ExplainAnalyzeContext(com.facebook.presto.execution.ExplainAnalyzeContext) SpatialJoinUtils.extractSupportedSpatialComparisons(com.facebook.presto.util.SpatialJoinUtils.extractSupportedSpatialComparisons) MemoryManagerConfig(com.facebook.presto.memory.MemoryManagerConfig) SystemSessionProperties.isOrderByAggregationSpillEnabled(com.facebook.presto.SystemSessionProperties.isOrderByAggregationSpillEnabled) HashSemiJoinOperatorFactory(com.facebook.presto.operator.HashSemiJoinOperator.HashSemiJoinOperatorFactory) UnnestNode(com.facebook.presto.sql.planner.plan.UnnestNode) RowExpression(com.facebook.presto.spi.relation.RowExpression) VerifyException(com.google.common.base.VerifyException) WindowOperatorFactory(com.facebook.presto.operator.WindowOperator.WindowOperatorFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RemoteProjectOperatorFactory(com.facebook.presto.operator.RemoteProjectOperator.RemoteProjectOperatorFactory) PartitionFunction(com.facebook.presto.operator.PartitionFunction) WindowFunctionDefinition(com.facebook.presto.operator.WindowFunctionDefinition) Ints(com.google.common.primitives.Ints) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) SetMultimap(com.google.common.collect.SetMultimap) OPTIMIZED(com.facebook.presto.spi.relation.ExpressionOptimizer.Level.OPTIMIZED) SetSupplier(com.facebook.presto.operator.SetBuilderOperator.SetSupplier) VisibleForTesting(com.google.common.annotations.VisibleForTesting) COMPILER_ERROR(com.facebook.presto.spi.StandardErrorCode.COMPILER_ERROR) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode) BYTE(io.airlift.units.DataSize.Unit.BYTE) DeleteOperatorFactory(com.facebook.presto.operator.DeleteOperator.DeleteOperatorFactory) JoinCompiler(com.facebook.presto.sql.gen.JoinCompiler) EnforceSingleRowNode(com.facebook.presto.sql.planner.plan.EnforceSingleRowNode) ExchangeNode(com.facebook.presto.sql.planner.plan.ExchangeNode) StatisticsWriterNode(com.facebook.presto.sql.planner.plan.StatisticsWriterNode) PartitionFunction(com.facebook.presto.operator.PartitionFunction) Optional(java.util.Optional) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) OptionalInt(java.util.OptionalInt) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression)

Example 30 with ConstantExpression

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

the class PinotFilterExpressionConverter method handleTimeValueCast.

private Optional<String> handleTimeValueCast(Function<VariableReferenceExpression, Selection> context, RowExpression timeFieldExpression, RowExpression timeValueExpression) {
    // Handle the binary comparison logic of <DATE/TIMESTAMP field> <binary op> <DATE/TIMESTAMP literal>.
    // Pinot stores time as:
    // - `DATE`: Stored as `INT`/`LONG` `daysSinceEpoch` value
    // - `TIMESTAMP`: Stored as `LONG` `millisSinceEpoch` value.
    // In order to push down this predicate, we need to convert the literal to the type of Pinot time field.
    // Below code compares the time type of both side:
    // - if same, then directly push down.
    // - if not same, then convert the literal time type to the field time type.
    // - if not compatible time types, returns Optional.empty(), indicates no change has been made in this cast.
    // Take an example of comparing a `DATE` field to a `TIMESTAMP` literal:
    // - Sample predicate: `WHERE eventDate < current_time`.
    // - Input type is the `eventDate` field data type, which is `DATE`.
    // - Expect type is the right side `literal type`, which means right side is `TIMESTAMP`.
    // The code below converts `current_time` from `millisSinceEpoch` value to `daysSinceEpoch` value, which is
    // comparable to values in `eventDate` column.
    Type inputType;
    Type expectedType;
    if (!isDateTimeConstantType(timeFieldExpression.getType()) || !isDateTimeConstantType(timeValueExpression.getType())) {
        return Optional.empty();
    }
    String timeValueString = timeValueExpression.accept(this, context).getDefinition();
    if (timeFieldExpression instanceof CallExpression) {
        // Handles cases like: `cast(eventDate as TIMESTAMP) <  DATE '2014-01-31'`
        // For cast function,
        // - inputType is the argument type,
        // - expectedType is the cast function return type.
        CallExpression callExpression = (CallExpression) timeFieldExpression;
        if (!standardFunctionResolution.isCastFunction(callExpression.getFunctionHandle())) {
            return Optional.empty();
        }
        if (callExpression.getArguments().size() != 1) {
            return Optional.empty();
        }
        inputType = callExpression.getArguments().get(0).getType();
        expectedType = callExpression.getType();
    } else if (timeFieldExpression instanceof VariableReferenceExpression) {
        // For VariableReferenceExpression,
        // Handles queries like: `eventDate <  TIMESTAMP '2014-01-31 00:00:00 UTC'`
        // - inputType is timeFieldExpression type,
        // - expectedType is the timeValueExpression type.
        inputType = timeFieldExpression.getType();
        expectedType = timeValueExpression.getType();
    } else if (timeFieldExpression instanceof ConstantExpression) {
        // timeFieldExpression is a ConstantExpression, directly return.
        return Optional.of(timeValueString);
    } else {
        return Optional.empty();
    }
    if (inputType == DateType.DATE && (expectedType == TimestampType.TIMESTAMP || expectedType == TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE)) {
        // time field is `DATE`, try to convert time value from `TIMESTAMP` to `DATE`
        try {
            return Optional.of(Long.toString(TimeUnit.MILLISECONDS.toDays(Long.parseLong(timeValueString))));
        } catch (NumberFormatException e) {
            throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), format("Unable to parse timestamp string: '%s'", timeValueString), e);
        }
    }
    if ((inputType == TimestampType.TIMESTAMP || inputType == TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) && expectedType == DateType.DATE) {
        // time field is `TIMESTAMP`, try to convert time value from `DATE` to `TIMESTAMP`
        try {
            return Optional.of(Long.toString(TimeUnit.DAYS.toMillis(Long.parseLong(timeValueString))));
        } catch (NumberFormatException e) {
            throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), format("Unable to parse date string: '%s'", timeValueString), e);
        }
    }
    // Vacuous cast from variable to same type: cast(eventDate, DAYS). Already handled by handleCast.
    return Optional.of(timeValueString);
}
Also used : PinotException(com.facebook.presto.pinot.PinotException) IntegerType(com.facebook.presto.common.type.IntegerType) Type(com.facebook.presto.common.type.Type) BigintType(com.facebook.presto.common.type.BigintType) VarcharType(com.facebook.presto.common.type.VarcharType) OperatorType(com.facebook.presto.common.function.OperatorType) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) DateType(com.facebook.presto.common.type.DateType) TimestampType(com.facebook.presto.common.type.TimestampType) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) PinotPushdownUtils.getLiteralAsString(com.facebook.presto.pinot.PinotPushdownUtils.getLiteralAsString) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Aggregations

ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)33 RowExpression (com.facebook.presto.spi.relation.RowExpression)25 CallExpression (com.facebook.presto.spi.relation.CallExpression)14 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)14 SpecialFormExpression (com.facebook.presto.spi.relation.SpecialFormExpression)10 Map (java.util.Map)8 ImmutableList (com.google.common.collect.ImmutableList)7 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)6 Type (com.facebook.presto.common.type.Type)6 InputReferenceExpression (com.facebook.presto.spi.relation.InputReferenceExpression)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 Slice (io.airlift.slice.Slice)6 List (java.util.List)6 Scope (com.facebook.presto.bytecode.Scope)5 Variable (com.facebook.presto.bytecode.Variable)5 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 Objects.requireNonNull (java.util.Objects.requireNonNull)5 Optional (java.util.Optional)5 IfStatement (com.facebook.presto.bytecode.control.IfStatement)4