Search in sources :

Example 21 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class HivePartitionManager method getPartitions.

public HivePartitionResult getPartitions(SemiTransactionalHiveMetastore metastore, ConnectorTableHandle tableHandle, Constraint<ColumnHandle> constraint) {
    HiveTableHandle hiveTableHandle = (HiveTableHandle) tableHandle;
    TupleDomain<ColumnHandle> effectivePredicate = constraint.getSummary();
    SchemaTableName tableName = hiveTableHandle.getSchemaTableName();
    Table table = getTable(metastore, tableName);
    Optional<HiveBucketHandle> hiveBucketHandle = getHiveBucketHandle(connectorId, table);
    List<HiveColumnHandle> partitionColumns = getPartitionKeyColumnHandles(connectorId, table);
    List<HiveBucket> buckets = getHiveBucketNumbers(table, effectivePredicate);
    TupleDomain<HiveColumnHandle> compactEffectivePredicate = toCompactTupleDomain(effectivePredicate, domainCompactionThreshold);
    if (effectivePredicate.isNone()) {
        return new HivePartitionResult(partitionColumns, ImmutableList.of(), TupleDomain.none(), TupleDomain.none(), hiveBucketHandle);
    }
    if (partitionColumns.isEmpty()) {
        return new HivePartitionResult(partitionColumns, ImmutableList.of(new HivePartition(tableName, compactEffectivePredicate, buckets)), effectivePredicate, TupleDomain.none(), hiveBucketHandle);
    }
    List<Type> partitionTypes = partitionColumns.stream().map(column -> typeManager.getType(column.getTypeSignature())).collect(toList());
    List<String> partitionNames = getFilteredPartitionNames(metastore, tableName, partitionColumns, effectivePredicate);
    // do a final pass to filter based on fields that could not be used to filter the partitions
    int partitionCount = 0;
    ImmutableList.Builder<HivePartition> partitions = ImmutableList.builder();
    for (String partitionName : partitionNames) {
        Optional<Map<ColumnHandle, NullableValue>> values = parseValuesAndFilterPartition(partitionName, partitionColumns, partitionTypes, constraint);
        if (values.isPresent()) {
            if (partitionCount == maxPartitions) {
                throw new PrestoException(HIVE_EXCEEDED_PARTITION_LIMIT, format("Query over table '%s' can potentially read more than %s partitions", hiveTableHandle.getSchemaTableName(), maxPartitions));
            }
            partitionCount++;
            partitions.add(new HivePartition(tableName, compactEffectivePredicate, partitionName, values.get(), buckets));
        }
    }
    // All partition key domains will be fully evaluated, so we don't need to include those
    TupleDomain<ColumnHandle> remainingTupleDomain = TupleDomain.withColumnDomains(Maps.filterKeys(effectivePredicate.getDomains().get(), not(Predicates.in(partitionColumns))));
    TupleDomain<ColumnHandle> enforcedTupleDomain = TupleDomain.withColumnDomains(Maps.filterKeys(effectivePredicate.getDomains().get(), Predicates.in(partitionColumns)));
    return new HivePartitionResult(partitionColumns, partitions.build(), remainingTupleDomain, enforcedTupleDomain, hiveBucketHandle);
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) Table(com.facebook.presto.hive.metastore.Table) TypeManager(com.facebook.presto.spi.type.TypeManager) Slice(io.airlift.slice.Slice) HiveUtil.getPartitionKeyColumnHandles(com.facebook.presto.hive.HiveUtil.getPartitionKeyColumnHandles) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ConnectorTableHandle(com.facebook.presto.spi.ConnectorTableHandle) ProtectMode(org.apache.hadoop.hive.metastore.ProtectMode) PrestoException(com.facebook.presto.spi.PrestoException) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ImmutableList(com.google.common.collect.ImmutableList) Predicates.not(com.google.common.base.Predicates.not) ValueSet(com.facebook.presto.spi.predicate.ValueSet) Type(com.facebook.presto.spi.type.Type) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Predicates(com.google.common.base.Predicates) HiveBucket(com.facebook.presto.hive.HiveBucketing.HiveBucket) NullableValue(com.facebook.presto.spi.predicate.NullableValue) HIVE_EXCEEDED_PARTITION_LIMIT(com.facebook.presto.hive.HiveErrorCode.HIVE_EXCEEDED_PARTITION_LIMIT) ImmutableMap(com.google.common.collect.ImmutableMap) ProtectMode.getProtectModeFromString(org.apache.hadoop.hive.metastore.ProtectMode.getProtectModeFromString) Constraint(com.facebook.presto.spi.Constraint) SemiTransactionalHiveMetastore(com.facebook.presto.hive.metastore.SemiTransactionalHiveMetastore) HiveBucketing.getHiveBucketHandle(com.facebook.presto.hive.HiveBucketing.getHiveBucketHandle) Maps(com.google.common.collect.Maps) String.format(java.lang.String.format) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) ColumnHandle(com.facebook.presto.spi.ColumnHandle) HiveBucketing.getHiveBucketNumbers(com.facebook.presto.hive.HiveBucketing.getHiveBucketNumbers) FileUtils(org.apache.hadoop.hive.common.FileUtils) Optional(java.util.Optional) HiveUtil.parsePartitionValue(com.facebook.presto.hive.HiveUtil.parsePartitionValue) HiveBucket(com.facebook.presto.hive.HiveBucketing.HiveBucket) ImmutableList(com.google.common.collect.ImmutableList) PrestoException(com.facebook.presto.spi.PrestoException) ProtectMode.getProtectModeFromString(org.apache.hadoop.hive.metastore.ProtectMode.getProtectModeFromString) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Table(com.facebook.presto.hive.metastore.Table) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Constraint(com.facebook.presto.spi.Constraint) Type(com.facebook.presto.spi.type.Type) HiveBucketing.getHiveBucketHandle(com.facebook.presto.hive.HiveBucketing.getHiveBucketHandle) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 22 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class GenericHiveRecordCursor method parseStringColumn.

private void parseStringColumn(int column) {
    loaded[column] = true;
    Object fieldData = rowInspector.getStructFieldData(rowData, structFields[column]);
    if (fieldData == null) {
        nulls[column] = true;
    } else {
        Object fieldValue = ((PrimitiveObjectInspector) fieldInspectors[column]).getPrimitiveJavaObject(fieldData);
        checkState(fieldValue != null, "fieldValue should not be null");
        Slice value;
        if (fieldValue instanceof String) {
            value = Slices.utf8Slice((String) fieldValue);
        } else if (fieldValue instanceof byte[]) {
            value = Slices.wrappedBuffer((byte[]) fieldValue);
        } else if (fieldValue instanceof HiveVarchar) {
            value = Slices.utf8Slice(((HiveVarchar) fieldValue).getValue());
        } else if (fieldValue instanceof HiveChar) {
            value = Slices.utf8Slice(((HiveChar) fieldValue).getValue());
        } else {
            throw new IllegalStateException("unsupported string field type: " + fieldValue.getClass().getName());
        }
        Type type = types[column];
        if (isVarcharType(type)) {
            value = truncateToLength(value, type);
        }
        if (isCharType(type)) {
            value = trimSpacesAndTruncateToLength(value, type);
        }
        slices[column] = value;
        nulls[column] = false;
    }
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) HiveUtil.isStructuralType(com.facebook.presto.hive.HiveUtil.isStructuralType) Type(com.facebook.presto.spi.type.Type) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType) Slice(io.airlift.slice.Slice) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) SerDeUtils.getBlockObject(com.facebook.presto.hive.util.SerDeUtils.getBlockObject) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar)

Example 23 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class HiveCoercionPolicy method canCoerce.

@Override
public boolean canCoerce(HiveType fromHiveType, HiveType toHiveType) {
    Type fromType = typeManager.getType(fromHiveType.getTypeSignature());
    Type toType = typeManager.getType(toHiveType.getTypeSignature());
    if (fromType instanceof VarcharType) {
        return toHiveType.equals(HIVE_BYTE) || toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG);
    }
    if (toType instanceof VarcharType) {
        return fromHiveType.equals(HIVE_BYTE) || fromHiveType.equals(HIVE_SHORT) || fromHiveType.equals(HIVE_INT) || fromHiveType.equals(HIVE_LONG);
    }
    if (fromHiveType.equals(HIVE_BYTE)) {
        return toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG);
    }
    if (fromHiveType.equals(HIVE_SHORT)) {
        return toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG);
    }
    if (fromHiveType.equals(HIVE_INT)) {
        return toHiveType.equals(HIVE_LONG);
    }
    if (fromHiveType.equals(HIVE_FLOAT)) {
        return toHiveType.equals(HIVE_DOUBLE);
    }
    return false;
}
Also used : Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) VarcharType(com.facebook.presto.spi.type.VarcharType)

Example 24 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class ColumnarBinaryHiveRecordCursor method getSlice.

@Override
public Slice getSlice(int fieldId) {
    checkState(!closed, "Cursor is closed");
    Type type = types[fieldId];
    if (!isVarcharType(type) && !isCharType(type) && !type.equals(VARBINARY) && !isStructuralType(hiveTypes[fieldId]) && !isLongDecimal(type)) {
        // we don't use Preconditions.checkArgument because it requires boxing fieldId, which affects inner loop performance
        throw new IllegalArgumentException(format("Expected field to be VARCHAR, CHAR, VARBINARY or DECIMAL, actual %s (field %s)", type, fieldId));
    }
    if (!loaded[fieldId]) {
        parseStringColumn(fieldId);
    }
    return slices[fieldId];
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) HiveUtil.isStructuralType(com.facebook.presto.hive.HiveUtil.isStructuralType) Type(com.facebook.presto.spi.type.Type) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType)

Example 25 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class ColumnarBinaryHiveRecordCursor method getObject.

@Override
public Object getObject(int fieldId) {
    checkState(!closed, "Cursor is closed");
    Type type = types[fieldId];
    if (!isStructuralType(hiveTypes[fieldId])) {
        // we don't use Preconditions.checkArgument because it requires boxing fieldId, which affects inner loop performance
        throw new IllegalArgumentException(format("Expected field to be structural, actual %s (field %s)", type, fieldId));
    }
    if (!loaded[fieldId]) {
        parseObjectColumn(fieldId);
    }
    return objects[fieldId];
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) HiveUtil.isStructuralType(com.facebook.presto.hive.HiveUtil.isStructuralType) Type(com.facebook.presto.spi.type.Type) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType)

Aggregations

Type (com.facebook.presto.spi.type.Type)392 Test (org.testng.annotations.Test)103 Block (com.facebook.presto.spi.block.Block)83 ImmutableList (com.google.common.collect.ImmutableList)79 ArrayType (com.facebook.presto.type.ArrayType)78 MapType (com.facebook.presto.type.MapType)72 Page (com.facebook.presto.spi.Page)68 PrestoException (com.facebook.presto.spi.PrestoException)51 List (java.util.List)50 VarcharType (com.facebook.presto.spi.type.VarcharType)48 DecimalType (com.facebook.presto.spi.type.DecimalType)44 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)40 MaterializedResult (com.facebook.presto.testing.MaterializedResult)40 ArrayList (java.util.ArrayList)40 MethodHandle (java.lang.invoke.MethodHandle)39 PlanNodeId (com.facebook.presto.sql.planner.plan.PlanNodeId)37 ImmutableMap (com.google.common.collect.ImmutableMap)36 Map (java.util.Map)36 Slice (io.airlift.slice.Slice)35 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)30