Skip to content

Commit 8da1a17

Browse files
committed
Add Row.fromSeq.
1 parent 9c99bc0 commit 8da1a17

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Row.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ object Row {
3737
* This method can be used to construct a [[Row]] with the given values.
3838
*/
3939
def apply(values: Any*): Row = new GenericRow(values.toArray)
40+
41+
/**
42+
* This method can be used to construct a [[Row]] from a [[Seq]] of values.
43+
*/
44+
def fromSeq(values: Seq[Any]): Row = new GenericRow(values.toArray)
4045
}
4146

4247
/**

sql/core/src/main/scala/org/apache/spark/sql/package.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ package object sql {
5050
* {{{
5151
* import org.apache.spark.sql._
5252
*
53+
* // Create a Row from values.
5354
* Row(value1, value2, value3, ...)
55+
* // Create a Row from a Seq of values.
56+
* Row.fromSeq(Seq(value1, value2, ...))
5457
* }}}
5558
*
5659
* A value of a row can be accessed through both generic access by ordinal,
@@ -272,7 +275,6 @@ package object sql {
272275
* `keyType: [[DataType]]` and `valueType: [[DataType]]`.
273276
* The field of `keyType` is used to specify the type of keys in the map.
274277
* The field of `valueType` is used to specify the type of values in the map.
275-
* For a [[MapType]] column, keys and values should not contain any `null` value.
276278
*
277279
* @group dataType
278280
*/
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql
19+
20+
import org.scalatest.FunSuite
21+
22+
import org.apache.spark.sql.catalyst.expressions.GenericMutableRow
23+
24+
class RowSuite extends FunSuite {
25+
26+
test("create row") {
27+
val expected = new GenericMutableRow(4)
28+
expected.update(0, 2147483647)
29+
expected.update(1, "this is a string")
30+
expected.update(2, false)
31+
expected.update(3, null)
32+
val actual1 = Row(2147483647, "this is a string", false, null)
33+
assert(expected.size === actual1.size)
34+
assert(expected.getInt(0) === actual1.getInt(0))
35+
assert(expected.getString(1) === actual1.getString(1))
36+
assert(expected.getBoolean(2) === actual1.getBoolean(2))
37+
assert(expected(3) === actual1(3))
38+
39+
val actual2 = Row.fromSeq(Seq(2147483647, "this is a string", false, null))
40+
assert(expected.size === actual2.size)
41+
assert(expected.getInt(0) === actual2.getInt(0))
42+
assert(expected.getString(1) === actual2.getString(1))
43+
assert(expected.getBoolean(2) === actual2.getBoolean(2))
44+
assert(expected(3) === actual2(3))
45+
}
46+
}

0 commit comments

Comments
 (0)