AngersZhuuuu commented on a change in pull request #30421: URL: https://github.com/apache/spark/pull/30421#discussion_r533410197
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala ########## @@ -509,13 +509,28 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logg } } + private def convertTypedLiteralToString(literal: Literal): String = literal match { + case Literal(data: Int, _: DateType) => + DateFormatter(getZoneId(SQLConf.get.sessionLocalTimeZone)).format(data) + case Literal(data: Long, _: TimestampType) => + TimestampFormatter.getFractionFormatter(getZoneId(SQLConf.get.sessionLocalTimeZone)) + .format(data) + case Literal(data: CalendarInterval, _: CalendarIntervalType) => data.toString + case Literal(data: Array[Byte], _: BinaryType) => + UTF8String.fromBytes(data).toString + case Literal(_, dataType) => + throw new IllegalArgumentException(s"Literals of type '$dataType' are not" + + " supported when use typed literal as partition col value.") + } + /** * Convert a constant of any type into a string. This is typically used in DDL commands, and its * main purpose is to prevent slight differences due to back to back conversions i.e.: * String -> Literal -> String. */ protected def visitStringConstant(ctx: ConstantContext): String = withOrigin(ctx) { Review comment: > can we support all the literals here? e.g. > > ``` > expression(ctx) match { > case l: Litral => if (l.value == null) null else Cast(l, StringType, timezone).eval() > case _ => fail... // this should never happen > } > ``` your mean support all `constant` here ``` constant : NULL #nullLiteral | interval #intervalLiteral | identifier STRING #typeConstructor | number #numericLiteral | booleanValue #booleanLiteral | STRING+ #stringLiteral ; ``` and use `expression` to visit this constant then we don't need so many convert logic. In this way we should add a new method such as : ``` protected def visitConstant(ctx: ConstantContext): String = withOrigin(ctx) { expression(ctx) match { case l: Literal => if (l.value == null) { null } else { Cast(l, StringType, Some(SQLConf.get.sessionLocalTimeZone)).eval() } case _ => throw IllegalArgumentException("") } } ``` I have tried similar way before, but `Cast.eval` can't run here. I have tried change `CastBase` code and extract `cast` functions as Half-life object class `object CastBase`. Then call `CastBase.castToString(literal)`. IMO, we don't need to expand the scope of the modification, but your suggestion is pretty good when we change partition spec to `Map[String, Literal]`. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org