[feature](multi-catalog) support select current_catalog(); (#18163)

This commit is contained in:
xueweizhang
2023-04-06 12:06:10 +08:00
committed by GitHub
parent 4ec6aa1691
commit 8b61709ec8
10 changed files with 91 additions and 0 deletions

View File

@ -309,6 +309,7 @@ terminal String
KW_CROSS,
KW_CUBE,
KW_CURRENT,
KW_CURRENT_CATALOG,
KW_CURRENT_TIMESTAMP,
KW_CURRENT_USER,
KW_DATA,
@ -6271,6 +6272,8 @@ non_pred_expr ::=
{: RESULT = new InformationFunction("USER"); :}
| KW_CURRENT_USER LPAREN RPAREN
{: RESULT = new InformationFunction("CURRENT_USER"); :}
| KW_CURRENT_CATALOG LPAREN RPAREN
{: RESULT = new InformationFunction("CURRENT_CATALOG"); :}
| KW_CONNECTION_ID LPAREN RPAREN
{: RESULT = new InformationFunction("CONNECTION_ID"); :}
| KW_PASSWORD LPAREN STRING_LITERAL:text RPAREN
@ -7311,6 +7314,8 @@ keyword ::=
{: RESULT = id; :}
| KW_HISTOGRAM:id
{: RESULT = id; :}
| KW_CURRENT_CATALOG:id
{: RESULT = id; :}
;
// Identifier that contain keyword

View File

@ -74,6 +74,9 @@ public class InformationFunction extends Expr {
type = Type.BIGINT;
intValue = analyzer.getConnectId();
strValue = "";
} else if (funcType.equalsIgnoreCase("CURRENT_CATALOG")) {
type = Type.VARCHAR;
strValue = ConnectContext.get().getDefaultCatalog();
}
}

View File

@ -90,6 +90,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.ConvertTo;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ConvertTz;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Cos;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CountEqual;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentCatalog;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentDate;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentTime;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentUser;
@ -417,6 +418,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(ConvertTz.class, "convert_tz"),
scalar(Cos.class, "cos"),
scalar(CountEqual.class, "countequal"),
scalar(CurrentCatalog.class, "current_catalog"),
scalar(CurrentDate.class, "curdate", "current_date"),
scalar(CurrentTime.class, "curtime", "current_time"),
scalar(CurrentUser.class, "current_user"),

View File

@ -47,6 +47,7 @@ import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunctio
import org.apache.doris.nereids.trees.expressions.functions.agg.NullableAggregateFunction;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Array;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ConnectionId;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentCatalog;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentUser;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Database;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Date;
@ -192,6 +193,12 @@ public class FoldConstantRuleOnFE extends AbstractExpressionRewriteRule {
return new VarcharLiteral(res);
}
@Override
public Expression visitCurrentCatalog(CurrentCatalog currentCatalog, ExpressionRewriteContext context) {
String res = context.connectContext.get().getDefaultCatalog();
return new VarcharLiteral(res);
}
@Override
public Expression visitUser(User user, ExpressionRewriteContext context) {
String res = context.connectContext.get().getUserIdentity().toString();

View File

@ -0,0 +1,54 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// http://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 org.apache.doris.nereids.trees.expressions.functions.scalar;
import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.shape.LeafExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.VarcharType;
import com.google.common.collect.ImmutableList;
import java.util.List;
/**
* ScalarFunction 'CurrentCatalog'.
*/
public class CurrentCatalog extends ScalarFunction
implements LeafExpression, ExplicitlyCastableSignature, AlwaysNotNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT).args()
);
public CurrentCatalog() {
super("current_catalog", ImmutableList.of());
}
@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}
@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitCurrentCatalog(this, context);
}
}

View File

@ -93,6 +93,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.ConvertTo;
import org.apache.doris.nereids.trees.expressions.functions.scalar.ConvertTz;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Cos;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CountEqual;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentCatalog;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentDate;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentTime;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentUser;
@ -647,6 +648,10 @@ public interface ScalarFunctionVisitor<R, C> {
return visitScalarFunction(currentUser, context);
}
default R visitCurrentCatalog(CurrentCatalog currentCatalog, C context) {
return visitScalarFunction(currentCatalog, context);
}
default R visitUser(User user, C context) {
return visitScalarFunction(user, context);
}

View File

@ -162,6 +162,7 @@ import org.apache.doris.qe.SqlModeHelper;
keywordMap.put("cross", new Integer(SqlParserSymbols.KW_CROSS));
keywordMap.put("cube", new Integer(SqlParserSymbols.KW_CUBE));
keywordMap.put("current", new Integer(SqlParserSymbols.KW_CURRENT));
keywordMap.put("current_catalog", new Integer(SqlParserSymbols.KW_CURRENT_CATALOG));
keywordMap.put("current_timestamp", new Integer(SqlParserSymbols.KW_CURRENT_TIMESTAMP));
keywordMap.put("current_user", new Integer(SqlParserSymbols.KW_CURRENT_USER));
keywordMap.put("data", new Integer(SqlParserSymbols.KW_DATA));