Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions crates/qcraft-core/src/ast/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,12 @@ impl Conditions {

/// `field IS NULL`
pub fn is_null(field: FieldRef) -> Self {
Self::comparison(field, CompareOp::IsNull, Expr::Value(Value::Null))
Self::comparison(field, CompareOp::IsNull, Expr::Value(Value::Bool(true)))
}

/// `field IS NOT NULL`
pub fn is_not_null(field: FieldRef) -> Self {
Self::and(vec![ConditionNode::Comparison(Box::new(Comparison {
left: Expr::Field(field),
op: CompareOp::IsNull,
right: Expr::Value(Value::Null),
negate: true,
}))])
Self::comparison(field, CompareOp::IsNull, Expr::Value(Value::Bool(false)))
}

/// `field LIKE pattern` (raw — caller provides the full pattern with wildcards)
Expand Down
11 changes: 10 additions & 1 deletion crates/qcraft-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,16 @@ impl Renderer for PostgresRenderer {
return Ok(());
}
CompareOp::IsNull => {
ctx.keyword("IS NULL");
match right {
Expr::Value(Value::Bool(true)) => ctx.keyword("IS NULL"),
Expr::Value(Value::Bool(false)) => ctx.keyword("IS NOT NULL"),
_ => {
return Err(RenderError::unsupported(
"IsNull",
"IsNull right operand must be a boolean",
));
}
};
return Ok(());
}
CompareOp::Similar => ctx.keyword("SIMILAR TO"),
Expand Down
86 changes: 86 additions & 0 deletions crates/qcraft-postgres/tests/dql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2672,3 +2672,89 @@ fn pg_xor_with_subquery_operand_is_allowed() {
let (sql, _params) = render_expr_pg(e);
assert!(sql.contains(" # "), "got: {sql}");
}

// ---------------------------------------------------------------------------
// IS NULL / IS NOT NULL (value-driven)
// ---------------------------------------------------------------------------

fn users_from() -> Vec<FromItem> {
vec![FromItem::table(SchemaRef::new("users"))]
}

#[test]
fn where_is_null() {
let stmt = QueryStmt {
columns: vec![SelectColumn::Star(None)],
from: Some(users_from()),
where_clause: Some(Conditions::is_null(FieldRef::new("users", "email"))),
..simple_query()
};
assert_eq!(
render(&stmt),
r#"SELECT * FROM "users" WHERE "users"."email" IS NULL"#
);
}

#[test]
fn where_is_not_null() {
let stmt = QueryStmt {
columns: vec![SelectColumn::Star(None)],
from: Some(users_from()),
where_clause: Some(Conditions::is_not_null(FieldRef::new("users", "email"))),
..simple_query()
};
assert_eq!(
render(&stmt),
r#"SELECT * FROM "users" WHERE "users"."email" IS NOT NULL"#
);
}

#[test]
fn where_is_null_negated() {
let stmt = QueryStmt {
columns: vec![SelectColumn::Star(None)],
from: Some(users_from()),
where_clause: Some(Conditions::is_null(FieldRef::new("users", "email")).negated()),
..simple_query()
};
assert_eq!(
render(&stmt),
r#"SELECT * FROM "users" WHERE NOT ("users"."email" IS NULL)"#
);
}

#[test]
fn where_is_not_null_negated() {
let stmt = QueryStmt {
columns: vec![SelectColumn::Star(None)],
from: Some(users_from()),
where_clause: Some(Conditions::is_not_null(FieldRef::new("users", "email")).negated()),
..simple_query()
};
assert_eq!(
render(&stmt),
r#"SELECT * FROM "users" WHERE NOT ("users"."email" IS NOT NULL)"#
);
}

#[test]
fn is_null_non_boolean_right_errors() {
let stmt = QueryStmt {
columns: vec![SelectColumn::Star(None)],
from: Some(users_from()),
where_clause: Some(Conditions::and(vec![ConditionNode::Comparison(Box::new(
Comparison {
left: Expr::Field(FieldRef::new("users", "email")),
op: CompareOp::IsNull,
right: Expr::Value(Value::Null),
negate: false,
},
))])),
..simple_query()
};
let err = PostgresRenderer::new()
.render_query_stmt(&stmt)
.unwrap_err()
.to_string();
assert!(err.contains("IsNull"), "unexpected error: {err}");
}
2 changes: 1 addition & 1 deletion crates/qcraft-postgres/tests/integration/dql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ fn where_is_null() {
Comparison {
left: Expr::Field(FieldRef::new("users", "age")),
op: CompareOp::IsNull,
right: Expr::Value(Value::Null),
right: Expr::Value(Value::Bool(true)),
negate: false,
},
))])),
Expand Down
11 changes: 10 additions & 1 deletion crates/qcraft-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,16 @@ impl Renderer for SqliteRenderer {
return Ok(());
}
CompareOp::IsNull => {
ctx.keyword("IS NULL");
match right {
Expr::Value(Value::Bool(true)) => ctx.keyword("IS NULL"),
Expr::Value(Value::Bool(false)) => ctx.keyword("IS NOT NULL"),
_ => {
return Err(RenderError::unsupported(
"IsNull",
"IsNull right operand must be a boolean",
));
}
};
return Ok(());
}
CompareOp::Regex => ctx.keyword("REGEXP"),
Expand Down
69 changes: 69 additions & 0 deletions crates/qcraft-sqlite/tests/dql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1998,3 +1998,72 @@ fn select_with_timedelta_param() {
}]
);
}

// ---------------------------------------------------------------------------
// IS NULL / IS NOT NULL (value-driven)
// ---------------------------------------------------------------------------

#[test]
fn where_is_null() {
let stmt = QueryStmt {
where_clause: Some(Conditions::is_null(FieldRef::new("users", "email"))),
..simple_query()
};
assert_eq!(
render(&stmt),
r#"SELECT * FROM "users" WHERE "users"."email" IS NULL"#
);
}

#[test]
fn where_is_not_null() {
let stmt = QueryStmt {
where_clause: Some(Conditions::is_not_null(FieldRef::new("users", "email"))),
..simple_query()
};
assert_eq!(
render(&stmt),
r#"SELECT * FROM "users" WHERE "users"."email" IS NOT NULL"#
);
}

#[test]
fn where_is_null_negated() {
let stmt = QueryStmt {
where_clause: Some(Conditions::is_null(FieldRef::new("users", "email")).negated()),
..simple_query()
};
assert_eq!(
render(&stmt),
r#"SELECT * FROM "users" WHERE NOT ("users"."email" IS NULL)"#
);
}

#[test]
fn where_is_not_null_negated() {
let stmt = QueryStmt {
where_clause: Some(Conditions::is_not_null(FieldRef::new("users", "email")).negated()),
..simple_query()
};
assert_eq!(
render(&stmt),
r#"SELECT * FROM "users" WHERE NOT ("users"."email" IS NOT NULL)"#
);
}

#[test]
fn is_null_non_boolean_right_errors() {
let stmt = QueryStmt {
where_clause: Some(Conditions::and(vec![ConditionNode::Comparison(Box::new(
Comparison {
left: Expr::Field(FieldRef::new("users", "email")),
op: CompareOp::IsNull,
right: Expr::Value(Value::Null),
negate: false,
},
))])),
..simple_query()
};
let err = render_err(&stmt);
assert!(err.contains("IsNull"), "unexpected error: {err}");
}
6 changes: 3 additions & 3 deletions crates/qcraft-sqlite/tests/integration_dql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ fn where_is_null() {
Comparison {
left: Expr::Field(FieldRef::new("users", "age")),
op: CompareOp::IsNull,
right: Expr::Value(Value::Null),
right: Expr::Value(Value::Bool(true)),
negate: false,
},
))])),
Expand All @@ -589,8 +589,8 @@ fn where_is_not_null() {
Comparison {
left: Expr::Field(FieldRef::new("users", "age")),
op: CompareOp::IsNull,
right: Expr::Value(Value::Null),
negate: true,
right: Expr::Value(Value::Bool(false)),
negate: false,
},
))])),
..simple_query()
Expand Down
Loading