Skip to content

Commit

Permalink
Redefine ToCharacter as RuntimeReplaceable
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGekk committed Aug 16, 2023
1 parent 8e47b84 commit f1d6134
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,23 @@ case class TryToNumber(left: Expression, right: Expression)
""",
since = "3.4.0",
group = "string_funcs")
case class ToCharacter(left: Expression, right: Expression, replacement: Expression)
extends RuntimeReplaceable with InheritAnalysisRules {
def this(left: Expression, right: Expression) = this(left, right,
left.dataType match {
case _: DatetimeType => DateFormatClass(left, right)
case _ => NumberToCharacter(left, right)
}
)

override def prettyName: String = "to_char"

override def parameters: Seq[Expression] = Seq(left, right)

override protected def withNewChildInternal(newChild: Expression): Expression =
this.copy(replacement = newChild)
}

case class NumberToCharacter(left: Expression, right: Expression)
extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant {
private lazy val numberFormatter = {
Expand Down Expand Up @@ -256,7 +273,6 @@ case class NumberToCharacter(left: Expression, right: Expression)
inputTypeCheck
}
}
override def prettyName: String = "to_char"
override def nullSafeEval(decimal: Any, format: Any): Any = {
val input = decimal.asInstanceOf[Decimal]
numberFormatter.format(input)
Expand All @@ -281,15 +297,3 @@ case class NumberToCharacter(left: Expression, right: Expression)
newLeft: Expression, newRight: Expression): NumberToCharacter =
copy(left = newLeft, right = newRight)
}

case class ToCharacter(left: Expression, right: Expression) extends RuntimeReplaceable {
override def children: Seq[Expression] = Seq(left, right)
override def replacement: Expression = left.dataType match {
case _: DatetimeType => DateFormatClass(left, right)
case _ => NumberToCharacter(left, right)
}

override def withNewChildrenInternal(newChildren: IndexedSeq[Expression]): Expression = {
copy(left = newChildren(0), right = newChildren(1))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ Aggregate [max(q1#xL) AS max(q1)#xL, max(q2#xL) AS max(q2)#xL]
SELECT '' AS to_char_1, to_char(q1, '9G999G999G999G999G999'), to_char(q2, '9,999,999,999,999,999')
FROM INT8_TBL
-- !query analysis
Project [ AS to_char_1#x, to_char(cast(q1#xL as decimal(20,0)), 9G999G999G999G999G999) AS to_char(q1, 9G999G999G999G999G999)#x, to_char(cast(q2#xL as decimal(20,0)), 9,999,999,999,999,999) AS to_char(q2, 9,999,999,999,999,999)#x]
Project [ AS to_char_1#x, to_char(q1#xL, 9G999G999G999G999G999) AS to_char(q1, 9G999G999G999G999G999)#x, to_char(q2#xL, 9,999,999,999,999,999) AS to_char(q2, 9,999,999,999,999,999)#x]
+- SubqueryAlias spark_catalog.default.int8_tbl
+- Relation spark_catalog.default.int8_tbl[q1#xL,q2#xL] parquet

Expand All @@ -490,7 +490,7 @@ Project [ AS to_char_1#x, to_char(cast(q1#xL as decimal(20,0)), 9G999G999G999G99
SELECT '' AS to_char_3, to_char( (q1 * -1), '9999999999999999PR'), to_char( (q2 * -1), '9999999999999999.999PR')
FROM INT8_TBL
-- !query analysis
Project [ AS to_char_3#x, to_char(cast((q1#xL * cast(-1 as bigint)) as decimal(20,0)), 9999999999999999PR) AS to_char((q1 * -1), 9999999999999999PR)#x, to_char(cast((q2#xL * cast(-1 as bigint)) as decimal(20,0)), 9999999999999999.999PR) AS to_char((q2 * -1), 9999999999999999.999PR)#x]
Project [ AS to_char_3#x, to_char((q1#xL * cast(-1 as bigint)), 9999999999999999PR) AS to_char((q1 * -1), 9999999999999999PR)#x, to_char((q2#xL * cast(-1 as bigint)), 9999999999999999.999PR) AS to_char((q2 * -1), 9999999999999999.999PR)#x]
+- SubqueryAlias spark_catalog.default.int8_tbl
+- Relation spark_catalog.default.int8_tbl[q1#xL,q2#xL] parquet

Expand All @@ -499,31 +499,31 @@ Project [ AS to_char_3#x, to_char(cast((q1#xL * cast(-1 as bigint)) as decimal(2
SELECT '' AS to_char_4, to_char( (q1 * -1), '9999999999999999S'), to_char( (q2 * -1), 'S9999999999999999')
FROM INT8_TBL
-- !query analysis
Project [ AS to_char_4#x, to_char(cast((q1#xL * cast(-1 as bigint)) as decimal(20,0)), 9999999999999999S) AS to_char((q1 * -1), 9999999999999999S)#x, to_char(cast((q2#xL * cast(-1 as bigint)) as decimal(20,0)), S9999999999999999) AS to_char((q2 * -1), S9999999999999999)#x]
Project [ AS to_char_4#x, to_char((q1#xL * cast(-1 as bigint)), 9999999999999999S) AS to_char((q1 * -1), 9999999999999999S)#x, to_char((q2#xL * cast(-1 as bigint)), S9999999999999999) AS to_char((q2 * -1), S9999999999999999)#x]
+- SubqueryAlias spark_catalog.default.int8_tbl
+- Relation spark_catalog.default.int8_tbl[q1#xL,q2#xL] parquet


-- !query
SELECT '' AS to_char_5, to_char(q2, 'MI9999999999999999') FROM INT8_TBL
-- !query analysis
Project [ AS to_char_5#x, to_char(cast(q2#xL as decimal(20,0)), MI9999999999999999) AS to_char(q2, MI9999999999999999)#x]
Project [ AS to_char_5#x, to_char(q2#xL, MI9999999999999999) AS to_char(q2, MI9999999999999999)#x]
+- SubqueryAlias spark_catalog.default.int8_tbl
+- Relation spark_catalog.default.int8_tbl[q1#xL,q2#xL] parquet


-- !query
SELECT '' AS to_char_9, to_char(q2, '0999999999999999') FROM INT8_TBL
-- !query analysis
Project [ AS to_char_9#x, to_char(cast(q2#xL as decimal(20,0)), 0999999999999999) AS to_char(q2, 0999999999999999)#x]
Project [ AS to_char_9#x, to_char(q2#xL, 0999999999999999) AS to_char(q2, 0999999999999999)#x]
+- SubqueryAlias spark_catalog.default.int8_tbl
+- Relation spark_catalog.default.int8_tbl[q1#xL,q2#xL] parquet


-- !query
SELECT '' AS to_char_10, to_char(q2, 'S0999999999999999') FROM INT8_TBL
-- !query analysis
Project [ AS to_char_10#x, to_char(cast(q2#xL as decimal(20,0)), S0999999999999999) AS to_char(q2, S0999999999999999)#x]
Project [ AS to_char_10#x, to_char(q2#xL, S0999999999999999) AS to_char(q2, S0999999999999999)#x]
+- SubqueryAlias spark_catalog.default.int8_tbl
+- Relation spark_catalog.default.int8_tbl[q1#xL,q2#xL] parquet

Expand Down

0 comments on commit f1d6134

Please sign in to comment.