From 500907f583c4471d009b130f639be1cae237b216 Mon Sep 17 00:00:00 2001 From: lidongsjtu Date: Tue, 20 Oct 2015 12:30:55 +0800 Subject: [PATCH] KYLIN-1033 Error when joining two sub-queries --- .../apache/kylin/query/relnode/OLAPJoinRel.java | 17 +++++++++--- .../query/relnode/OLAPToEnumerableConverter.java | 4 +++ .../test/resources/query/sql_subquery/query08.sql | 32 ++++++++++++++++++++++ .../apache/kylin/rest/service/QueryService.java | 4 ++- 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 query/src/test/resources/query/sql_subquery/query08.sql diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java index 30f059c..c6bdb73 100644 --- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java +++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPJoinRel.java @@ -262,10 +262,19 @@ public class OLAPJoinRel extends EnumerableJoin implements OLAPRel { @Override public Result implement(EnumerableRelImplementor implementor, Prefer pref) { - PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray()); - RelOptTable factTable = context.firstTableScan.getTable(); - MethodCallExpression exprCall = Expressions.call(factTable.getExpression(OLAPTable.class), "executeIndexQuery", implementor.getRootExpression(), Expressions.constant(context.id)); - return implementor.result(physType, Blocks.toBlock(exprCall)); + if (this.hasSubQuery) { + try { + return constr.newInstance(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), // + left, right, condition, leftKeys, rightKeys, joinType, variablesStopped).implement(implementor, pref); + } catch (Exception e) { + throw new IllegalStateException("Can't create EnumerableJoin!", e); + } + } else { + PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray()); + RelOptTable factTable = context.firstTableScan.getTable(); + MethodCallExpression exprCall = Expressions.call(factTable.getExpression(OLAPTable.class), "executeIndexQuery", implementor.getRootExpression(), Expressions.constant(context.id)); + return implementor.result(physType, Blocks.toBlock(exprCall)); + } } @Override diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java index c169706..7940205 100644 --- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java +++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPToEnumerableConverter.java @@ -69,6 +69,10 @@ public class OLAPToEnumerableConverter extends ConverterImpl implements Enumerab // find cube from olap context try { for (OLAPContext context : OLAPContext.getThreadLocalContexts()) { + if (context.olapSchema == null) { + // use defaulting Calcite behavior if not need to query cubes + continue; + } IRealization realization = QueryRouter.selectRealization(context); context.realization = realization; } diff --git a/query/src/test/resources/query/sql_subquery/query08.sql b/query/src/test/resources/query/sql_subquery/query08.sql new file mode 100644 index 0000000..7e9820d --- /dev/null +++ b/query/src/test/resources/query/sql_subquery/query08.sql @@ -0,0 +1,32 @@ +-- +-- 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. +-- + +SELECT t1.week_beg_dt, t1.sum_price, t2.cnt +FROM ( + select test_cal_dt.week_beg_dt, sum(test_kylin_fact.price) as sum_price + from test_kylin_fact + inner join edw.test_cal_dt as test_cal_dt ON test_kylin_fact.cal_dt = test_cal_dt.cal_dt + group by test_cal_dt.week_beg_dt +) t1 +inner join ( + select test_cal_dt.week_beg_dt, count(*) as cnt + from test_kylin_fact + inner join edw.test_cal_dt as test_cal_dt ON test_kylin_fact.cal_dt = test_cal_dt.cal_dt + group by test_cal_dt.week_beg_dt +) t2 +on t1.week_beg_dt=t2.week_beg_dt \ No newline at end of file diff --git a/server/src/main/java/org/apache/kylin/rest/service/QueryService.java b/server/src/main/java/org/apache/kylin/rest/service/QueryService.java index 8a397cd..2f68427 100644 --- a/server/src/main/java/org/apache/kylin/rest/service/QueryService.java +++ b/server/src/main/java/org/apache/kylin/rest/service/QueryService.java @@ -395,7 +395,9 @@ public class QueryService extends BasicService { if (OLAPContext.getThreadLocalContexts() != null) { // contexts can be null in case of 'explain plan for' for (OLAPContext ctx : OLAPContext.getThreadLocalContexts()) { isPartialResult |= ctx.storageContext.isPartialResultReturned(); - cube = ctx.realization.getName(); + if (ctx.realization != null) { + cube = ctx.realization.getName(); + } totalScanCount += ctx.storageContext.getTotalScanCount(); } } -- 2.3.8 (Apple Git-58)