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

feat(query): add range function & aliases in select list #11621

Merged
merged 16 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 docs/doc/15-sql-functions/04-array-functions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ SQL Array Functions and Usage.
|--------------------------------------|----------------------------------------------------------------------------------------------|---------------------------------------|--------------------------|
| **GET(array, index)** | Returns an element from the array by index (1-based) | **GET([1, 2], 2)** | 2 |
| **LENGTH(array)** | Returns the length of the array | **LENGTH([1, 2])** | 2 |
| **RANGE(start, end)** | Returns an array collected by [start, end) | **RANGE(1, 3)** | [1, 2] |
| **ARRAY_CONCAT(array1, array2)** | Concats two arrays | **ARRAY_CONCAT([1, 2], [3, 4]** | [1,2,3,4] |
| **ARRAY_CONTAINS(array, item)** | Checks if the array contains a specific element | **ARRAY_CONTAINS([1, 2], 1)** | 1 |
| **ARRAY_INDEXOF(array, item)** | Returns the index(1-based) of an element if the array contains the element | **ARRAY_INDEXOF([1, 2, 9], 9)** | 3 |
Expand Down
6 changes: 6 additions & 0 deletions src/query/functions/src/scalars/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ pub fn register(registry: &mut FunctionRegistry) {
|_, _| 0u8,
);

registry.register_2_arg::<NumberType<u64>, NumberType<u64>, ArrayType<NumberType<u64>>, _, _>(
"range",
|_, _| FunctionDomain::Full,
|start, end, _| (start..end).collect(),
);

registry.register_1_arg::<ArrayType<GenericType<0>>, NumberType<u64>, _, _>(
"length",
|_| FunctionDomain::Full,
Expand Down
7 changes: 4 additions & 3 deletions src/query/sql/src/planner/binder/bind_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,11 @@ impl BindContext {
let mut bind_context: &BindContext = self;
// Lookup parent context to resolve outer reference.
loop {
if self.expr_context.is_where_clause() {
// In where clause, check bound columns first.
if self.expr_context.is_where_clause() || self.expr_context.is_select_clause() {
// In where/select clause, check bound columns first.
Self::search_bound_columns(bind_context, database, table, column, &mut result);
if !result.is_empty() {

if self.expr_context.is_where_clause() && !result.is_empty() {
break;
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/query/sql/src/planner/binder/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,18 @@ impl Binder {
};
}
SelectTarget::AliasedExpr { expr, alias } => {
let previouse_aliases = output
.items
.iter()
.map(|item| (item.alias.clone(), item.scalar.clone()))
.collect::<Vec<_>>();

let mut scalar_binder = ScalarBinder::new(
input_context,
self.ctx.clone(),
&self.name_resolution_ctx,
self.metadata.clone(),
&[],
&previouse_aliases,
);
let (bound_expr, _) = scalar_binder.bind(expr).await?;

Expand Down
13 changes: 12 additions & 1 deletion tests/sqllogictests/suites/base/03_common/03_0007_select_alias
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
statement ok
set max_threads=1


statement error 1065
select number + 3 as number, number + 5 as c, c - 2 from numbers(3);


query III
select number + 3 as d, number + 5 as c, c - 2 from numbers(3);
----
3 5 3
4 6 4
5 7 5

sundy-li marked this conversation as resolved.
Show resolved Hide resolved
query II
SELECT (number+1) as c1, max(number) as c2 FROM numbers_mt(10) group by number+1 having c2>1 order by c1 desc, c2 asc
----
Expand Down Expand Up @@ -35,4 +47,3 @@ SELECT t.`id` AS C, `t`.id AS C1, `id` AS C2, `t`.`id` AS C3 FROM test.t

statement ok
DROP DATABASE test