Repository: JuliaData/Tables.jl (julia)

Issue:
Copying in matrix(table(A)) when A is a matrix
I was expecting that if `A` is a matrix and I do `t = table(A)` then `matrix(t)` just unwraps `A`, ie no copying.  But evidently this is not the case:

```julia
julia> A = [1 2; 2 4]
2×2 Array{Int64,2}:
 1  2
 2  4

julia> a = Tables.matrix(Tables.table(A))
2×2 Array{Int64,2}:
 1  2
 2  4

julia> A[1, 1] = 10_000
10000

julia> a
2×2 Array{Int64,2}:
 1  2
 2  4
```

Is there a reason for wanting this behaviour?

Pull request description:
This PR resolves #182 by adding specific method for `matrix(table::MatrixTable)`. This avoids creating a copy of the wrapped matrix.

Patch:
diff --git a/src/matrix.jl b/src/matrix.jl
index b9b70a4..dcdf47a 100644
--- a/src/matrix.jl
+++ b/src/matrix.jl
@@ -59,10 +59,12 @@ end
 """
     Tables.matrix(table; transpose::Bool=false)
 
-Materialize any table source input as a `Matrix`. If the table column types are not homogenous,
+Materialize any table source input as a new `Matrix` or in the case of a `MatrixTable`
+return the originally wrapped matrix. If the table column types are not homogenous,
 they will be promoted to a common type in the materialized `Matrix`. Note that column names are
 ignored in the conversion. By default, input table columns will be materialized as corresponding
-matrix columns; passing `transpose=true` will transpose the input with input columns as matrix rows.
+matrix columns; passing `transpose=true` will transpose the input with input columns as matrix rows
+or in the case of a `MatrixTable` apply `permutedims` to the originally wrapped matrix.
 """
 function matrix(table; transpose::Bool=false)
     cols = columns(table)
@@ -82,3 +84,9 @@ function matrix(table; transpose::Bool=false)
     end
     return matrix
 end
+	
+function matrix(table::MatrixTable; transpose::Bool=false) 
+    matrix = getfield(table, :matrix)
+    transpose || return matrix
+    return permutedims(matrix)
+end


Test patch:
diff --git a/test/runtests.jl b/test/runtests.jl
index 57bc8f6..749caf6 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -241,9 +241,14 @@ end
     @test propertynames(mattbl) == propertynames(matrow) == [:Column1, :Column2, :Column3]
 
     # #155
-    T = Tables.table(hcat([1,2,3],[1,2,3]))
+    m = hcat([1,2,3],[1,2,3])
+    T = Tables.table(m)
     M = Tables.matrix(T)
+    Mt = Tables.matrix(T, transpose=true)
     @test M[:, 1] == [1, 2, 3]
+    # 182
+    @test M === m #checks that both are the same object in memory
+    @test Mt == permutedims(m) 
     # 167
     @test !Tables.istable(Matrix{Union{}}(undef, 2, 3))
 end