Skip to content

Commit c35203f

Browse files
committed
Merge pull request #1 from marmbrus/pr/2701
Expose Metadata and MetadataBuilder through the public scala and java packages.
2 parents 589f314 + 886b85c commit c35203f

File tree

11 files changed

+90
-18
lines changed

11 files changed

+90
-18
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/Metadata.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ class MetadataBuilder {
204204

205205
private val map: mutable.Map[String, Any] = mutable.Map.empty
206206

207+
/** Returns the immutable version of this map. Used for java interop. */
208+
protected def getMap = map.toMap
209+
207210
/** Include the content of an existing [[Metadata]] instance. */
208211
def withMetadata(metadata: Metadata): this.type = {
209212
map ++= metadata.map

sql/core/src/main/java/org/apache/spark/sql/api/java/DataType.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import java.util.*;
2121

22-
import org.apache.spark.sql.catalyst.util.Metadata;
23-
2422
/**
2523
* The base type of all Spark SQL data types.
2624
*
@@ -175,7 +173,7 @@ public static StructField createStructField(
175173
* @see #createStructField(String, DataType, boolean, Metadata)
176174
*/
177175
public static StructField createStructField(String name, DataType dataType, boolean nullable) {
178-
return createStructField(name, dataType, nullable, Metadata.empty());
176+
return createStructField(name, dataType, nullable, (new MetadataBuilder()).build());
179177
}
180178

181179
/**
@@ -207,5 +205,4 @@ public static StructType createStructType(StructField[] fields) {
207205

208206
return new StructType(fields);
209207
}
210-
211208
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.api.java;
19+
20+
/**
21+
* Metadata is a wrapper over Map[String, Any] that limits the value type to simple ones: Boolean,
22+
* Long, Double, String, Metadata, Array[Boolean], Array[Long], Array[Double], Array[String], and
23+
* Array[Metadata]. JSON is used for serialization.
24+
*
25+
* The default constructor is private. User should use [[MetadataBuilder]].
26+
*/
27+
class Metadata extends org.apache.spark.sql.catalyst.util.Metadata {
28+
Metadata(scala.collection.immutable.Map<String, Object> map) {
29+
super(map);
30+
}
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.api.java;
19+
20+
/**
21+
* Builder for [[Metadata]]. If there is a key collision, the latter will overwrite the former.
22+
*/
23+
public class MetadataBuilder extends org.apache.spark.sql.catalyst.util.MetadataBuilder {
24+
@Override
25+
public Metadata build() {
26+
return new Metadata(getMap());
27+
}
28+
}

sql/core/src/main/java/org/apache/spark/sql/api/java/StructField.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import java.util.Map;
2121

22-
import org.apache.spark.sql.catalyst.util.Metadata;
23-
2422
/**
2523
* A StructField object represents a field in a StructType object.
2624
* A StructField object comprises three fields, {@code String name}, {@code DataType dataType},

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import org.apache.spark.sql.catalyst.expressions._
3232
import org.apache.spark.sql.catalyst.optimizer.{Optimizer, DefaultOptimizer}
3333
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
3434
import org.apache.spark.sql.catalyst.rules.RuleExecutor
35-
import org.apache.spark.sql.catalyst.types.DataType
36-
import org.apache.spark.sql.columnar.InMemoryRelation
3735
import org.apache.spark.sql.execution.{SparkStrategies, _}
3836
import org.apache.spark.sql.json._
3937
import org.apache.spark.sql.parquet.ParquetRelation

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ package object sql {
125125
@DeveloperApi
126126
type DataType = catalyst.types.DataType
127127

128+
@DeveloperApi
129+
val DataType = catalyst.types.DataType
130+
128131
/**
129132
* :: DeveloperApi ::
130133
*
@@ -414,4 +417,24 @@ package object sql {
414417
*/
415418
@DeveloperApi
416419
val StructField = catalyst.types.StructField
420+
421+
/**
422+
* :: DeveloperApi ::
423+
*
424+
* Metadata is a wrapper over Map[String, Any] that limits the value type to simple ones: Boolean,
425+
* Long, Double, String, Metadata, Array[Boolean], Array[Long], Array[Double], Array[String], and
426+
* Array[Metadata]. JSON is used for serialization.
427+
*
428+
* The default constructor is private. User should use either [[MetadataBuilder]] or
429+
* [[Metadata$#fromJson]] to create Metadata instances.
430+
*
431+
* @param map an immutable map that stores the data
432+
*/
433+
@DeveloperApi
434+
type Metadata = catalyst.util.Metadata
435+
436+
/**
437+
* Builder for [[Metadata]]. If there is a key collision, the latter will overwrite the former.
438+
*/
439+
type MetadataBuilder = catalyst.util.MetadataBuilder
417440
}

sql/core/src/main/scala/org/apache/spark/sql/types/util/DataTypeConversions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.spark.sql.types.util
1919

2020
import org.apache.spark.sql._
21-
import org.apache.spark.sql.api.java.{DataType => JDataType, StructField => JStructField}
21+
import org.apache.spark.sql.api.java.{DataType => JDataType, StructField => JStructField, MetadataBuilder => JMetaDataBuilder}
2222

2323
import scala.collection.JavaConverters._
2424

@@ -32,7 +32,7 @@ protected[sql] object DataTypeConversions {
3232
scalaStructField.name,
3333
asJavaDataType(scalaStructField.dataType),
3434
scalaStructField.nullable,
35-
scalaStructField.metadata)
35+
(new JMetaDataBuilder).withMetadata(scalaStructField.metadata).build())
3636
}
3737

3838
/**

sql/core/src/test/scala/org/apache/spark/sql/DataTypeSuite.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ package org.apache.spark.sql
1919

2020
import org.scalatest.FunSuite
2121

22-
import org.apache.spark.sql.catalyst.types.DataType
23-
import org.apache.spark.sql.catalyst.util.MetadataBuilder
24-
2522
class DataTypeSuite extends FunSuite {
2623

2724
test("construct an ArrayType") {

sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import org.apache.spark.sql.TestData._
2525
import org.apache.spark.sql.catalyst.errors.TreeNodeException
2626
import org.apache.spark.sql.catalyst.expressions._
2727
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
28-
import org.apache.spark.sql.execution.joins.BroadcastHashJoin
29-
import org.apache.spark.sql.catalyst.util.MetadataBuilder
28+
3029
import org.apache.spark.sql.test.TestSQLContext._
3130

3231
class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {

0 commit comments

Comments
 (0)