Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ object Metadata {
x.##
case x: Metadata =>
hash(x.map)
case null =>
Copy link
Member

Choose a reason for hiding this comment

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

Basically, we do not support null in Metadata.

Copy link
Member

Choose a reason for hiding this comment

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

nvm, I found we added the support in #8969

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the putNull method exists, but is actually not useable because of this bug. Having null in Metadata would be really helpful for me, as I'd like to store default values for fields in the Metadata. And NULL is a very common case as a default value.

0
case other =>
throw new RuntimeException(s"Do not support type ${other.getClass}.")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.
*/

package org.apache.spark.sql.types

import org.apache.spark.SparkFunSuite

class MetadataSuite extends SparkFunSuite {
test("String Metadata") {
val meta = new MetadataBuilder().putString("key", "value").build()
assert(meta === meta)
assert(meta.## !== 0)
assert(meta.getString("key") === "value")
assert(meta.contains("key"))
intercept[NoSuchElementException](meta.getString("no_such_key"))
intercept[ClassCastException](meta.getBoolean("key"))
}

test("Long Metadata") {
val meta = new MetadataBuilder().putLong("key", 12).build()
assert(meta === meta)
assert(meta.## !== 0)
assert(meta.getLong("key") === 12)
assert(meta.contains("key"))
intercept[NoSuchElementException](meta.getLong("no_such_key"))
intercept[ClassCastException](meta.getBoolean("key"))
}

test("Double Metadata") {
val meta = new MetadataBuilder().putDouble("key", 12).build()
assert(meta === meta)
assert(meta.## !== 0)
assert(meta.getDouble("key") === 12)
assert(meta.contains("key"))
intercept[NoSuchElementException](meta.getDouble("no_such_key"))
intercept[ClassCastException](meta.getBoolean("key"))
}

test("Boolean Metadata") {
val meta = new MetadataBuilder().putBoolean("key", true).build()
assert(meta === meta)
assert(meta.## !== 0)
assert(meta.getBoolean("key") === true)
assert(meta.contains("key"))
intercept[NoSuchElementException](meta.getBoolean("no_such_key"))
intercept[ClassCastException](meta.getString("key"))
}

test("Null Metadata") {
val meta = new MetadataBuilder().putNull("key").build()
assert(meta === meta)
assert(meta.## !== 0)
assert(meta.getString("key") === null)
assert(meta.getDouble("key") === 0)
assert(meta.getLong("key") === 0)
assert(meta.getBoolean("key") === false)
assert(meta.contains("key"))
intercept[NoSuchElementException](meta.getLong("no_such_key"))
}
}