From fe91e1afe0c39ab65d1ebfdd0d36fee100a707e3 Mon Sep 17 00:00:00 2001 From: nichunen Date: Mon, 15 Feb 2016 21:39:35 +0800 Subject: [PATCH] KYLIN-1419 NullPointerException occurs when query from subqueries with order by --- .../apache/kylin/query/relnode/OLAPSortRel.java | 21 ++++++++------ .../test/resources/query/sql_subquery/query09.sql | 33 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 query/src/test/resources/query/sql_subquery/query09.sql diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java index e8b7d17..96d768c 100644 --- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java +++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPSortRel.java @@ -81,15 +81,20 @@ public class OLAPSortRel extends Sort implements OLAPRel { public void implementRewrite(RewriteImplementor implementor) { implementor.visitChild(this, getInput()); - for (RelFieldCollation fieldCollation : this.collation.getFieldCollations()) { - int index = fieldCollation.getFieldIndex(); - StorageContext.OrderEnum order = getOrderEnum(fieldCollation.getDirection()); - OLAPRel olapChild = (OLAPRel) this.getInput(); - TblColRef orderCol = olapChild.getColumnRowType().getAllColumns().get(index); - MeasureDesc measure = findMeasure(orderCol); - if (measure != null) { - this.context.storageContext.addSort(measure, order); + if(this.context.realization != null){ + for (RelFieldCollation fieldCollation : this.collation.getFieldCollations()) { + int index = fieldCollation.getFieldIndex(); + StorageContext.OrderEnum order = getOrderEnum(fieldCollation.getDirection()); + OLAPRel olapChild = (OLAPRel) this.getInput(); + TblColRef orderCol = olapChild.getColumnRowType().getAllColumns().get(index); + MeasureDesc measure = findMeasure(orderCol); + if (measure != null) { + this.context.storageContext.addSort(measure, order); + } } + } + + if(this.collation.getFieldCollations().size() > 0){ this.context.storageContext.markSort(); } diff --git a/query/src/test/resources/query/sql_subquery/query09.sql b/query/src/test/resources/query/sql_subquery/query09.sql new file mode 100644 index 0000000..db6c5b6 --- /dev/null +++ b/query/src/test/resources/query/sql_subquery/query09.sql @@ -0,0 +1,33 @@ +-- +-- 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 +order by t1.week_beg_dt \ No newline at end of file -- 2.4.9 (Apple Git-60)