private static ObjectInspector getObjectInspectorForPrimitiveConstant()

in flink-connector-hive/src/main/java/org/apache/flink/table/functions/hive/conversion/HiveInspectors.java [471:573]


    private static ObjectInspector getObjectInspectorForPrimitiveConstant(
            PrimitiveTypeInfo primitiveTypeInfo, @Nullable Object value, HiveShim hiveShim) {
        String className;
        value = hiveShim.hivePrimitiveToWritable(value);
        switch (primitiveTypeInfo.getPrimitiveCategory()) {
            case BOOLEAN:
                className = WritableConstantBooleanObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, BooleanWritable.class, value);
            case BYTE:
                className = WritableConstantByteObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, ByteWritable.class, value);
            case SHORT:
                className = WritableConstantShortObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, ShortWritable.class, value);
            case INT:
                className = WritableConstantIntObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, IntWritable.class, value);
            case LONG:
                className = WritableConstantLongObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, LongWritable.class, value);
            case FLOAT:
                className = WritableConstantFloatObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, FloatWritable.class, value);
            case DOUBLE:
                className = WritableConstantDoubleObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, DoubleWritable.class, value);
            case STRING:
                className = WritableConstantStringObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, Text.class, value);
            case CHAR:
                try {
                    Constructor<WritableConstantHiveCharObjectInspector> constructor =
                            WritableConstantHiveCharObjectInspector.class.getDeclaredConstructor(
                                    CharTypeInfo.class, HiveCharWritable.class);
                    constructor.setAccessible(true);
                    return constructor.newInstance(primitiveTypeInfo, value);
                } catch (Exception e) {
                    throw new FlinkHiveUDFException(
                            "Failed to create writable constant object inspector", e);
                }
            case VARCHAR:
                try {
                    Constructor<WritableConstantHiveVarcharObjectInspector> constructor =
                            WritableConstantHiveVarcharObjectInspector.class.getDeclaredConstructor(
                                    VarcharTypeInfo.class, HiveVarcharWritable.class);
                    constructor.setAccessible(true);
                    return constructor.newInstance(primitiveTypeInfo, value);
                } catch (Exception e) {
                    throw new FlinkHiveUDFException(
                            "Failed to create writable constant object inspector", e);
                }
            case DATE:
                className = WritableConstantDateObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, hiveShim.getDateWritableClass(), value);
            case TIMESTAMP:
                className = WritableConstantTimestampObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, hiveShim.getTimestampWritableClass(), value);
            case DECIMAL:
                try {
                    Constructor<WritableConstantHiveDecimalObjectInspector> constructor =
                            WritableConstantHiveDecimalObjectInspector.class.getDeclaredConstructor(
                                    DecimalTypeInfo.class, HiveDecimalWritable.class);
                    constructor.setAccessible(true);
                    return constructor.newInstance(primitiveTypeInfo, value);
                } catch (Exception e) {
                    throw new FlinkHiveUDFException(
                            "Failed to create writable constant object inspector", e);
                }
            case BINARY:
                className = WritableConstantBinaryObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, BytesWritable.class, value);
            case VOID:
                try {
                    Constructor<WritableVoidObjectInspector> constructor =
                            WritableVoidObjectInspector.class.getDeclaredConstructor();
                    constructor.setAccessible(true);
                    return constructor.newInstance();
                } catch (Exception e) {
                    throw new FlinkHiveUDFException(
                            "Failed to create writable constant object inspector", e);
                }
            case UNKNOWN:
                // If type is unknown, we use the Constant String to replace
                className = WritableConstantStringObjectInspector.class.getName();
                return HiveReflectionUtils.createConstantObjectInspector(
                        className, Text.class, value == null ? null : value.toString());
            default:
                throw new FlinkHiveUDFException(
                        String.format(
                                "Cannot find ConstantObjectInspector for %s", primitiveTypeInfo));
        }
    }