eldenmoon commented on code in PR #35318:
URL: https://github.com/apache/doris/pull/35318#discussion_r1616900390


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java:
##########
@@ -396,4 +400,153 @@ public boolean isZero() {
         }
         return false;
     }
+
+    /**
+    ** get paramter length, port from  mysql get_param_length
+    **/
+    public static int getParmLen(ByteBuffer data) {
+        int maxLen = data.remaining();
+        if (maxLen < 1) {
+            return 0;
+        }
+        // get and advance 1 byte
+        int len = MysqlProto.readInt1(data);
+        if (len == 252) {
+            if (maxLen < 3) {
+                return 0;
+            }
+            // get and advance 2 bytes
+            return MysqlProto.readInt2(data);
+        } else if (len == 253) {
+            if (maxLen < 4) {
+                return 0;
+            }
+            // get and advance 3 bytes
+            return MysqlProto.readInt3(data);
+        } else if (len == 254) {
+            /*
+            In our client-server protocol all numbers bigger than 2^24
+            stored as 8 bytes with uint8korr. Here we always know that
+            parameter length is less than 2^4 so we don't look at the second
+            4 bytes. But still we need to obey the protocol hence 9 in the
+            assignment below.
+            */
+            if (maxLen < 9) {
+                return 0;
+            }
+            len = MysqlProto.readInt4(data);
+            MysqlProto.readFixedString(data, 4);
+            return len;
+        } else if (len == 255) {
+            return 0;
+        } else {
+            return len;
+        }
+    }
+
+    /**
+     * Retrieves a Literal object based on the MySQL type and the data 
provided.
+     *
+     * @param mysqlType the MySQL type identifier
+     * @param data      the ByteBuffer containing the data
+     * @return a Literal object corresponding to the MySQL type
+     * @throws AnalysisException if the MySQL type is unsupported or if data 
conversion fails
+     */
+    public static Literal getLiteralByMysqlType(MysqlColType mysqlType, 
ByteBuffer data) throws AnalysisException {
+        switch (mysqlType) {
+            case MYSQL_TYPE_TINY:
+                return new TinyIntLiteral(data.get());
+            case MYSQL_TYPE_SHORT:
+                return new SmallIntLiteral((short) data.getChar());
+            case MYSQL_TYPE_LONG:
+                return new IntegerLiteral(data.getInt());
+            case MYSQL_TYPE_LONGLONG:
+                return new BigIntLiteral(data.getLong());
+            case MYSQL_TYPE_FLOAT:
+                return new FloatLiteral(data.getFloat());
+            case MYSQL_TYPE_DOUBLE:
+                return new DoubleLiteral(data.getDouble());
+            case MYSQL_TYPE_DECIMAL:
+            case MYSQL_TYPE_NEWDECIMAL:
+                return handleDecimalLiteral(data);
+            case MYSQL_TYPE_DATE:
+                return handleDateLiteral(data);
+            case MYSQL_TYPE_DATETIME:
+            case MYSQL_TYPE_TIMESTAMP:
+            case MYSQL_TYPE_TIMESTAMP2:
+                return handleDateTimeLiteral(data);
+            case MYSQL_TYPE_STRING:
+            case MYSQL_TYPE_VARSTRING:
+                return handleStringLiteral(data);
+            case MYSQL_TYPE_VARCHAR:
+                return handleVarcharLiteral(data);
+            default:
+                throw new AnalysisException("Unsupported MySQL type: " + 
mysqlType);
+        }
+    }
+
+    private static DecimalLiteral handleDecimalLiteral(ByteBuffer data) throws 
AnalysisException {
+        int len = getParmLen(data);
+        byte[] bytes = new byte[len];
+        data.get(bytes);
+        try {
+            String value = new String(bytes);
+            BigDecimal v = new BigDecimal(value);
+            return new DecimalLiteral(v);
+        } catch (NumberFormatException e) {
+            throw new AnalysisException("Invalid decimal literal", e);
+        }
+    }
+
+    private static DateLiteral handleDateLiteral(ByteBuffer data) {
+        int len = getParmLen(data);
+        if (len >= 4) {
+            int year = (int) data.getChar();
+            int month = (int) data.get();
+            int day = (int) data.get();
+            return new DateLiteral(year, month, day);
+        } else {
+            return new DateLiteral(0, 1, 1);
+        }
+    }
+
+    private static DateTimeLiteral handleDateTimeLiteral(ByteBuffer data) {
+        int len = getParmLen(data);
+        if (len >= 4) {
+            int year = (int) data.getChar();
+            int month = (int) data.get();
+            int day = (int) data.get();
+            int hour = 0;
+            int minute = 0;
+            int second = 0;
+            int microsecond = 0;
+            if (len > 4) {
+                hour = (int) data.get();
+                minute = (int) data.get();
+                second = (int) data.get();
+            }
+            if (len > 7) {
+                microsecond = data.getInt();
+            }
+            return new DateTimeLiteral(DateTimeType.INSTANCE, year, month, 
day, hour, minute, second, microsecond);

Review Comment:
   done



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java:
##########
@@ -396,4 +400,153 @@ public boolean isZero() {
         }
         return false;
     }
+
+    /**
+    ** get paramter length, port from  mysql get_param_length
+    **/
+    public static int getParmLen(ByteBuffer data) {
+        int maxLen = data.remaining();
+        if (maxLen < 1) {
+            return 0;
+        }
+        // get and advance 1 byte
+        int len = MysqlProto.readInt1(data);
+        if (len == 252) {
+            if (maxLen < 3) {
+                return 0;
+            }
+            // get and advance 2 bytes
+            return MysqlProto.readInt2(data);
+        } else if (len == 253) {
+            if (maxLen < 4) {
+                return 0;
+            }
+            // get and advance 3 bytes
+            return MysqlProto.readInt3(data);
+        } else if (len == 254) {
+            /*
+            In our client-server protocol all numbers bigger than 2^24
+            stored as 8 bytes with uint8korr. Here we always know that
+            parameter length is less than 2^4 so we don't look at the second
+            4 bytes. But still we need to obey the protocol hence 9 in the
+            assignment below.
+            */
+            if (maxLen < 9) {
+                return 0;
+            }
+            len = MysqlProto.readInt4(data);
+            MysqlProto.readFixedString(data, 4);
+            return len;
+        } else if (len == 255) {
+            return 0;
+        } else {
+            return len;
+        }
+    }
+
+    /**
+     * Retrieves a Literal object based on the MySQL type and the data 
provided.
+     *
+     * @param mysqlType the MySQL type identifier
+     * @param data      the ByteBuffer containing the data
+     * @return a Literal object corresponding to the MySQL type
+     * @throws AnalysisException if the MySQL type is unsupported or if data 
conversion fails
+     */
+    public static Literal getLiteralByMysqlType(MysqlColType mysqlType, 
ByteBuffer data) throws AnalysisException {
+        switch (mysqlType) {
+            case MYSQL_TYPE_TINY:
+                return new TinyIntLiteral(data.get());
+            case MYSQL_TYPE_SHORT:
+                return new SmallIntLiteral((short) data.getChar());
+            case MYSQL_TYPE_LONG:
+                return new IntegerLiteral(data.getInt());
+            case MYSQL_TYPE_LONGLONG:
+                return new BigIntLiteral(data.getLong());
+            case MYSQL_TYPE_FLOAT:
+                return new FloatLiteral(data.getFloat());
+            case MYSQL_TYPE_DOUBLE:
+                return new DoubleLiteral(data.getDouble());
+            case MYSQL_TYPE_DECIMAL:
+            case MYSQL_TYPE_NEWDECIMAL:
+                return handleDecimalLiteral(data);
+            case MYSQL_TYPE_DATE:
+                return handleDateLiteral(data);
+            case MYSQL_TYPE_DATETIME:
+            case MYSQL_TYPE_TIMESTAMP:
+            case MYSQL_TYPE_TIMESTAMP2:
+                return handleDateTimeLiteral(data);
+            case MYSQL_TYPE_STRING:
+            case MYSQL_TYPE_VARSTRING:
+                return handleStringLiteral(data);
+            case MYSQL_TYPE_VARCHAR:
+                return handleVarcharLiteral(data);
+            default:
+                throw new AnalysisException("Unsupported MySQL type: " + 
mysqlType);
+        }
+    }
+
+    private static DecimalLiteral handleDecimalLiteral(ByteBuffer data) throws 
AnalysisException {
+        int len = getParmLen(data);
+        byte[] bytes = new byte[len];
+        data.get(bytes);
+        try {
+            String value = new String(bytes);
+            BigDecimal v = new BigDecimal(value);
+            return new DecimalLiteral(v);

Review Comment:
   done



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/TinyIntLiteral.java:
##########
@@ -65,4 +65,5 @@ public int getIntValue() {
     public Number getNumber() {
         return value;
     }
+

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to