GithubHelp home page GithubHelp logo

Comments (2)

Totktonada avatar Totktonada commented on May 17, 2024

Fast-written & dirty workaround for both cases described above.

local _slow_xflatten_helper
_slow_xflatten_helper = function(schema_names, top_path, obj_partial)
    local exps = {}
    for k, v in pairs(obj_partial) do
        local cur_path = (top_path == '') and k or (top_path .. '.' .. k)
        if type(v) == 'table' then
            local ok, new_exps = _slow_xflatten_helper(
                schema_names, cur_path, v)
            if not ok then
                return false, new_exps
            end
            for _, exp in ipairs(new_exps) do
                exps[#exps + 1] = exp
            end
        else
            local column_no = -1
            for i, name in ipairs(schema_names) do
                if name == cur_path then
                    column_no = i
                    break
                end
            end
            if column_no == -1 then
                local reason = 'cannot find fields name: ' .. cur_path
                return false, reason
            end
            exps[#exps + 1] = {'=', column_no, v}
        end
    end
    return true, exps
end

-- Note: no array, no enum support
local slow_xflatten = function(schema_handle, obj_partial)
    local schema_names = avro.get_names(schema_handle)
    return _slow_xflatten_helper(schema_names, '', obj_partial)
end

local xflatten_wrapper = function(model, schema_handle, obj_partial)
    local valid_exps = function(exps)
        for _, exp in ipairs(exps) do
            local valid_exp = type(exp) == 'table' and #exp == 3
            if valid_exp then
                -- XXX: more checks?
                -- local op, field_no, value = unpack(exp)
            else
                return false
            end
        end
        return true
    end
    
    local ok, exps = model.xflatten(obj_partial)
    local unkn_msg = 'Internal error: unknown code'
    local is_case_1 = not ok and type(exps) == 'string' and exps == unkn_msg
    local is_case_2 = ok and type(exps) == 'table' and not valid_exps(exps)
    if not (is_case_1 or is_case_2) then
        return ok, exps
    end
    return slow_xflatten(schema_handle, obj_partial)
end

Then invoke xflatten_wrapper(model, schema_handle, obj_partial) instead of model.xflatten(obj_partial), where schema_handle is what avro.create() returns and model is what avro.compile() returns (assuming local avro = require('avro_schema')).

from avro-schema.

Totktonada avatar Totktonada commented on May 17, 2024

Works correctly in both cases with debug (w/o optimization):

local _, model = avro.compile({schema_handler, debug=true})

The input of unparse_msgpack w/o optimization:

unparse_msgpack; *typeid: 0x0B (11) -- PUTARRAYC; value: 2   !!!
unparse_msgpack; *typeid: 0x0B (11) -- PUTARRAYC; value: 3
unparse_msgpack; *typeid: 0x12 (18) -- PUTSTRC; value: 1
unparse_msgpack; *typeid: 0x04 (4) -- PUTINT / PUTLONG; value: 2
unparse_msgpack; *typeid: 0x08 (8) -- PUTSTR; value: 3
unparse_msgpack; *typeid: 0x0B (11) -- PUTARRAYC; value: 3
unparse_msgpack; *typeid: 0x12 (18) -- PUTSTRC; value: 1
unparse_msgpack; *typeid: 0x04 (4) -- PUTINT / PUTLONG; value: 1
unparse_msgpack; *typeid: 0x08 (8) -- PUTSTR; value: 3

The input w/ optimization (broken):

unparse_msgpack; *typeid: 0x0B (11) -- PUTARRAYC; value: 3   !!!
unparse_msgpack; *typeid: 0x0B (11) -- PUTARRAYC; value: 3
unparse_msgpack; *typeid: 0x12 (18) -- PUTSTRC; value: 1
unparse_msgpack; *typeid: 0x04 (4) -- PUTINT / PUTLONG; value: 2
unparse_msgpack; *typeid: 0x08 (8) -- PUTSTR; value: 3
unparse_msgpack; *typeid: 0x00 (0) -- (zero); value: 0       !!!
unparse_msgpack; *typeid: 0x0B (11) -- PUTARRAYC; value: 3
unparse_msgpack; *typeid: 0x12 (18) -- PUTSTRC; value: 1
unparse_msgpack; *typeid: 0x04 (4) -- PUTINT / PUTLONG; value: 1
unparse_msgpack; *typeid: 0x08 (8) -- PUTSTR; value: 3

from avro-schema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.