摘要:
mysql列存储引擎-自定义函数-return返回select语句结果异常-问题定位
相关问题: https://stoneatom.yuque.com/staff-ft8n1u/luxxoy/dc73f9
DDL:
表结构:
CREATE TABLE `employees` (
`employee_id` int(11) NOT NULL AUTO_INCREMENT,
`employee_name` varchar(50) NOT NULL,
`employee_sex` varchar(10) DEFAULT '男',
`hire_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`employee_mgr` int(11) DEFAULT NULL,
`employee_salary` float DEFAULT '3000',
`department_id` int(11) DEFAULT NULL,
PRIMARY KEY (`employee_id`),
KEY `idx_department_id` (`department_id`)
) ENGINE=TIANMU DEFAULT CHARSET=utf8mb4;
function:
DELIMITER $$
CREATE FUNCTION GetEmployeeInformationByID(id INT)
RETURNS VARCHAR(300)
BEGIN
return (SELECT employee_name FROM employees WHERE employee_id=id);
END $$
DELIMITER ;
插入数据:
insert into employees(employee_id,employee_name) values(1, 'David Tian');
insert into employees(employee_id,employee_name) values(2, 'James');
出问题的SQL:
select GetEmployeeInformationByID(1);
问题定位:
追踪问题过程中的上下文:
(gdb) bt
#0 sp_eval_expr (thd=0x7f620c001420, result_field=0x7f620c0ae5f8, expr_item_ptr=0x7f620c0b80e8) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sp.cc:2774
#1 0x00000000022efd13 in sp_rcontext::set_return_value (this=0x7f620ca1e780, thd=0x7f620c001420, return_value_item=0x7f620c0b80e8)
at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sp_rcontext.cc:164
#2 0x00000000022e88fb in sp_instr_freturn::exec_core (this=0x7f620c0b8020, thd=0x7f620c001420, nextp=0x7f85693f5cb8) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sp_instr.cc:1428
#3 0x00000000022e642d in sp_lex_instr::reset_lex_and_exec_core (this=0x7f620c0b8020, thd=0x7f620c001420, nextp=0x7f85693f5cb8, open_tables=true)
at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sp_instr.cc:413
#4 0x00000000022e7128 in sp_lex_instr::validate_lex_and_execute_core (this=0x7f620c0b8020, thd=0x7f620c001420, nextp=0x7f85693f5cb8, open_tables=true)
at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sp_instr.cc:753
#5 0x00000000022e9fca in sp_lex_instr::execute (this=0x7f620c0b8020, thd=0x7f620c001420, nextp=0x7f85693f5cb8) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sp_instr.h:307
#6 0x00000000022dfe2e in sp_head::execute (this=0x7f620c0b0160, thd=0x7f620c001420, merge_da_on_success=true) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sp_head.cc:796
#7 0x00000000022e11fa in sp_head::execute_function (this=0x7f620c0b0160, thd=0x7f620c001420, argp=0x7f620c006a78, argcount=1, return_value_fld=0x7f620c0ae5f8)
at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sp_head.cc:1287
#8 0x0000000001dcebf3 in Item_func_sp::execute_impl (this=0x7f620c0069d0, thd=0x7f620c001420) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/item_func.cc:8706
#9 0x0000000001dce974 in Item_func_sp::execute (this=0x7f620c0069d0) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/item_func.cc:8637
#10 0x0000000001dd2b65 in Item_func_sp::val_str (this=0x7f620c0069d0, str=0x7f85693f7160) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/item_func.h:2780
#11 0x0000000001d5529f in Item::send (this=0x7f620c0069d0, protocol=0x7f620c0025c8, buffer=0x7f85693f7160) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/item.cc:7541
#12 0x000000000233d651 in THD::send_result_set_row (this=0x7f620c001420, row_items=0x7f620c005d68) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sql_class.cc:4757
#13 0x0000000002337ab8 in Query_result_send::send_data (this=0x7f620c007b68, items=...) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sql_class.cc:2742
#14 0x000000000234e369 in JOIN::exec (this=0x7f620c0aeb58) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sql_executor.cc:165
#15 0x0000000002c74e9b in Tianmu::core::Engine::Execute (this=0x52eeb50, thd=0x7f620c001420, lex=0x7f620c003748, result_output=0x7f620c007b68, unit_for_union=0x0)
at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/storage/tianmu/core/engine_execute.cpp:381
#16 0x0000000002c744f6 in Tianmu::core::Engine::HandleSelect (this=0x52eeb50, thd=0x7f620c001420, lex=0x7f620c003748, result=@0x7f85693f7cd8: 0x7f620c007b68, setup_tables_done_option=0,
res=@0x7f85693f7cd4: 32645, optimize_after_tianmu=@0x7f85693f7ccc: 1, tianmu_free_join=@0x7f85693f7cd0: 1, with_insert=0)
at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/storage/tianmu/core/engine_execute.cpp:238
#17 0x0000000002d5d8eb in Tianmu::dbhandler::TIANMU_HandleSelect (thd=0x7f620c001420, lex=0x7f620c003748, result=@0x7f85693f7cd8: 0x7f620c007b68, setup_tables_done_option=0,
res=@0x7f85693f7cd4: 32645, optimize_after_tianmu=@0x7f85693f7ccc: 1, tianmu_free_join=@0x7f85693f7cd0: 1, with_insert=0)
at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/storage/tianmu/handler/ha_rcengine.cpp:83
#18 0x000000000239d7e4 in execute_sqlcom_select (thd=0x7f620c001420, all_tables=0x0) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sql_parse.cc:5182
#19 0x0000000002396b68 in mysql_execute_command (thd=0x7f620c001420, first_level=true) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sql_parse.cc:2831
#20 0x000000000239e7ad in mysql_parse (thd=0x7f620c001420, parser_state=0x7f85693f8e70) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sql_parse.cc:5621
#21 0x0000000002393a45 in dispatch_command (thd=0x7f620c001420, com_data=0x7f85693f9610, command=COM_QUERY) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sql_parse.cc:1495
#22 0x0000000002392971 in do_command (thd=0x7f620c001420) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/sql_parse.cc:1034
#23 0x00000000024c5571 in handle_connection (arg=0x177d2e30) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/sql/conn_handler/connection_handler_per_thread.cc:313
#24 0x0000000002baac1c in pfs_spawn_thread (arg=0x177f4580) at /home/jenkins/workspace/stonedb5.7-zsl-centos7.9-30-119/storage/perfschema/pfs.cc:2197
#25 0x00007f85740b8ea5 in start_thread () from /lib64/libpthread.so.0
#26 0x00007f8571335b0d in clone () from /lib64/libc.so.6
错误原因:
现在列存储引擎的主键索引存在问题, 如果列为主键则查询出错。
去除主键属性,自定义函数的查询结果正确