lua entry thread aborted: runtime error: /usr/../process.lua:448: attempt to concatenate field 'np_sum_duration' (a userdata value)
【1】问题场景原代码
引起问题的原代码,访问数据库,汇总数据后,使用汇总结果报异常:
local function amount_sum_fee(cycleid) local select_productid = "select product_id from np_cdr_" .. cycleid .. " group by product_id;" local rtn, productid_set = executeSql(select_productid) if rtn then local sql_const = "insert into compare_fee_" .. cycleid .. " (product_id, np_id, np_sum_duration, np_total_times, " .. " np_sum_qty, np_sum_fee, un_sum_duration, un_total_times, un_sum_fee, sum_np_fee, sum_original_fee) values " if type(productid_set) == 'table' and next(productid_set) ~= nil then for key, record in pairs(productid_set) do local amount_table = {} amount_table["product_id"] = record["product_id"] amount_table["np_id"] = 'all' -- np总费用统计 local np_sum_fee = "SELECT product_id, SUM(duration) AS np_sum_duration, " .. " COUNT(id) AS np_sum_cdrCnt, " .. " SUM(service_qty) AS np_service_qty, " .. " SUM(service_fee) AS np_service_fee, " .. " SUM(np_fee) AS np_sum_fee, " .. " SUM(original_fee) AS np_sum_original_fee " .. " FROM np_cdr_" .. cycleid .. " WHERE product_id = '" .. record["product_id"] .. "';" local rtn, desc = executeSql(np_sum_fee) if rtn and next(desc) ~= nil then amount_table[]["np_sum_duration"] amount_table[]["np_sum_cdrCnt"] amount_table[]["np_service_qty"] amount_table[]["np_service_fee"] amount_table[]["np_sum_fee"] amount_table[]["np_sum_original_fee"] end -- un总费用统计 local un_sum_fee = "SELECT product_id, SUM(total_duration) AS un_sum_duration, " .. " SUM(total_times) AS un_sum_cdrCnt, " .. " SUM(total_fee) AS un_sum_fee " .. " FROM dat_bill_" .. cycleid .. " WHERE call_type & 4097 > 0 AND product_id = '" .. record["product_id"] .. "';" local rtn, desc = executeSql(un_sum_fee) if rtn and next(desc) ~= nil then amount_table[]["un_sum_duration"] amount_table[]["un_sum_cdrCnt"] amount_table[]["un_sum_fee"] end -- 构建sql语句 then insert_sql = insert_sql .. "," else insert_sql = sql_const end insert_sql = insert_sql .. "('" .. amount_table["product_id"] .. "', '" .. amount_table["np_id"] .. "', " .. amount_table["np_sum_duration"] .. ", " .. amount_table["np_total_times"] .. ", " .. amount_table["np_sum_qty"] .. ", " .. amount_table["np_sum_fee"] .. ", " .. amount_table["un_sum_duration"] .. ", " .. amount_table["un_total_times"].. ", " .. amount_table["un_sum_fee"] .. ", " .. amount_table["sum_np_fee"] .. ", " .. amount_table["sum_original_fee"] .. ")" value_count = value_count + == value_count then sum_count = sum_count + value_count insert_sql = insert_sql .. "; commit;" local rtn, result = executeSql(insert_sql) if not rtn then rtnstr[ rtnstr["msg"] = "Error. amount_sum_fee insert into sum_fee num : " .. sum_count .. " sql : " .. insert_sql return false else local result = "compare: success to amount_sum_fee insert into sum_fee num : " .. sum_count ngx.log(ngx.ERR, result) end insert_sql = "" value_count = end end then sum_count = sum_count + value_count insert_sql = insert_sql .. "; commit;" local rtn, result = executeSql(insert_sql) if not rtn then rtnstr[ rtnstr["msg"] = "Error. amount_sum_fee insert into sum_fee num : " .. sum_count .. " sql : " .. insert_sql return false else local result = "compare: success to amount_sum_fee insert into sum_fee num : " .. sum_count ngx.log(ngx.ERR, result) end insert_sql = "" value_count = end end else rtnstr[ rtnstr["msg"] = "Failed to select product_id amount_sum_fee error : " .. productid_set return false end return true end
使用的是SQL中的sum函数
【2】分析原因
数据库访问汇总数据时,SUM函数的结果可能为NULL值。如下图:
修改SQL语句,添加IFNULL判断,若为NULL,则为0即可。如下图:
如上原因分析。
【3】修复后代码
local function amount_sum_fee(cycleid) local select_productid = "select product_id from np_cdr_" .. cycleid .. " group by product_id;" local rtn, productid_set = executeSql(select_productid) if rtn then local sql_const = "insert into compare_fee_" .. cycleid .. " (product_id, np_id, np_sum_duration, np_total_times, " .. " np_sum_qty, np_sum_fee, un_sum_duration, un_total_times, un_sum_fee, sum_np_fee, sum_original_fee) values " if type(productid_set) == 'table' and next(productid_set) ~= nil then for key, record in pairs(productid_set) do local amount_table = {} amount_table["product_id"] = record["product_id"] amount_table["np_id"] = 'all' -- np总费用统计 local np_sum_fee = "SELECT product_id, IFNULL(SUM(duration), 0) AS np_sum_duration, " .. " COUNT(id) AS np_sum_cdrCnt, " .. " IFNULL(SUM(service_qty), 0) AS np_service_qty, " .. " IFNULL(SUM(service_fee), 0) AS np_service_fee, " .. " IFNULL(SUM(np_fee), 0) AS np_sum_fee, " .. " IFNULL(SUM(original_fee), 0) AS np_sum_original_fee " .. " FROM np_cdr_" .. cycleid .. " WHERE product_id = '" .. record["product_id"] .. "';" local rtn, desc = executeSql(np_sum_fee) if rtn and next(desc) ~= nil then amount_table[]["np_sum_duration"] amount_table[]["np_sum_cdrCnt"] amount_table[]["np_service_qty"] amount_table[]["np_service_fee"] amount_table[]["np_sum_fee"] amount_table[]["np_sum_original_fee"] end -- un总费用统计 local un_sum_fee = "SELECT product_id, IFNULL(SUM(total_duration), 0) AS un_sum_duration, " .. " IFNULL(SUM(total_times), 0) AS un_sum_cdrCnt, " .. " IFNULL(SUM(total_fee), 0) AS un_sum_fee " .. " FROM dat_bill_" .. cycleid .. " WHERE call_type & 4097 > 0 AND product_id = '" .. record["product_id"] .. "';" local rtn, desc = executeSql(un_sum_fee) if rtn and next(desc) ~= nil then amount_table[]["un_sum_duration"] amount_table[]["un_sum_cdrCnt"] amount_table[]["un_sum_fee"] end -- 构建sql语句 then insert_sql = insert_sql .. "," else insert_sql = sql_const end insert_sql = insert_sql .. "('" .. amount_table["product_id"] .. "', '" .. amount_table["np_id"] .. "', " .. amount_table["np_sum_duration"] .. ", " .. amount_table["np_total_times"] .. ", " .. amount_table["np_sum_qty"] .. ", " .. amount_table["np_sum_fee"] .. ", " .. amount_table["un_sum_duration"] .. ", " .. amount_table["un_total_times"].. ", " .. amount_table["un_sum_fee"] .. ", " .. amount_table["sum_np_fee"] .. ", " .. amount_table["sum_original_fee"] .. ")" value_count = value_count + == value_count then sum_count = sum_count + value_count insert_sql = insert_sql .. "; commit;" local rtn, result = executeSql(insert_sql) if not rtn then rtnstr[ rtnstr["msg"] = "Error. amount_sum_fee insert into sum_fee num : " .. sum_count .. " sql : " .. insert_sql return false else local result = "compare: success to amount_sum_fee insert into sum_fee num : " .. sum_count ngx.log(ngx.ERR, result) end insert_sql = "" value_count = end end then sum_count = sum_count + value_count insert_sql = insert_sql .. "; commit;" local rtn, result = executeSql(insert_sql) if not rtn then rtnstr[ rtnstr["msg"] = "Error. amount_sum_fee insert into sum_fee num : " .. sum_count .. " sql : " .. insert_sql return false else local result = "compare: success to amount_sum_fee insert into sum_fee num : " .. sum_count ngx.log(ngx.ERR, result) end insert_sql = "" value_count = end end else rtnstr[ rtnstr["msg"] = "Failed to select product_id amount_sum_fee error : " .. productid_set return false end return true end
【4】总结
Lua中使用SQL语句进行汇总数据时,切记需要作NULL判断。
Good Good Study, Day Day Up.
顺序 选择 循环 总结
lua entry thread aborted: runtime error: /usr/../process.lua:448: attempt to concatenate field 'np_sum_duration' (a userdata value)的更多相关文章
- openresty 报错:lua entry thread aborted: runtime error
[1]问题现象 (1)本地openresty系统 (2)报错信息 2019/09/10 08:13:55 [error] 2385#2385: *4 lua entry thread aborted: ...
- /usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "process_linux.go:245: running exec setns .....
docker创建容器时报错如下: containerd: start container" error="oci runtime error: container_linux.go ...
- centos7.2部署docker-17.06.0-ce的bug:Error response from daemon: oci runtime error: container_linux.go:262: starting container process caused "process_linux.go:339: container init caused \"\"".
现象: 操作系统:centos 7.2 kernel 3.10.0-327.el7.x86_64 mesos:1.3.0 docker:docker-17.06.0-ce 在做mesos验证时,通过m ...
- error: /usr/include/objc/objc-class.h: No such file or directory
When i use the example of ShareKit package,i have come across this error:"error: /usr/include/o ...
- Heka 编译安装后 运行报错 panic: runtime error: cgo argument has Go pointer to Go pointer
Heka 编译安装后 运行报错 panic: runtime error: cgo argument has Go pointer to Go pointer 解决办法: 1. Start heka ...
- ERROR: child process failed, exited with error number 100
[root@localhost ~]# mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --loga ...
- mongodb启动时报错ERROR: child process failed, exited with error number 1
不多说,直接上干货! 前期博客 Ubuntu14.04下Mongodb安装部署步骤(图文详解) Ubuntu16.04下Mongodb安装部署步骤(图文详解) root@zhouls-virtual- ...
- Finished with error: ProcessException: Process "D:\FlutterAPP\flutter_appfive\android\gradlew.bat" exited abnormally:
在使用Flutter进行开发是遇到这样一个问题 Finished with error: ProcessException: Process "D:\FlutterAPP\flutter_a ...
- lua.c:82:10: fatal error: readline/readline.h: No such file or directory #include <readline/readline.h>
make linuxcd src && make linuxmake[1]: Entering directory `/root/lua/lua-5.3.2/src'make all ...
随机推荐
- response.Close、response.End、response.Flush区别(下载文件)
今天在做文件下载功能用到的是response的方法,首先我们要了解这些方法的作用. 1.response.write():将信息写入http响应输出流. 2.response.Flush:向客户端发送 ...
- ES6入门系列 ----- Reflect
Reflect 是ES6 为了操作对象而提供的新的API, 目的是: 将Object 上一些明显属于语言内部的方法,比如 Object.defineProperty 放到 Reflect对象上 ...
- Vue+element 修改样式的scoped穿透方法
我们在修改element的一些样式的时候,在加了scoped的时候会不起作用,下面是解决方案: 解决方法:起一个类名将页面包裹起来,后面加 /deep/ <style scoped> 1 ...
- Flink流式计算
Structured Streaming A stream is converted into a dynamic table. A continuous query is evaluated on ...
- javascript 初探
JS ,前端3剑客之一,控制HTML标签的动作.浏览器通过解释JS代码识别ta要做什么,因为在浏览器操作,所以最好使用谷歌浏览器. 参考: https://www.cnblogs.com/yuanch ...
- 浅析Volatile关键字
浅析Volatile关键字 在java中线程并发中,线程之间通信方式分为两种:共享内存和消息传递.共享内存指的是多个线程之间共享内存的属性状态:消息传递指的是线程之间发送信息来通信.在介绍volati ...
- 【servlet】Servlet快速入门&使用Eclipse发布web项目
创建时间:6.15 1.什么是Servlet Servlet 运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是ja ...
- Wpf DataGrid动态添加列,行数据(一)
由于最近有这方面的需求,而且刚接触wpf不久,在网上找了很多方法,都不是使用MVVM模式的,因为DataGrid的列不能绑定 这就难受了,我想了个折中的方法,这个是使用了MVVMLight的消息机制, ...
- 异步HttpClient大量请求
由于项目中有用到HttpClient异步发送大量http请求,所以做已记录 思路:使用HttpClient连接池,多线程 public class HttpAsyncClient { private ...
- 03-docker入门-创建 docker 镜像
方法1:从运行的容器创建方法2:编写 DockFile 文件创建 方法1: 打包镜像 docker commit -m "Test a change" 610 ubuntu:tes ...