-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-6994] Allow to fetch field values by name in sql.Row
It looked weird that up to now there was no way in Spark's Scala API to access fields of `DataFrame/sql.Row` by name, only by their index. This tries to solve this issue. Author: vidmantas zemleris <[email protected]> Closes #5573 from vidma/features/row-with-named-fields and squashes the following commits: 6145ae3 [vidmantas zemleris] [SPARK-6994][SQL] Allow to fetch field values by name on Row 9564ebb [vidmantas zemleris] [SPARK-6994][SQL] Add fieldIndex to schema (StructType)
- Loading branch information
Showing
6 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
sql/catalyst/src/test/scala/org/apache/spark/sql/RowTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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 | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{GenericRow, GenericRowWithSchema} | ||
import org.apache.spark.sql.types._ | ||
import org.scalatest.{Matchers, FunSpec} | ||
|
||
class RowTest extends FunSpec with Matchers { | ||
|
||
val schema = StructType( | ||
StructField("col1", StringType) :: | ||
StructField("col2", StringType) :: | ||
StructField("col3", IntegerType) :: Nil) | ||
val values = Array("value1", "value2", 1) | ||
|
||
val sampleRow: Row = new GenericRowWithSchema(values, schema) | ||
val noSchemaRow: Row = new GenericRow(values) | ||
|
||
describe("Row (without schema)") { | ||
it("throws an exception when accessing by fieldName") { | ||
intercept[UnsupportedOperationException] { | ||
noSchemaRow.fieldIndex("col1") | ||
} | ||
intercept[UnsupportedOperationException] { | ||
noSchemaRow.getAs("col1") | ||
} | ||
} | ||
} | ||
|
||
describe("Row (with schema)") { | ||
it("fieldIndex(name) returns field index") { | ||
sampleRow.fieldIndex("col1") shouldBe 0 | ||
sampleRow.fieldIndex("col3") shouldBe 2 | ||
} | ||
|
||
it("getAs[T] retrieves a value by fieldname") { | ||
sampleRow.getAs[String]("col1") shouldBe "value1" | ||
sampleRow.getAs[Int]("col3") shouldBe 1 | ||
} | ||
|
||
it("Accessing non existent field throws an exception") { | ||
intercept[IllegalArgumentException] { | ||
sampleRow.getAs[String]("non_existent") | ||
} | ||
} | ||
|
||
it("getValuesMap() retrieves values of multiple fields as a Map(field -> value)") { | ||
val expected = Map( | ||
"col1" -> "value1", | ||
"col2" -> "value2" | ||
) | ||
sampleRow.getValuesMap(List("col1", "col2")) shouldBe expected | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters