Skip to content

Commit

Permalink
functionality and tests (#3235)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahyurick authored Aug 24, 2022
1 parent aa6a34b commit c11f303
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
64 changes: 64 additions & 0 deletions datafusion/core/tests/sql/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,70 @@ async fn query_is_not_null() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn query_is_true() -> 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 TRUE as t FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------+",
"| t |",
"+-------+",
"| true |",
"| false |",
"| false |",
"+-------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}

#[tokio::test]
async fn query_is_false() -> 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 FALSE as f FROM test";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-------+",
"| f |",
"+-------+",
"| false |",
"| true |",
"| false |",
"+-------+",
];
assert_batches_eq!(expected, &actual);
Ok(())
}

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

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

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


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

Expand Down

0 comments on commit c11f303

Please sign in to comment.