This is an automated email from the ASF dual-hosted git repository.

rongr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new 4226ea0505 modify the error response on controller. (#11624)
4226ea0505 is described below

commit 4226ea05054fe980a94815ced33dced46545024f
Author: Rong Rong <[email protected]>
AuthorDate: Thu Sep 21 10:45:15 2023 -0700

    modify the error response on controller. (#11624)
    
    modify the error response on controller
    - return will always be JSON format if return code is 200 (regardless of 
query success or process error)
    - when there's web/network/app issue, strictly non 2xx error code is 
returned; but content might not be JSON
    
    ---------
    
    Co-authored-by: Rong Rong <[email protected]>
---
 .../api/resources/PinotQueryResource.java          | 31 ++++++++++++++++------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git 
a/pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotQueryResource.java
 
b/pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotQueryResource.java
index 3c87e51e8c..90f75760be 100644
--- 
a/pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotQueryResource.java
+++ 
b/pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotQueryResource.java
@@ -57,6 +57,7 @@ import org.apache.helix.model.InstanceConfig;
 import org.apache.pinot.common.Utils;
 import org.apache.pinot.common.exception.QueryException;
 import org.apache.pinot.common.response.ProcessingException;
+import org.apache.pinot.common.response.broker.BrokerResponseNative;
 import org.apache.pinot.common.utils.config.TagNameUtils;
 import org.apache.pinot.common.utils.request.RequestUtils;
 import org.apache.pinot.controller.ControllerConf;
@@ -120,13 +121,13 @@ public class PinotQueryResource {
       return executeSqlQuery(httpHeaders, sqlQuery, traceEnabled, 
queryOptions, "/sql");
     } catch (ProcessingException pe) {
       LOGGER.error("Caught exception while processing post request {}", 
pe.getMessage());
-      return pe.getMessage();
+      return constructQueryExceptionResponse(pe);
     } catch (WebApplicationException wae) {
       LOGGER.error("Caught exception while processing post request", wae);
       throw wae;
     } catch (Exception e) {
       LOGGER.error("Caught exception while processing post request", e);
-      return QueryException.getException(QueryException.INTERNAL_ERROR, 
e).toString();
+      return 
constructQueryExceptionResponse(QueryException.getException(QueryException.INTERNAL_ERROR,
 e));
     }
   }
 
@@ -140,13 +141,13 @@ public class PinotQueryResource {
       return executeSqlQuery(httpHeaders, sqlQuery, traceEnabled, 
queryOptions, "/sql");
     } catch (ProcessingException pe) {
       LOGGER.error("Caught exception while processing get request {}", 
pe.getMessage());
-      return pe.getMessage();
+      return constructQueryExceptionResponse(pe);
     } catch (WebApplicationException wae) {
       LOGGER.error("Caught exception while processing get request", wae);
       throw wae;
     } catch (Exception e) {
       LOGGER.error("Caught exception while processing get request", e);
-      return QueryException.getException(QueryException.INTERNAL_ERROR, 
e).toString();
+      return 
constructQueryExceptionResponse(QueryException.getException(QueryException.INTERNAL_ERROR,
 e));
     }
   }
 
@@ -171,8 +172,7 @@ public class PinotQueryResource {
           CommonConstants.Helix.DEFAULT_MULTI_STAGE_ENGINE_ENABLED)) {
         return getMultiStageQueryResponse(sqlQuery, queryOptions, httpHeaders, 
endpointUrl, traceEnabled);
       } else {
-        throw new UnsupportedOperationException("V2 Multi-Stage query engine 
not enabled. "
-            + "Please see https://docs.pinot.apache.org/ for instruction to 
enable V2 engine.");
+        throw QueryException.getException(QueryException.INTERNAL_ERROR, "V2 
Multi-Stage query engine not enabled.");
       }
     } else {
       PinotSqlType sqlType = sqlNodeAndOptions.getSqlType();
@@ -186,7 +186,7 @@ public class PinotQueryResource {
                   .collect(Collectors.toMap(Pair::getKey, Pair::getValue));
           return _sqlQueryExecutor.executeDMLStatement(sqlNodeAndOptions, 
headers).toJsonString();
         default:
-          throw new UnsupportedOperationException("Unsupported SQL type - " + 
sqlType);
+          throw QueryException.getException(QueryException.INTERNAL_ERROR, 
"Unsupported SQL type - " + sqlType);
       }
     }
   }
@@ -203,7 +203,13 @@ public class PinotQueryResource {
 
     QueryEnvironment queryEnvironment = new QueryEnvironment(new 
TypeFactory(new TypeSystem()),
         CalciteSchemaBuilder.asRootSchema(new 
PinotCatalog(_pinotHelixResourceManager.getTableCache())), null, null);
-    List<String> tableNames = queryEnvironment.getTableNamesForQuery(query);
+    List<String> tableNames;
+    try {
+      tableNames = queryEnvironment.getTableNamesForQuery(query);
+    } catch (Exception e) {
+      return QueryException.getException(QueryException.SQL_PARSING_ERROR,
+          new Exception("Unable to find table for this query", e)).toString();
+    }
     List<String> instanceIds;
     if (tableNames.size() != 0) {
       List<TableConfig> tableConfigList = getListTableConfigs(tableNames);
@@ -465,4 +471,13 @@ public class PinotQueryResource {
       throw new AssertionError("Should not reach this");
     }
   }
+
+  private static String constructQueryExceptionResponse(ProcessingException 
pe) {
+    try {
+      return new BrokerResponseNative(pe).toJsonString();
+    } catch (IOException ioe) {
+      Utils.rethrowException(ioe);
+      throw new AssertionError("Should not reach this");
+    }
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to