Skip to content

Application defined window function triggers an assertion failure #1271

@DerZc

Description

@DerZc

Describe the bug

Hi,

The following java program triggers an assertion failure in SQLite, could you please help me point out what the issue is?

import org.sqlite.Function;
import java.sql.*;

public class test {
    public static void main(String[] args) {
        try {
            String cmd = "rm ./test.db";
            Runtime.getRuntime().exec(cmd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db")) {
            Adf.create(conn);

            Statement stmt = conn.createStatement();
            stmt.execute("CREATE TABLE t1 (c0 REAL);");
            stmt.execute("INSERT INTO t1 VALUES (1);");
            ResultSet rs = stmt.executeQuery("SELECT  adf() OVER ( ROWS BETWEEN  1 PRECEDING AND 2 PRECEDING) AS res FROM t1;");

            while (rs.next()) {
                System.out.println("Results: " + rs.getString("res"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class Adf extends Function.Window {
    @Override
    protected void xStep() throws SQLException {}

    @Override
    protected void xInverse() throws SQLException {}

    @Override
    protected void xValue() throws SQLException {
        result("1");
    }
    @Override
    protected void xFinal() throws SQLException {
        result("1");
    }
    public static void create(Connection conn) throws Exception {
        Function.Window.create(conn, "adf", new Adf());
    }
}

This is the error message java: src/main/java/org/sqlite/core/NativeDB.c:397: xValue: Assertion*func' failed.

I tried to write the following C program to reproduce this issus, but it can run normally.

#undef NDEBUG
#include <stdio.h>
#include <assert.h>
#include "sqlite3.h"

void xStep(sqlite3_context* c,int argc,sqlite3_value**argv){}
void xInverse(sqlite3_context* c,int argc,sqlite3_value**argv){}
void xFinal(sqlite3_context* c){
  sqlite3_result_int64(c, 1);
}
void xValue(sqlite3_context* c) {
  sqlite3_result_int64(c, 1);
}

int main(int argc, char const * const * argv){
  int rc = 0;
  sqlite3 * db = 0;
  sqlite3_stmt *q = 0;

  rc = sqlite3_open(":memory:", &db);
  assert( 0==rc );
  rc = sqlite3_create_window_function(db, "adf", -1, 0, 0, xStep, xFinal, xValue, xInverse, 0);
  assert( 0==rc );
  rc = sqlite3_exec(db, "CREATE TABLE t1 (c0 REAL);"
                    "INSERT INTO t1 VALUES (1);",
                    0, 0, 0);
  assert( 0==rc );
  rc = sqlite3_prepare(db, "SELECT adf() OVER ( ROWS BETWEEN  1 PRECEDING AND 2 PRECEDING) FROM t1;", -1, &q, 0);
  assert( 0==rc );
  while( SQLITE_ROW== (rc = sqlite3_step(q)) ){
    printf("res: %s\n", sqlite3_column_text(q, 0));
  }
  sqlite3_finalize(q);
  assert( SQLITE_DONE==rc );
  rc = 0;
  sqlite3_close(db);
  return rc;
}

Environment (please complete the following information):

  • OS: [Ubuntu 24.04]
  • CPU architecture: [x86_64]
  • sqlite-jdbc version [e.g. 3.49.1.0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions