-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support function translation in the TSQl and Snowflake
- Some functions must be translated from TSQL or Snowflake versions into the equivalent IR for Databricks SQL. - Provides a ConversionStrategy system that allows for simple name translations or more complicated IR representations when there is no equivalent. - Modifies FunctionBuilders etc to accept differnt dialects.
- Loading branch information
Showing
5 changed files
with
106 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
core/src/main/scala/com/databricks/labs/remorph/parsers/FunctionConverters.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.databricks.labs.remorph.parsers | ||
import com.databricks.labs.remorph.parsers.{intermediate => ir} | ||
|
||
import java.util.Locale | ||
|
||
// A set of functions that convert between different representations of SQL functions, sometimes | ||
// depending on the SQL dialect. They could be one big match statement if all the conversions are trivial renames, | ||
// however, if the conversions are more complex, they can be implemented as separate conversion strategies. | ||
|
||
sealed trait ConversionStrategy { | ||
def convert(irName: String, args: Seq[ir.Expression], dialect: SqlDialect): ir.Expression | ||
} | ||
|
||
object FunctionConverters { | ||
|
||
// Preserves case if the original name was all lower case. Otherwise, converts to lower case. | ||
// All bets are off if the original name was mixed case, but that is rarely seen in SQL and we are | ||
// just making reasonable efforts here. | ||
private def convertString(irName: String, newName: String): String = { | ||
if (irName.forall(_.isLower)) newName.toLowerCase(Locale.ROOT) else newName | ||
} | ||
|
||
object FunctionRename extends ConversionStrategy { | ||
override def convert(irName: String, args: Seq[ir.Expression], dialect: SqlDialect): ir.Expression = { | ||
(irName.toUpperCase(), dialect) match { | ||
case ("ISNULL", TSql) => ir.CallFunction(FunctionConverters.convertString(irName, "IFNULL"), args) | ||
case _ => ir.CallFunction(irName, args) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters