当前位置:网站首页>TiFlash 函数下推必知必会丨十分钟成为 TiFlash Contributor
TiFlash 函数下推必知必会丨十分钟成为 TiFlash Contributor
2022-06-22 11:16:00 【InfoQ】
背景知识

手把手教你下推函数
1. 确认要下推的函数的行为
- 主要逻辑
- 返回值类型
- 异常处理
- etc
sqrtfloat64DecimalevalRealfloorceilDecimalsqrtNaNNull2. 将 TiDB function 映射到 TiFlash function
tipb::ScalarFuncSigfunc_nametipb::ScalarFuncSigfunc_namefunc_nametipb::ScalarFuncSigfunc_name
window functionaggregate functiondistinct aggregation functionscalar functionwindow_func_map
- 用于 window function
agg_func_map
- 用于普通的聚合函数
distinct_agg_func_map
- 用于 distinct 的聚合函数
scalar_func_map
- 用于一般的标量函数
3. 注册 TiFlash 函数
tipb::ScalarFuncSigfunc_namefunc_name
reuse function
ifNull(arg1, arg2) -> if(isNull(arg1), arg2, arg1)ifNullDAGExpressionAnalyzerHelper::function_builder_mapDAGExpressionAnalyzerHelper::FunctionBuilderDAGExpressionAnalyzerHelper::function_builder_map<func_name, FunctionBuilder>DAGExpressionAnalyzerHelperFunctionBuildercreate function directly
dbms/src/FunctionsFunctionStringfactory.registerFunctionFunctionFactoryfactory.registerFunction4. TiFlash 侧开发函数
IFunctionIFunctionTiFlash vs. TiDB
- C++ 与 Golang 的区别
- TiFlash 里重度使用 C++ 模板去写函数,尤其是涉及数据类型的代码;
- TiFlash 的向量化函数体系和 TiDB 的函数体系(行式/向量化)的不同
- 表达式相关类的设计、使用与TiDB 差别很大
IDataType
IColumn
- 参数的 Column 类型(vector 和 const)组合会爆炸式增长。比如两个参数的 function 会有四种组合
- vector, const
- vector, vector
- const, vector
- const, const
FunctionSubStringIndex可以参考的函数实现
- TiDBConcat
- FunctionSubStringIndex
- Format
5. TiDB 侧下推函数
expression/expression.goscalarExprSupportedByFlashscalarExprSupportedByFlashexpression/expression.goscalarExprSupportedByFlash6. 验证函数真的下推了
- 首先我们需要现在本地启动一个 TiDB,TiKV,TiFlash 和 PD 的集群。这里推荐用 TiUP,按照官方文档安装 TiUP,用 playground 启动即可。
tiup playground nightly --tiflash 1 --db 1 --pd 1 --kv 1- 然后用自己 build 好的 TiDB 和 TiFlash 替换掉原有集群的 TiDB 和 TiFlash
- TiFlash
ps -ef | grep tiflashxzx 11238 11028 52 20:20 pts/0 00:00:05 /home/xzx/.tiup/components/tiflash/v5.0.0-nightly-20210706/tiflash/tiflash server --config-file=/home/xzx/.tiup/data/ScRdWJM/tiflash-0/tiflash.toml11238server --config-file=/home/xzx/.tiup/data/ScRdWJM/tiflash-0/tiflash.tomlkill 11238server --config-file=/home/xzx/.tiup/data/ScRdWJM/tiflash-0/tiflash.toml- TiDB

- 验证下推流程
explain select sum(sqrt(x)) from testcreate table test.t (xxx);
-- 因为通常本地起一个节点, 所以 tiflash 副本数只能设 1
alter table test.t set tiflash replica 1;
-- 尽量使用 MPP
set tidb_enforce_mpp=1;
-- 强制只能走 TiFlash
set tidb_isolation_read_engines='tiflash';
explain select xxxfunc(a) from t;
7. 测试
集成测试
integration-testtests/fullstack-test/exprfunc.testsubstring_index.test单元测试
形式
dbms/src/Functions/testgtest_${func_name}.cpp#include <TestUtils/FunctionTestUtils.h>
#include <TestUtils/TiFlashTestBasic.h>
namespace DB::tests
{
class {gtest_name} : public DB::tests::FunctionTest
{
};
TEST_F({gtest_name}, {gtest_unit_name})
try
{
const String & func_name = {function_name};
// case1
ASSERT_COLUMN_EQ(
{ouput_result},
executeFunction(
func_name,
{input_1},
{input_2},
...,
{input_n},);
// case2
...
// case3
...
}
CATCH
TEST_F({gtest_name}, {gtest_unit_name2})...
TEST_F({gtest_name}, {gtest_unit_name3})...
...
} // namespace DB::tests
createColumn内容
数据类型
列类型
- 如果该 Type 不为 nullable,需要测试两种形式的列:
- ColumnVector
- ColumnConst
- 如果该 Type 为 nullable,需要测试三种形式的列:
- ColumnVector
- ColumnConst(ColumnNullable(non-null value))
- ColumnConst(ColumnNullable(null value))
- 如果该 Type 为 DataTypeNullable(DataTypeNothing), 需要测试两种形式的列:
- ColumnVector
- ColumnConst(ColumnNullable(null value))
边界值
- 数值类型(int,double,decimal 等):最大/最小值,0 值,null 值
- 字符串类型:空字符串,中文等非 ascii 字符,null 值,有 collation/无 collation
- 日期类型:zero date,早于 1970-01-01 的某个时间,夏令时时间,null 值
返回值类型
- Decimal 类型在 TiFlash 的内部表示有四种:Decimal32,Decimal64,Decimal128 和 Decimal256,对于所有 Decimal 类型,这四种内部表示都需要测试到。
- 函数的每个 arg_i 可能的类型实际上应该以 TiDB 可能下推的类型为准,考虑到获取 TiDB 可能下推的类型比较麻烦,当前测试可以根据 TiFlash 目前支持的类型来写
- 有一部分 TiDB 下推的函数中,其下推的函数签名中包含了类型信息,例如对于 a = b ,TiDB 下推的函数签名包括:EQInt,EQReal,EQString,EQDecimal,EQTime,EQDuration,EQJson,虽然 a 和 b 各自都可以是 int/real/string/decimal/time/duration/json 类别,但是 TiDB 下推的时候保证了 a 和 b 的类别是一致的,从工作量角度考虑,当前测试只需要保证相同类别之间的 equal 函数被测试到即可,int = decimal 这种的可以先不测。
- 对于输入参数可以无穷多的函数(例如 case when),需要确保其最小循环单元被测试到。
- 预期测试过程中会发现很多 bug,对于一些比较容易 fix 的 bug,可以在测试的同时顺便 fix,对于一些比较难或者不确定需不需要 fix 的 bug,可以先开 issue,再将相应的测试注释掉。
常见的问题
- 函数即使返回 null,也需要给其对应的 nestedColumn 赋一个有意义的值
- 数值类型:零值
- Date相关类型:zerodate
- 字符串类型:空字符串
- 使用
useDefaultImplementationForConstants()简化函数开发
const, const, ..., constconst, const, ..., constvector, vector, .., vector- 使用
getArgumentsThatAreAlwaysConstant简化函数开发 (不推荐)
限量马克杯获取流程

- 认领 issue,issue 列表:Issue 列表:https://github.com/pingcap/tiflash/issues/5092;
- 提交 PR;
- PR 提交之后,请耐心等待维护者进行 Review;
- 代码提交后 CI 会执行测试,需要保证所有的单元测试是可以通过的。期间可能有其它的提交会与当前 PR 冲突,这时需要修复冲突;
- 维护者在 Review 过程中可能会提出一些修改意见。修改完成之后如果 reviewer 认为没问题了,你会收到 LGTM(looks good to me) 的回复。当收到两个及以上的 LGTM 后,该 PR 将会被合并;
- 合并 PR 后自动成为 Contributor,就可以填表单领取你的专属马克杯啦,表单地址:https://forms.pingcap.com/f/tidb-contribution-swag
- 后台 AI 核查 GitHub ID 及资料信息,确认无误后会快递寄出属于你的限量版马克杯。
边栏推荐
- IO之ByteArrayStream案例
- Microsoft edge browser dev 104 is released, and the deep / shallow theme switching is smoother
- 6-10 global status management - Global store
- [cloud picture] episode 244 three minute understanding of container image service
- Electron adding SQLite database
- Two ways of traversing binary tree: preorder, inorder and postorder
- Obtain the public IP address summary under the command line
- IO之Buffered流案例
- 2022年度敏捷教练行业现状报告(2022 State of Agile Coaching Report)
- 迪利克雷前缀和学习笔记
猜你喜欢

How many of the eight classic MySQL errors did you encounter?

微信小程序项目实例——图片处理小工具(自制低配版美图秀秀)

Pychart debugging is stuck and connected appears

CF751E Phys Ed Online

Wechat applet project example - image processing gadget (self-made low configuration version of Meitu XiuXiu)

配置GPU版本的pytorch和torchvision,初学GPU版本torch踩坑

PHP database mysql question

牛客挑战赛55E题解

NFT交易平台数字藏品系统开发技术
![[Software Engineering] Introduction & process and life cycle modeling](/img/92/433e2fae846406252ee0d4c47bd54b.png)
[Software Engineering] Introduction & process and life cycle modeling
随机推荐
Pychart debugging is stuck and connected appears
promise升级版async,await来袭,搭配try+catch更香哦
奋斗吧,程序员——第三十九章 人生不失意,焉能慕知己
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用summary函数获取模型汇总统计信息
APM set pitch four rotor control mode
TCP connection establishment process (in-depth understanding of the source code and three handshakes)
Rtklib postpos carding (taking single point positioning as an example)
NFT交易平台数字藏品系统开发技术
【软工】计划和项目管理
Exchange sort method
【用户案例-智能制造】数码大方“云”协同,飞跃千山 “保”生产 !
R language performs two sample t-test on the specified covariates based on the with function, and the t.test function performs Welch two sample t-test analysis and two independent sample t-test on the
IO之Reader案例
puzzle(019)平面正推问题
Kruskal reconstruction tree
奋斗吧,程序员——第三十八章 旧时茅店社林边,路转溪头忽见
[Software Engineering] Introduction & process and life cycle modeling
R语言dplyr包mutate函数将dataframe中的两个数值变量相除创建新的数据列(Create a new variable)
Noun analysis: ETL
Call center terminology