diff --git a/fe/src/main/java/org/apache/doris/analysis/LiteralExpr.java b/fe/src/main/java/org/apache/doris/analysis/LiteralExpr.java index 74ac79a52783cb..faf423d3fa288a 100644 --- a/fe/src/main/java/org/apache/doris/analysis/LiteralExpr.java +++ b/fe/src/main/java/org/apache/doris/analysis/LiteralExpr.java @@ -124,6 +124,8 @@ public Object getRealValue() { public abstract boolean isMinValue(); + // Only used by partition pruning and the derived class which can be used for pruning + // must handle MaxLiteral. public abstract int compareLiteral(LiteralExpr expr); // Returns the string representation of the literal's value. Used when passing diff --git a/fe/src/main/java/org/apache/doris/catalog/PartitionKey.java b/fe/src/main/java/org/apache/doris/catalog/PartitionKey.java index 2b070f60c2c6e7..211c522df787c0 100644 --- a/fe/src/main/java/org/apache/doris/catalog/PartitionKey.java +++ b/fe/src/main/java/org/apache/doris/catalog/PartitionKey.java @@ -134,7 +134,27 @@ public int compareTo(PartitionKey other) { int other_key_len = other.keys.size(); int min_len = Math.min(this_key_len, other_key_len); for (int i = 0; i < min_len; ++i) { - int ret = keys.get(i).compareLiteral(other.keys.get(i)); + final LiteralExpr oldKey = this.getKeys().get(i); + final LiteralExpr otherOldKey = other.getKeys().get(i); + int ret = 0; + if (oldKey instanceof MaxLiteral || otherOldKey instanceof MaxLiteral) { + ret = oldKey.compareLiteral(otherOldKey); + } else { + final Type destType = Type.getAssignmentCompatibleType(oldKey.getType(), otherOldKey.getType(), false); + try { + LiteralExpr newKey = oldKey; + if (oldKey.getType() != destType) { + newKey = (LiteralExpr) oldKey.castTo(destType); + } + LiteralExpr newOtherKey = otherOldKey; + if (otherOldKey.getType() != destType) { + newOtherKey = (LiteralExpr) otherOldKey.castTo(destType); + } + ret = newKey.compareLiteral(newOtherKey); + } catch (AnalysisException e) { + throw new RuntimeException("Cast error in partition"); + } + } if (0 != ret) { return ret; }