Skip to content

BoundReference

BoundReference is a LeafExpression that gives (evaluates to) the value of the given InternalRow at a specified position (and of a given data type).

Creating Instance

BoundReference takes the following to be created:

  • Position
  • Data type of the value
  • nullable flag (whether the value can be null or not)

BoundReference is created when:

Code-Generated Expression Evaluation

doGenCode(
  ctx: CodegenContext,
  ev: ExprCode): ExprCode

doGenCode is part of the Expression abstraction.

import org.apache.spark.sql.catalyst.expressions.BoundReference
import org.apache.spark.sql.types.LongType
val boundRef = BoundReference(ordinal = 0, dataType = LongType, nullable = true)

// doGenCode is used when Expression.genCode is executed

import org.apache.spark.sql.catalyst.expressions.codegen.CodegenContext
val ctx = new CodegenContext
val code = boundRef.genCode(ctx).code
scala> println(code)
boolean isNull_0 = i.isNullAt(0);
long value_0 = isNull_0 ?
  -1L : (i.getLong(0));

Interpreted Expression Evaluation

eval(
  input: InternalRow): Any

eval gives the value at position from the given InternalRow that is of a correct type.


eval returns null if the value at the position is null. Otherwise, eval uses the methods of InternalRow per the defined data type to access the value.


eval is part of the Expression abstraction.

Catalyst DSL

Catalyst DSL's at can be used to create a BoundReference.

import org.apache.spark.sql.catalyst.dsl.expressions._
val boundRef = 'id.string.at(4)
scala> :type boundRef
org.apache.spark.sql.catalyst.expressions.BoundReference

scala> println(boundRef)
input[4, string, true]

Demo

import org.apache.spark.sql.catalyst.expressions.BoundReference
import org.apache.spark.sql.types.LongType
val boundRef = BoundReference(ordinal = 0, dataType = LongType, nullable = true)
scala> println(boundRef)
input[0, bigint, true]
import org.apache.spark.sql.catalyst.InternalRow
val row = InternalRow(1L, "hello")
val value = boundRef.eval(row).asInstanceOf[Long]
Back to top