摘要:用戶創建hash分佈表,使用pbe方式執行使用分佈列作為查詢條件的語句時報錯 本文分享自華為雲社區《GaussDB(DWS)現網案例之collation報錯》,作者: 你是猴子請來的救兵嗎 。 用戶創建hash分佈表,使用pbe方式執行使用分佈列作為查詢條件的語句時報錯,ERROR: coul ...
摘要:用戶創建hash分佈表,使用pbe方式執行使用分佈列作為查詢條件的語句時報錯
本文分享自華為雲社區《GaussDB(DWS)現網案例之collation報錯》,作者: 你是猴子請來的救兵嗎 。
用戶創建hash分佈表,使用pbe方式執行使用分佈列作為查詢條件的語句時報錯,ERROR: could not determine which collation to use for string hashing
問題背景
內核版本:GaussDB 8.1.3
業務框架:jalor + mybatis
問題描述:用戶創建hash分佈表,使用pbe方式執行使用分佈列作為查詢條件的語句時報錯,ERROR: could not determine which collation to use for string hashing

根因分析
源表為hash分佈表,當使用分佈列作為查詢條件時,可以通過節點分區剪枝提升性能;
分佈列類型為nvarchar2(100),構造pbe剪枝語句時,需要對傳入變數進行類型轉換和精度轉換,未正確更新collation,導致執行報錯

場景復現
建表數據
drop table t1; create table t1(c1 nvarchar2(5),c2 varchar)with (orientation=column)distribute by hash(c1);--分佈列類型為nvarchar2(n) insert into t1(c1) values(generate_series(1,10));
場景1:client + p/e
prepare c1(nvarchar2) as select c2 from t1 where c1 = $1; execute c1(5);
場景2:jdbc + p/b/e
PreparedStatement pstmt = con.prepareStatement("select c2 from t1 where c1 = ?;"); pstmt.setString(1, "5"); ResultSet rs = pstmt.executeQuery();
場景3:jalor + *Dao.*.xml
<delete id="query"> select c2 from t1 where c1 = #{c1} </delete>
規避辦法
任選一種既可,推薦第一種,改動小影響小
1,將分佈列類型nvarchar2(n)修改為nvarchar2或varchar(n)
2,使用拼接sql的辦法執行語句,而不是pbe
3,語句中指定collate子句,如select c2 from t1 where c1 collate "default" = ?;
4,升級版本
知識小結
問題條件:
1,內核版本8.1.3 ≤ version ≤ 8.1.3.300
2,分佈列包含nvarchar2(n)類型欄位
3,使用pbe的方式執行語句
4,語句過濾條件包含所有分佈列
規避方法:
打破以上任一條件即可規避