Skip to content
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

TO_NUMBER/TO_DECIMAL/TO_NUMERIC without precision and scale #1053

Merged
merged 6 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
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 @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/databricks/labs/remorph/snow/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

This file was deleted.

Loading