-
Notifications
You must be signed in to change notification settings - Fork 0
Code gen code review. #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -435,37 +435,57 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w | |
if (evaluated == null) null else cast(evaluated) | ||
} | ||
|
||
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): Code = this match { | ||
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): Code = { | ||
// TODO(cg): Add support for more data types. | ||
(child.dataType, dataType) match { | ||
|
||
case Cast(child @ BinaryType(), StringType) => | ||
castOrNull (ctx, ev, c => | ||
s"new ${ctx.stringType}().set($c)") | ||
case (BinaryType, StringType) => | ||
defineCodeGen (ctx, ev, c => | ||
s"new ${ctx.stringType}().set($c)") | ||
|
||
case Cast(child @ DateType(), StringType) => | ||
castOrNull(ctx, ev, c => | ||
s"""new ${ctx.stringType}().set( | ||
case (DateType, StringType) => | ||
defineCodeGen(ctx, ev, c => | ||
s"""new ${ctx.stringType}().set( | ||
org.apache.spark.sql.catalyst.util.DateUtils.toString($c))""") | ||
|
||
case Cast(child @ BooleanType(), dt: NumericType) if !dt.isInstanceOf[DecimalType] => | ||
castOrNull(ctx, ev, c => s"(${ctx.primitiveType(dt)})($c?1:0)") | ||
case (BooleanType, dt: NumericType) if !dt.isInstanceOf[DecimalType] => | ||
defineCodeGen(ctx, ev, c => s"(${ctx.primitiveType(dt)})($c ? 1 : 0)") | ||
|
||
case Cast(child @ DecimalType(), IntegerType) => | ||
castOrNull(ctx, ev, c => s"($c).toInt()") | ||
case (_: NumericType, dt: NumericType) if !dt.isInstanceOf[DecimalType] => | ||
defineCodeGen(ctx, ev, c => s"(${ctx.primitiveType(dt)})($c)") | ||
|
||
case Cast(child @ DecimalType(), dt: NumericType) if !dt.isInstanceOf[DecimalType] => | ||
castOrNull(ctx, ev, c => s"($c).to${ctx.boxedType(dt)}()") | ||
case (_: DecimalType, ByteType) => | ||
defineCodeGen(ctx, ev, c => s"($c).toByte()") | ||
|
||
case Cast(child @ NumericType(), dt: NumericType) if !dt.isInstanceOf[DecimalType] => | ||
castOrNull(ctx, ev, c => s"(${ctx.primitiveType(dt)})($c)") | ||
case (_: DecimalType, ShortType) => | ||
defineCodeGen(ctx, ev, c => s"($c).toShort()") | ||
|
||
// Special handling required for timestamps in hive test cases since the toString function | ||
// does not match the expected output. | ||
case Cast(e, StringType) if e.dataType != TimestampType => | ||
castOrNull(ctx, ev, c => | ||
s"new ${ctx.stringType}().set(String.valueOf($c))") | ||
case (_: DecimalType, IntegerType) => | ||
defineCodeGen(ctx, ev, c => s"($c).toInt()") | ||
|
||
case other => | ||
super.genCode(ctx, ev) | ||
case (_: DecimalType, LongType) => | ||
defineCodeGen(ctx, ev, c => s"($c).toLong()") | ||
|
||
case (_: DecimalType, FloatType) => | ||
defineCodeGen(ctx, ev, c => s"($c).toFloat()") | ||
|
||
case (_: DecimalType, DoubleType) => | ||
defineCodeGen(ctx, ev, c => s"($c).toDouble()") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These could be covered by next case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok can you accept and just remove that? I have more comments you need to address anyway (and we should dicsuss them too). |
||
|
||
case (_: DecimalType, dt: NumericType) if !dt.isInstanceOf[DecimalType] => | ||
defineCodeGen(ctx, ev, c => s"($c).to${ctx.boxedType(dt)}()") | ||
|
||
// Special handling required for timestamps in hive test cases since the toString function | ||
// does not match the expected output. | ||
case (TimestampType, StringType) => | ||
super.genCode(ctx, ev) | ||
|
||
case (_, StringType) => | ||
defineCodeGen(ctx, ev, c => s"new ${ctx.stringType}().set(String.valueOf($c))") | ||
|
||
case other => | ||
super.genCode(ctx, ev) | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can not handle DecimalType => NumericType
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't that handled later?