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] Add function get_query_dump (backport #48105) #48208

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ public enum CompareMode {

private boolean isNullable = true;

<<<<<<< HEAD
=======
private Vector<Pair<String, Expr>> defaultArgExprs;

private boolean isMetaFunction = false;

>>>>>>> d42320f9a2 ([Feature] Add function get_query_dump (#48105))
// Only used for serialization
protected Function() {
}
Expand Down Expand Up @@ -180,6 +187,7 @@ public Function(Function other) {
isPolymorphic = other.isPolymorphic;
couldApplyDictOptimize = other.couldApplyDictOptimize;
isNullable = other.isNullable;
isMetaFunction = other.isMetaFunction;
}

public FunctionName getFunctionName() {
Expand Down Expand Up @@ -261,6 +269,14 @@ public boolean isPolymorphic() {
return isPolymorphic;
}

public boolean isMetaFunction() {
return isMetaFunction;
}

public void setMetaFunction(boolean metaFunction) {
isMetaFunction = metaFunction;
}

public long getFunctionId() {
return functionId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,15 @@
package com.starrocks.http.rest;

import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.starrocks.catalog.Database;
import com.starrocks.catalog.InternalCatalog;
import com.starrocks.common.DdlException;
import com.starrocks.common.Pair;
import com.starrocks.http.ActionController;
import com.starrocks.http.BaseRequest;
import com.starrocks.http.BaseResponse;
import com.starrocks.http.IllegalArgException;
import com.starrocks.persist.gson.GsonUtils;
import com.starrocks.qe.ConnectContext;
import com.starrocks.qe.StmtExecutor;
import com.starrocks.server.GlobalStateMgr;
import com.starrocks.sql.ast.StatementBase;
import com.starrocks.sql.optimizer.dump.DumpInfo;
import com.starrocks.sql.optimizer.dump.QueryDumpDeserializer;
import com.starrocks.sql.optimizer.dump.QueryDumpInfo;
import com.starrocks.sql.optimizer.dump.QueryDumpSerializer;
import com.starrocks.sql.parser.SqlParser;
import com.starrocks.sql.optimizer.dump.QueryDumper;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.apache.logging.log4j.LogManager;
Expand All @@ -37,7 +27,10 @@

public class QueryDumpAction extends RestBaseAction {
private static final Logger LOG = LogManager.getLogger(QueryDumpAction.class);

public static final String URL = "/api/query_dump";
private static final String DB = "db";
<<<<<<< HEAD
private static final Gson GSON = new GsonBuilder()
.addSerializationExclusionStrategy(new GsonUtils.HiddenAnnotationExclusionStrategy())
.addDeserializationExclusionStrategy(new GsonUtils.HiddenAnnotationExclusionStrategy())
Expand All @@ -46,46 +39,39 @@ public class QueryDumpAction extends RestBaseAction {
.registerTypeAdapter(QueryDumpInfo.class, new QueryDumpSerializer())
.registerTypeAdapter(QueryDumpInfo.class, new QueryDumpDeserializer())
.create();
=======
private static final String MOCK = "mock";
>>>>>>> d42320f9a2 ([Feature] Add function get_query_dump (#48105))

public QueryDumpAction(ActionController controller) {
super(controller);
}

public static void registerAction(ActionController controller) throws IllegalArgException {
controller.registerHandler(HttpMethod.POST, "/api/query_dump", new QueryDumpAction(controller));
controller.registerHandler(HttpMethod.POST, URL, new QueryDumpAction(controller));
}

@Override
public void executeWithoutPassword(BaseRequest request, BaseResponse response) throws DdlException {
ConnectContext context = ConnectContext.get();
String catalogDbName = request.getSingleParameter(DB);

String catalogName = "";
String dbName = "";
if (!Strings.isNullOrEmpty(catalogDbName)) {
String[] catalogDbNames = catalogDbName.split("\\.");

String catalogName = InternalCatalog.DEFAULT_INTERNAL_CATALOG_NAME;
catalogName = InternalCatalog.DEFAULT_INTERNAL_CATALOG_NAME;
if (catalogDbNames.length == 2) {
catalogName = catalogDbNames[0];
}
String dbName = catalogDbNames[catalogDbNames.length - 1];
context.setCurrentCatalog(catalogName);
Database db = GlobalStateMgr.getCurrentState().getMetadataMgr().getDb(catalogName, dbName);
if (db == null) {
response.getContent().append("Database [" + dbName + "] does not exists");
sendResult(request, response, HttpResponseStatus.NOT_FOUND);
return;
}
context.setDatabase(db.getFullName());
dbName = catalogDbNames[catalogDbNames.length - 1];
}
context.setIsHTTPQueryDump(true);

String query = request.getContent();
if (Strings.isNullOrEmpty(query)) {
response.getContent().append("not valid parameter");
sendResult(request, response, HttpResponseStatus.BAD_REQUEST);
return;
}

<<<<<<< HEAD
StatementBase parsedStmt;
try {
parsedStmt = SqlParser.parse(query, context.getSessionVariable()).get(0);
Expand All @@ -106,5 +92,11 @@ public void executeWithoutPassword(BaseRequest request, BaseResponse response) t
response.getContent().append("not use cbo planner, try again.");
sendResult(request, response, HttpResponseStatus.BAD_REQUEST);
}
=======
Pair<HttpResponseStatus, String> statusAndRes = QueryDumper.dumpQuery(catalogName, dbName, query, enableMock);

response.getContent().append(statusAndRes.second);
sendResult(request, response, statusAndRes.first);
>>>>>>> d42320f9a2 ([Feature] Add function get_query_dump (#48105))
}
}
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.sql.optimizer.dump;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.starrocks.catalog.Database;
import com.starrocks.common.Pair;
import com.starrocks.persist.gson.GsonUtils;
import com.starrocks.qe.ConnectContext;
import com.starrocks.qe.StmtExecutor;
import com.starrocks.server.GlobalStateMgr;
import com.starrocks.sql.ast.StatementBase;
import com.starrocks.sql.parser.SqlParser;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class QueryDumper {
private static final Logger LOG = LogManager.getLogger(QueryDumper.class);

private static final Gson GSON = new GsonBuilder()
.addSerializationExclusionStrategy(new GsonUtils.HiddenAnnotationExclusionStrategy())
.addDeserializationExclusionStrategy(new GsonUtils.HiddenAnnotationExclusionStrategy())
.enableComplexMapKeySerialization()
.disableHtmlEscaping()
.registerTypeAdapter(QueryDumpInfo.class, new QueryDumpSerializer())
.registerTypeAdapter(QueryDumpInfo.class, new QueryDumpDeserializer())
.create();

public static Pair<HttpResponseStatus, String> dumpQuery(String catalogName, String dbName, String query,
boolean enableMock) {
ConnectContext context = ConnectContext.get();
if (context == null) {
return Pair.create(HttpResponseStatus.BAD_REQUEST,
"There is no ConnectContext for this thread: " + Thread.currentThread().getName());
}

final String prevCatalog = context.getCurrentCatalog();
final String prevDb = context.getDatabase();
final boolean prevIsHTTPQueryDump = context.isHTTPQueryDump();

try {
if (StringUtils.isEmpty(query)) {
return Pair.create(HttpResponseStatus.BAD_REQUEST, "query is empty");
}

if (!StringUtils.isEmpty(catalogName)) {
context.setCurrentCatalog(catalogName);
}

if (!StringUtils.isEmpty(dbName)) {
Database db = GlobalStateMgr.getCurrentState().getMetadataMgr().getDb(catalogName, dbName);
if (db == null) {
return Pair.create(HttpResponseStatus.NOT_FOUND,
String.format("Database [%s.%s] does not exists", catalogName, dbName));
}
context.setDatabase(db.getFullName());
}

context.setIsHTTPQueryDump(true);

StatementBase parsedStmt;
try {
parsedStmt = SqlParser.parse(query, context.getSessionVariable()).get(0);
StmtExecutor executor = new StmtExecutor(context, parsedStmt);
executor.execute();
} catch (Exception e) {
LOG.warn("execute query failed. ", e);
return Pair.create(HttpResponseStatus.BAD_REQUEST, "execute query failed. " + e.getMessage());
}

DumpInfo dumpInfo = context.getDumpInfo();
if (dumpInfo != null) {
dumpInfo.setDesensitizedInfo(enableMock);
String dumpStr = GSON.toJson(dumpInfo, QueryDumpInfo.class);
return Pair.create(HttpResponseStatus.OK, dumpStr);
} else {
return Pair.create(HttpResponseStatus.BAD_REQUEST, "not use cbo planner, try again.");
}
} finally {
context.setCurrentCatalog(prevCatalog);
context.setDatabase(prevDb);
context.setIsHTTPQueryDump(prevIsHTTPQueryDump);
}
}
}
Loading
Loading