Search in sources :

Example 21 with CharType

use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.

the class OracleRowExpressionConverter method visitCall.

@Override
public String visitCall(CallExpression call, JdbcConverterContext context) {
    FunctionHandle functionHandle = call.getFunctionHandle();
    String functionName = functionMetadataManager.getFunctionMetadata(functionHandle).getName().getObjectName();
    if (timeExtractFields.contains(functionName)) {
        if (call.getArguments().size() == 1) {
            try {
                Time.ExtractField field = Time.ExtractField.valueOf(functionName.toUpperCase(ENGLISH));
                return format("EXTRACT(%s FROM %s)", field, call.getArguments().get(0).accept(this, context));
            } catch (IllegalArgumentException e) {
                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal argument: " + e);
            }
        } else {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal argument num of function " + functionName);
        }
    }
    if (functionName.equals(AT_TIMEZONE_FUNCTION_NAME)) {
        if (call.getArguments().size() == 2) {
            return format("%s AT TIME ZONE %s", call.getArguments().get(0).accept(this, context), call.getArguments().get(1).accept(this, context));
        } else {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal argument num of function " + functionName);
        }
    }
    if (standardFunctionResolution.isArrayConstructor(functionHandle)) {
        throw new PrestoException(NOT_SUPPORTED, "Oracle connector does not support array constructor");
    }
    if (standardFunctionResolution.isSubscriptFunction(functionHandle)) {
        throw new PrestoException(NOT_SUPPORTED, "Oracle connector does not support subscript expression");
    }
    if (standardFunctionResolution.isCastFunction(functionHandle)) {
        // deal with literal, when generic literal expression translate to rowExpression, it will be
        // translated to a 'CAST' rowExpression with a varchar type 'CONSTANT' rowExpression, in some
        // case, 'CAST' is superfluous
        RowExpression argument = call.getArguments().get(0);
        Type type = call.getType();
        if (argument instanceof ConstantExpression && argument.getType() instanceof VarcharType) {
            String value = argument.accept(this, context);
            if (type instanceof DateType) {
                return format("date %s", value);
            }
            if (type instanceof VarcharType || type instanceof CharType || type instanceof VarbinaryType || type instanceof DecimalType || type instanceof RealType || type instanceof DoubleType) {
                return value;
            }
        }
        if (call.getType().getDisplayName().equals(LIKE_PATTERN_NAME)) {
            return call.getArguments().get(0).accept(this, context);
        }
        return getCastExpression(call.getArguments().get(0).accept(this, context), call.getType());
    }
    return super.visitCall(call, context);
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Time(io.prestosql.spi.sql.expression.Time) PrestoException(io.prestosql.spi.PrestoException) RealType(io.prestosql.spi.type.RealType) CharType(io.prestosql.spi.type.CharType) DecimalType(io.prestosql.spi.type.DecimalType) DoubleType(io.prestosql.spi.type.DoubleType) Type(io.prestosql.spi.type.Type) RealType(io.prestosql.spi.type.RealType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) DateType(io.prestosql.spi.type.DateType) VarcharType(io.prestosql.spi.type.VarcharType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) DoubleType(io.prestosql.spi.type.DoubleType) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType) FunctionHandle(io.prestosql.spi.function.FunctionHandle) DateType(io.prestosql.spi.type.DateType)

Example 22 with CharType

use of io.prestosql.spi.type.CharType in project boostkit-bigdata by kunpengcompute.

the class HiveWriteUtils method getField.

public static <T> Object getField(Type type, Block<T> block, int position) {
    if (block.isNull(position)) {
        return null;
    }
    if (BooleanType.BOOLEAN.equals(type)) {
        return type.getBoolean(block, position);
    }
    if (BigintType.BIGINT.equals(type)) {
        return type.getLong(block, position);
    }
    if (IntegerType.INTEGER.equals(type)) {
        return toIntExact(type.getLong(block, position));
    }
    if (SmallintType.SMALLINT.equals(type)) {
        return Shorts.checkedCast(type.getLong(block, position));
    }
    if (TinyintType.TINYINT.equals(type)) {
        return SignedBytes.checkedCast(type.getLong(block, position));
    }
    if (RealType.REAL.equals(type)) {
        return intBitsToFloat((int) type.getLong(block, position));
    }
    if (DoubleType.DOUBLE.equals(type)) {
        return type.getDouble(block, position);
    }
    if (type instanceof VarcharType) {
        return new Text(type.getSlice(block, position).getBytes());
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        return new Text(padEnd(type.getSlice(block, position).toStringUtf8(), charType.getLength(), ' '));
    }
    if (VarbinaryType.VARBINARY.equals(type)) {
        return type.getSlice(block, position).getBytes();
    }
    if (DateType.DATE.equals(type)) {
        return Date.ofEpochDay(toIntExact(type.getLong(block, position)));
    }
    if (TimestampType.TIMESTAMP.equals(type)) {
        return Timestamp.ofEpochMilli(type.getLong(block, position));
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getHiveDecimal(decimalType, block, position);
    }
    if (isArrayType(type)) {
        Type elementType = type.getTypeParameters().get(0);
        Block arrayBlock = block.getObject(position, Block.class);
        List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
        for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
            Object element = getField(elementType, arrayBlock, i);
            list.add(element);
        }
        return Collections.unmodifiableList(list);
    }
    if (isMapType(type)) {
        Type keyType = type.getTypeParameters().get(0);
        Type valueType = type.getTypeParameters().get(1);
        Block mapBlock = block.getObject(position, Block.class);
        Map<Object, Object> map = new HashMap<>();
        for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
            Object key = getField(keyType, mapBlock, i);
            Object value = getField(valueType, mapBlock, i + 1);
            map.put(key, value);
        }
        return Collections.unmodifiableMap(map);
    }
    if (isRowType(type)) {
        Block rowBlock = block.getObject(position, Block.class);
        List<Type> fieldTypes = type.getTypeParameters();
        checkCondition(fieldTypes.size() == rowBlock.getPositionCount(), StandardErrorCode.GENERIC_INTERNAL_ERROR, "Expected row value field count does not match type field count");
        List<Object> row = new ArrayList<>(rowBlock.getPositionCount());
        for (int i = 0; i < rowBlock.getPositionCount(); i++) {
            Object element = getField(fieldTypes.get(i), rowBlock, i);
            row.add(element);
        }
        return Collections.unmodifiableList(row);
    }
    throw new PrestoException(NOT_SUPPORTED, "unsupported type: " + type);
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text) PrestoException(io.prestosql.spi.PrestoException) HiveUtil.isArrayType(io.prestosql.plugin.hive.HiveUtil.isArrayType) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) RealType(io.prestosql.spi.type.RealType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) TimestampType(io.prestosql.spi.type.TimestampType) CharType(io.prestosql.spi.type.CharType) IntegerType(io.prestosql.spi.type.IntegerType) TinyintType(io.prestosql.spi.type.TinyintType) HiveUtil.isMapType(io.prestosql.plugin.hive.HiveUtil.isMapType) BooleanType(io.prestosql.spi.type.BooleanType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) BigintType(io.prestosql.spi.type.BigintType) HiveUtil.isRowType(io.prestosql.plugin.hive.HiveUtil.isRowType) DoubleType(io.prestosql.spi.type.DoubleType) SmallintType(io.prestosql.spi.type.SmallintType) DateType(io.prestosql.spi.type.DateType) DecimalType(io.prestosql.spi.type.DecimalType) Block(io.prestosql.spi.block.Block) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) CharType(io.prestosql.spi.type.CharType)

Example 23 with CharType

use of io.prestosql.spi.type.CharType in project boostkit-bigdata by kunpengcompute.

the class SerDeUtils method serializePrimitive.

private static void serializePrimitive(Type type, BlockBuilder builder, Object object, PrimitiveObjectInspector inspector) {
    requireNonNull(builder, "parent builder is null");
    if (object == null) {
        builder.appendNull();
        return;
    }
    switch(inspector.getPrimitiveCategory()) {
        case BOOLEAN:
            BooleanType.BOOLEAN.writeBoolean(builder, ((BooleanObjectInspector) inspector).get(object));
            return;
        case BYTE:
            TinyintType.TINYINT.writeLong(builder, ((ByteObjectInspector) inspector).get(object));
            return;
        case SHORT:
            SmallintType.SMALLINT.writeLong(builder, ((ShortObjectInspector) inspector).get(object));
            return;
        case INT:
            IntegerType.INTEGER.writeLong(builder, ((IntObjectInspector) inspector).get(object));
            return;
        case LONG:
            BigintType.BIGINT.writeLong(builder, ((LongObjectInspector) inspector).get(object));
            return;
        case FLOAT:
            RealType.REAL.writeLong(builder, floatToRawIntBits(((FloatObjectInspector) inspector).get(object)));
            return;
        case DOUBLE:
            DoubleType.DOUBLE.writeDouble(builder, ((DoubleObjectInspector) inspector).get(object));
            return;
        case STRING:
            type.writeSlice(builder, Slices.utf8Slice(((StringObjectInspector) inspector).getPrimitiveJavaObject(object)));
            return;
        case VARCHAR:
            type.writeSlice(builder, Slices.utf8Slice(((HiveVarcharObjectInspector) inspector).getPrimitiveJavaObject(object).getValue()));
            return;
        case CHAR:
            CharType charType = (CharType) type;
            HiveChar hiveChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(object);
            type.writeSlice(builder, truncateToLengthAndTrimSpaces(Slices.utf8Slice(hiveChar.getValue()), charType.getLength()));
            return;
        case DATE:
            DateType.DATE.writeLong(builder, formatDateAsLong(object, (DateObjectInspector) inspector));
            return;
        case TIMESTAMP:
            TimestampType.TIMESTAMP.writeLong(builder, formatTimestampAsLong(object, (TimestampObjectInspector) inspector));
            return;
        case BINARY:
            VARBINARY.writeSlice(builder, Slices.wrappedBuffer(((BinaryObjectInspector) inspector).getPrimitiveJavaObject(object)));
            return;
        case DECIMAL:
            DecimalType decimalType = (DecimalType) type;
            HiveDecimalWritable hiveDecimal = ((HiveDecimalObjectInspector) inspector).getPrimitiveWritableObject(object);
            if (decimalType.isShort()) {
                decimalType.writeLong(builder, DecimalUtils.getShortDecimalValue(hiveDecimal, decimalType.getScale()));
            } else {
                decimalType.writeSlice(builder, DecimalUtils.getLongDecimalValue(hiveDecimal, decimalType.getScale()));
            }
            return;
    }
    throw new RuntimeException("Unknown primitive type: " + inspector.getPrimitiveCategory());
}
Also used : DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) DecimalType(io.prestosql.spi.type.DecimalType) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) CharType(io.prestosql.spi.type.CharType) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) HiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector)

Example 24 with CharType

use of io.prestosql.spi.type.CharType in project boostkit-bigdata by kunpengcompute.

the class HiveUtil method charPartitionKey.

public static Slice charPartitionKey(String value, String name, Type columnType) {
    Slice partitionKey = trimTrailingSpaces(Slices.utf8Slice(value));
    CharType charType = (CharType) columnType;
    if (SliceUtf8.countCodePoints(partitionKey) > charType.getLength()) {
        throw new PrestoException(HiveErrorCode.HIVE_INVALID_PARTITION_VALUE, format("Invalid partition value '%s' for %s partition key: %s", value, columnType.toString(), name));
    }
    return partitionKey;
}
Also used : Slice(io.airlift.slice.Slice) PrestoException(io.prestosql.spi.PrestoException) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) CharType(io.prestosql.spi.type.CharType)

Example 25 with CharType

use of io.prestosql.spi.type.CharType in project boostkit-bigdata by kunpengcompute.

the class HivePartitionManager method getFilteredPartitionNames.

private List<String> getFilteredPartitionNames(SemiTransactionalHiveMetastore metastore, HiveIdentity identity, SchemaTableName tableName, List<HiveColumnHandle> partitionKeys, TupleDomain<ColumnHandle> effectivePredicate, Table table) {
    checkArgument(effectivePredicate.getDomains().isPresent());
    List<String> filter = new ArrayList<>();
    for (HiveColumnHandle partitionKey : partitionKeys) {
        Domain domain = effectivePredicate.getDomains().get().get(partitionKey);
        if (domain != null && domain.isNullableSingleValue()) {
            Object value = domain.getNullableSingleValue();
            Type type = domain.getType();
            if (value == null) {
                filter.add(HivePartitionKey.HIVE_DEFAULT_DYNAMIC_PARTITION);
            } else if (type instanceof CharType) {
                Slice slice = (Slice) value;
                filter.add(padSpaces(slice, (CharType) type).toStringUtf8());
            } else if (type instanceof VarcharType) {
                Slice slice = (Slice) value;
                filter.add(slice.toStringUtf8());
            } else // unless we know that all partition values use the canonical Java representation.
            if (!assumeCanonicalPartitionKeys) {
                filter.add(PARTITION_VALUE_WILDCARD);
            } else if (type instanceof DecimalType && !((DecimalType) type).isShort()) {
                Slice slice = (Slice) value;
                filter.add(Decimals.toString(slice, ((DecimalType) type).getScale()));
            } else if (type instanceof DecimalType && ((DecimalType) type).isShort()) {
                filter.add(Decimals.toString((long) value, ((DecimalType) type).getScale()));
            } else if (type instanceof DateType) {
                DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.date().withZoneUTC();
                filter.add(dateTimeFormatter.print(TimeUnit.DAYS.toMillis((long) value)));
            } else if (type instanceof TimestampType) {
                // we don't have time zone info, so just add a wildcard
                filter.add(PARTITION_VALUE_WILDCARD);
            } else if (type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType || type instanceof BigintType || type instanceof DoubleType || type instanceof RealType || type instanceof BooleanType) {
                filter.add(value.toString());
            } else {
                throw new PrestoException(NOT_SUPPORTED, format("Unsupported partition key type: %s", type.getDisplayName()));
            }
        } else {
            filter.add(PARTITION_VALUE_WILDCARD);
        }
    }
    // fetch the partition names
    return metastore.getPartitionNamesByParts(identity, tableName.getSchemaName(), tableName.getTableName(), filter, table).orElseThrow(() -> new TableNotFoundException(tableName));
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) TinyintType(io.prestosql.spi.type.TinyintType) ArrayList(java.util.ArrayList) BooleanType(io.prestosql.spi.type.BooleanType) PrestoException(io.prestosql.spi.PrestoException) RealType(io.prestosql.spi.type.RealType) BigintType(io.prestosql.spi.type.BigintType) IntegerType(io.prestosql.spi.type.IntegerType) TableNotFoundException(io.prestosql.spi.connector.TableNotFoundException) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) RealType(io.prestosql.spi.type.RealType) TimestampType(io.prestosql.spi.type.TimestampType) BigintType(io.prestosql.spi.type.BigintType) CharType(io.prestosql.spi.type.CharType) DoubleType(io.prestosql.spi.type.DoubleType) SmallintType(io.prestosql.spi.type.SmallintType) IntegerType(io.prestosql.spi.type.IntegerType) TinyintType(io.prestosql.spi.type.TinyintType) DateType(io.prestosql.spi.type.DateType) BooleanType(io.prestosql.spi.type.BooleanType) VarcharType(io.prestosql.spi.type.VarcharType) Slice(io.airlift.slice.Slice) DoubleType(io.prestosql.spi.type.DoubleType) DecimalType(io.prestosql.spi.type.DecimalType) TimestampType(io.prestosql.spi.type.TimestampType) SmallintType(io.prestosql.spi.type.SmallintType) CharType(io.prestosql.spi.type.CharType) Domain(io.prestosql.spi.predicate.Domain) TupleDomain(io.prestosql.spi.predicate.TupleDomain) DateType(io.prestosql.spi.type.DateType) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Aggregations

CharType (io.prestosql.spi.type.CharType)48 DecimalType (io.prestosql.spi.type.DecimalType)40 VarcharType (io.prestosql.spi.type.VarcharType)39 PrestoException (io.prestosql.spi.PrestoException)23 Type (io.prestosql.spi.type.Type)23 Slice (io.airlift.slice.Slice)16 TimestampType (io.prestosql.spi.type.TimestampType)13 ArrayType (io.prestosql.spi.type.ArrayType)11 Chars.isCharType (io.prestosql.spi.type.Chars.isCharType)11 DateType (io.prestosql.spi.type.DateType)11 DoubleType (io.prestosql.spi.type.DoubleType)11 RealType (io.prestosql.spi.type.RealType)11 VarbinaryType (io.prestosql.spi.type.VarbinaryType)11 BigDecimal (java.math.BigDecimal)10 ArrayList (java.util.ArrayList)10 BigintType (io.prestosql.spi.type.BigintType)9 BooleanType (io.prestosql.spi.type.BooleanType)9 IntegerType (io.prestosql.spi.type.IntegerType)9 SmallintType (io.prestosql.spi.type.SmallintType)9 TinyintType (io.prestosql.spi.type.TinyintType)8