Comparing two strings identically return false


#1

Hi all,

I have a problem with my UDF lua file when I trying to compare 2 strings(one from record and another one from arguments). If I declare 2 variables and compare it I will get true as a result, but when I want to compare for example record[key] == value(where key and value are received as arguments, I get false)

From the info() when I are 2 identical strings, result of comparison is false.

Thank you very much

Here is my code:

local function my_email_filter(key, value)
    return function(record)
        info('RESULT is %s and %s', record[key] == value )
        info('%s | %s', record[key], value)
        if record[key] == value then
            return true
        else
            return false
        end
    end
end

local function mapper(record)
    local out = map()
    out['email'] = record['email']
    out['userId'] = record['userId']
    return out
end

function count(stream, key, email)
    local myfilter = my_email_filter(key, email)

    return stream : filter(myfilter) : map(mapper)
end

#2

I think you are not passing the record to filter function.

local myfilter = my_email_filter(record, key, email) … Then in my_email_filter(record, key, email) use the type check before compare.

local val = record[key] if type(val) == ‘string’ then …

Also see: How to use Aggregation?

I typically put my filter function inside the main function i am calling so i can pass my arguments to it without changing the signature of the filter function. If I recall, API says filter function can be passed only one argument - the record. So keen to know if your construct works.


#3

I tried what you said but, in that info() call I have those strings identically. I change my function to this, and I have the same results. Stored bins of type string should convert to something or ? Result of the info(rec[key]) and info(email) are the same and evaluation of that is false.

local function mapper(record)
    local out = map()
    out['email'] = record['email']
    out['userId'] = record['userId']
    return out
end

function count(stream, key, email)
    local function my_email_filter(rec)
        if type(rec[key]) ~= 'string' then
            return false
        end

        info('RESULT is %s', rec[key] == email )
        info('%s | %s', rec[key], email)
        if rec[key] == email then
            return true
        else
            return false
        end
    end

    return stream : filter(my_email_filter) : map(mapper)
end

#4

I assume “key” is the name of a bin in your record… correct?


#5

Yes, the “key” is the name of the bin.


#6

Just tested, works for me. Difference is: I have local val = rec[key]
and then I work with val.

See https://github.com/pygupta/aerospike-discuss/tree/master/topic3807_luastrCompare

aql>run ‘runscript’

insertData inserts sample data


#7

Thank you very much for your effort. It’s seems that problems comes from email because it’s contains non alpha chars like paul+test@gmail.com

I didn’t have a solution how to handle this type of strings to compare if it’s equals. if i look for paul@gmail.com it will compare as string but when I tried to look for paul+test@gmail.com it’s fail.

Thank you.


#8

I don’t see why that should be a problem. I tested my code, I can match with special chars. See updated github.

±--------------------------------+ | str_equal | ±--------------------------------+ | MAP(’{“key”:“a+b@gmail.com”}’) | ±--------------------------------+


#9

Thank you very much.

Maybe it’s something from me. Now, your script is working fine to me.