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 2021/11/30 05:51:32 UTC

[GitHub] [incubator-doris] morningman opened a new pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

morningman opened a new pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255


   ## Proposed changes
   
   1. Add table function node
   2. Add 3 table functions: explode_split, explode_bitmap and explode_json_array
   
   ## Types of changes
   
   What types of changes does your code introduce to Doris?
   _Put an `x` in the boxes that apply_
   
   - [ ] Bugfix (non-breaking change which fixes an issue)
   - [ ] New feature (non-breaking change which adds functionality)
   - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
   - [ ] Documentation Update (if none of the other choices apply)
   - [ ] Code refactor (Modify the code structure, format the code, etc...)
   - [ ] Optimization. Including functional usability improvements and performance improvements.
   - [ ] Dependency. Such as changes related to third-party components.
   - [ ] Other.
   
   ## Checklist
   
   _Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code._
   
   - [ ] I have created an issue on (Fix #ISSUE) and described the bug/feature there in detail
   - [ ] Compiling and unit tests pass locally with my changes
   - [ ] I have added tests that prove my fix is effective or that my feature works
   - [ ] If these changes need document changes, I have updated the document
   - [ ] Any dependent changes have been merged
   
   ## Further comments
   
   If this is a relatively large or complex change, kick off the discussion at dev@doris.apache.org by explaining why you chose the solution you did and what alternatives you considered, etc...
   


-- 
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


[GitHub] [incubator-doris] morningman merged pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman merged pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255


   


-- 
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


[GitHub] [incubator-doris] morningman commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r767156283



##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+
+Status TableFunctionNode::get_next(RuntimeState* state, RowBatch* row_batch, bool* eos) {
+    RETURN_IF_ERROR(exec_debug_action(TExecNodePhase::GETNEXT));
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+
+    const RowDescriptor& parent_rowdesc = row_batch->row_desc();
+    const RowDescriptor& child_rowdesc = _children[0]->row_desc();
+    if (_parent_tuple_desc_size == -1) {
+        _parent_tuple_desc_size = parent_rowdesc.tuple_descriptors().size();
+        _child_tuple_desc_size = child_rowdesc.tuple_descriptors().size();
+        for (int i = 0; i < _child_tuple_desc_size; ++i) {
+            _child_slot_sizes.push_back(child_rowdesc.tuple_descriptors()[i]->slots().size());
+        }    
+    }
+
+    uint8_t* tuple_buffer = nullptr;
+    Tuple* tuple_ptr = nullptr;
+    Tuple* pre_tuple_ptr = nullptr;
+    int row_idx = 0;
+
+    while (true) {
+        RETURN_IF_CANCELLED(state);
+        RETURN_IF_ERROR(state->check_query_state("TableFunctionNode, while getting next batch."));
+
+        if (_cur_child_batch == nullptr) {
+            _cur_child_batch.reset(new RowBatch(child_rowdesc, state->batch_size(), mem_tracker().get()));
+        }
+        if (_child_batch_exhausted) {
+            // current child batch is exhausted, get next batch from child
+            RETURN_IF_ERROR(_children[0]->get_next(state, _cur_child_batch.get(), eos));
+            if (*eos) {
+                break;
+            }
+            _cur_child_offset = 0;
+            RETURN_IF_ERROR(_process_next_child_row());

Review comment:
       It need to be called before we call `_find_last_fn_eos_idx()` at first time




-- 
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


[GitHub] [incubator-doris] github-actions[bot] commented on pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#issuecomment-994676966






-- 
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


[GitHub] [incubator-doris] morningman commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r767155170



##########
File path: fe/fe-core/src/main/java/org/apache/doris/qe/Coordinator.java
##########
@@ -573,6 +573,7 @@ private void sendFragment() throws TException, RpcException, UserException {
                         LOG.warn("catch a execute exception", e);
                         exception = e;
                         code = TStatusCode.THRIFT_RPC_ERROR;
+                        BackendServiceProxy.getInstance().removeProxy(pair.first.brpcAddress);

Review comment:
       We should remove proxy if rpc error happen




-- 
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


[GitHub] [incubator-doris] morningman commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r767155340



##########
File path: fe/fe-core/src/main/java/org/apache/doris/analysis/LateralViewRef.java
##########
@@ -17,10 +17,10 @@
 
 package org.apache.doris.analysis;
 
+import com.google.common.base.Preconditions;

Review comment:
       done




-- 
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


[GitHub] [incubator-doris] EmmyMiao87 commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
EmmyMiao87 commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r766503752



##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+

Review comment:
       Please add examples of real data above two while(true)

##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+
+Status TableFunctionNode::get_next(RuntimeState* state, RowBatch* row_batch, bool* eos) {
+    RETURN_IF_ERROR(exec_debug_action(TExecNodePhase::GETNEXT));
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+
+    const RowDescriptor& parent_rowdesc = row_batch->row_desc();
+    const RowDescriptor& child_rowdesc = _children[0]->row_desc();
+    if (_parent_tuple_desc_size == -1) {
+        _parent_tuple_desc_size = parent_rowdesc.tuple_descriptors().size();
+        _child_tuple_desc_size = child_rowdesc.tuple_descriptors().size();
+        for (int i = 0; i < _child_tuple_desc_size; ++i) {
+            _child_slot_sizes.push_back(child_rowdesc.tuple_descriptors()[i]->slots().size());
+        }    
+    }
+
+    uint8_t* tuple_buffer = nullptr;
+    Tuple* tuple_ptr = nullptr;
+    Tuple* pre_tuple_ptr = nullptr;
+    int row_idx = 0;
+
+    while (true) {
+        RETURN_IF_CANCELLED(state);
+        RETURN_IF_ERROR(state->check_query_state("TableFunctionNode, while getting next batch."));
+
+        if (_cur_child_batch == nullptr) {
+            _cur_child_batch.reset(new RowBatch(child_rowdesc, state->batch_size(), mem_tracker().get()));
+        }
+        if (_child_batch_exhausted) {
+            // current child batch is exhausted, get next batch from child
+            RETURN_IF_ERROR(_children[0]->get_next(state, _cur_child_batch.get(), eos));
+            if (*eos) {
+                break;
+            }
+            _cur_child_offset = 0;
+            RETURN_IF_ERROR(_process_next_child_row());
+            if (_child_batch_exhausted) {
+                _cur_child_batch->reset();
+                continue;
+            }
+        }
+
+        while (true) {
+            int idx = _find_last_fn_eos_idx();
+            if (idx == 0) {
+                // all table functions' results are exhausted, process next child row
+                RETURN_IF_ERROR(_process_next_child_row());
+                if (_child_batch_exhausted) {
+                    break;
+                }
+            } else if (idx < _fn_num && idx != -1) {
+                // some of table functions' results are exhausted
+                if (!_roll_table_functions(idx)) {
+                    // continue to process next child row
+                    continue; 
+                }
+            }
+
+            // get slots from every table function
+            // Notice that _fn_values[i] may be null if the table function has empty result set.
+            for (int i = 0; i < _fn_num; i++) {
+                RETURN_IF_ERROR(_fns[i]->get_value(&_fn_values[i]));
+            }
+
+            // allocate memory for row batch for the first time
+            if (tuple_buffer == nullptr) {
+                int64_t tuple_buffer_size;
+                RETURN_IF_ERROR(
+                        row_batch->resize_and_allocate_tuple_buffer(state, &tuple_buffer_size, &tuple_buffer));
+                tuple_ptr = reinterpret_cast<Tuple*>(tuple_buffer);
+            }
+            
+            pre_tuple_ptr = tuple_ptr;

Review comment:
       The logic of constructing a new line can be abstracted separately into a function




-- 
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


[GitHub] [incubator-doris] morningman commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r767154708



##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+
+Status TableFunctionNode::get_next(RuntimeState* state, RowBatch* row_batch, bool* eos) {
+    RETURN_IF_ERROR(exec_debug_action(TExecNodePhase::GETNEXT));
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+
+    const RowDescriptor& parent_rowdesc = row_batch->row_desc();
+    const RowDescriptor& child_rowdesc = _children[0]->row_desc();
+    if (_parent_tuple_desc_size == -1) {
+        _parent_tuple_desc_size = parent_rowdesc.tuple_descriptors().size();
+        _child_tuple_desc_size = child_rowdesc.tuple_descriptors().size();
+        for (int i = 0; i < _child_tuple_desc_size; ++i) {
+            _child_slot_sizes.push_back(child_rowdesc.tuple_descriptors()[i]->slots().size());
+        }    
+    }
+
+    uint8_t* tuple_buffer = nullptr;
+    Tuple* tuple_ptr = nullptr;
+    Tuple* pre_tuple_ptr = nullptr;
+    int row_idx = 0;
+
+    while (true) {
+        RETURN_IF_CANCELLED(state);
+        RETURN_IF_ERROR(state->check_query_state("TableFunctionNode, while getting next batch."));
+
+        if (_cur_child_batch == nullptr) {
+            _cur_child_batch.reset(new RowBatch(child_rowdesc, state->batch_size(), mem_tracker().get()));
+        }
+        if (_child_batch_exhausted) {
+            // current child batch is exhausted, get next batch from child
+            RETURN_IF_ERROR(_children[0]->get_next(state, _cur_child_batch.get(), eos));
+            if (*eos) {
+                break;
+            }
+            _cur_child_offset = 0;
+            RETURN_IF_ERROR(_process_next_child_row());
+            if (_child_batch_exhausted) {
+                _cur_child_batch->reset();
+                continue;
+            }
+        }
+
+        while (true) {
+            int idx = _find_last_fn_eos_idx();
+            if (idx == 0) {
+                // all table functions' results are exhausted, process next child row
+                RETURN_IF_ERROR(_process_next_child_row());
+                if (_child_batch_exhausted) {
+                    break;
+                }
+            } else if (idx < _fn_num && idx != -1) {
+                // some of table functions' results are exhausted
+                if (!_roll_table_functions(idx)) {
+                    // continue to process next child row
+                    continue; 
+                }
+            }
+
+            // get slots from every table function
+            // Notice that _fn_values[i] may be null if the table function has empty result set.
+            for (int i = 0; i < _fn_num; i++) {
+                RETURN_IF_ERROR(_fns[i]->get_value(&_fn_values[i]));
+            }
+
+            // allocate memory for row batch for the first time
+            if (tuple_buffer == nullptr) {
+                int64_t tuple_buffer_size;
+                RETURN_IF_ERROR(
+                        row_batch->resize_and_allocate_tuple_buffer(state, &tuple_buffer_size, &tuple_buffer));
+                tuple_ptr = reinterpret_cast<Tuple*>(tuple_buffer);
+            }
+            
+            pre_tuple_ptr = tuple_ptr;
+            // The tuples order in parent row batch should be
+            //      child1, child2, tf1, tf2, ...
+            TupleRow* parent_tuple_row = row_batch->get_row(row_idx++);
+            // 1. copy child tuples
+            int tuple_idx = 0;
+            for (int i = 0; i < _child_tuple_desc_size; tuple_idx++, i++) {
+                TupleDescriptor* child_tuple_desc = child_rowdesc.tuple_descriptors()[tuple_idx];
+                TupleDescriptor* parent_tuple_desc = parent_rowdesc.tuple_descriptors()[tuple_idx];
+
+                for (int j = 0; j < _child_slot_sizes[i]; ++j) {
+                    SlotDescriptor* child_slot_desc = child_tuple_desc->slots()[j];
+                    SlotDescriptor* parent_slot_desc = parent_tuple_desc->slots()[j];
+
+                    if (_output_slot_ids[parent_slot_desc->id()]) {
+                        Tuple* child_tuple = _cur_child_tuple_row->get_tuple(child_rowdesc.get_tuple_idx(child_tuple_desc->id()));
+                        void* dest_slot = tuple_ptr->get_slot(parent_slot_desc->tuple_offset());
+                        RawValue::write(child_tuple->get_slot(child_slot_desc->tuple_offset()), dest_slot, parent_slot_desc->type(), row_batch->tuple_data_pool());
+                        tuple_ptr->set_not_null(parent_slot_desc->null_indicator_offset());
+                    } else {
+                        tuple_ptr->set_null(parent_slot_desc->null_indicator_offset());
+                    }
+                }
+                parent_tuple_row->set_tuple(tuple_idx, tuple_ptr);
+                tuple_ptr = reinterpret_cast<Tuple*>(reinterpret_cast<uint8_t*>(tuple_ptr) + parent_tuple_desc->byte_size());
+            }
+
+            // 2. copy funtion result
+            for (int i = 0; tuple_idx < _parent_tuple_desc_size; tuple_idx++, i++) {
+                TupleDescriptor* parent_tuple_desc = parent_rowdesc.tuple_descriptors()[tuple_idx];
+                SlotDescriptor* parent_slot_desc = parent_tuple_desc->slots()[0];
+                void* dest_slot = tuple_ptr->get_slot(parent_slot_desc->tuple_offset());
+                // if (_fn_values[i] != nullptr && _output_slot_ids[parent_slot_desc->id()]) {
+                if (_fn_values[i] != nullptr) {
+                    RawValue::write(_fn_values[i], dest_slot, parent_slot_desc->type(), row_batch->tuple_data_pool());
+                    tuple_ptr->set_not_null(parent_slot_desc->null_indicator_offset());
+                } else {
+                    tuple_ptr->set_null(parent_slot_desc->null_indicator_offset());
+                }
+                parent_tuple_row->set_tuple(tuple_idx, tuple_ptr);
+
+                tuple_ptr = reinterpret_cast<Tuple*>(reinterpret_cast<uint8_t*>(tuple_ptr) + parent_tuple_desc->byte_size());
+            }
+
+            // 3. eval conjuncts
+            if (eval_conjuncts(&_conjunct_ctxs[0], _conjunct_ctxs.size(), parent_tuple_row)) {
+                row_batch->commit_last_row();
+                ++_num_rows_returned;
+            } else {
+                tuple_ptr = pre_tuple_ptr;
+            }
+
+            // Forward after write success.
+            // Because data in `_fn_values` points to the data saved in functions.
+            // And `forward` will change the data in functions.
+            bool tmp;
+            _fns[_fn_num - 1]->forward(&tmp);
+
+            if (row_batch->at_capacity()) {
+                *eos = false;
+                break;
+            }
+        }
+
+        if (row_batch->at_capacity()) {
+            break;
+        } 
+
+        if (_child_batch_exhausted) {

Review comment:
       removed

##########
File path: be/src/exprs/table_function/table_function_factory.h
##########
@@ -0,0 +1,36 @@
+// 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 "exprs/table_function/table_function_factory.h"
+#include "exprs/table_function/explode_split.h"
+
+#include "common/status.h"
+
+namespace doris {
+
+class ObjectPool;
+class TableFunction;
+class TableFunctionFactory {

Review comment:
       No, this method is to get a table function, not eval a table function.

##########
File path: fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
##########
@@ -688,36 +693,36 @@ public void analyzeImpl(Analyzer analyzer) throws AnalysisException {
                 fn = getTableFunction(fnName.getFunction(), childTypes,
                         Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
                 if (fn == null) {
-                    throw new AnalysisException("Doris only support `explode_split(varchar, varchar)` table function");
+                    throw new AnalysisException(UNKNOWN_TABLE_FUNCTION_MSG);
                 }
-                return;
-            }
-            // now first find function in built-in functions
-            if (Strings.isNullOrEmpty(fnName.getDb())) {
-                Type[] childTypes = collectChildReturnTypes();
-                fn = getBuiltinFunction(analyzer, fnName.getFunction(), childTypes,
-                        Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
-            }
-
-            // find user defined functions
-            if (fn == null) {
-                if (!analyzer.isUDFAllowed()) {
-                    throw new AnalysisException(
-                            "Does not support non-builtin functions, or function does not exist: " + this.toSqlImpl());
+            } else {

Review comment:
       We need to let the logic continue, so that we can cast the params of the function.

##########
File path: fe/fe-core/src/main/java/org/apache/doris/qe/Coordinator.java
##########
@@ -573,6 +573,7 @@ private void sendFragment() throws TException, RpcException, UserException {
                         LOG.warn("catch a execute exception", e);
                         exception = e;
                         code = TStatusCode.THRIFT_RPC_ERROR;
+                        BackendServiceProxy.getInstance().removeProxy(pair.first.brpcAddress);

Review comment:
       We should remove proxy if rpc error happen

##########
File path: fe/fe-core/src/main/java/org/apache/doris/analysis/LateralViewRef.java
##########
@@ -17,10 +17,10 @@
 
 package org.apache.doris.analysis;
 
+import com.google.common.base.Preconditions;

Review comment:
       done

##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+
+Status TableFunctionNode::get_next(RuntimeState* state, RowBatch* row_batch, bool* eos) {
+    RETURN_IF_ERROR(exec_debug_action(TExecNodePhase::GETNEXT));
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+
+    const RowDescriptor& parent_rowdesc = row_batch->row_desc();
+    const RowDescriptor& child_rowdesc = _children[0]->row_desc();
+    if (_parent_tuple_desc_size == -1) {
+        _parent_tuple_desc_size = parent_rowdesc.tuple_descriptors().size();
+        _child_tuple_desc_size = child_rowdesc.tuple_descriptors().size();
+        for (int i = 0; i < _child_tuple_desc_size; ++i) {
+            _child_slot_sizes.push_back(child_rowdesc.tuple_descriptors()[i]->slots().size());
+        }    
+    }
+
+    uint8_t* tuple_buffer = nullptr;
+    Tuple* tuple_ptr = nullptr;
+    Tuple* pre_tuple_ptr = nullptr;
+    int row_idx = 0;
+
+    while (true) {
+        RETURN_IF_CANCELLED(state);
+        RETURN_IF_ERROR(state->check_query_state("TableFunctionNode, while getting next batch."));
+
+        if (_cur_child_batch == nullptr) {
+            _cur_child_batch.reset(new RowBatch(child_rowdesc, state->batch_size(), mem_tracker().get()));
+        }
+        if (_child_batch_exhausted) {
+            // current child batch is exhausted, get next batch from child
+            RETURN_IF_ERROR(_children[0]->get_next(state, _cur_child_batch.get(), eos));
+            if (*eos) {
+                break;
+            }
+            _cur_child_offset = 0;
+            RETURN_IF_ERROR(_process_next_child_row());

Review comment:
       It need to be called before we call `_find_last_fn_eos_idx()` at first time

##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+

Review comment:
       OK

##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+
+Status TableFunctionNode::get_next(RuntimeState* state, RowBatch* row_batch, bool* eos) {
+    RETURN_IF_ERROR(exec_debug_action(TExecNodePhase::GETNEXT));
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+
+    const RowDescriptor& parent_rowdesc = row_batch->row_desc();
+    const RowDescriptor& child_rowdesc = _children[0]->row_desc();
+    if (_parent_tuple_desc_size == -1) {
+        _parent_tuple_desc_size = parent_rowdesc.tuple_descriptors().size();
+        _child_tuple_desc_size = child_rowdesc.tuple_descriptors().size();
+        for (int i = 0; i < _child_tuple_desc_size; ++i) {
+            _child_slot_sizes.push_back(child_rowdesc.tuple_descriptors()[i]->slots().size());
+        }    
+    }
+
+    uint8_t* tuple_buffer = nullptr;
+    Tuple* tuple_ptr = nullptr;
+    Tuple* pre_tuple_ptr = nullptr;
+    int row_idx = 0;
+
+    while (true) {
+        RETURN_IF_CANCELLED(state);
+        RETURN_IF_ERROR(state->check_query_state("TableFunctionNode, while getting next batch."));
+
+        if (_cur_child_batch == nullptr) {
+            _cur_child_batch.reset(new RowBatch(child_rowdesc, state->batch_size(), mem_tracker().get()));
+        }
+        if (_child_batch_exhausted) {
+            // current child batch is exhausted, get next batch from child
+            RETURN_IF_ERROR(_children[0]->get_next(state, _cur_child_batch.get(), eos));
+            if (*eos) {
+                break;
+            }
+            _cur_child_offset = 0;
+            RETURN_IF_ERROR(_process_next_child_row());
+            if (_child_batch_exhausted) {
+                _cur_child_batch->reset();
+                continue;
+            }
+        }
+
+        while (true) {
+            int idx = _find_last_fn_eos_idx();
+            if (idx == 0) {
+                // all table functions' results are exhausted, process next child row
+                RETURN_IF_ERROR(_process_next_child_row());
+                if (_child_batch_exhausted) {
+                    break;
+                }
+            } else if (idx < _fn_num && idx != -1) {
+                // some of table functions' results are exhausted
+                if (!_roll_table_functions(idx)) {
+                    // continue to process next child row
+                    continue; 
+                }
+            }
+
+            // get slots from every table function
+            // Notice that _fn_values[i] may be null if the table function has empty result set.
+            for (int i = 0; i < _fn_num; i++) {
+                RETURN_IF_ERROR(_fns[i]->get_value(&_fn_values[i]));
+            }
+
+            // allocate memory for row batch for the first time
+            if (tuple_buffer == nullptr) {
+                int64_t tuple_buffer_size;
+                RETURN_IF_ERROR(
+                        row_batch->resize_and_allocate_tuple_buffer(state, &tuple_buffer_size, &tuple_buffer));
+                tuple_ptr = reinterpret_cast<Tuple*>(tuple_buffer);
+            }
+            
+            pre_tuple_ptr = tuple_ptr;

Review comment:
       I tried. But it has to pass a lot param to that new method, which seems not necessary.

##########
File path: fe/fe-core/src/main/java/org/apache/doris/analysis/LateralViewRef.java
##########
@@ -77,32 +77,31 @@ public void analyze(Analyzer analyzer) throws UserException {
         if (!(expr instanceof FunctionCallExpr)) {
             throw new AnalysisException("Only support function call expr in lateral view");
         }
+
+        analyzeFunctionExpr(analyzer);
+
+        // analyze lateral view
+        desc = analyzer.registerTableRef(this);
+        explodeSlotRef = new SlotRef(new TableName(null, viewName), columnName);
+        explodeSlotRef.analyze(analyzer);
+        isAnalyzed = true;  // true now that we have assigned desc
+    }
+
+    private void analyzeFunctionExpr(Analyzer analyzer) throws AnalysisException {
         fnExpr = (FunctionCallExpr) expr;
         fnExpr.setTableFnCall(true);
         checkAndSupplyDefaultTableName(fnExpr);
         fnExpr.analyze(analyzer);
-        if (!fnExpr.getFnName().getFunction().equals(FunctionSet.EXPLODE_SPLIT)) {
-            throw new AnalysisException("Only support explode function in lateral view");
-        }
         checkScalarFunction(fnExpr.getChild(0));
-        if (!(fnExpr.getChild(1) instanceof StringLiteral)) {

Review comment:
       > The child(1) of explode_split must be a string literal.
   
   No, the child(1) maybe a SlotRef.




-- 
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


[GitHub] [incubator-doris] morningman commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r767163306



##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+
+Status TableFunctionNode::get_next(RuntimeState* state, RowBatch* row_batch, bool* eos) {
+    RETURN_IF_ERROR(exec_debug_action(TExecNodePhase::GETNEXT));
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+
+    const RowDescriptor& parent_rowdesc = row_batch->row_desc();
+    const RowDescriptor& child_rowdesc = _children[0]->row_desc();
+    if (_parent_tuple_desc_size == -1) {
+        _parent_tuple_desc_size = parent_rowdesc.tuple_descriptors().size();
+        _child_tuple_desc_size = child_rowdesc.tuple_descriptors().size();
+        for (int i = 0; i < _child_tuple_desc_size; ++i) {
+            _child_slot_sizes.push_back(child_rowdesc.tuple_descriptors()[i]->slots().size());
+        }    
+    }
+
+    uint8_t* tuple_buffer = nullptr;
+    Tuple* tuple_ptr = nullptr;
+    Tuple* pre_tuple_ptr = nullptr;
+    int row_idx = 0;
+
+    while (true) {
+        RETURN_IF_CANCELLED(state);
+        RETURN_IF_ERROR(state->check_query_state("TableFunctionNode, while getting next batch."));
+
+        if (_cur_child_batch == nullptr) {
+            _cur_child_batch.reset(new RowBatch(child_rowdesc, state->batch_size(), mem_tracker().get()));
+        }
+        if (_child_batch_exhausted) {
+            // current child batch is exhausted, get next batch from child
+            RETURN_IF_ERROR(_children[0]->get_next(state, _cur_child_batch.get(), eos));
+            if (*eos) {
+                break;
+            }
+            _cur_child_offset = 0;
+            RETURN_IF_ERROR(_process_next_child_row());
+            if (_child_batch_exhausted) {
+                _cur_child_batch->reset();
+                continue;
+            }
+        }
+
+        while (true) {
+            int idx = _find_last_fn_eos_idx();
+            if (idx == 0) {
+                // all table functions' results are exhausted, process next child row
+                RETURN_IF_ERROR(_process_next_child_row());
+                if (_child_batch_exhausted) {
+                    break;
+                }
+            } else if (idx < _fn_num && idx != -1) {
+                // some of table functions' results are exhausted
+                if (!_roll_table_functions(idx)) {
+                    // continue to process next child row
+                    continue; 
+                }
+            }
+
+            // get slots from every table function
+            // Notice that _fn_values[i] may be null if the table function has empty result set.
+            for (int i = 0; i < _fn_num; i++) {
+                RETURN_IF_ERROR(_fns[i]->get_value(&_fn_values[i]));
+            }
+
+            // allocate memory for row batch for the first time
+            if (tuple_buffer == nullptr) {
+                int64_t tuple_buffer_size;
+                RETURN_IF_ERROR(
+                        row_batch->resize_and_allocate_tuple_buffer(state, &tuple_buffer_size, &tuple_buffer));
+                tuple_ptr = reinterpret_cast<Tuple*>(tuple_buffer);
+            }
+            
+            pre_tuple_ptr = tuple_ptr;

Review comment:
       I tried. But it has to pass a lot param to that new method, which seems not necessary.




-- 
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


[GitHub] [incubator-doris] morningman commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r767154748



##########
File path: be/src/exprs/table_function/table_function_factory.h
##########
@@ -0,0 +1,36 @@
+// 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 "exprs/table_function/table_function_factory.h"
+#include "exprs/table_function/explode_split.h"
+
+#include "common/status.h"
+
+namespace doris {
+
+class ObjectPool;
+class TableFunction;
+class TableFunctionFactory {

Review comment:
       No, this method is to get a table function, not eval a table function.




-- 
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


[GitHub] [incubator-doris] morningman commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r769220300



##########
File path: fe/fe-core/src/test/java/org/apache/doris/planner/TableFunctionPlanTest.java
##########
@@ -19,20 +19,20 @@
 
 import org.apache.doris.analysis.CreateDbStmt;
 import org.apache.doris.analysis.CreateTableStmt;
+import org.apache.doris.analysis.FunctionCallExpr;
 import org.apache.doris.catalog.Catalog;
 import org.apache.doris.qe.ConnectContext;
 import org.apache.doris.utframe.UtFrameUtils;
 
 import org.apache.commons.io.FileUtils;
-
-import java.io.File;
-import java.util.UUID;
-
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import java.io.File;
+import java.util.UUID;
+
 public class TableFunctionPlanTest {

Review comment:
       OK




-- 
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


[GitHub] [incubator-doris] EmmyMiao87 commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
EmmyMiao87 commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r765505365



##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+
+Status TableFunctionNode::get_next(RuntimeState* state, RowBatch* row_batch, bool* eos) {
+    RETURN_IF_ERROR(exec_debug_action(TExecNodePhase::GETNEXT));
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+
+    const RowDescriptor& parent_rowdesc = row_batch->row_desc();
+    const RowDescriptor& child_rowdesc = _children[0]->row_desc();
+    if (_parent_tuple_desc_size == -1) {
+        _parent_tuple_desc_size = parent_rowdesc.tuple_descriptors().size();
+        _child_tuple_desc_size = child_rowdesc.tuple_descriptors().size();
+        for (int i = 0; i < _child_tuple_desc_size; ++i) {
+            _child_slot_sizes.push_back(child_rowdesc.tuple_descriptors()[i]->slots().size());
+        }    
+    }
+
+    uint8_t* tuple_buffer = nullptr;
+    Tuple* tuple_ptr = nullptr;
+    Tuple* pre_tuple_ptr = nullptr;
+    int row_idx = 0;
+
+    while (true) {
+        RETURN_IF_CANCELLED(state);
+        RETURN_IF_ERROR(state->check_query_state("TableFunctionNode, while getting next batch."));
+
+        if (_cur_child_batch == nullptr) {
+            _cur_child_batch.reset(new RowBatch(child_rowdesc, state->batch_size(), mem_tracker().get()));
+        }
+        if (_child_batch_exhausted) {
+            // current child batch is exhausted, get next batch from child
+            RETURN_IF_ERROR(_children[0]->get_next(state, _cur_child_batch.get(), eos));
+            if (*eos) {
+                break;
+            }
+            _cur_child_offset = 0;
+            RETURN_IF_ERROR(_process_next_child_row());
+            if (_child_batch_exhausted) {
+                _cur_child_batch->reset();
+                continue;
+            }
+        }
+
+        while (true) {
+            int idx = _find_last_fn_eos_idx();
+            if (idx == 0) {
+                // all table functions' results are exhausted, process next child row
+                RETURN_IF_ERROR(_process_next_child_row());
+                if (_child_batch_exhausted) {
+                    break;
+                }
+            } else if (idx < _fn_num && idx != -1) {
+                // some of table functions' results are exhausted
+                if (!_roll_table_functions(idx)) {
+                    // continue to process next child row
+                    continue; 
+                }
+            }
+
+            // get slots from every table function
+            // Notice that _fn_values[i] may be null if the table function has empty result set.
+            for (int i = 0; i < _fn_num; i++) {
+                RETURN_IF_ERROR(_fns[i]->get_value(&_fn_values[i]));
+            }
+
+            // allocate memory for row batch for the first time
+            if (tuple_buffer == nullptr) {
+                int64_t tuple_buffer_size;
+                RETURN_IF_ERROR(
+                        row_batch->resize_and_allocate_tuple_buffer(state, &tuple_buffer_size, &tuple_buffer));
+                tuple_ptr = reinterpret_cast<Tuple*>(tuple_buffer);
+            }
+            
+            pre_tuple_ptr = tuple_ptr;
+            // The tuples order in parent row batch should be
+            //      child1, child2, tf1, tf2, ...
+            TupleRow* parent_tuple_row = row_batch->get_row(row_idx++);
+            // 1. copy child tuples
+            int tuple_idx = 0;
+            for (int i = 0; i < _child_tuple_desc_size; tuple_idx++, i++) {
+                TupleDescriptor* child_tuple_desc = child_rowdesc.tuple_descriptors()[tuple_idx];
+                TupleDescriptor* parent_tuple_desc = parent_rowdesc.tuple_descriptors()[tuple_idx];
+
+                for (int j = 0; j < _child_slot_sizes[i]; ++j) {
+                    SlotDescriptor* child_slot_desc = child_tuple_desc->slots()[j];
+                    SlotDescriptor* parent_slot_desc = parent_tuple_desc->slots()[j];
+
+                    if (_output_slot_ids[parent_slot_desc->id()]) {
+                        Tuple* child_tuple = _cur_child_tuple_row->get_tuple(child_rowdesc.get_tuple_idx(child_tuple_desc->id()));
+                        void* dest_slot = tuple_ptr->get_slot(parent_slot_desc->tuple_offset());
+                        RawValue::write(child_tuple->get_slot(child_slot_desc->tuple_offset()), dest_slot, parent_slot_desc->type(), row_batch->tuple_data_pool());
+                        tuple_ptr->set_not_null(parent_slot_desc->null_indicator_offset());
+                    } else {
+                        tuple_ptr->set_null(parent_slot_desc->null_indicator_offset());
+                    }
+                }
+                parent_tuple_row->set_tuple(tuple_idx, tuple_ptr);
+                tuple_ptr = reinterpret_cast<Tuple*>(reinterpret_cast<uint8_t*>(tuple_ptr) + parent_tuple_desc->byte_size());
+            }
+
+            // 2. copy funtion result
+            for (int i = 0; tuple_idx < _parent_tuple_desc_size; tuple_idx++, i++) {
+                TupleDescriptor* parent_tuple_desc = parent_rowdesc.tuple_descriptors()[tuple_idx];
+                SlotDescriptor* parent_slot_desc = parent_tuple_desc->slots()[0];
+                void* dest_slot = tuple_ptr->get_slot(parent_slot_desc->tuple_offset());
+                // if (_fn_values[i] != nullptr && _output_slot_ids[parent_slot_desc->id()]) {
+                if (_fn_values[i] != nullptr) {
+                    RawValue::write(_fn_values[i], dest_slot, parent_slot_desc->type(), row_batch->tuple_data_pool());
+                    tuple_ptr->set_not_null(parent_slot_desc->null_indicator_offset());
+                } else {
+                    tuple_ptr->set_null(parent_slot_desc->null_indicator_offset());
+                }
+                parent_tuple_row->set_tuple(tuple_idx, tuple_ptr);
+
+                tuple_ptr = reinterpret_cast<Tuple*>(reinterpret_cast<uint8_t*>(tuple_ptr) + parent_tuple_desc->byte_size());
+            }
+
+            // 3. eval conjuncts
+            if (eval_conjuncts(&_conjunct_ctxs[0], _conjunct_ctxs.size(), parent_tuple_row)) {
+                row_batch->commit_last_row();
+                ++_num_rows_returned;
+            } else {
+                tuple_ptr = pre_tuple_ptr;
+            }
+
+            // Forward after write success.
+            // Because data in `_fn_values` points to the data saved in functions.
+            // And `forward` will change the data in functions.
+            bool tmp;
+            _fns[_fn_num - 1]->forward(&tmp);
+
+            if (row_batch->at_capacity()) {
+                *eos = false;
+                break;
+            }
+        }
+
+        if (row_batch->at_capacity()) {
+            break;
+        } 
+
+        if (_child_batch_exhausted) {

Review comment:
       What is the meaning of continue here?

##########
File path: be/src/exprs/table_function/table_function_factory.h
##########
@@ -0,0 +1,36 @@
+// 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 "exprs/table_function/table_function_factory.h"
+#include "exprs/table_function/explode_split.h"
+
+#include "common/status.h"
+
+namespace doris {
+
+class ObjectPool;
+class TableFunction;
+class TableFunctionFactory {

Review comment:
       Maybe TableFunctionEvaluator?

##########
File path: fe/fe-core/src/main/java/org/apache/doris/qe/Coordinator.java
##########
@@ -573,6 +573,7 @@ private void sendFragment() throws TException, RpcException, UserException {
                         LOG.warn("catch a execute exception", e);
                         exception = e;
                         code = TStatusCode.THRIFT_RPC_ERROR;
+                        BackendServiceProxy.getInstance().removeProxy(pair.first.brpcAddress);

Review comment:
       ??

##########
File path: fe/fe-core/src/main/java/org/apache/doris/analysis/LateralViewRef.java
##########
@@ -17,10 +17,10 @@
 
 package org.apache.doris.analysis;
 
+import com.google.common.base.Preconditions;

Review comment:
       pay attention to the order of import package

##########
File path: fe/fe-core/src/main/java/org/apache/doris/analysis/LateralViewRef.java
##########
@@ -77,32 +77,31 @@ public void analyze(Analyzer analyzer) throws UserException {
         if (!(expr instanceof FunctionCallExpr)) {
             throw new AnalysisException("Only support function call expr in lateral view");
         }
+
+        analyzeFunctionExpr(analyzer);
+
+        // analyze lateral view
+        desc = analyzer.registerTableRef(this);
+        explodeSlotRef = new SlotRef(new TableName(null, viewName), columnName);
+        explodeSlotRef.analyze(analyzer);
+        isAnalyzed = true;  // true now that we have assigned desc
+    }
+
+    private void analyzeFunctionExpr(Analyzer analyzer) throws AnalysisException {
         fnExpr = (FunctionCallExpr) expr;
         fnExpr.setTableFnCall(true);
         checkAndSupplyDefaultTableName(fnExpr);
         fnExpr.analyze(analyzer);
-        if (!fnExpr.getFnName().getFunction().equals(FunctionSet.EXPLODE_SPLIT)) {
-            throw new AnalysisException("Only support explode function in lateral view");
-        }
         checkScalarFunction(fnExpr.getChild(0));
-        if (!(fnExpr.getChild(1) instanceof StringLiteral)) {

Review comment:
       The child(1) of explode_split must be  a  string literal.

##########
File path: fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
##########
@@ -84,7 +85,10 @@
     private Expr originStmtFnExpr;
 
     private boolean isRewrote = false;
-    
+
+    public static final String UNKNOWN_TABLE_FUNCTION_MSG = "Currently only support `explode_split`, `explode_bitmap()` " +

Review comment:
       ```suggestion
       public static final String UNKNOWN_TABLE_FUNCTION_MSG = "Currently only support `explode_split`, `explode_bitmap` " +
   ```

##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+
+Status TableFunctionNode::get_next(RuntimeState* state, RowBatch* row_batch, bool* eos) {
+    RETURN_IF_ERROR(exec_debug_action(TExecNodePhase::GETNEXT));
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+
+    const RowDescriptor& parent_rowdesc = row_batch->row_desc();
+    const RowDescriptor& child_rowdesc = _children[0]->row_desc();
+    if (_parent_tuple_desc_size == -1) {
+        _parent_tuple_desc_size = parent_rowdesc.tuple_descriptors().size();
+        _child_tuple_desc_size = child_rowdesc.tuple_descriptors().size();
+        for (int i = 0; i < _child_tuple_desc_size; ++i) {
+            _child_slot_sizes.push_back(child_rowdesc.tuple_descriptors()[i]->slots().size());
+        }    
+    }
+
+    uint8_t* tuple_buffer = nullptr;
+    Tuple* tuple_ptr = nullptr;
+    Tuple* pre_tuple_ptr = nullptr;
+    int row_idx = 0;
+
+    while (true) {
+        RETURN_IF_CANCELLED(state);
+        RETURN_IF_ERROR(state->check_query_state("TableFunctionNode, while getting next batch."));
+
+        if (_cur_child_batch == nullptr) {
+            _cur_child_batch.reset(new RowBatch(child_rowdesc, state->batch_size(), mem_tracker().get()));
+        }
+        if (_child_batch_exhausted) {
+            // current child batch is exhausted, get next batch from child
+            RETURN_IF_ERROR(_children[0]->get_next(state, _cur_child_batch.get(), eos));
+            if (*eos) {
+                break;
+            }
+            _cur_child_offset = 0;
+            RETURN_IF_ERROR(_process_next_child_row());

Review comment:
       Can these lines be omitted?

##########
File path: fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
##########
@@ -688,36 +693,36 @@ public void analyzeImpl(Analyzer analyzer) throws AnalysisException {
                 fn = getTableFunction(fnName.getFunction(), childTypes,
                         Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
                 if (fn == null) {
-                    throw new AnalysisException("Doris only support `explode_split(varchar, varchar)` table function");
+                    throw new AnalysisException(UNKNOWN_TABLE_FUNCTION_MSG);
                 }
-                return;
-            }
-            // now first find function in built-in functions
-            if (Strings.isNullOrEmpty(fnName.getDb())) {
-                Type[] childTypes = collectChildReturnTypes();
-                fn = getBuiltinFunction(analyzer, fnName.getFunction(), childTypes,
-                        Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
-            }
-
-            // find user defined functions
-            if (fn == null) {
-                if (!analyzer.isUDFAllowed()) {
-                    throw new AnalysisException(
-                            "Does not support non-builtin functions, or function does not exist: " + this.toSqlImpl());
+            } else {

Review comment:
       No need to change here

##########
File path: fe/fe-core/src/test/java/org/apache/doris/planner/TableFunctionPlanTest.java
##########
@@ -19,20 +19,20 @@
 
 import org.apache.doris.analysis.CreateDbStmt;
 import org.apache.doris.analysis.CreateTableStmt;
+import org.apache.doris.analysis.FunctionCallExpr;
 import org.apache.doris.catalog.Catalog;
 import org.apache.doris.qe.ConnectContext;
 import org.apache.doris.utframe.UtFrameUtils;
 
 import org.apache.commons.io.FileUtils;
-
-import java.io.File;
-import java.util.UUID;
-
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import java.io.File;
+import java.util.UUID;
+
 public class TableFunctionPlanTest {

Review comment:
       Please add some UT about explode_bitmap, explode_json_array_xxx




-- 
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


[GitHub] [incubator-doris] morningman commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r767154708



##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+
+Status TableFunctionNode::get_next(RuntimeState* state, RowBatch* row_batch, bool* eos) {
+    RETURN_IF_ERROR(exec_debug_action(TExecNodePhase::GETNEXT));
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+
+    const RowDescriptor& parent_rowdesc = row_batch->row_desc();
+    const RowDescriptor& child_rowdesc = _children[0]->row_desc();
+    if (_parent_tuple_desc_size == -1) {
+        _parent_tuple_desc_size = parent_rowdesc.tuple_descriptors().size();
+        _child_tuple_desc_size = child_rowdesc.tuple_descriptors().size();
+        for (int i = 0; i < _child_tuple_desc_size; ++i) {
+            _child_slot_sizes.push_back(child_rowdesc.tuple_descriptors()[i]->slots().size());
+        }    
+    }
+
+    uint8_t* tuple_buffer = nullptr;
+    Tuple* tuple_ptr = nullptr;
+    Tuple* pre_tuple_ptr = nullptr;
+    int row_idx = 0;
+
+    while (true) {
+        RETURN_IF_CANCELLED(state);
+        RETURN_IF_ERROR(state->check_query_state("TableFunctionNode, while getting next batch."));
+
+        if (_cur_child_batch == nullptr) {
+            _cur_child_batch.reset(new RowBatch(child_rowdesc, state->batch_size(), mem_tracker().get()));
+        }
+        if (_child_batch_exhausted) {
+            // current child batch is exhausted, get next batch from child
+            RETURN_IF_ERROR(_children[0]->get_next(state, _cur_child_batch.get(), eos));
+            if (*eos) {
+                break;
+            }
+            _cur_child_offset = 0;
+            RETURN_IF_ERROR(_process_next_child_row());
+            if (_child_batch_exhausted) {
+                _cur_child_batch->reset();
+                continue;
+            }
+        }
+
+        while (true) {
+            int idx = _find_last_fn_eos_idx();
+            if (idx == 0) {
+                // all table functions' results are exhausted, process next child row
+                RETURN_IF_ERROR(_process_next_child_row());
+                if (_child_batch_exhausted) {
+                    break;
+                }
+            } else if (idx < _fn_num && idx != -1) {
+                // some of table functions' results are exhausted
+                if (!_roll_table_functions(idx)) {
+                    // continue to process next child row
+                    continue; 
+                }
+            }
+
+            // get slots from every table function
+            // Notice that _fn_values[i] may be null if the table function has empty result set.
+            for (int i = 0; i < _fn_num; i++) {
+                RETURN_IF_ERROR(_fns[i]->get_value(&_fn_values[i]));
+            }
+
+            // allocate memory for row batch for the first time
+            if (tuple_buffer == nullptr) {
+                int64_t tuple_buffer_size;
+                RETURN_IF_ERROR(
+                        row_batch->resize_and_allocate_tuple_buffer(state, &tuple_buffer_size, &tuple_buffer));
+                tuple_ptr = reinterpret_cast<Tuple*>(tuple_buffer);
+            }
+            
+            pre_tuple_ptr = tuple_ptr;
+            // The tuples order in parent row batch should be
+            //      child1, child2, tf1, tf2, ...
+            TupleRow* parent_tuple_row = row_batch->get_row(row_idx++);
+            // 1. copy child tuples
+            int tuple_idx = 0;
+            for (int i = 0; i < _child_tuple_desc_size; tuple_idx++, i++) {
+                TupleDescriptor* child_tuple_desc = child_rowdesc.tuple_descriptors()[tuple_idx];
+                TupleDescriptor* parent_tuple_desc = parent_rowdesc.tuple_descriptors()[tuple_idx];
+
+                for (int j = 0; j < _child_slot_sizes[i]; ++j) {
+                    SlotDescriptor* child_slot_desc = child_tuple_desc->slots()[j];
+                    SlotDescriptor* parent_slot_desc = parent_tuple_desc->slots()[j];
+
+                    if (_output_slot_ids[parent_slot_desc->id()]) {
+                        Tuple* child_tuple = _cur_child_tuple_row->get_tuple(child_rowdesc.get_tuple_idx(child_tuple_desc->id()));
+                        void* dest_slot = tuple_ptr->get_slot(parent_slot_desc->tuple_offset());
+                        RawValue::write(child_tuple->get_slot(child_slot_desc->tuple_offset()), dest_slot, parent_slot_desc->type(), row_batch->tuple_data_pool());
+                        tuple_ptr->set_not_null(parent_slot_desc->null_indicator_offset());
+                    } else {
+                        tuple_ptr->set_null(parent_slot_desc->null_indicator_offset());
+                    }
+                }
+                parent_tuple_row->set_tuple(tuple_idx, tuple_ptr);
+                tuple_ptr = reinterpret_cast<Tuple*>(reinterpret_cast<uint8_t*>(tuple_ptr) + parent_tuple_desc->byte_size());
+            }
+
+            // 2. copy funtion result
+            for (int i = 0; tuple_idx < _parent_tuple_desc_size; tuple_idx++, i++) {
+                TupleDescriptor* parent_tuple_desc = parent_rowdesc.tuple_descriptors()[tuple_idx];
+                SlotDescriptor* parent_slot_desc = parent_tuple_desc->slots()[0];
+                void* dest_slot = tuple_ptr->get_slot(parent_slot_desc->tuple_offset());
+                // if (_fn_values[i] != nullptr && _output_slot_ids[parent_slot_desc->id()]) {
+                if (_fn_values[i] != nullptr) {
+                    RawValue::write(_fn_values[i], dest_slot, parent_slot_desc->type(), row_batch->tuple_data_pool());
+                    tuple_ptr->set_not_null(parent_slot_desc->null_indicator_offset());
+                } else {
+                    tuple_ptr->set_null(parent_slot_desc->null_indicator_offset());
+                }
+                parent_tuple_row->set_tuple(tuple_idx, tuple_ptr);
+
+                tuple_ptr = reinterpret_cast<Tuple*>(reinterpret_cast<uint8_t*>(tuple_ptr) + parent_tuple_desc->byte_size());
+            }
+
+            // 3. eval conjuncts
+            if (eval_conjuncts(&_conjunct_ctxs[0], _conjunct_ctxs.size(), parent_tuple_row)) {
+                row_batch->commit_last_row();
+                ++_num_rows_returned;
+            } else {
+                tuple_ptr = pre_tuple_ptr;
+            }
+
+            // Forward after write success.
+            // Because data in `_fn_values` points to the data saved in functions.
+            // And `forward` will change the data in functions.
+            bool tmp;
+            _fns[_fn_num - 1]->forward(&tmp);
+
+            if (row_batch->at_capacity()) {
+                *eos = false;
+                break;
+            }
+        }
+
+        if (row_batch->at_capacity()) {
+            break;
+        } 
+
+        if (_child_batch_exhausted) {

Review comment:
       removed




-- 
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


[GitHub] [incubator-doris] morningman commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r767160915



##########
File path: be/src/exec/table_function_node.cpp
##########
@@ -0,0 +1,341 @@
+// 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 "exec/table_function_node.h"
+
+#include "exprs/expr.h"
+#include "exprs/expr_context.h"
+#include "runtime/descriptors.h"
+#include "runtime/raw_value.h"
+#include "runtime/row_batch.h"
+#include "runtime/runtime_state.h"
+#include "runtime/tuple_row.h"
+#include "exprs/table_function/table_function_factory.h"
+
+namespace doris {
+
+TableFunctionNode::TableFunctionNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs)
+    : ExecNode(pool, tnode, descs) {
+
+}
+
+TableFunctionNode::~TableFunctionNode() {
+
+}
+
+Status TableFunctionNode::init(const TPlanNode& tnode, RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::init(tnode, state));
+
+    for (const TExpr& texpr : tnode.table_function_node.fnCallExprList) {
+        ExprContext* ctx = nullptr;
+        RETURN_IF_ERROR(Expr::create_expr_tree(_pool, texpr, &ctx));
+        _fn_ctxs.push_back(ctx);
+
+        Expr* root = ctx->root();
+        const std::string& tf_name = root->fn().name.function_name; 
+        TableFunction* fn;
+        RETURN_IF_ERROR(TableFunctionFactory::get_fn(tf_name, _pool, &fn));
+        fn->set_expr_context(ctx);
+        _fns.push_back(fn);
+    }
+    _fn_num = _fns.size();
+    _fn_values.resize(_fn_num);
+
+    // Prepare output slot ids
+    RETURN_IF_ERROR(_prepare_output_slot_ids(tnode));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_prepare_output_slot_ids(const TPlanNode& tnode) {
+    // Prepare output slot ids
+    if (tnode.table_function_node.outputSlotIds.empty()) {
+        return Status::InternalError("Output slots of table function node is empty");
+    }
+    SlotId max_id = -1;
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        if (slot_id > max_id) {
+            max_id = slot_id;
+        }
+    }
+    _output_slot_ids = std::vector<bool>(max_id + 1, false);
+    for (auto slot_id : tnode.table_function_node.outputSlotIds) {
+        _output_slot_ids[slot_id] = true;
+    }
+
+    return Status::OK();
+}
+
+Status TableFunctionNode::prepare(RuntimeState* state) {
+    RETURN_IF_ERROR(ExecNode::prepare(state));
+    
+    RETURN_IF_ERROR(Expr::prepare(_fn_ctxs, state, _row_descriptor, expr_mem_tracker()));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->prepare());
+    }
+    return Status::OK();
+}
+
+Status TableFunctionNode::open(RuntimeState* state) {
+    SCOPED_TIMER(_runtime_profile->total_time_counter());
+    RETURN_IF_CANCELLED(state);
+    RETURN_IF_ERROR(ExecNode::open(state));
+
+    RETURN_IF_ERROR(Expr::open(_fn_ctxs, state));
+    for (auto fn : _fns) {
+        RETURN_IF_ERROR(fn->open());
+    }
+
+    RETURN_IF_ERROR(_children[0]->open(state));
+    return Status::OK();
+}
+
+Status TableFunctionNode::_process_next_child_row() {
+    if (_cur_child_offset == _cur_child_batch->num_rows()) {
+        _child_batch_exhausted = true;
+        return Status::OK();
+    }
+    _cur_child_tuple_row = _cur_child_batch->get_row(_cur_child_offset++);
+    for (TableFunction* fn : _fns) {
+        RETURN_IF_ERROR(fn->process(_cur_child_tuple_row));
+    }
+
+    _child_batch_exhausted = false;
+    return Status::OK();
+}
+
+// Returns the index of fn of the last eos counted from back to front
+// eg: there are 3 functions in `_fns`
+//      eos:    false, true, true
+//      return: 1
+//
+//      eos:    false, false, true
+//      return: 2
+//
+//      eos:    false, false, false
+//      return: -1
+//
+//      eos:    true, true, true
+//      return: 0
+//
+// return:
+//  0: all fns are eos
+// -1: all fns are not eos
+// >0: some of fns are eos
+int TableFunctionNode::_find_last_fn_eos_idx() {
+    for (int i = _fn_num - 1; i >=0; --i) {
+        if (!_fns[i]->eos()) {
+            if (i == _fn_num - 1) {
+                return -1;
+            } else {
+                return i + 1;
+            }
+        }
+    }
+    // all eos
+    return 0;
+}
+
+// Roll to reset the table function.
+// Eg:
+//  There are 3 functions f1, f2 and f3 in `_fns`.
+//  If `last_eos_idx` is 1, which means f2 and f3 are eos.
+//  So we need to forward f1, and reset f2 and f3.
+bool TableFunctionNode::_roll_table_functions(int last_eos_idx) {
+    bool fn_eos = false;
+    int i = last_eos_idx - 1;
+    for (; i >= 0; --i) {
+        _fns[i]->forward(&fn_eos);
+        if (!fn_eos) {
+            break;
+        }
+    }
+    if (i == -1) {
+        // after forward, all functions are eos.
+        // we should process next child row to get more table function results.
+        return false;
+    }
+
+    for (int j = i + 1; j < _fn_num; ++j) {
+        _fns[j]->reset();
+    }
+
+    return true;
+}
+

Review comment:
       OK




-- 
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


[GitHub] [incubator-doris] morningman commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r767154971



##########
File path: fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
##########
@@ -688,36 +693,36 @@ public void analyzeImpl(Analyzer analyzer) throws AnalysisException {
                 fn = getTableFunction(fnName.getFunction(), childTypes,
                         Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
                 if (fn == null) {
-                    throw new AnalysisException("Doris only support `explode_split(varchar, varchar)` table function");
+                    throw new AnalysisException(UNKNOWN_TABLE_FUNCTION_MSG);
                 }
-                return;
-            }
-            // now first find function in built-in functions
-            if (Strings.isNullOrEmpty(fnName.getDb())) {
-                Type[] childTypes = collectChildReturnTypes();
-                fn = getBuiltinFunction(analyzer, fnName.getFunction(), childTypes,
-                        Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF);
-            }
-
-            // find user defined functions
-            if (fn == null) {
-                if (!analyzer.isUDFAllowed()) {
-                    throw new AnalysisException(
-                            "Does not support non-builtin functions, or function does not exist: " + this.toSqlImpl());
+            } else {

Review comment:
       We need to let the logic continue, so that we can cast the params of the function.




-- 
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


[GitHub] [incubator-doris] morningman commented on a change in pull request #7255: [feat](lateral-view) Support execution of lateral view stmt

Posted by GitBox <gi...@apache.org>.
morningman commented on a change in pull request #7255:
URL: https://github.com/apache/incubator-doris/pull/7255#discussion_r767164027



##########
File path: fe/fe-core/src/main/java/org/apache/doris/analysis/LateralViewRef.java
##########
@@ -77,32 +77,31 @@ public void analyze(Analyzer analyzer) throws UserException {
         if (!(expr instanceof FunctionCallExpr)) {
             throw new AnalysisException("Only support function call expr in lateral view");
         }
+
+        analyzeFunctionExpr(analyzer);
+
+        // analyze lateral view
+        desc = analyzer.registerTableRef(this);
+        explodeSlotRef = new SlotRef(new TableName(null, viewName), columnName);
+        explodeSlotRef.analyze(analyzer);
+        isAnalyzed = true;  // true now that we have assigned desc
+    }
+
+    private void analyzeFunctionExpr(Analyzer analyzer) throws AnalysisException {
         fnExpr = (FunctionCallExpr) expr;
         fnExpr.setTableFnCall(true);
         checkAndSupplyDefaultTableName(fnExpr);
         fnExpr.analyze(analyzer);
-        if (!fnExpr.getFnName().getFunction().equals(FunctionSet.EXPLODE_SPLIT)) {
-            throw new AnalysisException("Only support explode function in lateral view");
-        }
         checkScalarFunction(fnExpr.getChild(0));
-        if (!(fnExpr.getChild(1) instanceof StringLiteral)) {

Review comment:
       > The child(1) of explode_split must be a string literal.
   
   No, the child(1) maybe a SlotRef.




-- 
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