-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(expr): Implement lambda function and array_transform (#11937)
Signed-off-by: TennyZhuang <[email protected]> Co-authored-by: stonepage <[email protected]>
- Loading branch information
Showing
21 changed files
with
442 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
statement ok | ||
SET RW_IMPLICIT_FLUSH TO true; | ||
|
||
query T | ||
select array_transform('{1,2,3}'::int[], |x| x * 2); | ||
---- | ||
{2,4,6} | ||
|
||
query T | ||
select array_transform('{1,2,3}'::int[], |x| (x::double precision+0.5)); | ||
---- | ||
{1.5,2.5,3.5} | ||
|
||
query T | ||
select array_transform('{1,2,3}'::int[], |x| (x::double precision+0.5)); | ||
---- | ||
{1.5,2.5,3.5} | ||
|
||
query T | ||
select array_transform( | ||
array_transform( | ||
array_transform('{1,2,3}'::int[], |x| x * 2), | ||
|x| x + 0.5 | ||
), | ||
|x| concat(x::varchar, '!') | ||
) | ||
---- | ||
{2.5!,4.5!,6.5!} | ||
|
||
query T | ||
select array_transform( | ||
ARRAY['Apple', 'Airbnb', 'Amazon', 'Facebook', 'Google', 'Microsoft', 'Netflix', 'Uber'], | ||
|x| case when x ilike 'A%' then 'A' else 'Other' end | ||
) | ||
---- | ||
{A,A,A,Other,Other,Other,Other,Other} | ||
|
||
statement ok | ||
create table t(v int, arr int[]); | ||
|
||
statement ok | ||
insert into t values (4, '{1,2,3}'), (5, '{4,5,6,8}'); | ||
|
||
# this makes sure `x + 1` is not extracted as common sub-expression by accident. See #11766 | ||
query TT | ||
select array_transform(arr, |x| x + 1), array_transform(arr, |x| x + 1 + 2) from t; | ||
---- | ||
{2,3,4} {4,5,6} | ||
{5,6,7,9} {7,8,9,11} | ||
|
||
# this clarifies that we do not support referencing columns. | ||
statement error | ||
select array_transform(arr, |x| x + v) from t; | ||
|
||
statement ok | ||
drop table t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023 RisingWave Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use risingwave_common::array::{ArrayRef, DataChunk, Vis}; | ||
use risingwave_common::row::OwnedRow; | ||
use risingwave_common::types::{DataType, Datum, ListValue, ScalarImpl}; | ||
|
||
use super::{BoxedExpression, Expression}; | ||
use crate::Result; | ||
|
||
#[derive(Debug)] | ||
pub struct ArrayTransformExpression { | ||
pub(super) array: BoxedExpression, | ||
pub(super) lambda: BoxedExpression, | ||
} | ||
|
||
#[async_trait] | ||
impl Expression for ArrayTransformExpression { | ||
fn return_type(&self) -> DataType { | ||
DataType::List(Box::new(self.lambda.return_type())) | ||
} | ||
|
||
async fn eval(&self, input: &DataChunk) -> Result<ArrayRef> { | ||
let lambda_input = self.array.eval_checked(input).await?; | ||
let lambda_input = Arc::unwrap_or_clone(lambda_input).into_list(); | ||
let new_list = lambda_input | ||
.map_inner(|flatten_input| async move { | ||
let flatten_len = flatten_input.len(); | ||
let chunk = | ||
DataChunk::new(vec![Arc::new(flatten_input)], Vis::Compact(flatten_len)); | ||
self.lambda.eval(&chunk).await.map(Arc::unwrap_or_clone) | ||
}) | ||
.await?; | ||
Ok(Arc::new(new_list.into())) | ||
} | ||
|
||
async fn eval_row(&self, input: &OwnedRow) -> Result<Datum> { | ||
let lambda_input = self.array.eval_row(input).await?; | ||
let lambda_input = lambda_input.map(ScalarImpl::into_list); | ||
if let Some(lambda_input) = lambda_input { | ||
let mut new_vals = Vec::with_capacity(lambda_input.values().len()); | ||
for val in lambda_input.values() { | ||
let row = OwnedRow::new(vec![val.clone()]); | ||
let res = self.lambda.eval_row(&row).await?; | ||
new_vals.push(res); | ||
} | ||
let new_list = ListValue::new(new_vals); | ||
Ok(Some(new_list.into())) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.