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

[Feature] support named arguments for table functions #36596

Merged
merged 10 commits into from
Dec 15, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ generate_series(start, end [,step])
- `step`: the value to increment or decrement, optional. Supported data types are INT, BIGINT, and LARGEINT. If not specified, the default step is 1. `step` can be either negative or positive, but cannot be zero.

The three parameters must have the same data type, for example, `generate_series(INT start, INT end [, INT step])`.
It supports named arguments from v3.3, where all parameters are input with names, like `name=>expr`, for example, `generate_series(start=>3, end=>7, step=>2)`.

## Return value

Expand Down Expand Up @@ -139,6 +140,17 @@ SELECT * FROM t_numbers, generate_series(t_numbers.start, t_numbers.end, -1);
| 9 | 6 | 6 |
+-------+------+-----------------+
```
Example 6: Generate a sequence of values within the range [2,5] in ascending order with the specified step `2`, using named arguments.

```SQL
MySQL > select * from TABLE(generate_series(start=>2, end=>5, step=>2));
+-----------------+
| generate_series |
+-----------------+
| 2 |
| 4 |
+-----------------+
```

The input row `(NULL, 10)` has a NULL value and zero rows are returned for this row.

Expand Down
9 changes: 9 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/analysis/Expr.java
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,15 @@ public static Function getBuiltinFunction(String name, Type[] argTypes, Function
return GlobalStateMgr.getCurrentState().getFunction(searchDesc, mode);
}

public static Function getBuiltinFunction(String name, Type[] argTypes, String[] argNames, Function.CompareMode mode) {
if (argNames == null) {
return getBuiltinFunction(name, argTypes, mode);
}
FunctionName fnName = new FunctionName(name);
Function searchDesc = new Function(fnName, argTypes, argNames, Type.INVALID, false);
return GlobalStateMgr.getCurrentState().getFunction(searchDesc, mode);
}

/**
* Pushes negation to the individual operands of a predicate
* tree rooted at 'root'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

package com.starrocks.analysis;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.starrocks.catalog.FunctionSet;
import com.starrocks.common.io.Writable;
Expand All @@ -42,6 +43,7 @@
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
Expand All @@ -53,14 +55,23 @@
public class FunctionParams implements Writable {
private boolean isStar;
private List<Expr> exprs;

private List<String> exprsNames;
private boolean isDistinct;

private List<OrderByElement> orderByElements;
// c'tor for non-star params
public FunctionParams(boolean isDistinct, List<Expr> exprs) {
if (exprs.stream().anyMatch(e -> e instanceof NamedArgument)) {
this.exprs = exprs.stream().map(e -> (e instanceof NamedArgument ? ((NamedArgument) e).getExpr()
: e)).collect(Collectors.toList());
this.exprsNames = exprs.stream().map(e -> (e instanceof NamedArgument ? ((NamedArgument) e).getName()
: "")).collect(Collectors.toList());
} else {
this.exprs = exprs;
}
isStar = false;
this.isDistinct = isDistinct;
this.exprs = exprs;
this.orderByElements = null;
}

Expand Down Expand Up @@ -89,6 +100,10 @@ private FunctionParams() {
orderByElements = null;
}

public List<String> getExprsNames() {
return exprsNames;
}

public static FunctionParams createStarParam() {
return new FunctionParams();
}
Expand Down Expand Up @@ -135,6 +150,34 @@ public List<Expr> exprs() {
return exprs;
}

public void reorderNamedArg(String[] names) {
Preconditions.checkState(names.length == exprsNames.size());
String[] newNames = new String[exprsNames.size()];
Expr[] newExprs = new Expr[exprsNames.size()];
for (int i = 0; i < exprsNames.size(); i++) {
for (int j = 0; j < names.length; j++) {
if (exprsNames.get(i).equals(names[j])) {
newNames[j] = exprsNames.get(i);
newExprs[j] = exprs.get(i);
}
}
}
exprs = Arrays.asList(newExprs);
exprsNames = Arrays.asList(newNames);
}

public String getNamedArgStr() {
Preconditions.checkState(exprs.size() == exprsNames.size());
String result = "";
for (int i = 0; i < exprs.size(); i++) {
if (i != 0) {
result = result.concat(",");
}
result = result.concat(exprsNames.get(i) + "=>" + exprs.get(i).toSql());
}
return result;
}

public void setIsDistinct(boolean v) {
isDistinct = v;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// 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
//
// https://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.

package com.starrocks.analysis;

import com.starrocks.sql.ast.AstVisitor;

public class NamedArgument extends Expr {
private final String name;

private Expr expr;

public NamedArgument(String name, Expr expr) {
this.name = name;
this.expr = expr;
}

public NamedArgument(NamedArgument other) {
this.name = other.name;
this.expr = other.expr;
}

public String getName() {
return name;
}

public Expr getExpr() {
return expr;
}

public void setExpr(Expr expr) {
this.expr = expr;
}

@Override
public Expr clone() {
return new NamedArgument(name, expr);
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitNamedArgument(this, context);
}

@Override
protected String toSqlImpl() {
return name + "=>" + expr.toSql();
}

@Override
public String toString() {
return name + "=>" + expr.toString();
}
}
Loading
Loading