Writing My Own Extension for Village SQL Part I

Since my previous post on Village SQL, life has been filled with too many other things. But finally I was able this past weekend to circle back and try to write my own extension.

The Village SQL TL;DR -> MySQL with PostgreSQL like extensions.  

The  documentation on writing new extensions seems complete, but I have run into issues that are probably caused by me, and not the docs.  

Starting with a bad joke 

I heard a joke about a husband who was sent to the store by a long suffering wife because she said a recipe needed five cubed potatoes.  So he bought one hundred and twenty five potatoes!  That made me think a first extension that cubes an integer might be a good idea.

But there are problems

I haven't written anything serious in C/C++ in a very long time. I asked Grok to create the code for me.

#include <villagesql/extension.h>
#include <cstdint>   // for int64_t (optional but recommended)

// Cube implementation - correct VEF numeric handling
void cube_impl(vef_context_t* ctx, vef_invalue_t* arg, vef_vdf_result_t* result) {
    if (arg->is_null) {
        result->type = VEF_RESULT_NULL;
        return;
    }

    int64_t val = arg->int_value;          // ← Correct field (was longlong_value)
    int64_t cubed = val * val * val;       // Note: may overflow int64_t for very large values

    result->int_value = cubed;             // ← Correct field
    result->type = VEF_RESULT_VALUE;
}

// Extension registration - use INT (not BIGINT)
VEF_GENERATE_ENTRY_POINTS(
    make_extension("vsql_cube", "0.0.1")   // Change name/version as you like
        .func(make_func<&cube_impl>("cube")
            .returns(INT)                  // ← Must be INT
            .param(INT)                    // ← Must be INT
            .build())
);

And I am pedantic the first time I try anything new. So I have been plodding.

The good news is that the extension is compiled and ready to go. But ...

mysql> install extension vsql_cube;
ERROR 3219 (HY000): Failed to parse manifest.json in 'vsql_cube.veb': Invalid value. at offset 0
mysql> 

Yup, the .veb is in the right place. But now I have to track down this manifest thingy.

More to come 

 I have had many kind inquiries about more on Village SQL and I am slowing making progress. Once I figure out what I am doing wrong, there will be a much more detailed post about this cube extension so others will have a cookbook-like guide for their own machinations.  

 

 

Comments

Popular posts from this blog

Migrating From MySQL To PostgreSQL in Five Simple Steps

Village SQL - A MySQL Fork With Easy Extensions

Do You Need An AI Assistant?