KYLIN-2806, fix no results issue

Project: http://git-wip-us.apache.org/repos/asf/kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/46c3e25f
Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/46c3e25f
Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/46c3e25f

Branch: refs/heads/master
Commit: 46c3e25fba32bc0ff772a9811f53d8c0dcac5e83
Parents: 38dae18
Author: Cheng Wang <cheng.w...@kyligence.io>
Authored: Thu Sep 14 21:03:40 2017 +0800
Committer: liyang-gmt8 <liy...@apache.org>
Committed: Fri Sep 15 15:13:15 2017 +0800

----------------------------------------------------------------------
 .../java/org/apache/kylin/jdbc/KylinClient.java | 41 ++++++++++++--------
 .../org/apache/kylin/jdbc/ITJDBCDriverTest.java |  6 ++-
 .../kylin/rest/controller/QueryController.java  |  2 +-
 .../apache/kylin/rest/service/QueryService.java | 25 ++++++++++--
 .../rest/controller/QueryControllerTest.java    | 10 +++--
 5 files changed, 58 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kylin/blob/46c3e25f/jdbc/src/main/java/org/apache/kylin/jdbc/KylinClient.java
----------------------------------------------------------------------
diff --git a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinClient.java 
b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinClient.java
index cd7b921..9f8c7dd 100644
--- a/jdbc/src/main/java/org/apache/kylin/jdbc/KylinClient.java
+++ b/jdbc/src/main/java/org/apache/kylin/jdbc/KylinClient.java
@@ -55,7 +55,6 @@ import org.apache.kylin.jdbc.KylinMeta.KMetaProject;
 import org.apache.kylin.jdbc.KylinMeta.KMetaSchema;
 import org.apache.kylin.jdbc.KylinMeta.KMetaTable;
 import org.apache.kylin.jdbc.json.PreparedQueryRequest;
-import org.apache.kylin.jdbc.json.QueryRequest;
 import org.apache.kylin.jdbc.json.SQLResponseStub;
 import org.apache.kylin.jdbc.json.StatementParameter;
 import org.apache.kylin.jdbc.json.TableMetaStub;
@@ -85,7 +84,8 @@ public class KylinClient implements IRemoteClient {
         if (isSSL()) {
             try {
                 SSLSocketFactory sslsf = new SSLSocketFactory(new 
TrustStrategy() {
-                    public boolean isTrusted(final X509Certificate[] chain, 
String authType) throws CertificateException {
+                    public boolean isTrusted(final X509Certificate[] chain, 
String authType)
+                            throws CertificateException {
                         // Oh, I am easy...
                         return true;
                     }
@@ -253,8 +253,9 @@ public class KylinClient implements IRemoteClient {
             throw asIOException(get, response);
         }
 
-        List<TableMetaStub> tableMetaStubs = 
jsonMapper.readValue(response.getEntity().getContent(), new 
TypeReference<List<TableMetaStub>>() {
-        });
+        List<TableMetaStub> tableMetaStubs = 
jsonMapper.readValue(response.getEntity().getContent(),
+                new TypeReference<List<TableMetaStub>>() {
+                });
 
         List<KMetaTable> tables = convertMetaTables(tableMetaStubs);
         List<KMetaSchema> schemas = convertMetaSchemas(tables);
@@ -316,15 +317,21 @@ public class KylinClient implements IRemoteClient {
         for (ColumnMetaStub columnStub : tableStub.getColumns()) {
             columns.add(convertMetaColumn(columnStub));
         }
-        return new KMetaTable(tableStub.getTABLE_CAT(), 
tableStub.getTABLE_SCHEM(), tableStub.getTABLE_NAME(), 
tableStub.getTABLE_TYPE(), columns);
+        return new KMetaTable(tableStub.getTABLE_CAT(), 
tableStub.getTABLE_SCHEM(), tableStub.getTABLE_NAME(),
+                tableStub.getTABLE_TYPE(), columns);
     }
 
     private KMetaColumn convertMetaColumn(ColumnMetaStub columnStub) {
-        return new KMetaColumn(columnStub.getTABLE_CAT(), 
columnStub.getTABLE_SCHEM(), columnStub.getTABLE_NAME(), 
columnStub.getCOLUMN_NAME(), columnStub.getDATA_TYPE(), 
columnStub.getTYPE_NAME(), columnStub.getCOLUMN_SIZE(), 
columnStub.getDECIMAL_DIGITS(), columnStub.getNUM_PREC_RADIX(), 
columnStub.getNULLABLE(), columnStub.getCHAR_OCTET_LENGTH(), 
columnStub.getORDINAL_POSITION(), columnStub.getIS_NULLABLE());
+        return new KMetaColumn(columnStub.getTABLE_CAT(), 
columnStub.getTABLE_SCHEM(), columnStub.getTABLE_NAME(),
+                columnStub.getCOLUMN_NAME(), columnStub.getDATA_TYPE(), 
columnStub.getTYPE_NAME(),
+                columnStub.getCOLUMN_SIZE(), columnStub.getDECIMAL_DIGITS(), 
columnStub.getNUM_PREC_RADIX(),
+                columnStub.getNULLABLE(), columnStub.getCHAR_OCTET_LENGTH(), 
columnStub.getORDINAL_POSITION(),
+                columnStub.getIS_NULLABLE());
     }
 
     @Override
-    public QueryResult executeQuery(String sql, List<AvaticaParameter> params, 
List<Object> paramValues, Map<String, String> queryToggles) throws IOException {
+    public QueryResult executeQuery(String sql, List<AvaticaParameter> params, 
List<Object> paramValues,
+            Map<String, String> queryToggles) throws IOException {
 
         SQLResponseStub queryResp = executeKylinQuery(sql, 
convertParameters(params, paramValues), queryToggles);
         if (queryResp.getIsException())
@@ -349,17 +356,14 @@ public class KylinClient implements IRemoteClient {
         return result;
     }
 
-    private SQLResponseStub executeKylinQuery(String sql, 
List<StatementParameter> params, Map<String, String> queryToggles) throws 
IOException {
+    private SQLResponseStub executeKylinQuery(String sql, 
List<StatementParameter> params,
+            Map<String, String> queryToggles) throws IOException {
         String url = baseUrl() + "/kylin/api/query";
         String project = conn.getProject();
 
-        QueryRequest request = null;
+        PreparedQueryRequest request = new PreparedQueryRequest();
         if (null != params) {
-            request = new PreparedQueryRequest();
-            ((PreparedQueryRequest) request).setParams(params);
-            url += "/prestate"; // means prepared statement..
-        } else {
-            request = new QueryRequest();
+            request.setParams(params);
         }
         request.setSql(sql);
         request.setProject(project);
@@ -391,7 +395,11 @@ public class KylinClient implements IRemoteClient {
             Class columnClass = convertType(scm.getColumnType());
             ScalarType type = ColumnMetaData.scalar(scm.getColumnType(), 
scm.getColumnTypeName(), Rep.of(columnClass));
 
-            ColumnMetaData meta = new ColumnMetaData(i, scm.isAutoIncrement(), 
scm.isCaseSensitive(), scm.isSearchable(), scm.isCurrency(), 
scm.getIsNullable(), scm.isSigned(), scm.getDisplaySize(), scm.getLabel(), 
scm.getName(), scm.getSchemaName(), scm.getPrecision(), scm.getScale(), 
scm.getTableName(), scm.getSchemaName(), type, scm.isReadOnly(), 
scm.isWritable(), scm.isWritable(), columnClass.getCanonicalName());
+            ColumnMetaData meta = new ColumnMetaData(i, scm.isAutoIncrement(), 
scm.isCaseSensitive(),
+                    scm.isSearchable(), scm.isCurrency(), scm.getIsNullable(), 
scm.isSigned(), scm.getDisplaySize(),
+                    scm.getLabel(), scm.getName(), scm.getSchemaName(), 
scm.getPrecision(), scm.getScale(),
+                    scm.getTableName(), scm.getSchemaName(), type, 
scm.isReadOnly(), scm.isWritable(), scm.isWritable(),
+                    columnClass.getCanonicalName());
 
             metas.add(meta);
         }
@@ -416,7 +424,8 @@ public class KylinClient implements IRemoteClient {
     }
 
     private IOException asIOException(HttpRequestBase request, HttpResponse 
response) throws IOException {
-        return new IOException(request.getMethod() + " failed, error code " + 
response.getStatusLine().getStatusCode() + " and response: " + 
EntityUtils.toString(response.getEntity()));
+        return new IOException(request.getMethod() + " failed, error code " + 
response.getStatusLine().getStatusCode()
+                + " and response: " + 
EntityUtils.toString(response.getEntity()));
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/kylin/blob/46c3e25f/kylin-it/src/test/java/org/apache/kylin/jdbc/ITJDBCDriverTest.java
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/java/org/apache/kylin/jdbc/ITJDBCDriverTest.java 
b/kylin-it/src/test/java/org/apache/kylin/jdbc/ITJDBCDriverTest.java
index ba293ae..765b12c 100644
--- a/kylin-it/src/test/java/org/apache/kylin/jdbc/ITJDBCDriverTest.java
+++ b/kylin-it/src/test/java/org/apache/kylin/jdbc/ITJDBCDriverTest.java
@@ -230,7 +230,11 @@ public class ITJDBCDriverTest extends 
HBaseMetadataTestCase {
 
         ResultSet rs = statement.executeQuery();
 
-        Assert.assertFalse(rs.next());
+        Assert.assertTrue(rs.next());
+
+        String format_name = rs.getString(1);
+
+        Assert.assertTrue("FP-GTC".equals(format_name));
 
         rs.close();
         statement.close();

http://git-wip-us.apache.org/repos/asf/kylin/blob/46c3e25f/server-base/src/main/java/org/apache/kylin/rest/controller/QueryController.java
----------------------------------------------------------------------
diff --git 
a/server-base/src/main/java/org/apache/kylin/rest/controller/QueryController.java
 
b/server-base/src/main/java/org/apache/kylin/rest/controller/QueryController.java
index 44b5e72..e944563 100644
--- 
a/server-base/src/main/java/org/apache/kylin/rest/controller/QueryController.java
+++ 
b/server-base/src/main/java/org/apache/kylin/rest/controller/QueryController.java
@@ -74,7 +74,7 @@ public class QueryController extends BasicController {
 
     @RequestMapping(value = "/query", method = RequestMethod.POST, produces = 
{ "application/json" })
     @ResponseBody
-    public SQLResponse query(@RequestBody SQLRequest sqlRequest) {
+    public SQLResponse query(@RequestBody PrepareSqlRequest sqlRequest) {
         return queryService.doQueryWithCache(sqlRequest);
     }
 

http://git-wip-us.apache.org/repos/asf/kylin/blob/46c3e25f/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
----------------------------------------------------------------------
diff --git 
a/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java 
b/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
index 2cbe516..2b1626f 100644
--- a/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
+++ b/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
@@ -804,14 +804,24 @@ public class QueryService extends BasicService {
 
         try {
 
-            // special case for prepare query. 
+            // special case for prepare query.
             if (BackdoorToggles.getPrepareOnly()) {
                 return getPrepareOnlySqlResponse(correctedSql, conn, 
isPushDown, results, columnMetas);
             }
 
-            stat = conn.createStatement();
-            processStatementAttr(stat, sqlRequest);
-            resultSet = stat.executeQuery(correctedSql);
+            if (isPrepareStatementWithParams(sqlRequest)) {
+
+                PreparedStatement preparedState = 
conn.prepareStatement(correctedSql);
+                processStatementAttr(preparedState, sqlRequest);
+                for (int i = 0; i < ((PrepareSqlRequest) 
sqlRequest).getParams().length; i++) {
+                    setParam(preparedState, i + 1, ((PrepareSqlRequest) 
sqlRequest).getParams()[i]);
+                }
+                resultSet = preparedState.executeQuery();
+            } else {
+                stat = conn.createStatement();
+                processStatementAttr(stat, sqlRequest);
+                resultSet = stat.executeQuery(correctedSql);
+            }
 
             ResultSetMetaData metaData = resultSet.getMetaData();
             int columnCount = metaData.getColumnCount();
@@ -906,6 +916,13 @@ public class QueryService extends BasicService {
         return buildSqlResponse(isPushDown, results, columnMetas);
     }
 
+    private boolean isPrepareStatementWithParams(SQLRequest sqlRequest) {
+        if (sqlRequest instanceof PrepareSqlRequest && ((PrepareSqlRequest) 
sqlRequest).getParams() != null
+                && ((PrepareSqlRequest) sqlRequest).getParams().length > 0)
+            return true;
+        return false;
+    }
+
     private SQLResponse buildSqlResponse(Boolean isPushDown, 
List<List<String>> results,
             List<SelectedColumnMeta> columnMetas) {
 

http://git-wip-us.apache.org/repos/asf/kylin/blob/46c3e25f/server/src/test/java/org/apache/kylin/rest/controller/QueryControllerTest.java
----------------------------------------------------------------------
diff --git 
a/server/src/test/java/org/apache/kylin/rest/controller/QueryControllerTest.java
 
b/server/src/test/java/org/apache/kylin/rest/controller/QueryControllerTest.java
index d9eb3fa..c9f9296 100644
--- 
a/server/src/test/java/org/apache/kylin/rest/controller/QueryControllerTest.java
+++ 
b/server/src/test/java/org/apache/kylin/rest/controller/QueryControllerTest.java
@@ -21,7 +21,7 @@ package org.apache.kylin.rest.controller;
 import org.apache.kylin.metadata.project.ProjectInstance;
 import org.apache.kylin.query.util.QueryUtil;
 import org.apache.kylin.rest.request.MetaRequest;
-import org.apache.kylin.rest.request.SQLRequest;
+import org.apache.kylin.rest.request.PrepareSqlRequest;
 import org.apache.kylin.rest.response.SQLResponse;
 import org.apache.kylin.rest.service.QueryService;
 import org.apache.kylin.rest.service.ServiceTestBase;
@@ -29,9 +29,9 @@ import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
 
 import net.sf.ehcache.CacheManager;
-import org.springframework.beans.factory.annotation.Qualifier;
 
 /**
  * @author xduo
@@ -57,7 +57,7 @@ public class QueryControllerTest extends ServiceTestBase {
 
     @Test(expected = Exception.class)
     public void testQueryException() throws Exception {
-        SQLRequest sqlRequest = new SQLRequest();
+        PrepareSqlRequest sqlRequest = new PrepareSqlRequest();
         sqlRequest.setSql("select * from not_exist_table");
         sqlRequest.setProject("default");
         SQLResponse response1 = queryController.query(sqlRequest);
@@ -70,7 +70,9 @@ public class QueryControllerTest extends ServiceTestBase {
     @Test
     public void testErrorMsg() {
         String errorMsg = "error while executing SQL \"select 
lkp.clsfd_ga_prfl_id, ga.sum_dt, sum(ga.bounces) as bounces, sum(ga.exits) as 
exits, sum(ga.entrances) as entrances, sum(ga.pageviews) as pageviews, 
count(distinct ga.GA_VSTR_ID, ga.GA_VST_ID) as visits, count(distinct 
ga.GA_VSTR_ID) as uniqVistors from CLSFD_GA_PGTYPE_CATEG_LOC ga left join 
clsfd_ga_prfl_lkp lkp on ga.SRC_GA_PRFL_ID = lkp.SRC_GA_PRFL_ID group by 
lkp.clsfd_ga_prfl_id,ga.sum_dt order by lkp.clsfd_ga_prfl_id,ga.sum_dt LIMIT 
50000\": From line 14, column 14 to line 14, column 29: Column 
'CLSFD_GA_PRFL_ID' not found in table 'LKP'";
-        assert QueryUtil.makeErrorMsgUserFriendly(errorMsg).equals("From line 
14, column 14 to line 14, column 29: Column 'CLSFD_GA_PRFL_ID' not found in 
table 'LKP'\n" + "while executing SQL: \"select lkp.clsfd_ga_prfl_id, 
ga.sum_dt, sum(ga.bounces) as bounces, sum(ga.exits) as exits, 
sum(ga.entrances) as entrances, sum(ga.pageviews) as pageviews, count(distinct 
ga.GA_VSTR_ID, ga.GA_VST_ID) as visits, count(distinct ga.GA_VSTR_ID) as 
uniqVistors from CLSFD_GA_PGTYPE_CATEG_LOC ga left join clsfd_ga_prfl_lkp lkp 
on ga.SRC_GA_PRFL_ID = lkp.SRC_GA_PRFL_ID group by 
lkp.clsfd_ga_prfl_id,ga.sum_dt order by lkp.clsfd_ga_prfl_id,ga.sum_dt LIMIT 
50000\"");
+        assert QueryUtil.makeErrorMsgUserFriendly(errorMsg).equals(
+                "From line 14, column 14 to line 14, column 29: Column 
'CLSFD_GA_PRFL_ID' not found in table 'LKP'\n"
+                        + "while executing SQL: \"select lkp.clsfd_ga_prfl_id, 
ga.sum_dt, sum(ga.bounces) as bounces, sum(ga.exits) as exits, 
sum(ga.entrances) as entrances, sum(ga.pageviews) as pageviews, count(distinct 
ga.GA_VSTR_ID, ga.GA_VST_ID) as visits, count(distinct ga.GA_VSTR_ID) as 
uniqVistors from CLSFD_GA_PGTYPE_CATEG_LOC ga left join clsfd_ga_prfl_lkp lkp 
on ga.SRC_GA_PRFL_ID = lkp.SRC_GA_PRFL_ID group by 
lkp.clsfd_ga_prfl_id,ga.sum_dt order by lkp.clsfd_ga_prfl_id,ga.sum_dt LIMIT 
50000\"");
     }
 
     @Test

Reply via email to