-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[Enhancement] Support Trino try expression #55144
Open
KKould
wants to merge
7
commits into
StarRocks:main
Choose a base branch
from
KKould:feat/trino_try_expr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+461
−6
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
045b1f0
feat: support Trino try expression
KKould cd2641a
chore: fix code for bot comment
KKould f9569b6
fix: pass `test_try_index_out_bound`
KKould 0657666
chore: codefmt
KKould 31020af
chore: remove TryExpr::analyzeImpl & add more test cases on test_try
KKould c2aa18b
chore: fix `TrinoQueryTest::testTry`
KKould 2816717
chore: add more unit test for trino try
KKould File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,49 @@ | ||
// 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. | ||
|
||
#include "exprs/try_expr.h" | ||
|
||
#include "column/chunk.h" | ||
#include "column/column_helper.h" | ||
#include "column/const_column.h" | ||
#include "column/fixed_length_column.h" | ||
#include "common/object_pool.h" | ||
|
||
namespace starrocks { | ||
|
||
class TryExpr final : public Expr { | ||
public: | ||
explicit TryExpr(const TExprNode& node) : Expr(node) {} | ||
|
||
TryExpr(const TryExpr&) = default; | ||
TryExpr(TryExpr&&) = default; | ||
|
||
StatusOr<ColumnPtr> evaluate_checked(ExprContext* context, Chunk* chunk) override { | ||
auto&& status = _children[0]->evaluate_checked(context, chunk); | ||
if (LIKELY(status.ok())) { | ||
return status; | ||
} | ||
return ColumnHelper::create_const_null_column(chunk->num_rows()); | ||
} | ||
|
||
Expr* clone(ObjectPool* pool) const override { return pool->add(new TryExpr(*this)); } | ||
}; | ||
|
||
Expr* TryExprFactory::from_thrift(const TExprNode& node) { | ||
DCHECK_EQ(TExprNodeType::TRY_EXPR, node.node_type); | ||
DCHECK_EQ(node.type.types.size(), 1); | ||
return new TryExpr(node); | ||
} | ||
|
||
} // namespace starrocks |
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,26 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include "exprs/expr.h" | ||
|
||
namespace starrocks { | ||
|
||
class TryExprFactory { | ||
public: | ||
static Expr* from_thrift(const TExprNode& node); | ||
}; | ||
|
||
} // namespace starrocks |
92 changes: 92 additions & 0 deletions
92
fe/fe-core/src/main/java/com/starrocks/analysis/TryExpr.java
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,92 @@ | ||
// 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.google.common.base.Joiner; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableList; | ||
import com.starrocks.catalog.Type; | ||
import com.starrocks.common.AnalysisException; | ||
import com.starrocks.sql.ast.AstVisitor; | ||
import com.starrocks.sql.parser.NodePosition; | ||
import com.starrocks.thrift.TExprNode; | ||
import com.starrocks.thrift.TExprNodeType; | ||
import org.apache.log4j.LogManager; | ||
import org.apache.log4j.Logger; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class TryExpr extends Expr { | ||
private static final Logger LOG = LogManager.getLogger(TryExpr.class); | ||
|
||
public TryExpr(Expr child) { | ||
this(child, NodePosition.ZERO); | ||
} | ||
|
||
public TryExpr(Expr child, NodePosition pos) { | ||
super(pos); | ||
this.addChild(child); | ||
setType(child.getType()); | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return getChild(0).getType(); | ||
} | ||
|
||
@Override | ||
public void setType(Type type) { | ||
super.setType(type); | ||
getChild(0).setType(type); | ||
} | ||
|
||
@Override | ||
protected String toSqlImpl() { | ||
return "TRY(" + getChild(0).toSqlImpl() + ")"; | ||
} | ||
|
||
@Override | ||
protected String explainImpl() { | ||
return "try(" + getChild(0).explain() + ")"; | ||
} | ||
|
||
@Override | ||
protected void toThrift(TExprNode msg) { | ||
msg.node_type = TExprNodeType.TRY_EXPR; | ||
msg.setOpcode(opcode); | ||
msg.setOutput_column(outputColumn); | ||
if (getChild(0).getType().isComplexType()) { | ||
msg.setChild_type_desc(getChild(0).getType().toThrift()); | ||
} else { | ||
msg.setChild_type(getChild(0).getType().getPrimitiveType().toThrift()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isNullable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Expr clone() { | ||
return new TryExpr(getChild(0).clone(), pos); | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(AstVisitor<R, C> visitor, C context) { | ||
return visitor.visitTryExpr(this, context); | ||
} | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,6 +134,7 @@ public enum OperatorType { | |
MULTI_IN, | ||
DICTIONARY_GET, | ||
MATCH_EXPR, | ||
TRY, | ||
|
||
/** | ||
* PATTERN | ||
|
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
95 changes: 95 additions & 0 deletions
95
fe/fe-core/src/main/java/com/starrocks/sql/optimizer/operator/scalar/TryOperator.java
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,95 @@ | ||
// 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.sql.optimizer.operator.scalar; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.starrocks.sql.optimizer.base.ColumnRefSet; | ||
import com.starrocks.sql.optimizer.operator.OperatorType; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class TryOperator extends ScalarOperator { | ||
private List<ScalarOperator> arguments; | ||
|
||
public TryOperator(ScalarOperator argument) { | ||
super(OperatorType.TRY, argument.getType()); | ||
arguments = Lists.newArrayList(argument); | ||
setType(argument.getType()); | ||
} | ||
|
||
@Override | ||
public boolean isNullable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public List<ScalarOperator> getChildren() { | ||
return arguments; | ||
} | ||
|
||
@Override | ||
public ScalarOperator getChild(int index) { | ||
return arguments.get(0); | ||
} | ||
|
||
@Override | ||
public void setChild(int index, ScalarOperator child) { | ||
arguments.set(0, child); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Try(" + arguments.get(0) + ")"; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(arguments); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
if (other == null || getClass() != other.getClass()) { | ||
return false; | ||
} | ||
TryOperator that = (TryOperator) other; | ||
return Objects.equals(arguments, that.arguments); | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(ScalarOperatorVisitor<R, C> visitor, C context) { | ||
return visitor.visitTryOperator(this, context); | ||
} | ||
|
||
@Override | ||
public ColumnRefSet getUsedColumns() { | ||
return arguments.get(0).getUsedColumns(); | ||
} | ||
|
||
@Override | ||
public ScalarOperator clone() { | ||
TryOperator operator = (TryOperator) super.clone(); | ||
// Deep copy here | ||
List<ScalarOperator> newArguments = Lists.newArrayList(); | ||
this.arguments.forEach(p -> newArguments.add(p.clone())); | ||
operator.arguments = newArguments; | ||
return operator; | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The most risky bug in this code is: You can modify the code like this: @Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
TryOperator that = (TryOperator) other;
return Objects.equals(arguments, that.arguments);
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The most risky bug in this code is:
The
clone
method calls theTryExpr
constructor withthis
, but there's no constructor forTryExpr
that accepts aTryExpr
as an argument, which will result in a compilation error.You can modify the code like this:
This change correctly clones the child expression and also provides the current
NodePosition
.