diff --git a/core/src/main/scala/com/databricks/labs/remorph/parsers/snowflake/rules/SnowflakeCallMapper.scala b/core/src/main/scala/com/databricks/labs/remorph/parsers/snowflake/rules/SnowflakeCallMapper.scala index ea3bdda45..193511814 100644 --- a/core/src/main/scala/com/databricks/labs/remorph/parsers/snowflake/rules/SnowflakeCallMapper.scala +++ b/core/src/main/scala/com/databricks/labs/remorph/parsers/snowflake/rules/SnowflakeCallMapper.scala @@ -149,7 +149,7 @@ class SnowflakeCallMapper extends ir.CallMapper with ir.IRHelpers { private def toNumber(args: Seq[ir.Expression]): ir.Expression = { val getArg: Int => Option[ir.Expression] = args.lift if (args.size < 2) { - throw TranspileException(ir.WrongNumberOfArguments("TO_NUMBER", args.size, "at least 2")) + ir.Cast(args.head, ir.DecimalType(38, 0)) } else if (args.size == 2) { ir.ToNumber(args.head, args(1)) } else { diff --git a/core/src/test/scala/com/databricks/labs/remorph/transpilers/SnowflakeToDatabricksTranspilerTest.scala b/core/src/test/scala/com/databricks/labs/remorph/transpilers/SnowflakeToDatabricksTranspilerTest.scala index 29f7f3204..4536d9fbb 100644 --- a/core/src/test/scala/com/databricks/labs/remorph/transpilers/SnowflakeToDatabricksTranspilerTest.scala +++ b/core/src/test/scala/com/databricks/labs/remorph/transpilers/SnowflakeToDatabricksTranspilerTest.scala @@ -6,6 +6,26 @@ class SnowflakeToDatabricksTranspilerTest extends AnyWordSpec with TranspilerTes protected val transpiler = new SnowflakeToDatabricksTranspiler + "transpile TO_NUMBER and TO_DECIMAL" should { + "transpile TO_NUMBER" in { + "select TO_NUMBER(EXPR) from test_tbl;" transpilesTo + """SELECT CAST(EXPR AS DECIMAL(38, 0)) FROM test_tbl + | ;""".stripMargin + } + + "transpile TO_NUMBER with precision and scale" in { + "select TO_NUMBER(EXPR,38,0) from test_tbl;" transpilesTo + """SELECT CAST(EXPR AS DECIMAL(38, 0)) FROM test_tbl + | ;""".stripMargin + } + + "transpile TO_DECIMAL" in { + "select TO_DECIMAL(EXPR) from test_tbl;" transpilesTo + """SELECT CAST(EXPR AS DECIMAL(38, 0)) FROM test_tbl + | ;""".stripMargin + } + } + "snowsql commands" should { "transpile BANG with semicolon" in { diff --git a/src/databricks/labs/remorph/snow/databricks.py b/src/databricks/labs/remorph/snow/databricks.py index 1ff927738..f60d71e89 100644 --- a/src/databricks/labs/remorph/snow/databricks.py +++ b/src/databricks/labs/remorph/snow/databricks.py @@ -270,6 +270,10 @@ def _to_number(self, expression: local_expression.ToNumber): if precision: return f"CAST({func_expr} AS DECIMAL({precision}, {scale}))" return func_expr + if not precision: + precision = 38 + if not scale: + scale = 0 if not expression.expression and not precision: exception_msg = f"""Error Parsing expression {expression}: * `format`: is required in Databricks [mandatory] diff --git a/tests/resources/functional/snowflake/functions/conversion/test_to_number/test_to_number_9.sql b/tests/resources/functional/snowflake/functions/conversion/test_to_number/test_to_number_9.sql new file mode 100644 index 000000000..7ef5962aa --- /dev/null +++ b/tests/resources/functional/snowflake/functions/conversion/test_to_number/test_to_number_9.sql @@ -0,0 +1,6 @@ + +-- snowflake sql: +select TO_NUMBER(EXPR) from test_tbl; + +-- databricks sql: +SELECT CAST(EXPR AS DECIMAL(38, 0)) FROM test_tbl; \ No newline at end of file diff --git a/tests/resources/functional/snowflake_expected_exceptions/test_to_number_9.sql b/tests/resources/functional/snowflake_expected_exceptions/test_to_number_9.sql deleted file mode 100644 index 0db4ed4e6..000000000 --- a/tests/resources/functional/snowflake_expected_exceptions/test_to_number_9.sql +++ /dev/null @@ -1,6 +0,0 @@ - --- snowflake sql: -SELECT TO_DECIMAL('$345') AS col1; - --- databricks sql: -SELECT CAST(TO_NUMBER('$345', '$999.00') AS DECIMAL(38, 0)) AS col1;