Skip to content
Closed
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
Expand Up @@ -366,7 +366,7 @@ case class StringInstr(str: Expression, substr: Expression)
* in given string after position pos.
*/
case class StringLocate(substr: Expression, str: Expression, start: Expression)
extends Expression with ImplicitCastInputTypes with CodegenFallback {
extends Expression with ImplicitCastInputTypes {

def this(substr: Expression, str: Expression) = {
this(substr, str, Literal(0))
Expand Down Expand Up @@ -400,6 +400,31 @@ case class StringLocate(substr: Expression, str: Expression, start: Expression)
}
}

override protected def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
val substrGen = substr.gen(ctx)
val strGen = str.gen(ctx)
val startGen = start.gen(ctx)
s"""
int ${ev.primitive} = 0;
boolean ${ev.isNull} = false;
${startGen.code}
if (!${startGen.isNull}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@tarekauel can we specialize code gen for cases where str / start is foldable?

Once you do that, make sure you update the test cases to use NonFoldableLiteral (just search the code base for how it is used).

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we do not need the special case here, because we can directly call UTF8String.indexOf()

${substrGen.code}
if (!${substrGen.isNull}) {
${strGen.code}
if (!${strGen.isNull}) {
${ev.primitive} = ${strGen.primitive}.indexOf(${substrGen.primitive},
${startGen.primitive}) + 1;
} else {
${ev.isNull} = true;
}
} else {
${ev.isNull} = true;
}
}
"""
}

override def prettyName: String = "locate"
}

Expand Down