Search in sources :

Example 56 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class RelationPlanner method visitUnnest.

@Override
protected RelationPlan visitUnnest(Unnest node, Void context) {
    Scope scope = analysis.getScope(node);
    ImmutableList.Builder<VariableReferenceExpression> outputVariablesBuilder = ImmutableList.builder();
    for (Field field : scope.getRelationType().getVisibleFields()) {
        VariableReferenceExpression variable = variableAllocator.newVariable(field);
        outputVariablesBuilder.add(variable);
    }
    List<VariableReferenceExpression> unnestedVariables = outputVariablesBuilder.build();
    // If we got here, then we must be unnesting a constant, and not be in a join (where there could be column references)
    ImmutableList.Builder<VariableReferenceExpression> argumentVariables = ImmutableList.builder();
    ImmutableList.Builder<RowExpression> values = ImmutableList.builder();
    ImmutableMap.Builder<VariableReferenceExpression, List<VariableReferenceExpression>> unnestVariables = ImmutableMap.builder();
    Iterator<VariableReferenceExpression> unnestedVariablesIterator = unnestedVariables.iterator();
    for (Expression expression : node.getExpressions()) {
        Type type = analysis.getType(expression);
        Expression rewritten = Coercer.addCoercions(expression, analysis);
        rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), rewritten);
        values.add(castToRowExpression(rewritten));
        VariableReferenceExpression input = variableAllocator.newVariable(rewritten, type);
        argumentVariables.add(new VariableReferenceExpression(getSourceLocation(rewritten), input.getName(), type));
        if (type instanceof ArrayType) {
            Type elementType = ((ArrayType) type).getElementType();
            if (!SystemSessionProperties.isLegacyUnnest(session) && elementType instanceof RowType) {
                ImmutableList.Builder<VariableReferenceExpression> unnestVariableBuilder = ImmutableList.builder();
                for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
                    unnestVariableBuilder.add(unnestedVariablesIterator.next());
                }
                unnestVariables.put(input, unnestVariableBuilder.build());
            } else {
                unnestVariables.put(input, ImmutableList.of(unnestedVariablesIterator.next()));
            }
        } else if (type instanceof MapType) {
            unnestVariables.put(input, ImmutableList.of(unnestedVariablesIterator.next(), unnestedVariablesIterator.next()));
        } else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<VariableReferenceExpression> ordinalityVariable = node.isWithOrdinality() ? Optional.of(unnestedVariablesIterator.next()) : Optional.empty();
    checkState(!unnestedVariablesIterator.hasNext(), "Not all output variables were matched with input variables");
    ValuesNode valuesNode = new ValuesNode(getSourceLocation(node), idAllocator.getNextId(), argumentVariables.build(), ImmutableList.of(values.build()));
    UnnestNode unnestNode = new UnnestNode(getSourceLocation(node), idAllocator.getNextId(), valuesNode, ImmutableList.of(), unnestVariables.build(), ordinalityVariable);
    return new RelationPlan(unnestNode, scope, unnestedVariables);
}
Also used : ValuesNode(com.facebook.presto.spi.plan.ValuesNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) RowType(com.facebook.presto.common.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Field(com.facebook.presto.sql.analyzer.Field) TypeUtils.isEnumType(com.facebook.presto.common.type.TypeUtils.isEnumType) ArrayType(com.facebook.presto.common.type.ArrayType) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) Scope(com.facebook.presto.sql.analyzer.Scope) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) ExpressionTreeUtils.isEqualComparisonExpression(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.isEqualComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) UnnestNode(com.facebook.presto.sql.planner.plan.UnnestNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 57 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class TestWriterBlockRawSize method testRowType.

@Test
public void testRowType() {
    Type elementType = INTEGER;
    Type rowType = RowType.anonymous(ImmutableList.of(elementType, elementType));
    ColumnWriter columnWriter = createColumnWriter(rowType);
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, NUM_ELEMENTS);
    boolean[] isNull = new boolean[NUM_ELEMENTS * 2];
    for (int i = 0; i < NUM_ELEMENTS; i++) {
        elementType.writeLong(blockBuilder, i);
        isNull[i * 2] = true;
    }
    Block elementBlock = blockBuilder.build();
    Block[] elementBlocks = new Block[] { elementBlock, elementBlock };
    Block rowBlock = RowBlock.fromFieldBlocks(NUM_ELEMENTS * 2, Optional.of(isNull), elementBlocks);
    long rawSize = columnWriter.writeBlock(rowBlock);
    long expectedSize = NUM_ELEMENTS + (NUM_ELEMENTS * 2 * ((FixedWidthType) elementType).getFixedSize());
    assertEquals(rawSize, expectedSize);
}
Also used : TestOrcMapNullKey.createMapType(com.facebook.presto.orc.TestOrcMapNullKey.createMapType) TimestampType(com.facebook.presto.common.type.TimestampType) ArrayType(com.facebook.presto.common.type.ArrayType) OrcType(com.facebook.presto.orc.metadata.OrcType) Type(com.facebook.presto.common.type.Type) FixedWidthType(com.facebook.presto.common.type.FixedWidthType) RowType(com.facebook.presto.common.type.RowType) RowBlock(com.facebook.presto.common.block.RowBlock) Block(com.facebook.presto.common.block.Block) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 58 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class OrcStorageManager method toOrcFileType.

static Type toOrcFileType(Type raptorType, TypeManager typeManager) {
    // TIMESTAMPS are stored as BIGINT to void the poor encoding in ORC
    if (raptorType == TimestampType.TIMESTAMP) {
        return BIGINT;
    }
    if (raptorType instanceof ArrayType) {
        Type elementType = toOrcFileType(((ArrayType) raptorType).getElementType(), typeManager);
        return new ArrayType(elementType);
    }
    if (raptorType instanceof MapType) {
        TypeSignature keyType = toOrcFileType(((MapType) raptorType).getKeyType(), typeManager).getTypeSignature();
        TypeSignature valueType = toOrcFileType(((MapType) raptorType).getValueType(), typeManager).getTypeSignature();
        return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
    }
    if (raptorType instanceof RowType) {
        List<RowType.Field> fields = ((RowType) raptorType).getFields().stream().map(field -> new RowType.Field(field.getName(), toOrcFileType(field.getType(), typeManager))).collect(toImmutableList());
        return RowType.from(fields);
    }
    return raptorType;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) Page(com.facebook.presto.common.Page) DateTimeZone(org.joda.time.DateTimeZone) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) FileSystem(org.apache.hadoop.fs.FileSystem) TypeSignature(com.facebook.presto.common.type.TypeSignature) RaptorColumnHandle.isShardRowIdColumn(com.facebook.presto.raptor.RaptorColumnHandle.isShardRowIdColumn) Future(java.util.concurrent.Future) Map(java.util.Map) OrcDataSource(com.facebook.presto.orc.OrcDataSource) ColumnInfo(com.facebook.presto.raptor.metadata.ColumnInfo) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) RAPTOR_LOCAL_DISK_FULL(com.facebook.presto.raptor.RaptorErrorCode.RAPTOR_LOCAL_DISK_FULL) RAPTOR_RECOVERY_ERROR(com.facebook.presto.raptor.RaptorErrorCode.RAPTOR_RECOVERY_ERROR) HiveFileContext(com.facebook.presto.hive.HiveFileContext) JsonCodec.jsonCodec(com.facebook.airlift.json.JsonCodec.jsonCodec) NULL_COLUMN(com.facebook.presto.raptor.storage.OrcPageSource.NULL_COLUMN) ORC(com.facebook.presto.orc.OrcEncoding.ORC) StandardTypes(com.facebook.presto.common.type.StandardTypes) DecimalType(com.facebook.presto.common.type.DecimalType) ColumnStats(com.facebook.presto.raptor.metadata.ColumnStats) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) DwrfKeyProvider(com.facebook.presto.orc.DwrfKeyProvider) OrcReaderOptions(com.facebook.presto.orc.OrcReaderOptions) OrcOptimizedWriterStage(com.facebook.presto.raptor.storage.StorageManagerConfig.OrcOptimizedWriterStage) ArrayList(java.util.ArrayList) ROWID_COLUMN(com.facebook.presto.raptor.storage.OrcPageSource.ROWID_COLUMN) OptionalLong(java.util.OptionalLong) MoreFutures.allAsList(com.facebook.airlift.concurrent.MoreFutures.allAsList) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) ArrayType(com.facebook.presto.common.type.ArrayType) RaptorColumnHandle(com.facebook.presto.raptor.RaptorColumnHandle) OrcWriterStats(com.facebook.presto.orc.OrcWriterStats) ColumnReference(com.facebook.presto.orc.TupleDomainOrcPredicate.ColumnReference) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) StorageStripeMetadataSource(com.facebook.presto.orc.StorageStripeMetadataSource) IOException(java.io.IOException) UTC(org.joda.time.DateTimeZone.UTC) Throwables.throwIfInstanceOf(com.google.common.base.Throwables.throwIfInstanceOf) ExecutionException(java.util.concurrent.ExecutionException) RAPTOR_ERROR(com.facebook.presto.raptor.RaptorErrorCode.RAPTOR_ERROR) BUCKET_NUMBER_COLUMN(com.facebook.presto.raptor.storage.OrcPageSource.BUCKET_NUMBER_COLUMN) RaptorConnectorId(com.facebook.presto.raptor.RaptorConnectorId) OrcReader(com.facebook.presto.orc.OrcReader) RowType(com.facebook.presto.common.type.RowType) JsonCodec(com.facebook.airlift.json.JsonCodec) TupleDomainOrcPredicate(com.facebook.presto.orc.TupleDomainOrcPredicate) TimeoutException(java.util.concurrent.TimeoutException) DataSink(com.facebook.presto.common.io.DataSink) Duration(io.airlift.units.Duration) PreDestroy(javax.annotation.PreDestroy) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) RaptorOrcAggregatedMemoryContext(com.facebook.presto.raptor.RaptorOrcAggregatedMemoryContext) Path(org.apache.hadoop.fs.Path) RuntimeStats(com.facebook.presto.common.RuntimeStats) RaptorColumnHandle.isBucketNumberColumn(com.facebook.presto.raptor.RaptorColumnHandle.isBucketNumberColumn) HdfsContext(com.facebook.presto.hive.HdfsContext) StripeMetadataSourceFactory(com.facebook.presto.orc.StripeMetadataSourceFactory) ImmutableSet(com.google.common.collect.ImmutableSet) NodeManager(com.facebook.presto.spi.NodeManager) ImmutableMap(com.google.common.collect.ImmutableMap) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) INITIAL_BATCH_SIZE(com.facebook.presto.orc.OrcReader.INITIAL_BATCH_SIZE) RAPTOR_RECOVERY_TIMEOUT(com.facebook.presto.raptor.RaptorErrorCode.RAPTOR_RECOVERY_TIMEOUT) OrcPredicate(com.facebook.presto.orc.OrcPredicate) UUID(java.util.UUID) RaptorColumnHandle.isHiddenColumn(com.facebook.presto.raptor.RaptorColumnHandle.isHiddenColumn) Math.min(java.lang.Math.min) ENABLED_AND_VALIDATED(com.facebook.presto.raptor.storage.StorageManagerConfig.OrcOptimizedWriterStage.ENABLED_AND_VALIDATED) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Threads.daemonThreadsNamed(com.facebook.airlift.concurrent.Threads.daemonThreadsNamed) DataSize(io.airlift.units.DataSize) List(java.util.List) CompressionKind(com.facebook.presto.orc.metadata.CompressionKind) Optional(java.util.Optional) OrcBatchRecordReader(com.facebook.presto.orc.OrcBatchRecordReader) TimestampType(com.facebook.presto.common.type.TimestampType) BackupManager(com.facebook.presto.raptor.backup.BackupManager) FileSystemUtil.xxhash64(com.facebook.presto.raptor.filesystem.FileSystemUtil.xxhash64) MapType(com.facebook.presto.common.type.MapType) CompletableFuture(java.util.concurrent.CompletableFuture) BackupStore(com.facebook.presto.raptor.backup.BackupStore) PrestoException(com.facebook.presto.spi.PrestoException) OptionalInt(java.util.OptionalInt) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) TypeManager(com.facebook.presto.common.type.TypeManager) Objects.requireNonNull(java.util.Objects.requireNonNull) OrcType(com.facebook.presto.orc.metadata.OrcType) Math.toIntExact(java.lang.Math.toIntExact) OrcFileTailSource(com.facebook.presto.orc.cache.OrcFileTailSource) Type(com.facebook.presto.common.type.Type) ExecutorService(java.util.concurrent.ExecutorService) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) OrcAggregatedMemoryContext(com.facebook.presto.orc.OrcAggregatedMemoryContext) RaptorColumnHandle.isShardUuidColumn(com.facebook.presto.raptor.RaptorColumnHandle.isShardUuidColumn) PETABYTE(io.airlift.units.DataSize.Unit.PETABYTE) NO_ENCRYPTION(com.facebook.presto.orc.DwrfEncryptionProvider.NO_ENCRYPTION) VARBINARY(com.facebook.presto.common.type.VarbinaryType.VARBINARY) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) ShardInfo(com.facebook.presto.raptor.metadata.ShardInfo) Executors.newFixedThreadPool(java.util.concurrent.Executors.newFixedThreadPool) ShardRecorder(com.facebook.presto.raptor.metadata.ShardRecorder) TimeUnit(java.util.concurrent.TimeUnit) ShardStats.computeColumnStats(com.facebook.presto.raptor.storage.ShardStats.computeColumnStats) Collectors.toList(java.util.stream.Collectors.toList) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) Closeable(java.io.Closeable) SHARD_UUID_COLUMN(com.facebook.presto.raptor.storage.OrcPageSource.SHARD_UUID_COLUMN) RowFieldName(com.facebook.presto.common.type.RowFieldName) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BitSet(java.util.BitSet) Block(com.facebook.presto.common.block.Block) CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) ArrayType(com.facebook.presto.common.type.ArrayType) RowType(com.facebook.presto.common.type.RowType) TimestampType(com.facebook.presto.common.type.TimestampType) MapType(com.facebook.presto.common.type.MapType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) OrcType(com.facebook.presto.orc.metadata.OrcType) Type(com.facebook.presto.common.type.Type) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType)

Example 59 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class H2QueryRunner method mapRowValues.

private static Object[] mapRowValues(RowType rowType, Object[] values) {
    int fieldCount = rowType.getFields().size();
    Object[] fields = new Object[fieldCount];
    for (int j = 0; j < fieldCount; j++) {
        Type fieldType = rowType.getTypeParameters().get(j);
        if (fieldType instanceof RowType) {
            fields[j] = newArrayList(mapRowValues((RowType) fieldType, (Object[]) values[j]));
        } else {
            fields[j] = values[j];
        }
    }
    return fields;
}
Also used : Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) RowType(com.facebook.presto.common.type.RowType)

Example 60 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class QueryRewriter method getColumnTypeRewrite.

private Optional<Type> getColumnTypeRewrite(Type type) {
    if (type.equals(DATE) || type.equals(TIME)) {
        return Optional.of(TIMESTAMP);
    }
    if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) {
        return Optional.of(VARCHAR);
    }
    if (type.equals(UNKNOWN)) {
        return Optional.of(BIGINT);
    }
    if (type instanceof DecimalType) {
        return Optional.of(DOUBLE);
    }
    if (type instanceof ArrayType) {
        return getColumnTypeRewrite(((ArrayType) type).getElementType()).map(ArrayType::new);
    }
    if (type instanceof MapType) {
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        Optional<Type> keyTypeRewrite = getColumnTypeRewrite(keyType);
        Optional<Type> valueTypeRewrite = getColumnTypeRewrite(valueType);
        if (keyTypeRewrite.isPresent() || valueTypeRewrite.isPresent()) {
            return Optional.of(typeManager.getType(new TypeSignature(MAP, TypeSignatureParameter.of(keyTypeRewrite.orElse(keyType).getTypeSignature()), TypeSignatureParameter.of(valueTypeRewrite.orElse(valueType).getTypeSignature()))));
        }
        return Optional.empty();
    }
    if (type instanceof RowType) {
        List<Field> fields = ((RowType) type).getFields();
        List<Field> fieldsRewrite = new ArrayList<>();
        boolean rewrite = false;
        for (Field field : fields) {
            Optional<Type> fieldTypeRewrite = getColumnTypeRewrite(field.getType());
            rewrite = rewrite || fieldTypeRewrite.isPresent();
            fieldsRewrite.add(new Field(field.getName(), fieldTypeRewrite.orElse(field.getType())));
        }
        return rewrite ? Optional.of(RowType.from(fieldsRewrite)) : Optional.empty();
    }
    return Optional.empty();
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Field(com.facebook.presto.common.type.RowType.Field) ClusterType(com.facebook.presto.verifier.framework.ClusterType) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) TypeSignature(com.facebook.presto.common.type.TypeSignature) ArrayList(java.util.ArrayList) DecimalType(com.facebook.presto.common.type.DecimalType) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType)

Aggregations

RowType (com.facebook.presto.common.type.RowType)61 ArrayType (com.facebook.presto.common.type.ArrayType)37 Type (com.facebook.presto.common.type.Type)32 MapType (com.facebook.presto.common.type.MapType)28 ImmutableList (com.google.common.collect.ImmutableList)19 ArrayList (java.util.ArrayList)18 DecimalType (com.facebook.presto.common.type.DecimalType)16 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)15 Test (org.testng.annotations.Test)15 List (java.util.List)14 VarcharType (com.facebook.presto.common.type.VarcharType)12 Block (com.facebook.presto.common.block.Block)11 CharType (com.facebook.presto.common.type.CharType)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 PrestoException (com.facebook.presto.spi.PrestoException)8 Map (java.util.Map)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)6 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)6 TimestampType (com.facebook.presto.common.type.TimestampType)5