postgres.git / summary / log / commit / refs

commit    0c6d95e134a195aba19f8c8544934db22a48a3c2
Author:   David Rowley <drowley@postgresql.org>
Date:     Tue Nov 17 23:43:30 2026 +0000

    Resolve mismatch between JIT and non-JIT hashing
    
    adf97c156 taught expression evaluation to produce hash values.  That
    required both JIT and non-JIT versions.  The JIT version wasn't quite
    correct as it didn't truncate the produced hash value down to 32 bits
    before rotating and combining with the previously stored intermediate hash
    value when hashing multiple keys.
    
    When a hash function returned a negative int32, the value was
    sign-extended to 64 bits, and the JIT code's 1-bit left rotation then
    shifted those set upper bits into the lower 32, setting every bit of the
    intermediate hash to 1 no matter what the earlier keys hashed to.  This
    meant all rows for which an earlier key had a negative hash value
    effectively hashed only on the remaining keys, causing many hash
    collisions resulting in slow hash aggregates and hash joins.
    
    Here we fix by adjusting the JIT code to match what the non-JIT version
    does.
    
    Backpatch to 18, where adf97c156 was introduced.
    
    Reported-by: Tor Erik Linnerud <tel@jklm.no>
    Author: David Rowley <dgrowleyml@gmail.com>
    Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
    Discussion: https://postgr.es/m/CAEGP1v12rRmZo1h3KZ+ZCS66A9xLoSOuLAyBXu92nd97E3jJPw@mail.gmail.com
    Backpatch-through: 18


src/backend/jit/llvm/llvmjit_expr.c | 41 ++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/backend/jit/llvm/llvmjit_expr.c b/src/backend/jit/llvm/llvmjit_expr.c index 29617437477..660026a7196 100644 --- a/src/backend/jit/llvm/llvmjit_expr.c +++ b/src/backend/jit/llvm/llvmjit_expr.c @@ -2062,17 +2062,14 @@ llvm_compile_expr(ExprState *state) * previous hash operation stored it. */ v_prevhash = l_load(b, TypeDatum, tmp, "prevhash"); + v_prevhash = LLVMBuildTrunc(b, v_prevhash, + LLVMInt32TypeInContext(lc), ""); - /* - * Rotate bits left by 1 bit. Be careful not to - * overflow uint32 when working with Datum. - */ - v_tmp1 = LLVMBuildShl(b, v_prevhash, l_datum_const(1), + /* Rotate the 32-bit hash value left by 1 bit */ + v_tmp1 = LLVMBuildShl(b, v_prevhash, l_int32_const(lc, 1), ""); - v_tmp1 = LLVMBuildAnd(b, v_tmp1, - l_datum_const(0xffffffff), ""); v_tmp2 = LLVMBuildLShr(b, v_prevhash, - l_datum_const(31), ""); + l_int32_const(lc, 31), ""); v_prevhash = LLVMBuildOr(b, v_tmp1, v_tmp2, "rotatedhash"); } @@ -2138,7 +2135,9 @@ llvm_compile_expr(ExprState *state) * Save the rotated hash value and skip to the * next op. */ - LLVMBuildStore(b, v_prevhash, v_resvaluep); + LLVMBuildStore(b, + LLVMBuildZExt(b, v_prevhash, TypeDatum, ""), + v_resvaluep); } else { @@ -2190,15 +2189,18 @@ llvm_compile_expr(ExprState *state) v_prevhash = l_load(b, TypeDatum, tmp, "prevhash"); /* - * Rotate bits left by 1 bit. Be careful not to - * overflow uint32 when working with Datum. + * Truncate the value to 32 bits. This is equivalent + * to the DatumGetUInt32 in execExprInterp.c's + * EEOP_HASHDATUM_NEXT32_STRICT case. */ - v_tmp1 = LLVMBuildShl(b, v_prevhash, l_datum_const(1), + v_prevhash = LLVMBuildTrunc(b, v_prevhash, + LLVMInt32TypeInContext(lc), ""); + + /* Rotate the 32-bit hash value left by 1 bit */ + v_tmp1 = LLVMBuildShl(b, v_prevhash, l_int32_const(lc, 1), ""); - v_tmp1 = LLVMBuildAnd(b, v_tmp1, - l_datum_const(0xffffffff), ""); v_tmp2 = LLVMBuildLShr(b, v_prevhash, - l_datum_const(31), ""); + l_int32_const(lc, 31), ""); v_prevhash = LLVMBuildOr(b, v_tmp1, v_tmp2, "rotatedhash"); } @@ -2209,12 +2211,19 @@ llvm_compile_expr(ExprState *state) /* * For NEXT32 ops, XOR (^) the returned hash value with - * the existing hash value. + * the existing hash value. We truncate to 32 bits to + * match the DatumGetUInt32() usages in execExprInterp.c's + * EEOP_HASHDATUM_NEXT32_* cases. */ if (opcode == EEOP_HASHDATUM_NEXT32 || opcode == EEOP_HASHDATUM_NEXT32_STRICT) + { + v_retval = LLVMBuildTrunc(b, v_retval, + LLVMInt32TypeInContext(lc), ""); v_retval = LLVMBuildXor(b, v_prevhash, v_retval, "xorhash"); + v_retval = LLVMBuildZExt(b, v_retval, TypeDatum, ""); + } LLVMBuildStore(b, v_retval, v_resvaluep); LLVMBuildStore(b, l_sbool_const(0), v_resnullp); [parent: 898f60dd7559]