parthchandra commented on code in PR #307: URL: https://github.com/apache/datafusion-comet/pull/307#discussion_r1581637504
########## core/src/execution/datafusion/expressions/cast.rs: ########## @@ -142,6 +226,311 @@ impl Cast { } } +fn cast_string_to_i8(str: &str, eval_mode: EvalMode) -> CometResult<Option<i8>> { + Ok(cast_string_to_int_with_range_check( + str, + eval_mode, + "TINYINT", + i8::MIN as i32, + i8::MAX as i32, + )? + .map(|v| v as i8)) +} + +fn cast_string_to_i16(str: &str, eval_mode: EvalMode) -> CometResult<Option<i16>> { + Ok(cast_string_to_int_with_range_check( + str, + eval_mode, + "SMALLINT", + i16::MIN as i32, + i16::MAX as i32, + )? + .map(|v| v as i16)) +} + +fn cast_string_to_i32(str: &str, eval_mode: EvalMode) -> CometResult<Option<i32>> { + let mut accum = CastStringToInt32::default(); + do_cast_string_to_int(&mut accum, str, eval_mode, "INT")?; + Ok(accum.result) +} + +fn cast_string_to_i64(str: &str, eval_mode: EvalMode) -> CometResult<Option<i64>> { + let mut accum = CastStringToInt64::default(); + do_cast_string_to_int(&mut accum, str, eval_mode, "BIGINT")?; + Ok(accum.result) +} + +fn cast_string_to_int_with_range_check( + str: &str, + eval_mode: EvalMode, + type_name: &str, + min: i32, + max: i32, +) -> CometResult<Option<i32>> { + let mut accum = CastStringToInt32::default(); Review Comment: I don't this should cause any heap allocation at all. I would expect this to be allocated on the stack which shouldn't be expensive. But a benchmark is always a more convincing argument. ########## spark/src/test/scala/org/apache/comet/CometCastSuite.scala: ########## @@ -66,19 +66,22 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper { castTest(testValues, DataTypes.BooleanType) } - ignore("cast string to byte") { - castTest(generateStrings(numericPattern, 8).toDF("a"), DataTypes.ByteType) + test("cast string to byte") { + val testValues = + Seq("", ".", "0", "-0", "+1", "-1", ".2", "-.2", "1e1", "127", "128", "-128", "-129") ++ + generateStrings(numericPattern, 8) + castTest(testValues.toDF("a"), DataTypes.ByteType) } - ignore("cast string to short") { + test("cast string to short") { Review Comment: +1 for these test btw -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org