-
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.
[SQL Generator] Generate SQL code for literals (#706)
Co-authored-by: Valentin Kasas <[email protected]>
- Loading branch information
Showing
7 changed files
with
194 additions
and
47 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
core/src/main/scala/com/databricks/labs/remorph/generators/GeneratorContext.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,23 @@ | ||
package com.databricks.labs.remorph.generators | ||
|
||
case class GeneratorContext( | ||
maxLineWidth: Int = 120, | ||
private val indent: Int = 0, | ||
private val layer: Int = 0, | ||
private val joins: Int = 0, | ||
wrapLiteral: Boolean = true) { | ||
def nest: GeneratorContext = | ||
GeneratorContext(maxLineWidth = maxLineWidth, joins = joins, layer = layer, indent = indent + 1) | ||
|
||
def ws: String = " " * indent | ||
|
||
def subQuery: GeneratorContext = | ||
GeneratorContext(maxLineWidth = maxLineWidth, joins = joins, layer = layer + 1, indent = indent + 1) | ||
|
||
def layerName: String = s"layer_$layer" | ||
|
||
def withRawLiteral: GeneratorContext = | ||
GeneratorContext(maxLineWidth = maxLineWidth, joins = joins, indent = indent, layer = layer, wrapLiteral = false) | ||
|
||
def hasJoins: Boolean = joins > 0 | ||
} |
67 changes: 67 additions & 0 deletions
67
core/src/main/scala/com/databricks/labs/remorph/generators/sql/ExpressionGenerator.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,67 @@ | ||
package com.databricks.labs.remorph.generators.sql | ||
|
||
import com.databricks.labs.remorph.generators.GeneratorContext | ||
import com.databricks.labs.remorph.parsers.intermediate.Literal | ||
import com.databricks.labs.remorph.parsers.{intermediate => ir} | ||
|
||
import java.text.SimpleDateFormat | ||
import java.util.Locale | ||
|
||
class ExpressionGenerator { | ||
private val dateFormat = new SimpleDateFormat("yyyy-MM-dd") | ||
private val timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") | ||
|
||
def expression(ctx: GeneratorContext, expr: ir.Expression): String = { | ||
expr match { | ||
case l: ir.Literal => literal(ctx, l) | ||
case _ => throw new IllegalArgumentException(s"Unsupported expression: $expr") | ||
} | ||
} | ||
|
||
private def literal(ctx: GeneratorContext, l: Literal): String = { | ||
l.dataType match { | ||
case ir.NullType => "NULL" | ||
case ir.BinaryType => orNull(l.binary.map(_.map("%02X" format _).mkString)) | ||
case ir.BooleanType => orNull(l.boolean.map(_.toString.toUpperCase(Locale.getDefault))) | ||
case ir.ShortType => orNull(l.short.map(_.toString)) | ||
case ir.IntegerType => orNull(l.integer.map(_.toString)) | ||
case ir.LongType => orNull(l.long.map(_.toString)) | ||
case ir.FloatType => orNull(l.float.map(_.toString)) | ||
case ir.DoubleType => orNull(l.double.map(_.toString)) | ||
case ir.StringType => orNull(l.string.map(doubleQuote)) | ||
case ir.DateType => | ||
l.date match { | ||
case Some(date) => doubleQuote(dateFormat.format(date)) | ||
case None => "NULL" | ||
} | ||
case ir.TimestampType => | ||
l.timestamp match { | ||
case Some(timestamp) => doubleQuote(timeFormat.format(timestamp)) | ||
case None => "NULL" | ||
} | ||
case ir.ArrayType(_) => orNull(l.array.map(arrayExpr(ctx))) | ||
case ir.MapType(_, _) => orNull(l.map.map(mapExpr(ctx))) | ||
case _ => throw new IllegalArgumentException(s"Unsupported expression: ${l.dataType}") | ||
} | ||
} | ||
|
||
private def mapExpr(ctx: GeneratorContext)(map: ir.MapExpr): String = { | ||
val entries = map.keys.zip(map.values).map { case (key, value) => | ||
s"${literal(ctx, key)}, ${expression(ctx, value)}" | ||
} | ||
// TODO: line-width formatting | ||
s"MAP(${entries.mkString(", ")})" | ||
} | ||
|
||
private def arrayExpr(ctx: GeneratorContext)(array: ir.ArrayExpr): String = { | ||
val elements = array.elements.map { element => | ||
expression(ctx, element) | ||
} | ||
// TODO: line-width formatting | ||
s"ARRAY(${elements.mkString(", ")})" | ||
} | ||
|
||
private def orNull(option: Option[String]): String = option.getOrElse("NULL") | ||
|
||
private def doubleQuote(s: String): String = s""""$s"""" | ||
} |
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
51 changes: 51 additions & 0 deletions
51
core/src/test/scala/com/databricks/labs/remorph/generators/sql/ExpressionGeneratorTest.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,51 @@ | ||
package com.databricks.labs.remorph.generators.sql | ||
|
||
import com.databricks.labs.remorph.generators.GeneratorContext | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
import org.scalatestplus.mockito.MockitoSugar | ||
import com.databricks.labs.remorph.parsers.{intermediate => ir} | ||
|
||
class ExpressionGeneratorTest extends AnyWordSpec with Matchers with MockitoSugar { | ||
private def generate(expr: ir.Expression): String = { | ||
new ExpressionGenerator().expression(new GeneratorContext(), expr) | ||
} | ||
|
||
"literal" should { | ||
"be generated" in { | ||
generate(ir.Literal()) shouldBe "NULL" | ||
|
||
generate(ir.Literal(binary = Some(Array(0x01, 0x02, 0x03)))) shouldBe "010203" | ||
|
||
generate(ir.Literal(boolean = Some(true))) shouldBe "TRUE" | ||
|
||
generate(ir.Literal(short = Some(123))) shouldBe "123" | ||
|
||
generate(ir.Literal(integer = Some(123))) shouldBe "123" | ||
|
||
generate(ir.Literal(long = Some(123))) shouldBe "123" | ||
|
||
generate(ir.Literal(float = Some(123.4f))) shouldBe "123.4" | ||
|
||
generate(ir.Literal(double = Some(123.4))) shouldBe "123.4" | ||
|
||
generate(ir.Literal(string = Some("abc"))) shouldBe "\"abc\"" | ||
|
||
generate(ir.Literal(date = Some(1721757801000L))) shouldBe "\"2024-07-23\"" | ||
|
||
generate(ir.Literal(timestamp = Some(1721757801000L))) shouldBe "\"2024-07-23 18:03:21.000\"" | ||
} | ||
|
||
"arrays" in { | ||
generate(ir.Literal(Seq(ir.Literal("abc"), ir.Literal("def")))) shouldBe "ARRAY(\"abc\", \"def\")" | ||
} | ||
|
||
"maps" in { | ||
generate( | ||
ir.Literal( | ||
Map( | ||
"foo" -> ir.Literal("bar"), | ||
"baz" -> ir.Literal("qux")))) shouldBe "MAP(\"foo\", \"bar\", \"baz\", \"qux\")" | ||
} | ||
} | ||
} |
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