From 47394313f6e4f63e453b07ee229d046996883aa6 Mon Sep 17 00:00:00 2001 From: Ruihao Chen Date: Tue, 17 Mar 2026 05:57:02 -0400 Subject: [PATCH 1/2] expression: enable SHA2 function pushdown to TiKV TiKV already implements SHA2 in its coprocessor (impl_encryption.rs), and TiDB has the PbCode mapping (ScalarFuncSig_SHA2) and distsql deserialization support, but SHA2 was missing from the scalarExprSupportedByTiKV allowlist. This adds it alongside the existing MD5 and SHA1 entries. Verified with tiup playground that all hash lengths (0/224/256/384/512) and all column types (varchar, char, text, blob, binary, int, bigint, double, decimal, datetime, date, time, bit, enum, set) produce identical results between TiDB-local and TiKV-pushdown evaluation. Co-Authored-By: Claude Opus 4.6 (1M context) --- pkg/expression/infer_pushdown.go | 2 +- .../r/planner/core/casetest/pushdown/push_down.result | 5 +++++ .../t/planner/core/casetest/pushdown/push_down.test | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/expression/infer_pushdown.go b/pkg/expression/infer_pushdown.go index c4c8a8e98f8aa..c7f4272d0d936 100644 --- a/pkg/expression/infer_pushdown.go +++ b/pkg/expression/infer_pushdown.go @@ -241,7 +241,7 @@ func scalarExprSupportedByTiKV(ctx EvalContext, sf *ScalarFunction) bool { ast.Sysdate, /* ast.StrToDate, */ // encryption functions. - ast.MD5, ast.SHA1, ast.UncompressedLength, + ast.MD5, ast.SHA1, ast.SHA2, ast.UncompressedLength, ast.Cast, diff --git a/tests/integrationtest/r/planner/core/casetest/pushdown/push_down.result b/tests/integrationtest/r/planner/core/casetest/pushdown/push_down.result index 0eff3497318a6..afe14075c08a6 100644 --- a/tests/integrationtest/r/planner/core/casetest/pushdown/push_down.result +++ b/tests/integrationtest/r/planner/core/casetest/pushdown/push_down.result @@ -121,6 +121,11 @@ Projection root date_format(planner__core__casetest__pushdown__push_down.t.t, % explain format = 'plan_tree' select md5(s) from t; id task access object operator info Projection root md5(planner__core__casetest__pushdown__push_down.t.s)->Column +└─TableReader root data:TableFullScan + └─TableFullScan cop[tikv] table:t keep order:false, stats:pseudo +explain format = 'plan_tree' select sha2(s, 256) from t; +id task access object operator info +Projection root sha2(planner__core__casetest__pushdown__push_down.t.s, 256)->Column └─TableReader root data:TableFullScan └─TableFullScan cop[tikv] table:t keep order:false, stats:pseudo explain format = 'plan_tree' select c from t where a+1=3; diff --git a/tests/integrationtest/t/planner/core/casetest/pushdown/push_down.test b/tests/integrationtest/t/planner/core/casetest/pushdown/push_down.test index 3f33751537faf..949945ec0d64b 100644 --- a/tests/integrationtest/t/planner/core/casetest/pushdown/push_down.test +++ b/tests/integrationtest/t/planner/core/casetest/pushdown/push_down.test @@ -35,6 +35,7 @@ explain format = 'plan_tree' select json_unquote(a) from t2; explain format = 'plan_tree' select i * 2 from t; explain format = 'plan_tree' select DATE_FORMAT(t, '%Y-%m-%d %H') as date from t; explain format = 'plan_tree' select md5(s) from t; +explain format = 'plan_tree' select sha2(s, 256) from t; explain format = 'plan_tree' select c from t where a+1=3; explain format = 'plan_tree' select /*+ hash_agg()*/ count(b) from (select id + 1 as b from t)A; explain format = 'plan_tree' select /*+ hash_agg()*/ count(*) from (select id + 1 as b from t)A; From 5feb1e7cae9e8edae0cf66b4574f51e6fd947f25 Mon Sep 17 00:00:00 2001 From: Ruihao Chen Date: Tue, 17 Mar 2026 22:02:11 -0400 Subject: [PATCH 2/2] expression: use WHERE predicate to verify SHA2 TiKV pushdown Change the integration test from a SELECT projection to a WHERE predicate so the explain plan shows Selection cop[tikv] with sha2(), proving the function is actually evaluated on TiKV. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../r/planner/core/casetest/pushdown/push_down.result | 6 +++--- .../t/planner/core/casetest/pushdown/push_down.test | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/integrationtest/r/planner/core/casetest/pushdown/push_down.result b/tests/integrationtest/r/planner/core/casetest/pushdown/push_down.result index afe14075c08a6..6dc3a1f12bc4a 100644 --- a/tests/integrationtest/r/planner/core/casetest/pushdown/push_down.result +++ b/tests/integrationtest/r/planner/core/casetest/pushdown/push_down.result @@ -123,10 +123,10 @@ id task access object operator info Projection root md5(planner__core__casetest__pushdown__push_down.t.s)->Column └─TableReader root data:TableFullScan └─TableFullScan cop[tikv] table:t keep order:false, stats:pseudo -explain format = 'plan_tree' select sha2(s, 256) from t; +explain format = 'plan_tree' select * from t where sha2(s, 256) = 'abc'; id task access object operator info -Projection root sha2(planner__core__casetest__pushdown__push_down.t.s, 256)->Column -└─TableReader root data:TableFullScan +TableReader root data:Selection +└─Selection cop[tikv] eq(sha2(planner__core__casetest__pushdown__push_down.t.s, 256), "abc") └─TableFullScan cop[tikv] table:t keep order:false, stats:pseudo explain format = 'plan_tree' select c from t where a+1=3; id task access object operator info diff --git a/tests/integrationtest/t/planner/core/casetest/pushdown/push_down.test b/tests/integrationtest/t/planner/core/casetest/pushdown/push_down.test index 949945ec0d64b..fa2ac1ac64287 100644 --- a/tests/integrationtest/t/planner/core/casetest/pushdown/push_down.test +++ b/tests/integrationtest/t/planner/core/casetest/pushdown/push_down.test @@ -35,7 +35,7 @@ explain format = 'plan_tree' select json_unquote(a) from t2; explain format = 'plan_tree' select i * 2 from t; explain format = 'plan_tree' select DATE_FORMAT(t, '%Y-%m-%d %H') as date from t; explain format = 'plan_tree' select md5(s) from t; -explain format = 'plan_tree' select sha2(s, 256) from t; +explain format = 'plan_tree' select * from t where sha2(s, 256) = 'abc'; explain format = 'plan_tree' select c from t where a+1=3; explain format = 'plan_tree' select /*+ hash_agg()*/ count(b) from (select id + 1 as b from t)A; explain format = 'plan_tree' select /*+ hash_agg()*/ count(*) from (select id + 1 as b from t)A;