Adds coalescing to default values for dimension table columns.

pull/1/head
Abhinav Sarkar 2015-12-24 17:42:47 +05:30
parent ff4ca5e235
commit b994955399
1 changed files with 42 additions and 20 deletions

View File

@ -45,24 +45,25 @@ constraintDefnSQL Table {..} constraint =
ForeignKey oTableName cNamePairs -> ForeignKey oTableName cNamePairs ->
[ alterTableSQL <> "FOREIGN KEY (" <> colNamesString (map fst cNamePairs) <> ") REFERENCES " [ alterTableSQL <> "FOREIGN KEY (" <> colNamesString (map fst cNamePairs) <> ") REFERENCES "
<> oTableName <> " (" <> colNamesString (map snd cNamePairs) <> ")" ] <> oTableName <> " (" <> colNamesString (map snd cNamePairs) <> ")" ]
UniqueKey cNames -> let UniqueKey cNames -> ["CREATE UNIQUE INDEX ON " <> tableName <> "(" <> colNamesString cNames <> ")"]
(notNullCols, nullCols) = -- let
both (map columnName) -- (notNullCols, nullCols) =
$ partition ((== NotNull) . columnNullable) -- both (map columnName)
$ catMaybes [ findColumn cName tableColumns | cName <- cNames ] -- $ partition ((== NotNull) . columnNullable)
combinations = -- $ catMaybes [ findColumn cName tableColumns | cName <- cNames ]
map (\cs -> (cs, [ c | c <- nullCols, c `notElem` cs ])) -- combinations =
. sortBy (comparing length) -- map (\cs -> (cs, [ c | c <- nullCols, c `notElem` cs ]))
$ subsequences nullCols -- . sortBy (comparing length)
in [ "CREATE UNIQUE INDEX ON " <> tableName -- $ subsequences nullCols
<> " (" <> colNamesString (notNullCols ++ nnCols) <> ")" -- in [ "CREATE UNIQUE INDEX ON " <> tableName
<> if null whereClauses -- <> " (" <> colNamesString (notNullCols ++ nnCols) <> ")"
then "" -- <> if null whereClauses
else "\nWHERE "<> Text.intercalate "\nAND " whereClauses -- then ""
| (nnCols, nCols) <- combinations -- else "\nWHERE "<> Text.intercalate "\nAND " whereClauses
, not $ null (notNullCols ++ nnCols) -- | (nnCols, nCols) <- combinations
, let whereClauses = -- , not $ null (notNullCols ++ nnCols)
[ c <> " IS NOT NULL" | c <- nnCols ] ++ [ c <> " IS NULL" | c <- nCols ] ] -- , let whereClauses =
-- [ c <> " IS NOT NULL" | c <- nnCols ] ++ [ c <> " IS NULL" | c <- nCols ] ]
tableDefnSQL :: Table -> [Text] tableDefnSQL :: Table -> [Text]
tableDefnSQL table@Table {..} = tableDefnSQL table@Table {..} =
@ -95,11 +96,32 @@ dimColumnMapping dimPrefix fact dimTableName =
[ (dimColumnName dName cName, cName) [ (dimColumnName dName cName, cName)
| DimVal dName cName <- factColumns fact , dimPrefix <> dName == dimTableName] | DimVal dName cName <- factColumns fact , dimPrefix <> dName == dimTableName]
coalesceColumn :: Column -> Text
coalesceColumn Column{..} =
if columnNullable == Null
then "coalesce(" <> columnName <> "," <> defVal columnType <> ")"
else columnName
where
defVal colType
| "integer" `Text.isPrefixOf` colType = "-42"
| "timestamp" `Text.isPrefixOf` colType = "'00-00-00 00:00:00'"
| "character" `Text.isPrefixOf` colType = "'XXX_UNKNOWN_'"
| "uuid" `Text.isPrefixOf` colType = "'00000000-0000-0000-0000-000000000000'::uuid"
| "boolean" `Text.isPrefixOf` colType = "false"
| otherwise = error $ "Unknown column type: " ++ Text.unpack colType
dimensionTablePopulateSQL :: TablePopulationMode -> Fact -> TableName -> Reader Env Text dimensionTablePopulateSQL :: TablePopulationMode -> Fact -> TableName -> Reader Env Text
dimensionTablePopulateSQL popMode fact dimTableName = do dimensionTablePopulateSQL popMode fact dimTableName = do
dimPrefix <- settingDimPrefix <$> asks envSettings dimPrefix <- settingDimPrefix <$> asks envSettings
let colMapping = dimColumnMapping dimPrefix fact dimTableName tables <- asks envTables
baseSelectC = "SELECT DISTINCT\n" <> colNamesString (map snd colMapping) <> "\n" let factTable = fromJust $ findTable (factTableName fact) tables
colMapping = dimColumnMapping dimPrefix fact dimTableName
baseSelectC = "SELECT DISTINCT\n"
<> colNamesString
(map (\(_, c) ->
coalesceColumn . fromJust . findColumn c $ (tableColumns factTable))
colMapping)
<> "\n"
<> "FROM " <> factTableName fact <> "FROM " <> factTableName fact
insertC selectC = "INSERT INTO " <> dimTableName insertC selectC = "INSERT INTO " <> dimTableName
<> " (\n" <> colNamesString (map fst colMapping) <> "\n) " <> " (\n" <> colNamesString (map fst colMapping) <> "\n) "