public static List getResultExpression()

in fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnBE.java [354:584]


    public static List<Literal> getResultExpression(DataType type, PValues resultContent) {
        List<Literal> res = new ArrayList<>();
        if (type.isNullType()) {
            int num = resultContent.getNullMapCount();
            for (int i = 0; i < num; ++i) {
                res.add(new NullLiteral(type));
            }
        } else if (type.isBooleanType()) {
            int num = resultContent.getUint32ValueCount();
            for (int i = 0; i < num; ++i) {
                Literal literal = BooleanLiteral.of(resultContent.getUint32Value(i) != 0);
                res.add(literal);
            }
        } else if (type.isTinyIntType()) {
            int num = resultContent.getInt32ValueCount();
            for (int i = 0; i < num; ++i) {
                Literal literal = new TinyIntLiteral((byte) resultContent.getInt32Value(i));
                res.add(literal);
            }
        } else if (type.isSmallIntType()) {
            int num = resultContent.getInt32ValueCount();
            for (int i = 0; i < num; ++i) {
                Literal literal = new SmallIntLiteral((short) resultContent.getInt32Value(i));
                res.add(literal);
            }
        } else if (type.isIntegerType()) {
            int num = resultContent.getInt32ValueCount();
            for (int i = 0; i < num; ++i) {
                Literal literal = new IntegerLiteral(resultContent.getInt32Value(i));
                res.add(literal);
            }
        } else if (type.isBigIntType()) {
            int num = resultContent.getInt64ValueCount();
            for (int i = 0; i < num; ++i) {
                Literal literal = new BigIntLiteral(resultContent.getInt64Value(i));
                res.add(literal);
            }
        } else if (type.isLargeIntType()) {
            int num = resultContent.getBytesValueCount();
            for (int i = 0; i < num; ++i) {
                ByteString bytesValue = resultContent.getBytesValue(i);
                byte[] bytes = convertByteOrder(bytesValue.toByteArray());
                BigInteger convertedBigInteger = new BigInteger(bytes);
                Literal literal = new LargeIntLiteral(convertedBigInteger);
                res.add(literal);
            }
        } else if (type.isFloatType()) {
            int num = resultContent.getFloatValueCount();
            for (int i = 0; i < num; ++i) {
                float value = resultContent.getFloatValue(i);
                Literal literal = null;
                if (Float.isNaN(value)) {
                    literal = new NullLiteral(type);
                } else {
                    literal = new FloatLiteral(value);
                }
                res.add(literal);
            }
        } else if (type.isDoubleType()) {
            int num = resultContent.getDoubleValueCount();
            for (int i = 0; i < num; ++i) {
                double value = resultContent.getDoubleValue(i);
                Literal literal = null;
                if (Double.isNaN(value)) {
                    literal = new NullLiteral(type);
                } else {
                    literal = new DoubleLiteral(value);
                }
                res.add(literal);
            }
        } else if (type.isDecimalV2Type()) {
            int num = resultContent.getBytesValueCount();
            for (int i = 0; i < num; ++i) {
                ByteString bytesValue = resultContent.getBytesValue(i);
                byte[] bytes = convertByteOrder(bytesValue.toByteArray());
                BigInteger value = new BigInteger(bytes);
                BigDecimal bigDecimal = new BigDecimal(value, 9); // decimalv2 scale always 9
                Literal literal = new DecimalLiteral(bigDecimal);
                res.add(literal);
            }
        } else if (type.isDecimalV3Type()) {
            int num = resultContent.getBytesValueCount();
            DecimalV3Type decimalV3Type = (DecimalV3Type) type;
            for (int i = 0; i < num; ++i) {
                ByteString bytesValue = resultContent.getBytesValue(i);
                byte[] bytes = convertByteOrder(bytesValue.toByteArray());
                BigInteger value = new BigInteger(bytes);
                BigDecimal bigDecimal = new BigDecimal(value, decimalV3Type.getScale());
                Literal literal = new DecimalV3Literal(decimalV3Type, bigDecimal);
                res.add(literal);
            }
        } else if (type.isDateTimeV2Type()) {
            int num = resultContent.getUint64ValueCount();
            for (int i = 0; i < num; ++i) {
                long uint64Value = resultContent.getUint64Value(i);
                LocalDateTime dateTimeV2 = convertToJavaDateTimeV2(uint64Value);
                if (dateTimeV2 == null && resultContent.hasHasNull()) {
                    res.add(new NullLiteral(type));
                } else {
                    Literal literal = new DateTimeV2Literal((DateTimeV2Type) type, dateTimeV2.getYear(),
                            dateTimeV2.getMonthValue(), dateTimeV2.getDayOfMonth(), dateTimeV2.getHour(),
                            dateTimeV2.getMinute(), dateTimeV2.getSecond(), dateTimeV2.getNano() / 1000);
                    res.add(literal);
                }
            }
        } else if (type.isDateV2Type()) {
            int num = resultContent.getUint32ValueCount();
            for (int i = 0; i < num; ++i) {
                int uint32Value = resultContent.getUint32Value(i);
                LocalDate localDate = convertToJavaDateV2(uint32Value);
                if (localDate == null && resultContent.hasHasNull()) {
                    res.add(new NullLiteral(type));
                } else {
                    DateV2Literal dateV2Literal = new DateV2Literal(localDate.getYear(), localDate.getMonthValue(),
                            localDate.getDayOfMonth());
                    res.add(dateV2Literal);
                }
            }
        } else if (type.isIPv4Type()) {
            int num = resultContent.getUint32ValueCount();
            for (int i = 0; i < num; ++i) {
                Inet4Address inet4Address = InetAddresses.fromInteger(resultContent.getUint32Value(i));
                IPv4Literal iPv4Literal = new IPv4Literal(inet4Address.getHostAddress());
                res.add(iPv4Literal);
            }
        } else if (type.isJsonType()) {
            int num = resultContent.getStringValueCount();
            for (int i = 0; i < num; ++i) {
                String stringValue = resultContent.getStringValue(i);
                // maybe need handle NULL_IN_CSV_FOR_ORDINARY_TYPE = "\\N";
                if ("\\N".equalsIgnoreCase(stringValue) && resultContent.hasHasNull()) {
                    res.add(new NullLiteral(type));
                } else {
                    res.add(new JsonLiteral(stringValue));
                }
            }
        } else if (type.isStringLikeType()) {
            int num = resultContent.getStringValueCount();
            for (int i = 0; i < num; ++i) {
                // get the raw byte data to avoid character encoding conversion problems
                ByteString bytesValues = resultContent.getBytesValue(i);
                // use UTF-8 encoding to ensure proper handling of binary data
                String stringValue = bytesValues.toStringUtf8();
                // handle special NULL value cases
                if ("\\N".equalsIgnoreCase(stringValue) && resultContent.hasHasNull()) {
                    res.add(new NullLiteral(type));
                } else {
                    res.add(new StringLiteral(stringValue));
                }
            }
        } else if (type.isArrayType()) {
            ArrayType arrayType = (ArrayType) type;
            int childCount = resultContent.getChildElementCount();
            List<Literal> allLiterals = new ArrayList<>();
            for (int i = 0; i < childCount; ++i) {
                allLiterals.addAll(getResultExpression(arrayType.getItemType(),
                        resultContent.getChildElement(i)));
            }
            int offsetCount = resultContent.getChildOffsetCount();
            if (offsetCount == 1) {
                ArrayLiteral arrayLiteral = new ArrayLiteral(allLiterals, arrayType);
                res.add(arrayLiteral);
            } else {
                for (int i = 0; i < offsetCount; ++i) {
                    List<Literal> childLiteral = new ArrayList<>();
                    int startOffset = (int) ((i == 0) ? 0 : resultContent.getChildOffset(i - 1));
                    int endOffset = (int) resultContent.getChildOffset(i);
                    for (int off = startOffset; off < endOffset; ++off) {
                        childLiteral.add(allLiterals.get(off));
                    }
                    ArrayLiteral arrayLiteral = new ArrayLiteral(childLiteral, arrayType);
                    res.add(arrayLiteral);
                }
            }
        } else if (type.isMapType()) {
            MapType mapType = (MapType) type;
            int childCount = resultContent.getChildElementCount();
            List<Literal> allKeys = new ArrayList<>();
            List<Literal> allValues = new ArrayList<>();
            for (int i = 0; i < childCount; i = i + 2) {
                allKeys.addAll(getResultExpression(mapType.getKeyType(),
                        resultContent.getChildElement(i)));
                allValues.addAll(getResultExpression(mapType.getValueType(),
                        resultContent.getChildElement(i + 1)));
            }
            int offsetCount = resultContent.getChildOffsetCount();
            if (offsetCount == 1) {
                MapLiteral mapLiteral = new MapLiteral(allKeys, allValues, mapType);
                res.add(mapLiteral);
            } else {
                for (int i = 0; i < offsetCount; ++i) {
                    List<Literal> keyLiteral = new ArrayList<>();
                    List<Literal> valueLiteral = new ArrayList<>();
                    int startOffset = (int) ((i == 0) ? 0 : resultContent.getChildOffset(i - 1));
                    int endOffset = (int) resultContent.getChildOffset(i);
                    for (int off = startOffset; off < endOffset; ++off) {
                        keyLiteral.add(allKeys.get(off));
                        valueLiteral.add(allValues.get(off));
                    }
                    MapLiteral mapLiteral = new MapLiteral(keyLiteral, valueLiteral, mapType);
                    res.add(mapLiteral);
                }
            }
        } else if (type.isStructType()) {
            StructType structType = (StructType) type;
            int childCount = resultContent.getChildElementCount();
            List<List<Literal>> allFields = new ArrayList<>();
            for (int i = 0; i < childCount; ++i) {
                allFields.add(getResultExpression(structType.getFields().get(i).getDataType(),
                        resultContent.getChildElement(i)));
            }
            for (int i = 0; i < allFields.get(0).size(); ++i) {
                List<Literal> fields = new ArrayList<>();
                for (int child = 0; child < childCount; ++child) {
                    fields.add(allFields.get(child).get(i));
                }
                StructLiteral structLiteral = new StructLiteral(fields, structType);
                res.add(structLiteral);
            }
        } else {
            LOG.warn("the type: {} is not support, should implement it", type.toString());
        }
        if (resultContent.hasHasNull()) {
            for (int i = 0; i < resultContent.getNullMapCount(); ++i) {
                if (resultContent.getNullMap(i)) {
                    res.set(i, new NullLiteral(type));
                }
            }
        }
        return res;
    }