Skip to content

Commit c8f55c8

Browse files
committed
filter_lua: fix handling of integer/double values (#1932 #1647)
This patch makes to recognize the preferred numeric data type returned by the Lua script in the record fields. Signed-off-by: Eduardo Silva <eduardo@treasure-data.com>
1 parent 702805f commit c8f55c8

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

plugins/filter_lua/lua.c

+31-6
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ static void lua_pushmsgpack(lua_State *l, msgpack_object *o)
4848
break;
4949

5050
case MSGPACK_OBJECT_POSITIVE_INTEGER:
51-
lua_pushnumber(l, (double) o->via.u64);
51+
lua_pushinteger(l, (double) o->via.u64);
5252
break;
5353

5454
case MSGPACK_OBJECT_NEGATIVE_INTEGER:
55-
lua_pushnumber(l, (double) o->via.i64);
55+
lua_pushinteger(l, (double) o->via.i64);
5656
break;
5757

5858
case MSGPACK_OBJECT_FLOAT32:
@@ -157,6 +157,22 @@ static void try_to_convert_data_type(struct lua_filter *lf,
157157
lua_tomsgpack(lf, pck, 0);
158158
}
159159

160+
static int lua_isinteger(lua_State *L, int index)
161+
{
162+
lua_Number n;
163+
lua_Integer i;
164+
165+
if (lua_type(L, index) == LUA_TNUMBER) {
166+
n = lua_tonumber(L, index);
167+
i = lua_tointeger(L, index);
168+
169+
if (i == n) {
170+
return 1;
171+
}
172+
}
173+
return 0;
174+
}
175+
160176
static void lua_tomsgpack(struct lua_filter *lf, msgpack_packer *pck, int index)
161177
{
162178
int len;
@@ -177,8 +193,14 @@ static void lua_tomsgpack(struct lua_filter *lf, msgpack_packer *pck, int index)
177193
break;
178194
case LUA_TNUMBER:
179195
{
180-
double num = lua_tonumber(l, -1 + index);
181-
msgpack_pack_double(pck, num);
196+
if (lua_isinteger(l, -1 + index)) {
197+
int64_t num = lua_tointeger(l, -1 + index);
198+
msgpack_pack_int64(pck, num);
199+
}
200+
else {
201+
double num = lua_tonumber(l, -1 + index);
202+
msgpack_pack_double(pck, num);
203+
}
182204
}
183205
break;
184206
case LUA_TBOOLEAN:
@@ -411,12 +433,15 @@ static int cb_lua_filter(const void *data, size_t bytes,
411433
/* Get timestamp */
412434
flb_time_pop_from_msgpack(&t, &result, &p);
413435
t_orig = t;
414-
ts = flb_time_to_double(&t);
415436

416437
/* Prepare function call, pass 3 arguments, expect 3 return values */
417438
lua_getglobal(ctx->lua->state, ctx->call);
418439
lua_pushstring(ctx->lua->state, tag);
440+
441+
/* Timestamp */
442+
ts = flb_time_to_double(&t);
419443
lua_pushnumber(ctx->lua->state, ts);
444+
420445
lua_pushmsgpack(ctx->lua->state, p);
421446
if (ctx->protected_mode) {
422447
ret = lua_pcall(ctx->lua->state, 3, 3, 0);
@@ -458,7 +483,7 @@ static int cb_lua_filter(const void *data, size_t bytes,
458483
if (l_code == 1) {
459484
flb_time_from_double(&t, l_timestamp);
460485
}
461-
else if(l_code == 2) {
486+
else if (l_code == 2) {
462487
/* Keep the timestamp */
463488
t = t_orig;
464489
}

0 commit comments

Comments
 (0)