Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Comments.
  • Loading branch information
JoshRosen committed Jun 4, 2016
commit 4efd3ee498902cbd9e5b8905c50d514771dcc6ac
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ package object expressions {
StructType(attrs.map(a => StructField(a.name, a.dataType, a.nullable)))
}

private lazy val inputArr = attrs.toArray
// It's possible that `attrs` is a linked list, which can lead to bad O(n^2) loops when
// accessing attributes by their ordinals. To avoid this performance penalty, convert the input
// to an array.
private lazy val attrsArray = attrs.toArray

private lazy val exprIdToOrdinal = {
val arr = inputArr
val arr = attrsArray
val map = Maps.newHashMapWithExpectedSize[ExprId, Int](arr.length)
var index = 0
while (index < arr.length) {
Expand All @@ -110,8 +113,14 @@ package object expressions {
map
}

def apply(ordinal: Int): Attribute = inputArr(ordinal)
/**
* Returns the attribute at the given index.
*/
def apply(ordinal: Int): Attribute = attrsArray(ordinal)

/**
* Returns the index of first attribute with a matching expression id, or -1 if no match exists.
*/
def getOrdinalWithExprId(exprId: ExprId): Int = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would indexOf be more clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this originally and then moved to this name in anticipation of a future change which would add more "get index with property" methods, but a lot of those methods aren't cachable (e.g. semanticEquals), so I'll revert this back to my first name choice.

Option(exprIdToOrdinal.get(exprId)).getOrElse(-1)
}
Expand Down