Skip to content
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
1 change: 1 addition & 0 deletions proto/expr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ message ExprNode {
FORMAT_TYPE = 534;
ARRAY_DISTINCT = 535;
ARRAY_LENGTH = 536;
CARDINALITY = 537;

// Jsonb functions

Expand Down
63 changes: 63 additions & 0 deletions src/expr/src/expr/expr_cardinality.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 risingwave_common::array::ListRef;
use risingwave_expr_macro::function;

/// Returns the total number of elements in the array.
///
/// ```sql
/// cardinality ( array anyarray) → int64
/// ```
///
/// Examples:
///
/// ```slt
/// query T
/// select cardinality(null::int[]);
/// ----
/// NULL
///
/// query T
/// select cardinality(array[1,2,3]);
/// ----
/// 3
///
/// query T
/// select cardinality(array[1,2,3,4,1]);
/// ----
/// 5
///
/// query T
/// select cardinality(array[array[1, 2, 3]]);
/// ----
/// 3
///
/// query T
/// select cardinality(array[array[array[3,4,5],array[2,2,2]],array[array[6,7,8],array[0,0,0]]]);
/// ----
/// 12
///
/// query T
/// select cardinality(array[NULL]);
/// ----
/// 1
///
/// query error unknown type
/// select cardinality(null);
/// ```
#[function("cardinality(list) -> int64")]
fn cardinality(array: ListRef<'_>) -> i64 {
array.flatten().len() as _
}
1 change: 1 addition & 0 deletions src/expr/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ mod expr_array_length;
mod expr_array_to_string;
mod expr_binary_nonnull;
mod expr_binary_nullable;
mod expr_cardinality;
mod expr_case;
mod expr_coalesce;
mod expr_concat_ws;
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/binder/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ impl Binder {
("array_to_string", raw_call(ExprType::ArrayToString)),
("array_distinct", raw_call(ExprType::ArrayDistinct)),
("array_length", raw_call(ExprType::ArrayLength)),
("cardinality", raw_call(ExprType::Cardinality)),
// jsonb
("jsonb_object_field", raw_call(ExprType::JsonbAccessInner)),
("jsonb_array_element", raw_call(ExprType::JsonbAccessInner)),
Expand Down
18 changes: 18 additions & 0 deletions src/frontend/src/expr/type_inference/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,24 @@ fn infer_type_for_special(
_ => Ok(None),
}
}
ExprType::Cardinality => {
ensure_arity!("cardinality", | inputs | == 1);
let return_type = inputs[0].return_type();

if inputs[0].is_unknown() {
return Err(ErrorCode::BindError(
"Cannot get cardinality of unknown type".to_string(),
)
.into());
}

match return_type {
DataType::List {
datatype: _list_elem_type,
} => Ok(Some(DataType::Int64)),
_ => Ok(None),
}
}
ExprType::Vnode => {
ensure_arity!("vnode", 1 <= | inputs |);
Ok(Some(DataType::Int16))
Expand Down