-
Notifications
You must be signed in to change notification settings - Fork 6
[SPARK-51708] Add CaseInsensitiveDictionary
#40
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// 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. | ||
// | ||
import Foundation | ||
|
||
/// A dictionary in which keys are case insensitive. The input dictionary can be | ||
/// accessed for cases where case-sensitive information is required. | ||
public struct CaseInsensitiveDictionary: Sendable { | ||
public var originalDictionary: [String: Sendable] | ||
private var keyLowerCasedDictionary: [String: Sendable] = [:] | ||
|
||
init(_ originalDictionary: [String: Sendable] = [:]) { | ||
self.originalDictionary = originalDictionary | ||
for (key, value) in originalDictionary { | ||
keyLowerCasedDictionary[key.lowercased()] = value | ||
} | ||
} | ||
|
||
subscript(key: String) -> Sendable? { | ||
get { | ||
return keyLowerCasedDictionary[key.lowercased()] | ||
} | ||
set { | ||
var newMap = originalDictionary.filter { $0.key.caseInsensitiveCompare(key) != .orderedSame } | ||
newMap[key] = newValue | ||
self.originalDictionary = newMap | ||
self.keyLowerCasedDictionary[key.lowercased()] = newValue | ||
} | ||
} | ||
|
||
public func toDictionary() -> [String: Sendable] { | ||
return originalDictionary | ||
} | ||
|
||
public func toStringDictionary() -> [String: String] { | ||
var dict = [String: String]() | ||
|
||
for (key, value) in originalDictionary { | ||
dict[key] = String(describing: value) | ||
} | ||
return dict | ||
} | ||
|
||
public var count: Int { | ||
return keyLowerCasedDictionary.count | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// 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. | ||
// | ||
|
||
import Foundation | ||
import Testing | ||
|
||
@testable import SparkConnect | ||
|
||
/// A test suite for `CaseInsensitiveDictionary` | ||
struct CaseInsensitiveDictionaryTests { | ||
@Test | ||
func empty() async throws { | ||
let dict = CaseInsensitiveDictionary([:]) | ||
#expect(dict.count == 0) | ||
} | ||
|
||
@Test | ||
func originalDictionary() async throws { | ||
let dict = CaseInsensitiveDictionary([ | ||
"key1": "value1", | ||
"KEY1": "VALUE1", | ||
]) | ||
#expect(dict.count == 1) | ||
#expect(dict.originalDictionary.count == 2) | ||
} | ||
|
||
@Test | ||
func toDictionary() async throws { | ||
let dict = CaseInsensitiveDictionary([ | ||
"key1": "value1", | ||
"KEY1": "VALUE1", | ||
]) | ||
#expect(dict.toDictionary().count == 2) | ||
} | ||
|
||
@Test | ||
func `subscript`() async throws { | ||
var dict = CaseInsensitiveDictionary([:]) | ||
#expect(dict.count == 0) | ||
|
||
dict["KEY1"] = "value1" | ||
#expect(dict.count == 1) | ||
#expect(dict["key1"] as! String == "value1") | ||
#expect(dict["KEY1"] as! String == "value1") | ||
#expect(dict["KeY1"] as! String == "value1") | ||
|
||
dict["key2"] = false | ||
#expect(dict.count == 2) | ||
#expect(dict["kEy2"] as! Bool == false) | ||
|
||
dict["key3"] = 2025 | ||
#expect(dict.count == 3) | ||
#expect(dict["key3"] as! Int == 2025) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the scenario where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the removal of keys are working as you expected. Initially, I thought you are saying that supporting Anyway, you are right in terms of the removal of keys. I accepted all the other code suggestions too. |
||
} | ||
|
||
@Test | ||
func updatedOriginalDictionary() async throws { | ||
var dict = CaseInsensitiveDictionary([ | ||
"key1": "value1", | ||
"KEY1": "VALUE1", | ||
]) | ||
#expect(dict.count == 1) | ||
#expect(dict.originalDictionary.count == 2) | ||
|
||
dict["KEY1"] = "Swift" | ||
#expect(dict["KEY1"] as! String == "Swift") | ||
#expect(dict.count == 1) | ||
#expect(dict.originalDictionary.count == 1) | ||
#expect(dict.toDictionary().count == 1) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the original code pattern roughly follows Apache Spark code pattern which creates a new map.
https://github.com/apache/spark/blob/510edd763b912a21883e8a0dbaa3f661fedec234/sql/api/src/main/scala/org/apache/spark/sql/catalyst/util/CaseInsensitiveMap.scala#L41
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me update with yours.