Skip to content

Commit d978255

Browse files
beliefercloud-fan
authored andcommitted
[SPARK-38633][SQL][FOLLOWUP] JDBCSQLBuilder should build cast to type of databases
### What changes were proposed in this pull request? DS V2 supports push down CAST to database. The current implement only uses the typeName of DataType. For example: `Cast(column, StringType)` will be build to `CAST(column AS String)`. But it should be `CAST(column AS TEXT)` for Postgres or `CAST(column AS VARCHAR2(255))` for Oracle. ### Why are the changes needed? Improve the implement of push down CAST. ### Does this PR introduce _any_ user-facing change? 'No'. Just new feature. ### How was this patch tested? Exists tests Closes #35999 from beliefer/SPARK-38633_followup. Authored-by: Jiaan Geng <[email protected]> Signed-off-by: Wenchen Fan <[email protected]> (cherry picked from commit ca7200b) Signed-off-by: Wenchen Fan <[email protected]>
1 parent a258412 commit d978255

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala

+6
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ abstract class JdbcDialect extends Serializable with Logging{
234234
}
235235
quoteIdentifier(namedRef.fieldNames.head)
236236
}
237+
238+
override def visitCast(l: String, dataType: DataType): String = {
239+
val databaseTypeDefinition =
240+
getJDBCType(dataType).map(_.databaseTypeDefinition).getOrElse(dataType.typeName)
241+
s"CAST($l AS $databaseTypeDefinition)"
242+
}
237243
}
238244

239245
/**

0 commit comments

Comments
 (0)