Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fields of struct array support to dataframe creation functions #334

Merged
merged 1 commit into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Add fields of struct array support to dataframe creation functions (#333
)
  • Loading branch information
gavinkflam committed Sep 5, 2021
commit 9f0132ace31a4c8bf1c18049e30a074c1f3d3141
7 changes: 4 additions & 3 deletions src/clojure/zero_one/geni/core/dataset_creation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@
"Creates an ArrayType by specifying the data type of elements `val-type` and
whether the array contains null values `nullable`."
[val-type nullable]
(DataTypes/createArrayType
(data-type->spark-type val-type)
nullable))
(let [spark-type (if (instance? DataType val-type)
val-type
(data-type->spark-type val-type))]
(DataTypes/createArrayType spark-type nullable)))

(defn map-type
"Creates a MapType by specifying the data type of keys `key-type`, the data type
Expand Down
49 changes: 35 additions & 14 deletions test/zero_one/geni/dataset_creation_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,41 @@
=> {:j "FloatType"}))

(fact "can instantiate dataframe with data-oriented schema" :schema
(g/dtypes
(g/create-dataframe
@tr/spark
[(g/row 32 "horse" (g/dense 1.0 2.0) (g/sparse 4 [1 3] [3.0 4.0]))
(g/row 64 "mouse" (g/dense 3.0 4.0) (g/sparse 4 [0 2] [1.0 2.0]))]
{:number :int
:word :str
:dense :vector
:sparse :vector}))
=> #(and (= (:number %) "IntegerType")
(= (:word %) "StringType")
(includes? (:dense %) "VectorUDT")
(includes? (:sparse %) "VectorUDT")
(= (set (keys %)) #{:dense :number :sparse :word})))
(fact "of simple data type fields"
(g/dtypes
(g/create-dataframe
@tr/spark
[(g/row 32 "horse")
(g/row 64 "mouse")]
{:number :int :word :str}))
=> {:number "IntegerType"
:word "StringType"})
(fact "of vector fields"
(g/dtypes
(g/create-dataframe
@tr/spark
[(g/row (g/dense 1.0 2.0) (g/sparse 4 [1 3] [3.0 4.0]))
(g/row (g/dense 3.0 4.0) (g/sparse 4 [0 2] [1.0 2.0]))]
{:dense :vector :sparse :vector}))
=> #(and (includes? (:dense %) "VectorUDT")
(includes? (:sparse %) "VectorUDT")
(= (set (keys %)) #{:dense :sparse})))
(fact "of struct fields"
(g/dtypes
(g/create-dataframe
@tr/spark
[(g/row (g/row 27 42))
(g/row (g/row 57 18))]
{:coord {:x :int :y :int}}))
=> {:coord "StructType(StructField(x,IntegerType,true), StructField(y,IntegerType,true))"})
(fact "of struct array fields"
(g/dtypes
(g/create-dataframe
@tr/spark
[(g/row [(g/row 27 42)])
(g/row [(g/row 57 18)])]
{:coords [{:x :int :y :int}]}))
=> {:coords "ArrayType(StructType(StructField(x,IntegerType,true), StructField(y,IntegerType,true)),true)"}))

(facts "On building blocks"
(fact "can instantiate vectors"
Expand Down