Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement IS UNKNOWN/IS NOT UNKNOWN operators #3246

Merged
merged 1 commit into from
Aug 24, 2022
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
64 changes: 64 additions & 0 deletions datafusion/core/tests/sql/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,70 @@ async fn query_is_false() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn query_is_unknown() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Boolean, true)]));

let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(BooleanArray::from(vec![
Some(true),
Some(false),
None,
]))],
)?;

let table = MemTable::try_new(schema, vec![vec![data]])?;

let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT c1 IS UNKNOWN as t FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------+",
"| t |",
"+-------+",
"| false |",
"| false |",
"| true |",
"+-------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}

#[tokio::test]
async fn query_is_not_unknown() -> Result<()> {
let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Boolean, true)]));

let data = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(BooleanArray::from(vec![
Some(true),
Some(false),
None,
]))],
)?;

let table = MemTable::try_new(schema, vec![vec![data]])?;

let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = "SELECT c1 IS NOT UNKNOWN as t FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------+",
"| t |",
"+-------+",
"| true |",
"| true |",
"| false |",
"+-------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}

#[tokio::test]
async fn query_without_from() -> Result<()> {
// Test for SELECT <expression> without FROM.
Expand Down
11 changes: 11 additions & 0 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,17 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
right: Box::new(lit(false)),
}),

SQLExpr::IsUnknown(expr) => Ok(Expr::BinaryExpr {
left: Box::new(self.sql_expr_to_logical_expr(*expr, schema, ctes)?),
op: Operator::IsNotDistinctFrom,
right: Box::new(lit(ScalarValue::Boolean(None))),
}),

SQLExpr::IsNotUnknown(expr) => Ok(Expr::BinaryExpr {
left: Box::new(self.sql_expr_to_logical_expr(*expr, schema, ctes)?),
op: Operator::IsDistinctFrom,
right: Box::new(lit(ScalarValue::Boolean(None))),
}),

SQLExpr::UnaryOp { op, expr } => self.parse_sql_unary_op(op, *expr, schema, ctes),

Expand Down