From bcffbbaa7ca6691a1d6883e08e5d9f2dbf5e21c8 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Tue, 17 Nov 2026 10:23:02 +1300 Subject: [PATCH v1] 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 Author: David Rowley 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 fc80d3e55fe..2f3742c56f3 100644 --- a/src/backend/jit/llvm/llvmjit_expr.c +++ b/src/backend/jit/llvm/llvmjit_expr.c @@ -2060,17 +2060,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"); } @@ -2136,7 +2133,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 { @@ -2188,15 +2187,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"); } @@ -2207,12 +2209,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); -- 2.53.0