Skip to content

Commit 8265dc7

Browse files
lianchengpwendell
authored andcommitted
Fixed coding style issues in Spark SQL
This PR addresses various coding style issues in Spark SQL, including but not limited to those mentioned by @mateiz in PR apache#146. As this PR affects lots of source files and may cause potential conflicts, it would be better to merge this as soon as possible *after* PR apache#205 (In-memory columnar representation for Spark SQL) is merged. Author: Cheng Lian <lian.cs.zju@gmail.com> Closes apache#208 from liancheng/fixCodingStyle and squashes the following commits: fc2b528 [Cheng Lian] Merge branch 'master' into fixCodingStyle b531273 [Cheng Lian] Fixed coding style issues in sql/hive 0b56f77 [Cheng Lian] Fixed coding style issues in sql/core fae7b02 [Cheng Lian] Addressed styling issues mentioned by @marmbrus 9265366 [Cheng Lian] Fixed coding style issues in sql/core 3dcbbbd [Cheng Lian] Fixed relative package imports for package catalyst
1 parent 57a4379 commit 8265dc7

File tree

87 files changed

+448
-490
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+448
-490
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
package catalyst
20+
21+
import org.apache.spark.sql.catalyst.expressions.Attribute
22+
import org.apache.spark.sql.catalyst.expressions.AttributeReference
23+
import org.apache.spark.sql.catalyst.plans.logical.LocalRelation
24+
import org.apache.spark.sql.catalyst.types._
25+
26+
/**
27+
* Provides experimental support for generating catalyst schemas for scala objects.
28+
*/
29+
object ScalaReflection {
30+
import scala.reflect.runtime.universe._
31+
32+
/** Returns a Sequence of attributes for the given case class type. */
33+
def attributesFor[T: TypeTag]: Seq[Attribute] = schemaFor[T] match {
34+
case s: StructType =>
35+
s.fields.map(f => AttributeReference(f.name, f.dataType, nullable = true)())
36+
}
37+
38+
/** Returns a catalyst DataType for the given Scala Type using reflection. */
39+
def schemaFor[T: TypeTag]: DataType = schemaFor(typeOf[T])
40+
41+
/** Returns a catalyst DataType for the given Scala Type using reflection. */
42+
def schemaFor(tpe: `Type`): DataType = tpe match {
43+
case t if t <:< typeOf[Product] =>
44+
val params = t.member("<init>": TermName).asMethod.paramss
45+
StructType(
46+
params.head.map(p => StructField(p.name.toString, schemaFor(p.typeSignature), true)))
47+
case t if t <:< typeOf[Seq[_]] =>
48+
val TypeRef(_, _, Seq(elementType)) = t
49+
ArrayType(schemaFor(elementType))
50+
case t if t <:< typeOf[String] => StringType
51+
case t if t <:< definitions.IntTpe => IntegerType
52+
case t if t <:< definitions.LongTpe => LongType
53+
case t if t <:< definitions.DoubleTpe => DoubleType
54+
case t if t <:< definitions.ShortTpe => ShortType
55+
case t if t <:< definitions.ByteTpe => ByteType
56+
}
57+
58+
implicit class CaseClassRelation[A <: Product : TypeTag](data: Seq[A]) {
59+
60+
/**
61+
* Implicitly added to Sequences of case class objects. Returns a catalyst logical relation
62+
* for the the data in the sequence.
63+
*/
64+
def asRelation: LocalRelation = {
65+
val output = attributesFor[A]
66+
LocalRelation(output, data)
67+
}
68+
}
69+
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/SqlParser.scala

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,28 @@
1717

1818
package org.apache.spark.sql.catalyst
1919

20-
import scala.util.matching.Regex
21-
import scala.util.parsing.combinator._
20+
import scala.util.parsing.combinator.lexical.StdLexical
21+
import scala.util.parsing.combinator.syntactical.StandardTokenParsers
2222
import scala.util.parsing.input.CharArrayReader.EofCh
23-
import lexical._
24-
import syntactical._
25-
import token._
2623

27-
import analysis._
28-
import expressions._
29-
import plans._
30-
import plans.logical._
31-
import types._
24+
import org.apache.spark.sql.catalyst.analysis._
25+
import org.apache.spark.sql.catalyst.expressions._
26+
import org.apache.spark.sql.catalyst.plans._
27+
import org.apache.spark.sql.catalyst.plans.logical._
28+
import org.apache.spark.sql.catalyst.types._
3229

3330
/**
34-
* A very simple SQL parser. Based loosly on:
31+
* A very simple SQL parser. Based loosely on:
3532
* https://github.com/stephentu/scala-sql-parser/blob/master/src/main/scala/parser.scala
3633
*
3734
* Limitations:
3835
* - Only supports a very limited subset of SQL.
3936
* - Keywords must be capital.
4037
*
4138
* This is currently included mostly for illustrative purposes. Users wanting more complete support
42-
* for a SQL like language should checkout the HiveQL support in the sql/hive subproject.
39+
* for a SQL like language should checkout the HiveQL support in the sql/hive sub-project.
4340
*/
4441
class SqlParser extends StandardTokenParsers {
45-
4642
def apply(input: String): LogicalPlan = {
4743
phrase(query)(new lexical.Scanner(input)) match {
4844
case Success(r, x) => r
@@ -196,7 +192,7 @@ class SqlParser extends StandardTokenParsers {
196192

197193
protected lazy val from: Parser[LogicalPlan] = FROM ~> relations
198194

199-
// Based very loosly on the MySQL Grammar.
195+
// Based very loosely on the MySQL Grammar.
200196
// http://dev.mysql.com/doc/refman/5.0/en/join.html
201197
protected lazy val relations: Parser[LogicalPlan] =
202198
relation ~ "," ~ relation ^^ { case r1 ~ _ ~ r2 => Join(r1, r2, Inner, None) } |
@@ -261,9 +257,9 @@ class SqlParser extends StandardTokenParsers {
261257
andExpression * (OR ^^^ { (e1: Expression, e2: Expression) => Or(e1,e2) })
262258

263259
protected lazy val andExpression: Parser[Expression] =
264-
comparisionExpression * (AND ^^^ { (e1: Expression, e2: Expression) => And(e1,e2) })
260+
comparisonExpression * (AND ^^^ { (e1: Expression, e2: Expression) => And(e1,e2) })
265261

266-
protected lazy val comparisionExpression: Parser[Expression] =
262+
protected lazy val comparisonExpression: Parser[Expression] =
267263
termExpression ~ "=" ~ termExpression ^^ { case e1 ~ _ ~ e2 => Equals(e1, e2) } |
268264
termExpression ~ "<" ~ termExpression ^^ { case e1 ~ _ ~ e2 => LessThan(e1, e2) } |
269265
termExpression ~ "<=" ~ termExpression ^^ { case e1 ~ _ ~ e2 => LessThanOrEqual(e1, e2) } |

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ package org.apache.spark.sql
1919
package catalyst
2020
package analysis
2121

22-
import expressions._
23-
import plans.logical._
24-
import rules._
22+
import org.apache.spark.sql.catalyst.expressions._
23+
import org.apache.spark.sql.catalyst.plans.logical._
24+
import org.apache.spark.sql.catalyst.rules._
25+
2526

2627
/**
2728
* A trivial [[Analyzer]] with an [[EmptyCatalog]] and [[EmptyFunctionRegistry]]. Used for testing

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Catalog.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ package org.apache.spark.sql
1919
package catalyst
2020
package analysis
2121

22-
import plans.logical.{LogicalPlan, Subquery}
2322
import scala.collection.mutable
2423

24+
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Subquery}
25+
2526
/**
2627
* An interface for looking up relations by name. Used by an [[Analyzer]].
2728
*/

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package org.apache.spark.sql
1919
package catalyst
2020
package analysis
2121

22-
import expressions._
22+
import org.apache.spark.sql.catalyst.expressions.Expression
2323

2424
/** A catalog for looking up user defined functions, used by an [[Analyzer]]. */
2525
trait FunctionRegistry {

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package org.apache.spark.sql
1919
package catalyst
2020
package analysis
2121

22-
import expressions._
23-
import plans.logical._
24-
import rules._
25-
import types._
22+
import org.apache.spark.sql.catalyst.expressions._
23+
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Project, Union}
24+
import org.apache.spark.sql.catalyst.rules.Rule
25+
import org.apache.spark.sql.catalyst.types._
2626

2727
/**
2828
* A collection of [[catalyst.rules.Rule Rules]] that can be used to coerce differing types that

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/MultiInstanceRelation.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
package org.apache.spark.sql.catalyst
1919
package analysis
2020

21-
import plans.logical.LogicalPlan
22-
import rules._
21+
import org.apache.spark.sql.catalyst.rules.Rule
22+
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
2323

2424
/**
2525
* A trait that should be mixed into query operators where an single instance might appear multiple
2626
* times in a logical query plan. It is invalid to have multiple copies of the same attribute
27-
* produced by distinct operators in a query tree as this breaks the gurantee that expression
28-
* ids, which are used to differentate attributes, are unique.
27+
* produced by distinct operators in a query tree as this breaks the guarantee that expression
28+
* ids, which are used to differentiate attributes, are unique.
2929
*
3030
* Before analysis, all operators that include this trait will be asked to produce a new version
3131
* of itself with globally unique expression ids.

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/package.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
package org.apache.spark.sql
1819
package catalyst
1920

2021
/**

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ package org.apache.spark.sql
1919
package catalyst
2020
package analysis
2121

22-
import expressions._
23-
import plans.logical.BaseRelation
24-
import trees.TreeNode
22+
import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, Expression, NamedExpression}
23+
import org.apache.spark.sql.catalyst.plans.logical.BaseRelation
24+
import org.apache.spark.sql.catalyst.trees.TreeNode
2525

2626
/**
2727
* Thrown when an invalid attempt is made to access a property of a tree that has yet to be fully
@@ -95,7 +95,7 @@ case class Star(
9595
// If there is no table specified, use all input attributes.
9696
case None => input
9797
// If there is a table, pick out attributes that are part of this table.
98-
case Some(table) => input.filter(_.qualifiers contains table)
98+
case Some(t) => input.filter(_.qualifiers contains t)
9999
}
100100
val mappedAttributes = expandedAttributes.map(mapFunction).zip(input).map {
101101
case (n: NamedExpression, _) => n

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/dsl/package.scala

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,12 @@ package org.apache.spark.sql
1919
package catalyst
2020

2121
import scala.language.implicitConversions
22-
import scala.reflect.runtime.universe.TypeTag
2322

24-
import analysis.UnresolvedAttribute
25-
import expressions._
26-
import plans._
27-
import plans.logical._
28-
import types._
29-
30-
/**
31-
* Provides experimental support for generating catalyst schemas for scala objects.
32-
*/
33-
object ScalaReflection {
34-
import scala.reflect.runtime.universe._
35-
36-
/** Returns a Sequence of attributes for the given case class type. */
37-
def attributesFor[T: TypeTag]: Seq[Attribute] = schemaFor[T] match {
38-
case s: StructType =>
39-
s.fields.map(f => AttributeReference(f.name, f.dataType, nullable = true)())
40-
}
41-
42-
/** Returns a catalyst DataType for the given Scala Type using reflection. */
43-
def schemaFor[T: TypeTag]: DataType = schemaFor(typeOf[T])
44-
45-
/** Returns a catalyst DataType for the given Scala Type using reflection. */
46-
def schemaFor(tpe: `Type`): DataType = tpe match {
47-
case t if t <:< typeOf[Product] =>
48-
val params = t.member("<init>": TermName).asMethod.paramss
49-
StructType(
50-
params.head.map(p => StructField(p.name.toString, schemaFor(p.typeSignature), true)))
51-
case t if t <:< typeOf[Seq[_]] =>
52-
val TypeRef(_, _, Seq(elementType)) = t
53-
ArrayType(schemaFor(elementType))
54-
case t if t <:< typeOf[String] => StringType
55-
case t if t <:< definitions.IntTpe => IntegerType
56-
case t if t <:< definitions.LongTpe => LongType
57-
case t if t <:< definitions.DoubleTpe => DoubleType
58-
case t if t <:< definitions.ShortTpe => ShortType
59-
case t if t <:< definitions.ByteTpe => ByteType
60-
}
61-
62-
implicit class CaseClassRelation[A <: Product : TypeTag](data: Seq[A]) {
63-
64-
/**
65-
* Implicitly added to Sequences of case class objects. Returns a catalyst logical relation
66-
* for the the data in the sequence.
67-
*/
68-
def asRelation: LocalRelation = {
69-
val output = attributesFor[A]
70-
LocalRelation(output, data)
71-
}
72-
}
73-
}
23+
import org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute
24+
import org.apache.spark.sql.catalyst.expressions._
25+
import org.apache.spark.sql.catalyst.plans.logical._
26+
import org.apache.spark.sql.catalyst.plans.{Inner, JoinType}
27+
import org.apache.spark.sql.catalyst.types._
7428

7529
/**
7630
* A collection of implicit conversions that create a DSL for constructing catalyst data structures.

0 commit comments

Comments
 (0)