1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
<!---
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.
-->
# MATLAB Interface to Apache Arrow
## Status
> **Warning** The MATLAB interface is under active development and should be considered experimental.
This is a very early stage MATLAB interface to the Apache Arrow C++ libraries.
Currently, the MATLAB interface supports:
1. Converting between a subset of Arrow `Array` types and MATLAB array types (see table below)
2. Converting between MATLAB `table`s and `arrow.tabular.RecordBatch`s
3. Creating Arrow `Field`s, `Schema`s, and `Type`s
4. Reading and writing Feather V1 files
Supported `arrow.array.Array` types are included in the table below.
**NOTE**: All Arrow `Array` classes listed below are part of the `arrow.array` package (e.g. `arrow.array.Float64Array`).
| MATLAB Array Type | Arrow Array Type |
| ----------------- | ---------------- |
| `uint8` | `UInt8Array` |
| `uint16` | `UInt16Array` |
| `uint32` | `UInt32Array` |
| `uint64` | `UInt64Array` |
| `int8` | `Int8Array` |
| `int16` | `Int16Array` |
| `int32` | `Int32Array` |
| `int64` | `Int64Array` |
| `single` | `Float32Array` |
| `double` | `Float64Array` |
| `logical` | `BooleanArray` |
| `string` | `StringArray` |
| `datetime` | `TimestampArray` |
| `datetime` | `Date32Array` |
| `datetime` | `Date64Array` |
| `duration` | `Time32Array` |
| `duration` | `Time64Array` |
| `cell` | `ListArray` |
| `table` | `StructArray` |
## Prerequisites
To build the MATLAB Interface to Apache Arrow from source, the following software must be installed on the target machine:
1. [MATLAB](https://www.mathworks.com/products/get-matlab.html)
2. [CMake](https://cmake.org/cmake/help/latest/)
3. C++ compiler which supports C++20 (e.g. [`gcc`](https://gcc.gnu.org/) on Linux, [`Xcode`](https://developer.apple.com/xcode/) on macOS, or [`Visual Studio`](https://visualstudio.microsoft.com/) on Windows)
4. [Git](https://git-scm.com/)
## Setup
To set up a local working copy of the source code, start by cloning the [`apache/arrow`](https://github.com/apache/arrow) GitHub repository using [Git](https://git-scm.com/):
```console
$ git clone https://github.com/apache/arrow.git
```
After cloning, change the working directory to the `matlab` subdirectory:
```console
$ cd arrow/matlab
```
## Build
To build the MATLAB interface, use [CMake](https://cmake.org/cmake/help/latest/):
```console
$ cmake -S . -B build
$ cmake --build build --config Release
```
## Install
To install the MATLAB interface to the default software installation location for the target machine (e.g. `/usr/local` on Linux or `C:\Program Files` on Windows), pass the `--target install` flag to CMake.
```console
$ cmake --build build --config Release --target install
```
As part of the install step, the installation directory is added to the [MATLAB Search Path](https://mathworks.com/help/matlab/matlab_env/what-is-the-matlab-search-path.html).
**Note**: This step may fail if the current user is lacking necessary filesystem permissions. If the install step fails, the installation directory can be manually added to the MATLAB Search Path using the [`addpath`](https://www.mathworks.com/help/matlab/ref/addpath.html) command.
## Test
To run the MATLAB tests, start MATLAB in the `arrow/matlab` directory and call the [`runtests`](https://mathworks.com/help/matlab/ref/runtests.html) command on the `test` directory with `IncludeSubFolders=true`:
``` matlab
>> runtests("test", IncludeSubFolders=true);
```
Refer to [Testing Guidelines](doc/testing_guidelines_for_the_matlab_interface_to_apache_arrow.md) for more information.
## Usage
Included below are some example code snippets that illustrate how to use the MATLAB interface.
### Arrow `Array` classes (i.e. `arrow.array.<Array>`)
#### Create an Arrow `Float64Array` from a MATLAB `double` array
```matlab
>> matlabArray = double([1, 2, 3])
matlabArray =
1 2 3
>> arrowArray = arrow.array(matlabArray)
arrowArray =
Float64Array with 3 elements and 0 null values:
1 | 2 | 3
```
#### Create a MATLAB `logical` array from an Arrow `BooleanArray`
```matlab
>> arrowArray = arrow.array([true, false, true])
arrowArray =
BooleanArray with 3 elements and 0 null values:
true | false | true
>> matlabArray = toMATLAB(arrowArray)
matlabArray =
3×1 logical array
1
0
1
```
#### Specify `Null` Values when constructing an `arrow.array.Int8Array`
```matlab
>> matlabArray = int8([122, -1, 456, -10, 789])
matlabArray =
1×5 int8 row vector
122 -1 127 -10 127
% Treat all negative array elements as Null
>> validElements = matlabArray > 0
validElements =
1×5 logical array
1 0 1 0 1
% Specify which values are Null/Valid by supplying a logical validity "mask"
>> arrowArray = arrow.array(matlabArray, Valid=validElements)
arrowArray =
Int8Array with 5 elements and 2 null values:
122 | null | 127 | null | 127
```
### Arrow `RecordBatch` class
#### Create an Arrow `RecordBatch` from a MATLAB `table`
```matlab
>> matlabTable = table(["A"; "B"; "C"], [1; 2; 3], [true; false; true])
matlabTable =
3x3 table
Var1 Var2 Var3
____ ____ _____
"A" 1 true
"B" 2 false
"C" 3 true
>> arrowRecordBatch = arrow.recordBatch(matlabTable)
arrowRecordBatch =
Arrow RecordBatch with 3 rows and 3 columns:
Schema:
Var1: String | Var2: Float64 | Var3: Boolean
First Row:
"A" | 1 | true
```
#### Create a MATLAB `table` from an Arrow `RecordBatch`
```matlab
>> arrowRecordBatch
arrowRecordBatch =
Arrow RecordBatch with 3 rows and 3 columns:
Schema:
Var1: String | Var2: Float64 | Var3: Boolean
First Row:
"A" | 1 | true
>> matlabTable = table(arrowRecordBatch)
matlabTable =
3x3 table
Var1 Var2 Var3
____ ____ _____
"A" 1 true
"B" 2 false
"C" 3 true
```
#### Create an Arrow `RecordBatch` from multiple Arrow `Array`s
```matlab
>> stringArray = arrow.array(["A", "B", "C"])
stringArray =
StringArray with 3 elements and 0 null values:
"A" | "B" | "C"
>> timestampArray = arrow.array([datetime(1997, 01, 01), datetime(1998, 01, 01), datetime(1999, 01, 01)])
timestampArray =
TimestampArray with 3 elements and 0 null values:
1997-01-01 00:00:00.000000 | 1998-01-01 00:00:00.000000 | 1999-01-01 00:00:00.000000
>> booleanArray = arrow.array([true, false, true])
booleanArray =
BooleanArray with 3 elements and 0 null values:
true | false | true
>> arrowRecordBatch = arrow.tabular.RecordBatch.fromArrays(stringArray, timestampArray, booleanArray)
arrowRecordBatch =
Arrow RecordBatch with 3 rows and 3 columns:
Schema:
Column1: String | Column2: Timestamp | Column3: Boolean
First Row:
"A" | 1997-01-01 00:00:00.000000 | true
```
#### Extract a column from a `RecordBatch` by index
```matlab
>> arrowRecordBatch = arrow.tabular.RecordBatch.fromArrays(stringArray, timestampArray, booleanArray)
arrowRecordBatch =
Arrow RecordBatch with 3 rows and 3 columns:
Schema:
Column1: String | Column2: Timestamp | Column3: Boolean
First Row:
"A" | 1997-01-01 00:00:00.000000 | true
>> timestampArray = arrowRecordBatch.column(2)
timestampArray =
TimestampArray with 3 elements and 0 null values:
1997-01-01 00:00:00.000000 | 1998-01-01 00:00:00.000000 | 1999-01-01 00:00:00.000000
```
### Arrow `Type` classes (i.e. `arrow.type.<Type>`)
#### Create an Arrow `Int8Type` object
```matlab
>> type = arrow.int8()
type =
Int8Type with properties:
ID: Int8
```
#### Create an Arrow `TimestampType` object with a specific `TimeUnit` and `TimeZone`
```matlab
>> type = arrow.timestamp(TimeUnit="Second", TimeZone="Asia/Kolkata")
type =
TimestampType with properties:
ID: Timestamp
TimeUnit: Second
TimeZone: "Asia/Kolkata"
```
#### Get the type enumeration `ID` for an Arrow `Type` object
```matlab
>> type.ID
ans =
ID enumeration
Timestamp
>> type = arrow.string()
type =
StringType with properties:
ID: String
>> type.ID
ans =
ID enumeration
String
```
### Arrow `Field` class
#### Create an Arrow `Field` with type `Int8Type`
```matlab
>> field = arrow.field("Number", arrow.int8())
field =
Field with properties:
Name: "Number"
Type: [1x1 arrow.type.Int8Type]
>> field.Name
ans =
"Number"
>> field.Type
ans =
Int8Type with properties:
ID: Int8
```
#### Create an Arrow `Field` with type `StringType`
```matlab
>> field = arrow.field("Letter", arrow.string())
field =
Field with properties:
Name: "Letter"
Type: [1x1 arrow.type.StringType]
>> field.Name
ans =
"Letter"
>> field.Type
ans =
StringType with properties:
ID: String
```
#### Extract an Arrow `Field` from an Arrow `Schema` by index
```matlab
>> arrowSchema
arrowSchema =
Arrow Schema with 2 fields:
Letter: String | Number: Int8
% Specify the field to extract by its index (i.e. 2)
>> field = arrowSchema.field(2)
field =
Field with properties:
Name: "Number"
Type: [1x1 arrow.type.Int8Type]
```
#### Extract an Arrow `Field` from an Arrow `Schema` by name
```matlab
>> arrowSchema
arrowSchema =
Arrow Schema with 2 fields:
Letter: String | Number: Int8
% Specify the field to extract by its name (i.e. "Letter")
>> field = arrowSchema.field("Letter")
field =
Field with properties:
Name: "Letter"
Type: [1x1 arrow.type.StringType]
```
### Arrow `Schema` class
#### Create an Arrow `Schema` from multiple Arrow `Field`s
```matlab
>> letter = arrow.field("Letter", arrow.string())
letter =
Field with properties:
Name: "Letter"
Type: [1x1 arrow.type.StringType]
>> number = arrow.field("Number", arrow.int8())
number =
Field with properties:
Name: "Number"
Type: [1x1 arrow.type.Int8Type]
>> schema = arrow.schema([letter, number])
schema =
Arrow Schema with 2 fields:
Letter: String | Number: Int8
```
#### Get the `Schema` of an Arrow `RecordBatch`
```matlab
>> matlabTable = table(["A"; "B"; "C"], [1; 2; 3], VariableNames=["Letter", "Number"])
matlabTable =
3x2 table
Letter Number
______ ______
"A" 1
"B" 2
"C" 3
>> arrowRecordBatch = arrow.recordBatch(matlabTable)
arrowRecordBatch =
Arrow RecordBatch with 3 rows and 2 columns:
Schema:
Letter: String | Number: Float64
First Row:
"A" | 1
>> arrowSchema = arrowRecordBatch.Schema
arrowSchema =
Arrow Schema with 2 fields:
Letter: String | Number: Float64
```
### Feather V1
#### Write a MATLAB table to a Feather V1 file
``` matlab
>> t = table(["A"; "B"; "C"], [1; 2; 3], [true; false; true])
t =
3×3 table
Var1 Var2 Var3
____ ____ _____
"A" 1 true
"B" 2 false
"C" 3 true
>> filename = "table.feather";
>> featherwrite(filename, t)
```
#### Read a Feather V1 file into a MATLAB table
``` matlab
>> filename = "table.feather";
>> t = featherread(filename)
t =
3×3 table
Var1 Var2 Var3
____ ____ _____
"A" 1 true
"B" 2 false
"C" 3 true
```
|