Skip to content

Commit

Permalink
c data prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
sgilmore10 committed May 9, 2024
1 parent da1ca86 commit cdaee14
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 4 deletions.
85 changes: 85 additions & 0 deletions matlab/src/cpp/arrow/matlab/c/proxy/array_importer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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.

// 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 "arrow/array.h"
#include "arrow/c/bridge.h"

#include "arrow/matlab/c/proxy/array_importer.h"
#include "arrow/matlab/array/proxy/wrap.h"
#include "arrow/matlab/error/error.h"

#include "libmexclass/proxy/ProxyManager.h"

namespace arrow::matlab::c::proxy {

ArrayImporter::ArrayImporter() {
// Register Proxy methods.
REGISTER_METHOD(ArrayImporter, importFromC);
}

libmexclass::proxy::MakeResult ArrayImporter::make(const libmexclass::proxy::FunctionArguments& constructor_arguments) {
return std::make_shared<ArrayImporter>();
}

void ArrayImporter::importFromC(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;
using namespace libmexclass::proxy;

mda::StructArray args = context.inputs[0];
const mda::TypedArray<uint64_t> array_address_mda = args[0]["ArrowArrayAddress"];
const mda::TypedArray<uint64_t> schema_address_mda = args[0]["ArrowSchemaAddress"];

const auto array_address = uint64_t(array_address_mda[0]);
const auto schema_address = uint64_t(schema_address_mda[0]);

struct ArrowArray* arrow_array = reinterpret_cast<struct ArrowArray*>(array_address);
struct ArrowSchema* arrow_schema = reinterpret_cast<struct ArrowSchema*>(schema_address);

MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(std::shared_ptr<arrow::Array> array,
arrow::ImportArray(arrow_array, arrow_schema),
context, "arrow:c:ImportFailed");

MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto array_proxy,
arrow::matlab::array::proxy::wrap(array), context,
error::UNKNOWN_PROXY_FOR_ARRAY_TYPE);

mda::ArrayFactory factory;
const auto array_proxy_id = ProxyManager::manageProxy(array_proxy);
const auto array_proxy_id_mda = factory.createScalar(array_proxy_id);
const auto array_type_id_mda = factory.createScalar(static_cast<int32_t>(array->type_id()));

context.outputs[0] = array_proxy_id_mda;
context.outputs[1] = array_type_id_mda;
}
} // namespace arrow::matlab::c::proxy
37 changes: 37 additions & 0 deletions matlab/src/cpp/arrow/matlab/c/proxy/array_importer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 "libmexclass/proxy/Proxy.h"

namespace arrow::matlab::c::proxy {

class ArrayImporter : public libmexclass::proxy::Proxy {
public:
ArrayImporter();

~ArrayImporter() = default;

static libmexclass::proxy::MakeResult make(
const libmexclass::proxy::FunctionArguments& constructor_arguments);

protected:
void importFromC(libmexclass::proxy::method::Context& context);
};

} // namespace arrow::matlab::c::proxy
4 changes: 4 additions & 0 deletions matlab/src/cpp/arrow/matlab/proxy/factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "arrow/matlab/array/proxy/time64_array.h"
#include "arrow/matlab/array/proxy/timestamp_array.h"
#include "arrow/matlab/buffer/proxy/buffer.h"
#include "arrow/matlab/c/proxy/array_importer.h"
#include "arrow/matlab/error/error.h"
#include "arrow/matlab/io/csv/proxy/table_reader.h"
#include "arrow/matlab/io/csv/proxy/table_writer.h"
Expand All @@ -44,6 +45,7 @@
#include "arrow/matlab/type/proxy/time64_type.h"
#include "arrow/matlab/type/proxy/timestamp_type.h"


#include "factory.h"

namespace arrow::matlab::proxy {
Expand Down Expand Up @@ -99,6 +101,8 @@ libmexclass::proxy::MakeResult Factory::make_proxy(
REGISTER_PROXY(arrow.io.feather.proxy.Reader , arrow::matlab::io::feather::proxy::Reader);
REGISTER_PROXY(arrow.io.csv.proxy.TableWriter , arrow::matlab::io::csv::proxy::TableWriter);
REGISTER_PROXY(arrow.io.csv.proxy.TableReader , arrow::matlab::io::csv::proxy::TableReader);
REGISTER_PROXY(arrow.c.proxy.ArrayImporter , arrow::matlab::c::proxy::ArrayImporter);

// clang-format on

return libmexclass::error::Error{error::UNKNOWN_PROXY_ERROR_ID,
Expand Down
4 changes: 2 additions & 2 deletions matlab/src/matlab/+arrow/+c/+internal/ArrayImporter.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

methods
function obj = ArrayImporter()
proxyName = "arrow.tabular.proxy.RecordBatch";
proxy = arrow.internal.proxy.create(proxyName);
proxyName = "arrow.c.proxy.ArrayImporter";
proxy = arrow.internal.proxy.create(proxyName, struct());
obj.Proxy = proxy;
end

Expand Down
4 changes: 3 additions & 1 deletion matlab/src/matlab/+arrow/+c/import.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
arrowSchemaAddress(1, 1) uint64
end


importer = arrow.c.internal.ArrayImporter();
array = importer.import(arrowArrayAddress, arrowSchemaAddress);

end

3 changes: 2 additions & 1 deletion matlab/tools/cmake/BuildMatlabArrowInterface.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ set(MATLAB_ARROW_LIBMEXCLASS_CLIENT_PROXY_SOURCES "${CMAKE_SOURCE_DIR}/src/cpp/a
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/csv/proxy/table_writer.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/csv/proxy/table_reader.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/index/validate.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/buffer/proxy/buffer.cc")
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/buffer/proxy/buffer.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/c/proxy/array_importer.cc")


set(MATLAB_ARROW_LIBMEXCLASS_CLIENT_PROXY_FACTORY_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/proxy")
Expand Down

0 comments on commit cdaee14

Please sign in to comment.