Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.catalyst.expressions

import org.apache.spark.Logging
import org.apache.spark.sql.catalyst.errors.attachTree
import org.apache.spark.sql.catalyst.types._
import org.apache.spark.sql.catalyst.trees

import scala.collection.mutable

/**
* Builds a map that is keyed by an Attribute's expression id. Using the expression id allows values
* to be looked up even when the attributes used differ cosmetically (i.e., the capitalization
* of the name, or the expected nullability).
*/
object AttributeMap {
def apply[A](kvs: Seq[(Attribute, A)]) =
new AttributeMap(kvs.map(kv => (kv._1.exprId, (kv._1, kv._2))).toMap)
}

class AttributeMap[A] protected (baseMap: Map[ExprId, (Attribute, A)])
extends Map[Attribute, A] with Serializable {

override def get(k: Attribute): Option[A] = baseMap.get(k.exprId).map(_._2)

override def +[B1 >: A](kv: (Attribute, B1)): Map[Attribute, B1] = // scalastyle:ignore
(baseMap.map(_._2) + kv).toMap

override def iterator: Iterator[(Attribute, A)] = baseMap.map(_._2).iterator

override def -(key: Attribute): Map[Attribute, A] = (baseMap.map(_._2) - key).toMap
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ case class BoundReference(ordinal: Int, dataType: DataType, nullable: Boolean)
}

object BindReferences extends Logging {
def bindReference[A <: Expression](expression: A, input: Seq[Attribute]): A = {

def bindReference[A <: Expression](
expression: A,
input: Seq[Attribute],
allowFailures: Boolean = false): A = {
expression.transform { case a: AttributeReference =>
attachTree(a, "Binding attribute") {
val ordinal = input.indexWhere(_.exprId == a.exprId)
if (ordinal == -1) {
sys.error(s"Couldn't find $a in ${input.mkString("[", ",", "]")}")
if (allowFailures) {
a
} else {
sys.error(s"Couldn't find $a in ${input.mkString("[", ",", "]")}")
}
} else {
BoundReference(ordinal, a.dataType, a.nullable)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private[sql] trait ColumnBuilder {
/**
* Column statistics information
*/
def columnStats: ColumnStats[_, _]
def columnStats: ColumnStats

/**
* Returns the final columnar byte buffer.
Expand All @@ -47,7 +47,7 @@ private[sql] trait ColumnBuilder {
}

private[sql] class BasicColumnBuilder[T <: DataType, JvmType](
val columnStats: ColumnStats[T, JvmType],
val columnStats: ColumnStats,
val columnType: ColumnType[T, JvmType])
extends ColumnBuilder {

Expand All @@ -70,6 +70,7 @@ private[sql] class BasicColumnBuilder[T <: DataType, JvmType](

override def appendFrom(row: Row, ordinal: Int) {
val field = columnType.getField(row, ordinal)
columnStats.gatherStats(row, ordinal)
buffer = ensureFreeSpace(buffer, columnType.actualSize(field))
columnType.append(field, buffer)
}
Expand All @@ -82,18 +83,18 @@ private[sql] class BasicColumnBuilder[T <: DataType, JvmType](

private[sql] abstract class ComplexColumnBuilder[T <: DataType, JvmType](
columnType: ColumnType[T, JvmType])
extends BasicColumnBuilder[T, JvmType](new NoopColumnStats[T, JvmType], columnType)
extends BasicColumnBuilder[T, JvmType](new NoopColumnStats, columnType)
with NullableColumnBuilder

private[sql] abstract class NativeColumnBuilder[T <: NativeType](
override val columnStats: NativeColumnStats[T],
override val columnStats: ColumnStats,
override val columnType: NativeColumnType[T])
extends BasicColumnBuilder[T, T#JvmType](columnStats, columnType)
with NullableColumnBuilder
with AllCompressionSchemes
with CompressibleColumnBuilder[T]

private[sql] class BooleanColumnBuilder extends NativeColumnBuilder(new BooleanColumnStats, BOOLEAN)
private[sql] class BooleanColumnBuilder extends NativeColumnBuilder(new NoopColumnStats, BOOLEAN)

private[sql] class IntColumnBuilder extends NativeColumnBuilder(new IntColumnStats, INT)

Expand Down
Loading