Skip to content

Commit

Permalink
backend/clickhouse: subcolumns synonyms
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-bodavskiy committed Dec 4, 2024
1 parent b4e418b commit 9fe341b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,3 @@ zipColumnsWithSynonyms6 (c1, c2, c3, c4, c5, c6) = zipColumns [showColumn c1, sh

zipColumns :: [String] -> SubQueryLevel -> String
zipColumns columns l = List.intercalate ", " $ zipWith (\n column -> column <> " as " <> getColumnSynonym n l) [1 ..] columns

getColumnSynonym :: ColumnNumber -> SubQueryLevel -> String
getColumnSynonym n 0 = "res" <> show n
getColumnSynonym n l = "res" <> show n <> "_sub" <> show l
56 changes: 30 additions & 26 deletions lib/mobility-core/src/Kernel/Storage/ClickhouseV2/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ instance {-# OVERLAPPING #-} (ClickhouseValue v, Num v) => ClickhouseNum (Maybe
data Column (a :: IsAggregated) t v where
Column :: (ClickhouseTable t) => FieldModification t v -> Column 'NOT_AGG t v -- initial column
Group :: (ClickhouseTable t, ClickhouseValue v) => Column 'NOT_AGG t v -> Column 'AGG t v -- column from groupBy clause
ResetGroup :: (ClickhouseTable t, ClickhouseValue v) => Column a t v -> Column 'NOT_AGG t v -- required for subqueries
SubColumn :: (ClickhouseTable t, ClickhouseValue v) => Column a t v -> ColumnNumber -> SubQueryLevel -> Column 'NOT_AGG t v -- column synonym will be generated based on these two guys SubQueryLevel and ColumnNumber
Sum :: (ClickhouseTable t, ClickhouseNum v) => Column 'NOT_AGG t v -> Column 'AGG t v
Count :: (ClickhouseTable t, ClickhouseValue v, ClickhouseValue Int) => Column 'NOT_AGG t v -> Column 'AGG t Int
Distinct :: (ClickhouseTable t, ClickhouseValue v) => Column a t v -> Column a t v -- should not be used in where clause
Expand Down Expand Up @@ -118,33 +118,33 @@ instance (ClickhouseTable t, C6 ClickhouseValue v1 v2 v3 v4 v5 v6) => IsGroupCol
groupColumns (c1, c2, c3, c4, c5, c6) = (Group @t @v1 c1, Group @t @v2 c2, Group @t @v3 c3, Group @t @v4 c4, Group @t @v5 c5, Group @t @v6 c6)

-- we need to reset group columns for subqueries, thus we can group twice, first time in subquery, second time in main query
class ResetGroupColumns cols where
type ResetGroupColumnsType cols
resetGroupColumns :: cols -> ResetGroupColumnsType cols
class MkSubColumns cols where
type SubColumnsType cols
subColumnsValue :: cols -> SubQueryLevel -> SubColumnsType cols

instance (ClickhouseTable t, ClickhouseValue v) => ResetGroupColumns (Column a t v) where
type ResetGroupColumnsType (Column a t v) = Column 'NOT_AGG t v
resetGroupColumns = ResetGroup @t @v
instance (ClickhouseTable t, ClickhouseValue v) => MkSubColumns (Column a t v) where
type SubColumnsType (Column a t v) = Column 'NOT_AGG t v
subColumnsValue c l = SubColumn @t @v c 1 l

instance (ClickhouseTable t, C2 ClickhouseValue v1 v2) => ResetGroupColumns (T2 (Column a t) v1 v2) where
type ResetGroupColumnsType (T2 (Column a t) v1 v2) = (T2 (Column 'NOT_AGG t) v1 v2)
resetGroupColumns (c1, c2) = (ResetGroup @t @v1 c1, ResetGroup @t @v2 c2)
instance (ClickhouseTable t, C2 ClickhouseValue v1 v2) => MkSubColumns (T2 (Column a t) v1 v2) where
type SubColumnsType (T2 (Column a t) v1 v2) = (T2 (Column 'NOT_AGG t) v1 v2)
subColumnsValue (c1, c2) l = (SubColumn @t @v1 c1 1 l, SubColumn @t @v2 c2 2 l)

instance (ClickhouseTable t, C3 ClickhouseValue v1 v2 v3) => ResetGroupColumns (T3 (Column a t) v1 v2 v3) where
type ResetGroupColumnsType (T3 (Column a t) v1 v2 v3) = (T3 (Column 'NOT_AGG t) v1 v2 v3)
resetGroupColumns (c1, c2, c3) = (ResetGroup @t @v1 c1, ResetGroup @t @v2 c2, ResetGroup @t @v3 c3)
instance (ClickhouseTable t, C3 ClickhouseValue v1 v2 v3) => MkSubColumns (T3 (Column a t) v1 v2 v3) where
type SubColumnsType (T3 (Column a t) v1 v2 v3) = (T3 (Column 'NOT_AGG t) v1 v2 v3)
subColumnsValue (c1, c2, c3) l = (SubColumn @t @v1 c1 1 l, SubColumn @t @v2 c2 2 l, SubColumn @t @v3 c3 3 l)

instance (ClickhouseTable t, C4 ClickhouseValue v1 v2 v3 v4) => ResetGroupColumns (T4 (Column a t) v1 v2 v3 v4) where
type ResetGroupColumnsType (T4 (Column a t) v1 v2 v3 v4) = (T4 (Column 'NOT_AGG t) v1 v2 v3 v4)
resetGroupColumns (c1, c2, c3, c4) = (ResetGroup @t @v1 c1, ResetGroup @t @v2 c2, ResetGroup @t @v3 c3, ResetGroup @t @v4 c4)
instance (ClickhouseTable t, C4 ClickhouseValue v1 v2 v3 v4) => MkSubColumns (T4 (Column a t) v1 v2 v3 v4) where
type SubColumnsType (T4 (Column a t) v1 v2 v3 v4) = (T4 (Column 'NOT_AGG t) v1 v2 v3 v4)
subColumnsValue (c1, c2, c3, c4) l = (SubColumn @t @v1 c1 1 l, SubColumn @t @v2 c2 2 l, SubColumn @t @v3 c3 3 l, SubColumn @t @v4 c4 4 l)

instance (ClickhouseTable t, C5 ClickhouseValue v1 v2 v3 v4 v5) => ResetGroupColumns (T5 (Column a t) v1 v2 v3 v4 v5) where
type ResetGroupColumnsType (T5 (Column a t) v1 v2 v3 v4 v5) = (T5 (Column 'NOT_AGG t) v1 v2 v3 v4 v5)
resetGroupColumns (c1, c2, c3, c4, c5) = (ResetGroup @t @v1 c1, ResetGroup @t @v2 c2, ResetGroup @t @v3 c3, ResetGroup @t @v4 c4, ResetGroup @t @v5 c5)
instance (ClickhouseTable t, C5 ClickhouseValue v1 v2 v3 v4 v5) => MkSubColumns (T5 (Column a t) v1 v2 v3 v4 v5) where
type SubColumnsType (T5 (Column a t) v1 v2 v3 v4 v5) = (T5 (Column 'NOT_AGG t) v1 v2 v3 v4 v5)
subColumnsValue (c1, c2, c3, c4, c5) l = (SubColumn @t @v1 c1 1 l, SubColumn @t @v2 c2 2 l, SubColumn @t @v3 c3 3 l, SubColumn @t @v4 c4 4 l, SubColumn @t @v5 c5 5 l)

instance (ClickhouseTable t, C6 ClickhouseValue v1 v2 v3 v4 v5 v6) => ResetGroupColumns (T6 (Column a t) v1 v2 v3 v4 v5 v6) where
type ResetGroupColumnsType (T6 (Column a t) v1 v2 v3 v4 v5 v6) = (T6 (Column 'NOT_AGG t) v1 v2 v3 v4 v5 v6)
resetGroupColumns (c1, c2, c3, c4, c5, c6) = (ResetGroup @t @v1 c1, ResetGroup @t @v2 c2, ResetGroup @t @v3 c3, ResetGroup @t @v4 c4, ResetGroup @t @v5 c5, ResetGroup @t @v6 c6)
instance (ClickhouseTable t, C6 ClickhouseValue v1 v2 v3 v4 v5 v6) => MkSubColumns (T6 (Column a t) v1 v2 v3 v4 v5 v6) where
type SubColumnsType (T6 (Column a t) v1 v2 v3 v4 v5 v6) = (T6 (Column 'NOT_AGG t) v1 v2 v3 v4 v5 v6)
subColumnsValue (c1, c2, c3, c4, c5, c6) l = (SubColumn @t @v1 c1 1 l, SubColumn @t @v2 c2 2 l, SubColumn @t @v3 c3 3 l, SubColumn @t @v4 c4 4 l, SubColumn @t @v5 c5 5 l, SubColumn @t @v6 c6 6 l)

data NotGrouped

Expand Down Expand Up @@ -201,8 +201,8 @@ instance HasAvailableColumns (AllColumns db table) where
availableColumnsValue (AllColumns cols) = cols

instance HasAvailableColumns (SubSelectColumns db table subcols) where
type AvailableColumnsType (SubSelectColumns db table subcols) = ResetGroupColumnsType subcols
availableColumnsValue (SubSelectColumns (Select subcols _ _)) = resetGroupColumns subcols
type AvailableColumnsType (SubSelectColumns db table subcols) = SubColumnsType subcols
availableColumnsValue (SubSelectColumns (Select subcols _ q)) = subColumnsValue subcols q.subQueryLevelQ

getAvailableColumnsValue ::
AvailableColumns db table acols ->
Expand All @@ -216,7 +216,7 @@ data AllColumns db table where
AllColumns :: (ClickhouseDb db, ClickhouseTable table) => Columns 'NOT_AGG table -> AllColumns db table

data SubSelectColumns db table subcols where
SubSelectColumns :: (ClickhouseDb db, ClickhouseTable table, ClickhouseQuery (Select a db table subcols gr ord acols), ResetGroupColumns subcols) => Select a db table subcols gr ord acols -> SubSelectColumns db table subcols
SubSelectColumns :: (ClickhouseDb db, ClickhouseTable table, ClickhouseQuery (Select a db table subcols gr ord acols), MkSubColumns subcols) => Select a db table subcols gr ord acols -> SubSelectColumns db table subcols

type AvailableAllColumns db table = AvailableColumns db table (AllColumns db table)

Expand All @@ -225,7 +225,7 @@ type AvailableSubSelectColumns db table subcols = AvailableColumns db table (Sub
showColumn :: Column a t v -> String
showColumn (Column column) = getFieldModification column
showColumn (Group column) = showColumn column
showColumn (ResetGroup column) = showColumn column
showColumn (SubColumn _column n l) = getColumnSynonym n l
showColumn (Sum column) = "SUM" <> addBrackets' (showColumn column)
showColumn (Count column) = "COUNT" <> addBrackets' (showColumn column)
showColumn (Distinct column) = "DISTINCT" <> addBrackets' (showColumn column)
Expand Down Expand Up @@ -267,3 +267,7 @@ newtype SubQueryLevel = SubQueryLevel {getSubQueryLevel :: Int}

newtype ColumnNumber = ColumnNumber {getColumnNumber :: Int}
deriving newtype (Show, Num, Enum, Eq)

getColumnSynonym :: ColumnNumber -> SubQueryLevel -> String
getColumnSynonym n 0 = "res" <> show n
getColumnSynonym n l = "res" <> show n <> "_sub" <> show l
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ subSelect_ ::
( ClickhouseDb db,
ClickhouseTable table,
ClickhouseQuery (Select a db table subcols gr ord acols),
ResetGroupColumns subcols
MkSubColumns subcols
) =>
Select a db table subcols gr ord acols ->
(AvailableSubSelectColumns db table subcols, SubQueryLevel)
Expand Down

0 comments on commit 9fe341b

Please sign in to comment.