You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2023/01/03 10:19:35 UTC

[GitHub] [doris] Gabriel39 commented on a diff in pull request #15558: [Improvement](topn) runtime prune for topn query

Gabriel39 commented on code in PR #15558:
URL: https://github.com/apache/doris/pull/15558#discussion_r1060405188


##########
be/src/vec/exec/vsort_node.cpp:
##########
@@ -123,6 +126,33 @@ Status VSortNode::open(RuntimeState* state) {
                                   _children[0], std::placeholders::_1, std::placeholders::_2,
                                   std::placeholders::_3)),
                 child(0)->get_next_span(), eos);
+
+        if (upstream_block->rows() != 0) {
+            RETURN_IF_ERROR(_sorter->append_block(upstream_block.get()));
+            RETURN_IF_CANCELLED(state);
+            RETURN_IF_ERROR(state->check_query_state("vsort, while sorting input."));
+
+            // runtime predicate
+            // SCOPED_TIMER(_update_runtime_predicate_timer);
+            if (_use_topn_opt) {
+                Field new_top = _sorter->get_top_value();
+                if (!new_top.is_null() && new_top != old_top) {
+                    auto & sort_description = _sorter->get_sort_description();
+                    auto col = upstream_block->get_by_position(sort_description[0].column_number);
+                    auto type = remove_nullable(col.type)->get_type_id();
+                    bool is_reverse = sort_description[0].direction < 0;
+                    auto query_ctx = _runtime_state->get_query_fragments_ctx();
+                    std::vector<vectorized::Field> values = {new_top};

Review Comment:
   Why use vector here?



##########
be/src/runtime/runtime_predicate.cpp:
##########
@@ -0,0 +1,173 @@
+// 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.
+
+#include "runtime/runtime_predicate.h"
+#include "olap/predicate_creator.h"
+
+
+namespace doris {
+
+namespace vectorized {
+
+Status RuntimePredicate::update(std::vector<Field>& values, const String& col_name,
+                const TypeIndex type, bool is_reverse) {
+    std::unique_lock<std::shared_mutex> wlock(_rwlock);
+
+    if (!_predicate_mem_pool) {
+        _predicate_mem_pool.reset(new MemPool());
+    }
+
+    bool updated = false;
+
+    if (UNLIKELY(_orderby_extrems.size() == 0)) {
+        _orderby_extrems.push_back(values[0]);
+        updated = true;
+    } else if (is_reverse) {
+        if (values[0] > _orderby_extrems[0]) {
+            _orderby_extrems[0] = values[0];
+            updated = true;
+        }
+    } else {
+        if (values[0] < _orderby_extrems[0]) {
+            _orderby_extrems[0] = values[0];
+            updated = true;
+        }
+    }
+
+    if (!updated)
+        return Status::OK();
+
+    TCondition condition;
+    condition.__set_column_name(col_name);
+    condition.__set_column_unique_id(_tablet_schema->column(col_name).unique_id());
+    condition.__set_condition_op(is_reverse ? ">=" : "<=");

Review Comment:
   Should we use ">" and "<" instead?



##########
be/src/vec/exec/vsort_node.cpp:
##########
@@ -123,6 +126,33 @@ Status VSortNode::open(RuntimeState* state) {
                                   _children[0], std::placeholders::_1, std::placeholders::_2,
                                   std::placeholders::_3)),
                 child(0)->get_next_span(), eos);
+
+        if (upstream_block->rows() != 0) {

Review Comment:
   This code block should be inside `sink`



##########
be/src/runtime/runtime_predicate.cpp:
##########
@@ -0,0 +1,173 @@
+// 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.
+
+#include "runtime/runtime_predicate.h"
+#include "olap/predicate_creator.h"
+
+
+namespace doris {
+
+namespace vectorized {
+
+Status RuntimePredicate::update(std::vector<Field>& values, const String& col_name,
+                const TypeIndex type, bool is_reverse) {
+    std::unique_lock<std::shared_mutex> wlock(_rwlock);
+
+    if (!_predicate_mem_pool) {
+        _predicate_mem_pool.reset(new MemPool());
+    }
+
+    bool updated = false;
+
+    if (UNLIKELY(_orderby_extrems.size() == 0)) {
+        _orderby_extrems.push_back(values[0]);
+        updated = true;
+    } else if (is_reverse) {
+        if (values[0] > _orderby_extrems[0]) {
+            _orderby_extrems[0] = values[0];
+            updated = true;
+        }
+    } else {
+        if (values[0] < _orderby_extrems[0]) {
+            _orderby_extrems[0] = values[0];
+            updated = true;
+        }
+    }
+
+    if (!updated)
+        return Status::OK();
+
+    TCondition condition;
+    condition.__set_column_name(col_name);
+    condition.__set_column_unique_id(_tablet_schema->column(col_name).unique_id());
+    condition.__set_condition_op(is_reverse ? ">=" : "<=");
+
+    Field& field = _orderby_extrems[0];
+    String str_value;
+    int scale = 0;
+
+    switch (type) {

Review Comment:
   This switch-case is too time-consuming. Since the `type` is fixed for a specific query plan, should we initialize a function pointer once and call the function directly?



##########
be/src/runtime/runtime_predicate.h:
##########
@@ -0,0 +1,72 @@
+// 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.
+
+#pragma once
+
+#include <atomic>
+#include <string>
+#include <mutex>
+#include <shared_mutex>
+
+#include "common/config.h"
+#include "common/object_pool.h"
+#include "gen_cpp/PaloInternalService_types.h" // for TQueryOptions
+#include "gen_cpp/Types_types.h"               // for TUniqueId
+#include "runtime/datetime_value.h"
+#include "runtime/exec_env.h"
+#include "runtime/mem_pool.h"
+#include "util/threadpool.h"
+#include "vec/core/types.h"
+#include "vec/core/field.h"
+
+#include "exec/olap_common.h"
+#include "olap/tablet_schema.h"
+#include "olap/column_predicate.h"
+// #include "olap/predicate_creator.h"
+
+
+namespace doris {
+
+namespace vectorized {
+
+class RuntimePredicate {
+public:
+    RuntimePredicate() { }
+
+    void set_tablet_schema(TabletSchemaSPtr tablet_schema) {
+        std::unique_lock<std::shared_mutex> wlock(_rwlock);
+        _tablet_schema = tablet_schema;
+    }
+
+    std::shared_ptr<ColumnPredicate> get_predictate() {
+        std::shared_lock<std::shared_mutex> rlock(_rwlock);
+        return _predictate;
+    }
+
+    Status update(std::vector<Field>& values, const String& col_name,
+                  const TypeIndex type, bool is_reverse);
+
+private:
+    mutable std::shared_mutex _rwlock;
+    std::vector<vectorized::Field> _orderby_extrems;

Review Comment:
   why use a vector?



##########
be/src/runtime/runtime_predicate.cpp:
##########
@@ -0,0 +1,173 @@
+// 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.
+
+#include "runtime/runtime_predicate.h"
+#include "olap/predicate_creator.h"
+
+
+namespace doris {
+
+namespace vectorized {
+
+Status RuntimePredicate::update(std::vector<Field>& values, const String& col_name,
+                const TypeIndex type, bool is_reverse) {
+    std::unique_lock<std::shared_mutex> wlock(_rwlock);
+
+    if (!_predicate_mem_pool) {

Review Comment:
   Initialize `_predicate_mem_pool` once in a individual init function will help us to avoid this conditional judgement



##########
be/src/runtime/runtime_predicate.cpp:
##########
@@ -0,0 +1,173 @@
+// 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.
+
+#include "runtime/runtime_predicate.h"
+#include "olap/predicate_creator.h"
+
+
+namespace doris {
+
+namespace vectorized {
+
+Status RuntimePredicate::update(std::vector<Field>& values, const String& col_name,
+                const TypeIndex type, bool is_reverse) {

Review Comment:
   Maybe we can use PrimitiveType instead of TypeIndex. If so, we can use macro to refactor the complex switch-case block below



##########
be/src/runtime/runtime_predicate.h:
##########
@@ -0,0 +1,72 @@
+// 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.
+
+#pragma once
+
+#include <atomic>
+#include <string>
+#include <mutex>
+#include <shared_mutex>
+
+#include "common/config.h"
+#include "common/object_pool.h"
+#include "gen_cpp/PaloInternalService_types.h" // for TQueryOptions
+#include "gen_cpp/Types_types.h"               // for TUniqueId
+#include "runtime/datetime_value.h"
+#include "runtime/exec_env.h"
+#include "runtime/mem_pool.h"
+#include "util/threadpool.h"
+#include "vec/core/types.h"
+#include "vec/core/field.h"
+
+#include "exec/olap_common.h"
+#include "olap/tablet_schema.h"
+#include "olap/column_predicate.h"
+// #include "olap/predicate_creator.h"
+
+
+namespace doris {
+
+namespace vectorized {
+
+class RuntimePredicate {
+public:
+    RuntimePredicate() { }
+
+    void set_tablet_schema(TabletSchemaSPtr tablet_schema) {
+        std::unique_lock<std::shared_mutex> wlock(_rwlock);
+        _tablet_schema = tablet_schema;
+    }
+
+    std::shared_ptr<ColumnPredicate> get_predictate() {
+        std::shared_lock<std::shared_mutex> rlock(_rwlock);
+        return _predictate;

Review Comment:
   Better to use unique_ptr and return a pure pointer here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org