-
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
[Feature] pinot sql transformation to sr sql with date functionPinot dialect date function #55106
Open
maggie-zhu
wants to merge
8
commits into
StarRocks:main
Choose a base branch
from
maggie-zhu:pinotDialectDateFunction
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.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
71bd436
support sql transformation from pinot to sr for some functions
maggie-zhu f42998f
fix some format issue
maggie-zhu 4d518bb
change the code according to the review
maggie-zhu 37aa7b7
delete the print
maggie-zhu 115199d
fix some error
maggie-zhu 6b134bf
simplify some functions in complex transformer
maggie-zhu 7bdb70f
fix code format error
maggie-zhu 082ebbd
fix sonar issue
maggie-zhu 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
22 changes: 22 additions & 0 deletions
22
...core/src/main/java/com/starrocks/connector/parser/BaseComplexFunctionCallTransformer.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,22 @@ | ||
// 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.connector.parser; | ||
|
||
import com.starrocks.analysis.Expr; | ||
|
||
|
||
public abstract class BaseComplexFunctionCallTransformer { | ||
|
||
protected abstract Expr transform(String functionName, Expr... args); | ||
} |
100 changes: 100 additions & 0 deletions
100
fe/fe-core/src/main/java/com/starrocks/connector/parser/BaseFunctionCallTransformer.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,100 @@ | ||
// 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.connector.parser; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Maps; | ||
import com.starrocks.analysis.Expr; | ||
import com.starrocks.analysis.FunctionCallExpr; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public abstract class BaseFunctionCallTransformer { | ||
public static Map<String, List<FunctionCallTransformer>> TRANSFORMER_MAP = Maps.newHashMap(); | ||
|
||
public BaseFunctionCallTransformer() { | ||
registerAllFunctionTransformer(); | ||
} | ||
|
||
protected abstract void registerAllFunctionTransformer(); | ||
|
||
protected abstract Expr convert(String fnName, List<Expr> children); | ||
|
||
public static Expr convertRegisterFn(String fnName, List<Expr> children) { | ||
List<FunctionCallTransformer> transformers = TRANSFORMER_MAP.get(fnName); | ||
if (transformers == null) { | ||
return null; | ||
} | ||
|
||
FunctionCallTransformer matcher = null; | ||
for (FunctionCallTransformer transformer : transformers) { | ||
if (transformer.match(children)) { | ||
matcher = transformer; | ||
} | ||
} | ||
if (matcher == null) { | ||
return null; | ||
} | ||
return matcher.transform(children); | ||
} | ||
|
||
protected static FunctionCallExpr buildStarRocksFunctionCall(String starRocksFnName, | ||
List<Class<? extends Expr>> starRocksArgumentsClass) { | ||
List<Expr> arguments = Lists.newArrayList(); | ||
for (int index = 0; index < starRocksArgumentsClass.size(); ++index) { | ||
// For a FunctionCallExpr, do not know the actual arguments here, so we use a PlaceholderExpr to replace it. | ||
arguments.add(new PlaceholderExpr(index + 1, starRocksArgumentsClass.get(index))); | ||
} | ||
return new FunctionCallExpr(starRocksFnName, arguments); | ||
} | ||
|
||
|
||
protected static void registerFunctionTransformer(String originFnName, int originFnArgNums, String starRocksFnName, | ||
List<Class<? extends Expr>> starRocksArgumentsClass) { | ||
FunctionCallExpr starRocksFunctionCall = buildStarRocksFunctionCall(starRocksFnName, starRocksArgumentsClass); | ||
registerFunctionTransformer(originFnName, originFnArgNums, starRocksFunctionCall); | ||
} | ||
|
||
protected static void registerFunctionTransformer(String originFnName, String starRocksFnName) { | ||
FunctionCallExpr starRocksFunctionCall = buildStarRocksFunctionCall(starRocksFnName, Lists.newArrayList()); | ||
registerFunctionTransformer(originFnName, 0, starRocksFunctionCall); | ||
} | ||
|
||
protected static void registerFunctionTransformerWithVarArgs(String originFnName, String starRocksFnName, | ||
List<Class<? extends Expr>> starRocksArgumentsClass) { | ||
Preconditions.checkState(starRocksArgumentsClass.size() == 1); | ||
FunctionCallExpr starRocksFunctionCall = buildStarRocksFunctionCall(starRocksFnName, starRocksArgumentsClass); | ||
registerFunctionTransformerWithVarArgs(originFnName, starRocksFunctionCall); | ||
} | ||
|
||
protected static void registerFunctionTransformer(String originFnName, int originFnArgNums, | ||
FunctionCallExpr starRocksFunctionCall) { | ||
FunctionCallTransformer transformer = new FunctionCallTransformer(starRocksFunctionCall, originFnArgNums); | ||
|
||
List<FunctionCallTransformer> transformerList = TRANSFORMER_MAP.computeIfAbsent(originFnName, | ||
k -> Lists.newArrayList()); | ||
transformerList.add(transformer); | ||
} | ||
|
||
protected static void registerFunctionTransformerWithVarArgs(String originFnName, FunctionCallExpr starRocksFunctionCall) { | ||
FunctionCallTransformer transformer = new FunctionCallTransformer(starRocksFunctionCall, true); | ||
|
||
List<FunctionCallTransformer> transformerList = TRANSFORMER_MAP.computeIfAbsent(originFnName, | ||
k -> Lists.newArrayList()); | ||
transformerList.add(transformer); | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
fe/fe-core/src/main/java/com/starrocks/connector/parser/pinot/AstBuilder.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,74 @@ | ||
// 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.connector.parser.pinot; | ||
|
||
import com.starrocks.analysis.ArithmeticExpr; | ||
import com.starrocks.analysis.Expr; | ||
import com.starrocks.analysis.FunctionCallExpr; | ||
import com.starrocks.analysis.FunctionName; | ||
import com.starrocks.analysis.FunctionParams; | ||
import com.starrocks.analysis.HintNode; | ||
import com.starrocks.analysis.IntLiteral; | ||
import com.starrocks.analysis.ParseNode; | ||
import com.starrocks.sql.parser.NodePosition; | ||
import com.starrocks.sql.parser.StarRocksParser; | ||
import com.starrocks.sql.parser.SyntaxSugars; | ||
import org.antlr.v4.runtime.ParserRuleContext; | ||
|
||
import java.util.IdentityHashMap; | ||
import java.util.List; | ||
|
||
public class AstBuilder extends com.starrocks.sql.parser.AstBuilder { | ||
|
||
protected AstBuilder(long sqlMode) { | ||
super(sqlMode); | ||
} | ||
|
||
public AstBuilder(long sqlMode, IdentityHashMap<ParserRuleContext, List<HintNode>> hintMap) { | ||
super(sqlMode, hintMap); | ||
} | ||
|
||
@Override | ||
public ParseNode visitFunctionCallExpression(StarRocksParser.FunctionCallExpressionContext ctx) { | ||
return visitChildren(ctx); | ||
} | ||
|
||
public ParseNode visitSimpleFunctionCall(StarRocksParser.SimpleFunctionCallContext context) { | ||
String fullFunctionName = getQualifiedName(context.qualifiedName()).toString(); | ||
NodePosition pos = createPos(context); | ||
|
||
List<Expr> arguments = visit(context.expression(), Expr.class); | ||
FunctionName fnName = FunctionName.createFnName(fullFunctionName); | ||
String functionName = fnName.getFunction(); | ||
|
||
Pinot2SRFunctionCallTransformer transformer = new Pinot2SRFunctionCallTransformer(); | ||
Expr convertedFunctionCall = transformer.convert(functionName, arguments); | ||
|
||
if (convertedFunctionCall != null) { | ||
if (functionName.equalsIgnoreCase("fromdatetime")) { | ||
ArithmeticExpr toMillis = new ArithmeticExpr(ArithmeticExpr.Operator.MULTIPLY, | ||
convertedFunctionCall, new IntLiteral(1000)); | ||
return toMillis; | ||
} | ||
return convertedFunctionCall; | ||
} else { | ||
FunctionCallExpr functionCallExpr = new FunctionCallExpr(fnName, | ||
new FunctionParams(false, arguments), pos); | ||
if (context.over() != null) { | ||
return buildOverClause(functionCallExpr, context.over(), pos); | ||
} | ||
return SyntaxSugars.parse(functionCallExpr); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
make it a static ?