# Lua udf reduce function seems not excuted

**URL:** <https://discuss.aerospike.com/t/lua-udf-reduce-function-seems-not-excuted/4214>\
**Category:** General Discussion\
**Tags:** udf\
**Created:** [June 1, 2017, 3:30am UTC](https://discuss.aerospike.com/t/lua-udf-reduce-function-seems-not-excuted/4214 "2017-06-01T03:30:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiss01](https://avatars.discourse-cdn.com/v4/letter/k/aca169/32.png) [@kiss01](https://discuss.aerospike.com/u/kiss01)\
**Post date:** [June 1, 2017, 3:30am UTC](https://discuss.aerospike.com/t/lua-udf-reduce-function-seems-not-excuted/4214/1 "2017-06-01T03:30:43Z")

</div>

here is my lua udf script

```auto
function distance_to(src_lng, src_lat, des_lng, des_lat)
    xSide2 = (src_lat - des_lat) * (src_lat - des_lat);
    ySide2 = (src_lng - des_lng) * (src_lng - des_lng);
    return math.sqrt(xSide2 + ySide2);
end

function split(str, sep)
    local sep, fields = sep or "\t", {}
    local pattern = string.format("([^%s]+)", sep)
    str:gsub(pattern, function(c) fields[#fields+1] = c end)
    return fields
end

local function reduce_stats(a, b)
    local out = map()
    if (a["dis"] > b["dis"]) then
        out["dis"] = b["dis"]
        out["area"] = b["area"]
    else
        out["dis"] = a["dis"]
        out["area"] = a["area"]
    end
    return out
end

local function map_stat(out, rec)
    local src_lng = out['src_lng']
    local src_lat = out['src_lat']
    local nearest_dis = out['dis']
    local current_area_id = rec['areaid']
    local lbs_info = rec['lbs']
    local lbs_fields = split(lbs_info, 'x')
    local des_lng = tonumber(lbs_fields[1])
    local des_lat = tonumber(lbs_fields[2])
    local current_dis = distance_to(src_lng, src_lat, des_lng, des_lat)
    if ( nearest_dis > current_dis ) then
        out['dis'] = current_dis
        out['area'] = current_area_id
    end
    return out
end

function geo_nearest(stream, lng, lat)
    return stream :
    aggregate(map{src_lng = lng, src_lat = lat, area = "", dis = 100}, map_stat) :
    reduce(reduce_stats)
end

```

which returns a serial of values,not a single one.Is that reduce function didn’t execute on the client node?Need some addtion ops?

---

<div class="post-metadata">

**Author:** ![pgupta](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/pgupta/32/2351_2.png) [@pgupta](https://discuss.aerospike.com/u/pgupta)\
**Post date:** [June 1, 2017, 3:41am UTC](https://discuss.aerospike.com/t/lua-udf-reduce-function-seems-not-excuted/4214/2 "2017-06-01T03:41:17Z")

</div>

See [How do I debug the 'reduce()' operation of a stream UDF?](https://discuss.aerospike.com/t/how-do-i-debug-the-reduce-operation-of-a-stream-udf/2250) and track it all the way through.

---

<div class="post-metadata">

**Author:** ![kiss01](https://avatars.discourse-cdn.com/v4/letter/k/aca169/32.png) [@kiss01](https://discuss.aerospike.com/u/kiss01)\
**Post date:** [June 1, 2017, 12:12pm UTC](https://discuss.aerospike.com/t/lua-udf-reduce-function-seems-not-excuted/4214/3 "2017-06-01T12:12:45Z")

</div>

tks,I have read this.Btw I’m trying to query geospatial information about nearest place to a given geospatial point using hadoop mapreduce program and async aerospike client api.But performance degrades to around 20TPS.Is there some best practise on geospatial query? thanks a lot.

---

<div class="post-metadata">

**Author:** ![pgupta](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/pgupta/32/2351_2.png) [@pgupta](https://discuss.aerospike.com/u/pgupta)\
**Post date:** [June 1, 2017, 1:58pm UTC](https://discuss.aerospike.com/t/lua-udf-reduce-function-seems-not-excuted/4214/4 "2017-06-01T13:58:04Z")

</div>

Perhaps post that as a separate question with different topic - under [Use Cases - Aerospike Community Forum](https://discuss.aerospike.com/c/how-developers-are-using-aerospike/use-cases) ? - so folks doing geo-spatial may be able to comment.
