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

refactor(expr): support any type in function signatures #12262

Merged
merged 27 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c07c223
macro: rename "list" to "anyarray"
wangrunji0408 Sep 8, 2023
6b7a6da
rename "list" to "anyarray" in function signatures
wangrunji0408 Sep 8, 2023
d33d533
merge function signatures
wangrunji0408 Sep 8, 2023
f24a7fd
generate the new signature
wangrunji0408 Sep 8, 2023
ae27c60
support any type
wangrunji0408 Sep 11, 2023
8ec6b3a
fix all build
wangrunji0408 Sep 11, 2023
93d3312
fix unit tests
wangrunji0408 Sep 11, 2023
dcea092
rename function registry
wangrunji0408 Sep 11, 2023
f7cb73c
fix unit test
wangrunji0408 Sep 11, 2023
b3f4b47
auto type inference for any and anyarray
wangrunji0408 Sep 11, 2023
27a06f7
fix new functions
wangrunji0408 Sep 13, 2023
3dea306
fix build
wangrunji0408 Sep 15, 2023
5760603
refine array_sum test
wangrunji0408 Sep 15, 2023
7edd9c0
gen: fix "crate" -> "risingwave_expr"
wangrunji0408 Sep 19, 2023
343cb14
fix clippy
wangrunji0408 Sep 19, 2023
31e7c5a
auto type inference for `array_agg` and `mode`
wangrunji0408 Sep 19, 2023
86563e1
Merge remote-tracking branch 'origin/main' into wrj/sig
wangrunji0408 Oct 9, 2023
c7fea7b
fix panic on _pg_expandarray
wangrunji0408 Oct 9, 2023
fca9d45
fix non-unique match snapshot
wangrunji0408 Oct 9, 2023
2bc5527
resolve comments
wangrunji0408 Oct 9, 2023
081db38
fix test
wangrunji0408 Oct 10, 2023
940a710
fix sqlsmith
wangrunji0408 Oct 10, 2023
8a63ca3
Merge remote-tracking branch 'origin/main' into wrj/sig
wangrunji0408 Oct 10, 2023
d5601eb
fix sqlsmith
wangrunji0408 Oct 10, 2023
69fa3e9
move global functions into `FunctionRegistry`
wangrunji0408 Oct 10, 2023
3c9dd48
revert type names
wangrunji0408 Oct 10, 2023
bf0381a
fix sqlsmith
wangrunji0408 Oct 10, 2023
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
30 changes: 15 additions & 15 deletions e2e_test/batch/functions/array_sum.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@ select array_sum(array[1, 2, 3]);
----
6

# Testing for int16 with positive numbers
# Testing for SMALLINT with positive numbers
query I
select array_sum(array[10, 20, 30]);
select array_sum(array[10, 20, 30]::smallint[]);
----
60

# Testing for int16 with a mix of positive and negative numbers
# Testing for SMALLINT with a mix of positive and negative numbers
query I
select array_sum(array[-10, 20, -30]);
select array_sum(array[-10, 20, -30]::smallint[]);
----
-20

# Testing for int16 with all zeros
# Testing for SMALLINT with all zeros
query I
select array_sum(array[0, 0, 0]);
select array_sum(array[0, 0, 0]::smallint[]);
----
0

# Testing for int32 with larger positive numbers
# Testing for INT with larger positive numbers
query I
select array_sum(array[1000, 2000, 3000]);
select array_sum(array[1000, 2000, 3000]::int[]);
----
6000

# Testing for int32 with a mix of larger positive and negative numbers
# Testing for INT with a mix of larger positive and negative numbers
query I
select array_sum(array[-1000, 2000, -3000]);
select array_sum(array[-1000, 2000, -3000]::int[]);
----
-2000

# Testing for int64 with much larger numbers
# Testing for BIGINT with much larger numbers
query I
select array_sum(array[1000000000, 2000000000, 3000000000]);
select array_sum(array[1000000000, 2000000000, 3000000000]::bigint[]);
----
6000000000

# Testing for int64 with a mix of much larger positive and negative numbers
# Testing for BIGINT with a mix of much larger positive and negative numbers
query I
select array_sum(array[-1000000000, 2000000000, -3000000000]);
select array_sum(array[-1000000000, 2000000000, -3000000000]::bigint[]);
----
-2000000000
-2000000000
6 changes: 6 additions & 0 deletions src/common/src/array/list_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,12 @@ impl ToText for ListRef<'_> {
}
}

impl<'a> From<&'a ListValue> for ListRef<'a> {
fn from(val: &'a ListValue) -> Self {
ListRef::ValueRef { val }
}
}

#[cfg(test)]
mod tests {
use more_asserts::{assert_gt, assert_lt};
Expand Down
22 changes: 22 additions & 0 deletions src/common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ impl DataType {
DataTypeName::from(self).is_scalar()
}

pub fn is_array(&self) -> bool {
matches!(self, DataType::List(_))
}

pub fn is_struct(&self) -> bool {
matches!(self, DataType::Struct(_))
}

pub fn is_int(&self) -> bool {
matches!(self, DataType::Int16 | DataType::Int32 | DataType::Int64)
}
Expand Down Expand Up @@ -950,7 +958,21 @@ impl ScalarImpl {
};
Ok(res)
}
}

impl From<ScalarRefImpl<'_>> for ScalarImpl {
fn from(scalar_ref: ScalarRefImpl<'_>) -> Self {
scalar_ref.into_scalar_impl()
}
}

impl<'a> From<&'a ScalarImpl> for ScalarRefImpl<'a> {
fn from(scalar: &'a ScalarImpl) -> Self {
scalar.as_scalar_ref_impl()
}
}

impl ScalarImpl {
/// A lite version of casting from string to target type. Used by frontend to handle types that have
/// to be created by casting.
///
Expand Down
32 changes: 14 additions & 18 deletions src/expr/core/src/aggregate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use std::fmt::Debug;
use std::ops::Range;

use downcast_rs::{impl_downcast, Downcast};
use itertools::Itertools;
use risingwave_common::array::StreamChunk;
use risingwave_common::estimate_size::EstimateSize;
use risingwave_common::types::{DataType, DataTypeName, Datum};
use risingwave_common::types::{DataType, Datum};

use crate::sig::FuncSigDebug;
use crate::{ExprError, Result};

// aggregate definition
Expand Down Expand Up @@ -136,25 +136,21 @@ pub fn build_retractable(agg: &AggCall) -> Result<BoxedAggregateFunction> {
/// NOTE: This function ignores argument indices, `column_orders`, `filter` and `distinct` in
/// `AggCall`. Such operations should be done in batch or streaming executors.
pub fn build(agg: &AggCall, append_only: bool) -> Result<BoxedAggregateFunction> {
let args = (agg.args.arg_types().iter())
.map(|t| t.into())
.collect::<Vec<DataTypeName>>();
let ret_type = (&agg.return_type).into();
let desc = crate::sig::agg::AGG_FUNC_SIG_MAP
.get(agg.kind, &args, ret_type, append_only)
let desc = crate::sig::FUNCTION_REGISTRY
.get_aggregate(
agg.kind,
agg.args.arg_types(),
&agg.return_type,
append_only,
)
.ok_or_else(|| {
ExprError::UnsupportedFunction(format!(
"{:?}",
FuncSigDebug {
func: agg.kind,
inputs_type: &args,
ret_type,
set_returning: false,
deprecated: false,
append_only,
}
"{}({}) -> {}",
agg.kind.to_protobuf().as_str_name().to_ascii_lowercase(),
agg.args.arg_types().iter().format(", "),
agg.return_type,
))
})?;

(desc.build)(agg)
desc.build_aggregate(agg)
}
28 changes: 9 additions & 19 deletions src/expr/core/src/expr/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ use super::expr_udf::UdfExpression;
use super::expr_vnode::VnodeExpression;
use super::wrapper::{Checked, EvalErrorReport, NonStrict};
use crate::expr::{BoxedExpression, Expression, InputRefExpression, LiteralExpression};
use crate::sig::func::FUNC_SIG_MAP;
use crate::sig::FuncSigDebug;
use crate::sig::FUNCTION_REGISTRY;
use crate::{bail, ExprError, Result};

/// Build an expression from protobuf.
Expand Down Expand Up @@ -195,27 +194,18 @@ pub fn build_func(
return Ok(ArrayTransformExpression { array, lambda }.boxed());
}

let args = children
.iter()
.map(|c| c.return_type().into())
.collect_vec();
let desc = FUNC_SIG_MAP
.get(func, &args, (&ret_type).into())
let args = children.iter().map(|c| c.return_type()).collect_vec();
let desc = FUNCTION_REGISTRY
.get(func, &args, &ret_type)
.ok_or_else(|| {
ExprError::UnsupportedFunction(format!(
"{:?}",
FuncSigDebug {
func: func.as_str_name(),
inputs_type: &args,
ret_type: (&ret_type).into(),
set_returning: false,
deprecated: false,
append_only: false,
}
"{}({}) -> {}",
func.as_str_name().to_ascii_lowercase(),
args.iter().format(", "),
ret_type,
))
})?;

(desc.build)(ret_type, children)
desc.build_scalar(ret_type, children)
}

/// Build an expression in `FuncCall` variant in non-strict mode.
Expand Down
122 changes: 0 additions & 122 deletions src/expr/core/src/sig/agg.rs

This file was deleted.

Loading
Loading