Search code examples
ignite

User defined functions in Apache Ignite


I think I must be missing something in the documentation. In a typical DBMS system you would be able to write a UDF which consumes data from another table. Its not at all clear how one would do that from an Ignite UDF. Has anyone done something similar, or is it even possible to do something like this (obviously contrived)?

static class SqlFunctions {
    @QuerySqlFunction
    public static int cpi_rate(int x) {
        /*sql = select rate from "RATES".cpi where x=?*/
        return x;
    }
}


Solution

  • Ignite is built on top of H2 DataBase for SQL processing thereafter it supports user-defined functions written in Java:

    public static ResultSet query(Connection conn, String sql) throws SQLException {
        return conn.createStatement().executeQuery(sql);
    }
    
    CREATE ALIAS QUERY FOR "org.h2.samples.Function.query";
    CALL QUERY('SELECT * FROM TEST');
    

    Since it's possible to access another table or cache within a custom function, doing this might have side effects and even block your original query execution if used improperly.

    That's said, I think, it's better either to run cpi_rate explicitly inside a larger query, for example as a sub-select statement, depending on your needs. Or you might run cpi_rate query periodically and cache execution result inside a simple Java hashmap or something like that, later you can filter that static map without any side effects.