Skip to content

Commit

Permalink
In statement for filter (#1883)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

- Fix : IN statement also supports queries with the left operand being a
constant.
- Add test file for IN statement.

Issue #1839 

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
- [x] Test cases
  • Loading branch information
vsian authored Sep 18, 2024
1 parent e7497e4 commit 5f3023e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/executor/expression/expression_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,22 @@ void ExpressionEvaluator::Execute(const SharedPtr<InExpression> &expr, SharedPtr
SharedPtr<ColumnVector> &left_state_output = left_state->OutputColumnVector();
Execute(left_expression, left_state, left_state_output);

SizeT left_result_count = left_state_output->Size();
//in expression evaluates to a constant
if(left_state->OutputColumnVector()->vector_type() == ColumnVectorType::kConstant) {
bool in_result = expr->Exists(left_state_output->GetValue(0));
for(SizeT idx = 0; idx < input_data_block_->row_count(); idx++) {
output_column_vector->buffer_->SetCompactBit(idx, in_result);
}
return;
}
if(expr->in_type() == InType::kIn) {
for(SizeT idx = 0; idx < left_result_count; idx++) {
for(SizeT idx = 0; idx < input_data_block_->row_count(); idx++) {
output_column_vector->buffer_->SetCompactBit(idx, expr->Exists(left_state_output->GetValue(idx)));
}
return;
}
if (expr->in_type() == InType::kNotIn) {
for(SizeT idx = 0; idx < left_result_count; idx++) {
for(SizeT idx = 0; idx < input_data_block_->row_count(); idx++) {
output_column_vector->buffer_->SetCompactBit(idx, !expr->Exists(left_state_output->GetValue(idx)));
}
return;
Expand Down
79 changes: 79 additions & 0 deletions test/sql/dql/function/in/in.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
statement ok
DROP TABLE IF EXISTS test_in;

statement ok
CREATE TABLE test_in(num INTEGER, name VARCHAR, score FLOAT);

statement ok
INSERT INTO test_in values
(1, 'Tom', 90.5),
(2, 'Henry', 70.0),
(3, 'james', 75.0),
(4, 'Toby', 92.0),
(5, 'Thomas', 72.5),
(6, 'Charlie', 69.0),
(7, 'Chris', 88.0),
(8, 'Bill', 90.0),
(9, 'Stefan', 86.5),
(10, 'steve', 86.0);

query III
SELECT * FROM test_in where num in (3, 6, 9);
----
3 james 75.000000
6 Charlie 69.000000
9 Stefan 86.500000

query III
SELECT * FROM test_in where name in ('Tom', 'Toby', 'stevE');
----
1 Tom 90.500000
4 Toby 92.000000

query III
SELECT * FROM test_in where score in ('90.5', '70.0', '86.5');
----
1 Tom 90.500000
2 Henry 70.000000
9 Stefan 86.500000

# some cast
query III
SELECT * FROM test_in where num in ('3', '6', '9');
----
3 james 75.000000
6 Charlie 69.000000
9 Stefan 86.500000

query III
SELECT * FROM test_in where score in ('92', '70', '86');
----
2 Henry 70.000000
4 Toby 92.000000
10 steve 86.000000

# some functions in left operand
query III
SELECT * FROM test_in where num + 2 in ('3', '4', '5');
----
1 Tom 90.500000
2 Henry 70.000000
3 james 75.000000

# constant in left operand
query III
SELECT * FROM test_in where 4 in ('3', '4', '5');
----
1 Tom 90.500000
2 Henry 70.000000
3 james 75.000000
4 Toby 92.000000
5 Thomas 72.500000
6 Charlie 69.000000
7 Chris 88.000000
8 Bill 90.000000
9 Stefan 86.500000
10 steve 86.000000

statement ok
DROP TABLE test_in;

0 comments on commit 5f3023e

Please sign in to comment.