Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cpp/core/compute/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

namespace gluten {

struct JniInputIteratorContext;
class ResultIterator;

struct SparkTaskInfo {
Expand Down Expand Up @@ -190,6 +191,8 @@ class Runtime : public std::enable_shared_from_this<Runtime> {
return dumper_.get();
}

virtual std::unique_ptr<ColumnarBatchIterator> createJniInputIterator(const JniInputIteratorContext& context);

ObjectHandle saveObject(std::shared_ptr<void> obj) {
return objStore_->save(obj);
}
Expand Down
6 changes: 6 additions & 0 deletions cpp/core/jni/JniCommon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ gluten::Runtime* gluten::getRuntime(JNIEnv* env, jobject runtimeAware) {
return ctx;
}

std::unique_ptr<gluten::ColumnarBatchIterator> gluten::Runtime::createJniInputIterator(
const JniInputIteratorContext& context) {
return std::make_unique<JniColumnarBatchIterator>(
context.env, context.jColumnarBatchIterator, this, context.iteratorIndex);
}

std::unique_ptr<gluten::JniColumnarBatchIterator>
gluten::makeJniColumnarBatchIterator(JNIEnv* env, jobject jColumnarBatchItr, gluten::Runtime* runtime) {
return std::make_unique<JniColumnarBatchIterator>(env, jColumnarBatchItr, runtime);
Expand Down
10 changes: 10 additions & 0 deletions cpp/core/jni/JniCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ static T* jniCastOrThrow(jlong handle) {
}
namespace gluten {

class StreamReader;

std::shared_ptr<StreamReader> makeShuffleStreamReader(JNIEnv* env, jobject jShuffleStreamReader);

class JniCommonState {
public:
virtual ~JniCommonState() = default;
Expand Down Expand Up @@ -286,6 +290,12 @@ DEFINE_SAFE_GET_PRIMITIVE_ARRAY_FUNCTIONS(kLong, jlongArray, Long)
DEFINE_SAFE_GET_PRIMITIVE_ARRAY_FUNCTIONS(kFloat, jfloatArray, Float)
DEFINE_SAFE_GET_PRIMITIVE_ARRAY_FUNCTIONS(kDouble, jdoubleArray, Double)

struct JniInputIteratorContext {
JNIEnv* env;
jobject jColumnarBatchIterator;
int32_t iteratorIndex;
};

class JniColumnarBatchIterator : public ColumnarBatchIterator {
public:
explicit JniColumnarBatchIterator(
Expand Down
10 changes: 9 additions & 1 deletion cpp/core/jni/JniWrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ class ShuffleStreamReader : public StreamReader {

} // namespace

namespace gluten {

std::shared_ptr<StreamReader> makeShuffleStreamReader(JNIEnv* env, jobject jShuffleStreamReader) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to wrap it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to wrap it.

ShuffleStreamReader is a private implementation within JniWrapper.cc; we need a function to expose the mechanism for creating ShuffleStreamReader instances for different backends.

return std::make_shared<::ShuffleStreamReader>(env, jShuffleStreamReader);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove "::"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove "::"

::ClassName means: look for ClassName in the global namespace. It avoids name conflicts by explicitly referring to the class in the global namespace.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is merely a defensive strategy intended to prevent misuse.

}

} // namespace gluten

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -566,7 +574,7 @@ Java_org_apache_gluten_vectorized_PlanEvaluatorJniWrapper_nativeCreateKernelWith
inputIters.reserve(itersLen);
for (int idx = 0; idx < itersLen; idx++) {
jobject iter = env->GetObjectArrayElement(batchItrArray, idx);
auto arrayIter = std::make_unique<JniColumnarBatchIterator>(env, iter, ctx, idx);
auto arrayIter = ctx->createJniInputIterator({env, iter, idx});
auto resultIter = std::make_shared<ResultIterator>(std::move(arrayIter));
inputIters.push_back(std::move(resultIter));
}
Expand Down
1 change: 1 addition & 0 deletions cpp/core/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ add_test_case(round_robin_partitioner_test SOURCES RoundRobinPartitionerTest.cc)
add_test_case(object_store_test SOURCES ObjectStoreTest.cc)
add_test_case(memory_allocator_test SOURCES MemoryAllocatorTest.cc)
add_test_case(ffor_codec_test SOURCES FForCodecTest.cc)
add_test_case(jni_input_iterator_test SOURCES JniInputIteratorTest.cc)
65 changes: 65 additions & 0 deletions cpp/core/tests/JniInputIteratorTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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 "jni/JniCommon.h"

#include <gtest/gtest.h>

using namespace gluten;

namespace {

class TestColumnarBatchIterator final : public ColumnarBatchIterator {
public:
std::shared_ptr<ColumnarBatch> next() override {
return nullptr;
}
};

class TestRuntime final : public Runtime {
public:
TestRuntime() : Runtime("jni-input-iterator-test", nullptr, nullptr, {}) {}

std::unique_ptr<ColumnarBatchIterator> createJniInputIterator(const JniInputIteratorContext& context) override {
env = context.env;
iterator = context.jColumnarBatchIterator;
iteratorIndex = context.iteratorIndex;
return std::make_unique<TestColumnarBatchIterator>();
}

JNIEnv* env{nullptr};
jobject iterator{nullptr};
int32_t iteratorIndex{-1};
};

TEST(JniInputIterator, DispatchesThroughRuntime) {
static int envMarker;
static int iteratorMarker;
auto* env = reinterpret_cast<JNIEnv*>(&envMarker);
auto iterator = reinterpret_cast<jobject>(&iteratorMarker);

TestRuntime runtime;
Runtime* baseRuntime = &runtime;
auto result = baseRuntime->createJniInputIterator({env, iterator, 7});

ASSERT_NE(result, nullptr);
EXPECT_EQ(runtime.env, env);
Comment thread
wangxinshuo-bolt marked this conversation as resolved.
Outdated
EXPECT_EQ(runtime.iterator, iterator);
EXPECT_EQ(runtime.iteratorIndex, 7);
}

} // namespace
Loading