Skip to content

Commit

Permalink
implement isnull / not null for filter expressions
Browse files Browse the repository at this point in the history
Allow us to write queries like:

	SELECT name, type, sql FROM sqlite_schema where sql isnull

and

	SELECT name, type, sql FROM sqlite_schema where sql not null
  • Loading branch information
glommer committed Jan 30, 2025
1 parent f086c1b commit effde1c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/translate/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,22 @@ pub fn translate_condition_expr(
);
}
}
ast::Expr::NotNull(expr) => {
let cur_reg = program.alloc_register();
translate_expr(program, Some(referenced_tables), expr, cur_reg, resolver)?;
program.emit_insn(Insn::IsNull {
src: cur_reg,
target_pc: condition_metadata.jump_target_when_false,
});
}
ast::Expr::IsNull(expr) => {
let cur_reg = program.alloc_register();
translate_expr(program, Some(referenced_tables), expr, cur_reg, resolver)?;
program.emit_insn(Insn::NotNull {
reg: cur_reg,
target_pc: condition_metadata.jump_target_when_false,
});
}
_ => todo!("op {:?} not implemented", expr),
}
Ok(())
Expand Down
8 changes: 8 additions & 0 deletions testing/where.test
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ do_execsql_test where-clause-eq-string {
select count(1) from users where last_name = 'Rodriguez';
} {61}

do_execsql_test where-clause-isnull {
select count(1) from users where last_name isnull;
} {0}

do_execsql_test where-clause-notnull {
select count(1) from users where last_name not null;
} {10000}

do_execsql_test where-clause-ne {
select count(1) from users where id != 2000;
} {9999}
Expand Down

0 comments on commit effde1c

Please sign in to comment.