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] pinot sql transformation to sr sql with date functionPinot dialect date function #55106

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
@@ -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);
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package com.starrocks.connector.parser.trino;
package com.starrocks.connector.parser;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package com.starrocks.connector.parser.trino;
package com.starrocks.connector.parser;

import com.starrocks.analysis.Expr;
import com.starrocks.sql.ast.AstVisitor;
Expand Down
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")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it a static ?

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);
}
}
}
Loading
Loading