Skip to content

Commit

Permalink
Databricks: Quote catalog and schema names in SQL statements (close #…
Browse files Browse the repository at this point in the history
  • Loading branch information
voropaevp committed Dec 1, 2022
1 parent c283f9f commit 4bfe0ff
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ object Databricks {
}

private def qualify(tableName: String): String = tgt.catalog match {
case Some(catalog) => s"${catalog}.${tgt.schema}.$tableName"
case None => s"${tgt.schema}.$tableName"
case Some(catalog) => s"`${catalog}`.`${tgt.schema}`.$tableName"
case None => s"`${tgt.schema}`.$tableName"
}
}
Right(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ object EventsTable {

def withSchema(dbSchema: String, tableName: String): String =
if (dbSchema.isEmpty) tableName
else dbSchema + "." + tableName
else s""""$dbSchema".$tableName"""

def withSchema(dbSchema: String): String =
withSchema(dbSchema, EventsTable.MainName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ object Redshift {
Fragment.const0(str)
case Statement.GetColumns(tableName) =>
sql"""SELECT column_name FROM information_schema.columns
WHERE table_name = $tableName and table_schema = $schema"""
WHERE table_name = $tableName and table_schema = '$schema'"""
case Statement.ManifestAdd(message) =>
val tableName = Fragment.const(qualify(Manifest.Name))
val types = message.types.asJson.noSpaces
Expand Down Expand Up @@ -268,7 +268,7 @@ object Redshift {
}

private def qualify(tableName: String): String =
s"$schema.$tableName"
s""""$schema".$tableName"""
}

Right(result)
Expand Down Expand Up @@ -311,7 +311,7 @@ object Redshift {
/** Add `schema` to otherwise static definition of manifest table */
private def getManifestDef(schema: String): CreateTable =
CreateTable(
s"$schema.${Manifest.Name}",
s""""$schema".${Manifest.Name}""",
ManifestColumns,
Set.empty,
Set(Diststyle(Key), DistKeyTable("base"), SortKeyTable(None, NonEmptyList.one("ingestion_tstamp")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ object Snowflake {
sql"""|SELECT EXISTS (
| SELECT 1
| FROM information_schema.tables
| WHERE TABLE_SCHEMA = ${schema.toUpperCase}
| WHERE TABLE_SCHEMA = '${schema.toUpperCase}'
| AND TABLE_NAME = ${tableName.toUpperCase})""".stripMargin
case Statement.GetVersion(_) =>
throw new IllegalStateException("Snowflake Loader does not support table versioning")
Expand Down Expand Up @@ -282,7 +282,7 @@ object Snowflake {
}

private def qualify(tableName: String): String =
s"$schema.$tableName"
s""""$schema".$tableName"""

private def columnsForCopy(columns: ColumnsToCopy): String = {
val columnNames = columns.names.map(_.value).mkString(",")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object Statements {
val frConstraint = primaryKey.map(c => fr", ${c.toDdl}").getOrElse(Fragment.empty)
val frCols = columns.map(_.toDdl).intercalate(fr",")
val frTemp = if (temporary) Fragment.const("TEMPORARY") else Fragment.empty
val frTableName = Fragment.const0(s"$schema.$name")
val frTableName = Fragment.const0(s""""$schema".$name""")
sql"""CREATE ${frTemp}TABLE IF NOT EXISTS $frTableName (
$frCols$frConstraint
)"""
Expand All @@ -42,7 +42,7 @@ object Statements {
datatype: SnowflakeDatatype
) {
def toFragment: Fragment = {
val frTableName = Fragment.const0(s"$schema.$table")
val frTableName = Fragment.const0(s"""""$schema".$table""")
val frColumn = Fragment.const0(column)
sql"ALTER TABLE $frTableName ADD COLUMN $frColumn ${datatype.toDdl}"
}
Expand Down

0 comments on commit 4bfe0ff

Please sign in to comment.