Skip to content

Commit 4806358

Browse files
geooo109georgesittas
authored andcommitted
ref
1 parent e0b2164 commit 4806358

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

sqlglot/generators/snowflake.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -708,21 +708,23 @@ def tonumber_sql(self, expression: exp.ToNumber) -> str:
708708
precision = expression.args.get("precision")
709709
scale = expression.args.get("scale")
710710

711-
is_default = (
712-
isinstance(precision, exp.Literal)
713-
and precision.name == "38"
714-
and isinstance(scale, exp.Literal)
715-
and scale.name == "0"
716-
)
711+
default_precision = isinstance(precision, exp.Literal) and precision.name == "38"
712+
default_scale = isinstance(scale, exp.Literal) and scale.name == "0"
713+
714+
if default_precision and default_scale:
715+
precision = None
716+
scale = None
717+
elif default_scale:
718+
scale = None
717719

718720
func_name = "TRY_TO_NUMBER" if expression.args.get("safe") else "TO_NUMBER"
719721

720722
return self.func(
721723
func_name,
722724
expression.this,
723725
expression.args.get("format"),
724-
None if is_default else precision,
725-
None if is_default else scale,
726+
precision,
727+
scale,
726728
)
727729

728730
def timestampfromparts_sql(self, expression: exp.TimestampFromParts) -> str:

tests/dialects/test_snowflake.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,8 @@ def test_snowflake(self):
499499
self.validate_identity(
500500
"TO_DECIMAL(expr, fmt, precision, scale)", "TO_NUMBER(expr, fmt, precision, scale)"
501501
)
502-
self.validate_identity("TO_NUMBER(expr)")
503-
self.validate_identity("TO_NUMBER(expr, fmt)")
504-
self.validate_identity("TO_NUMBER(expr, fmt, precision, scale)")
502+
self.validate_identity("TO_NUMBER(expr, 38, 0)", "TO_NUMBER(expr)")
503+
self.validate_identity("TO_NUMBER(expr, 38)", "TO_NUMBER(expr)")
505504

506505
ast = self.validate_identity("TO_NUMBER('12.3456')")
507506
self.assertIsInstance(ast, exp.ToNumber)
@@ -527,7 +526,7 @@ def test_snowflake(self):
527526
self.assertEqual(ast.args.get("precision").name, "10")
528527
self.assertEqual(ast.args.get("scale").name, "1")
529528

530-
ast = self.validate_identity("TO_NUMBER('12.3456', 3)", "TO_NUMBER('12.3456', 3, 0)")
529+
ast = self.validate_identity("TO_NUMBER('12.3456', 3)")
531530
self.assertIsInstance(ast, exp.ToNumber)
532531
self.assertIsNone(ast.args.get("format"))
533532
self.assertEqual(ast.args.get("precision").name, "3")
@@ -573,9 +572,8 @@ def test_snowflake(self):
573572
self.validate_identity("TRY_TO_FILE(object_col)")
574573
self.validate_identity("TRY_TO_FILE('file.csv')")
575574
self.validate_identity("TRY_TO_FILE('file.csv', 'relativepath/')")
576-
self.validate_identity("TRY_TO_NUMBER('123.45')")
577-
self.validate_identity("TRY_TO_NUMBER('123.45', '999.99')")
578-
self.validate_identity("TRY_TO_NUMBER('123.45', '999.99', 10, 2)")
575+
self.validate_identity("TRY_TO_NUMBER(expr, 38, 0)", "TRY_TO_NUMBER(expr)")
576+
self.validate_identity("TRY_TO_NUMBER(expr, 38)", "TRY_TO_NUMBER(expr)")
579577

580578
ast = self.validate_identity("TRY_TO_NUMBER('12.3456')")
581579
self.assertIsInstance(ast, exp.ToNumber)
@@ -605,9 +603,7 @@ def test_snowflake(self):
605603
self.assertEqual(ast.args.get("scale").name, "1")
606604
self.assertTrue(ast.args.get("safe"))
607605

608-
ast = self.validate_identity(
609-
"TRY_TO_NUMBER('12.3456', 3)", "TRY_TO_NUMBER('12.3456', 3, 0)"
610-
)
606+
ast = self.validate_identity("TRY_TO_NUMBER('12.3456', 3)")
611607
self.assertIsInstance(ast, exp.ToNumber)
612608
self.assertIsNone(ast.args.get("format"))
613609
self.assertEqual(ast.args.get("precision").name, "3")

0 commit comments

Comments
 (0)