<commit_before>module Database.DSH.Flattening.Optimizer.VL.Properties.ReqColumns where

import           Data.List
import           Database.Algebra.VL.Data
import Database.DSH.Flattening.Optimizer.VL.Properties.Types

unp :: Show a => VectorProp a -> a
unp (VProp x) = x
unp x         = error $ "ReqColumns.unp " ++ (show x)

colUnion :: VectorProp ReqCols -> VectorProp ReqCols -> VectorProp ReqCols
colUnion (VProp (Just cols1)) (VProp (Just cols2)) = VProp $ Just $ cols1 `union` cols2
colUnion (VProp (Just cols1)) (VProp Nothing)      = VProp $ Just $ cols1
colUnion (VProp Nothing)      (VProp (Just cols2)) = VProp $ Just $ cols2
colUnion (VProp Nothing)      (VProp Nothing)      = VProp $ Nothing
colUnion p1                   p2                   = error ("ReqColumns.colUnion" ++ " " ++ (show p1) ++ " " ++ (show p2))

none :: VectorProp ReqCols
none = VProp $ Just []

one :: VectorProp ReqCols
one = VProp $ Just [1]

na :: VectorProp ReqCols
na = VProp Nothing

reqProjCols :: [PayloadProj] -> [DBCol]
reqProjCols ((PLCol col) : ps) = col : (reqProjCols ps)
reqProjCols ((PLConst _) : ps) = reqProjCols ps
reqProjCols []                 = []

reqExpr1Cols :: Expr1 -> [DBCol]
reqExpr1Cols (App1 _ e1 e2) = reqExpr1Cols e1 `union` reqExpr1Cols e2
reqExpr1Cols (Column1 col)  = [col]
reqExpr1Cols (Constant1 _)  = []

reqExpr2ColsLeft :: Expr2 -> [DBCol]
reqExpr2ColsLeft (App2 _ e1 e2)        = reqExpr2ColsLeft e1 `union` reqExpr2ColsLeft e2
reqExpr2ColsLeft (Column2Left (L col)) = [col]
reqExpr2ColsLeft (Column2Right _)      = []
reqExpr2ColsLeft (Constant2 _)         = []

reqExpr2ColsRight :: Expr2 -> [DBCol]
reqExpr2ColsRight (App2 _ e1 e2)         = reqExpr2ColsRight e1 `union` reqExpr2ColsRight e2
reqExpr2ColsRight (Column2Right (R col)) = [col]
reqExpr2ColsRight (Column2Left _)        = []
reqExpr2ColsRight (Constant2 _)          = []

inferReqColumnsUnOp :: VectorProp ReqCols
                -> VectorProp ReqCols
                -> UnOp
                -> VectorProp ReqCols
inferReqColumnsUnOp ownReqColumns childReqColumns op =
  case op of
    Unique -> colUnion ownReqColumns childReqColumns

    UniqueL -> colUnion ownReqColumns childReqColumns

    NotPrim -> one

    NotVec -> one

    LengthA -> colUnion ownReqColumns childReqColumns

    DescToRename -> na

    Segment -> colUnion ownReqColumns childReqColumns
    Unsegment -> colUnion ownReqColumns childReqColumns

    VecSum _ -> one
    VecAvg -> one

    VecMin -> one

    VecMinL -> one

    VecMax -> one
    VecMaxL -> one

    ProjectL cols -> colUnion childReqColumns (VProp $ Just cols)
    ProjectA cols -> colUnion childReqColumns (VProp $ Just cols)

    IntegerToDoubleA -> one
    IntegerToDoubleL -> one

    ReverseA -> colUnion ownReqColumns childReqColumns
    ReverseL -> colUnion ownReqColumns childReqColumns

    FalsePositions -> one

    ProjectRename _ -> none

    ProjectPayload ps -> colUnion childReqColumns (VProp $ Just $ reqProjCols ps)

    ProjectAdmin _ -> colUnion ownReqColumns childReqColumns

    SelectExpr e -> colUnion childReqColumns (VProp $ Just $ reqExpr1Cols e)

    CompExpr1L e -> colUnion childReqColumns (VProp $ Just $ reqExpr1Cols e)

    SelectPos1 _ _   ->
      case ownReqColumns of
        VPropPair cols _ -> colUnion childReqColumns (VProp cols)
        _                           -> error "SelectPos1"

    SelectPos1L _ _   ->
      case ownReqColumns of
        VPropPair cols _ -> colUnion childReqColumns (VProp cols)
        _                -> error "SelectPos1L"
        
    VecAggr gs as -> colUnion childReqColumns (VProp $ Just $ nub $ gs ++ concatMap aggrInputCol as)

      where aggrInputCol :: AggrFun -> [DBCol]
            aggrInputCol (Max c) = [c]
            aggrInputCol (Min c) = [c]
            aggrInputCol (Sum c) = [c]
            aggrInputCol Count   = []

    R1               ->
      case childReqColumns of
        VProp _                       -> error $ "ReqColumns.R1 " ++ (show childReqColumns)
        VPropPair cols1 cols2         -> VPropPair (unp (colUnion (VProp cols1) ownReqColumns)) cols2
        VPropTriple cols1 cols2 cols3 -> VPropTriple (unp (colUnion (VProp cols1) ownReqColumns)) cols2 cols3

    R2               ->
      case childReqColumns of
        VProp _              -> error "ReqColumns.R2"
        VPropPair cols1 cols2      -> VPropPair cols1 (unp (colUnion (VProp cols2) ownReqColumns))
        VPropTriple cols1 cols2 cols3 -> VPropTriple cols1 (unp (colUnion (VProp cols2) ownReqColumns)) cols3
    R3               ->
      case childReqColumns of
        VProp _              -> error "ReqColumns.R3/1"
        VPropPair _ _        -> error "ReqColumns.R3/2"
        VPropTriple cols1 cols2 cols3 -> VPropTriple cols1 cols2 (unp (colUnion (VProp cols3) ownReqColumns))

    Only -> undefined
    Singleton -> undefined

allCols :: BottomUpProps -> VectorProp ReqCols<commit_msg>Fixed missing case (Avg) in VL.Properties.ReqColumns.<commit_after>module Database.DSH.Flattening.Optimizer.VL.Properties.ReqColumns where

import           Data.List
import           Database.Algebra.VL.Data
import Database.DSH.Flattening.Optimizer.VL.Properties.Types

unp :: Show a => VectorProp a -> a
unp (VProp x) = x
unp x         = error $ "ReqColumns.unp " ++ (show x)

colUnion :: VectorProp ReqCols -> VectorProp ReqCols -> VectorProp ReqCols
colUnion (VProp (Just cols1)) (VProp (Just cols2)) = VProp $ Just $ cols1 `union` cols2
colUnion (VProp (Just cols1)) (VProp Nothing)      = VProp $ Just $ cols1
colUnion (VProp Nothing)      (VProp (Just cols2)) = VProp $ Just $ cols2
colUnion (VProp Nothing)      (VProp Nothing)      = VProp $ Nothing
colUnion p1                   p2                   = error ("ReqColumns.colUnion" ++ " " ++ (show p1) ++ " " ++ (show p2))

none :: VectorProp ReqCols
none = VProp $ Just []

one :: VectorProp ReqCols
one = VProp $ Just [1]

na :: VectorProp ReqCols
na = VProp Nothing

reqProjCols :: [PayloadProj] -> [DBCol]
reqProjCols ((PLCol col) : ps) = col : (reqProjCols ps)
reqProjCols ((PLConst _) : ps) = reqProjCols ps
reqProjCols []                 = []

reqExpr1Cols :: Expr1 -> [DBCol]
reqExpr1Cols (App1 _ e1 e2) = reqExpr1Cols e1 `union` reqExpr1Cols e2
reqExpr1Cols (Column1 col)  = [col]
reqExpr1Cols (Constant1 _)  = []

reqExpr2ColsLeft :: Expr2 -> [DBCol]
reqExpr2ColsLeft (App2 _ e1 e2)        = reqExpr2ColsLeft e1 `union` reqExpr2ColsLeft e2
reqExpr2ColsLeft (Column2Left (L col)) = [col]
reqExpr2ColsLeft (Column2Right _)      = []
reqExpr2ColsLeft (Constant2 _)         = []

reqExpr2ColsRight :: Expr2 -> [DBCol]
reqExpr2ColsRight (App2 _ e1 e2)         = reqExpr2ColsRight e1 `union` reqExpr2ColsRight e2
reqExpr2ColsRight (Column2Right (R col)) = [col]
reqExpr2ColsRight (Column2Left _)        = []
reqExpr2ColsRight (Constant2 _)          = []

inferReqColumnsUnOp :: VectorProp ReqCols
                -> VectorProp ReqCols
                -> UnOp
                -> VectorProp ReqCols
inferReqColumnsUnOp ownReqColumns childReqColumns op =
  case op of
    Unique -> colUnion ownReqColumns childReqColumns

    UniqueL -> colUnion ownReqColumns childReqColumns

    NotPrim -> one

    NotVec -> one

    LengthA -> colUnion ownReqColumns childReqColumns

    DescToRename -> na

    Segment -> colUnion ownReqColumns childReqColumns
    Unsegment -> colUnion ownReqColumns childReqColumns

    VecSum _ -> one
    VecAvg -> one

    VecMin -> one

    VecMinL -> one

    VecMax -> one
    VecMaxL -> one

    ProjectL cols -> colUnion childReqColumns (VProp $ Just cols)
    ProjectA cols -> colUnion childReqColumns (VProp $ Just cols)

    IntegerToDoubleA -> one
    IntegerToDoubleL -> one

    ReverseA -> colUnion ownReqColumns childReqColumns
    ReverseL -> colUnion ownReqColumns childReqColumns

    FalsePositions -> one

    ProjectRename _ -> none

    ProjectPayload ps -> colUnion childReqColumns (VProp $ Just $ reqProjCols ps)

    ProjectAdmin _ -> colUnion ownReqColumns childReqColumns

    SelectExpr e -> colUnion childReqColumns (VProp $ Just $ reqExpr1Cols e)

    CompExpr1L e -> colUnion childReqColumns (VProp $ Just $ reqExpr1Cols e)

    SelectPos1 _ _   ->
      case ownReqColumns of
        VPropPair cols _ -> colUnion childReqColumns (VProp cols)
        _                           -> error "SelectPos1"

    SelectPos1L _ _   ->
      case ownReqColumns of
        VPropPair cols _ -> colUnion childReqColumns (VProp cols)
        _                -> error "SelectPos1L"
        
    VecAggr gs as -> colUnion childReqColumns (VProp $ Just $ nub $ gs ++ concatMap aggrInputCol as)

      where aggrInputCol :: AggrFun -> [DBCol]
            aggrInputCol (Max c) = [c]
            aggrInputCol (Min c) = [c]
            aggrInputCol (Sum c) = [c]
            aggrInputCol (Avg c) = [c]
            aggrInputCol Count   = []

    R1               ->
      case childReqColumns of
        VProp _                       -> error $ "ReqColumns.R1 " ++ (show childReqColumns)
        VPropPair cols1 cols2         -> VPropPair (unp (colUnion (VProp cols1) ownReqColumns)) cols2
        VPropTriple cols1 cols2 cols3 -> VPropTriple (unp (colUnion (VProp cols1) ownReqColumns)) cols2 cols3

    R2               ->
      case childReqColumns of
        VProp _              -> error "ReqColumns.R2"
        VPropPair cols1 cols2      -> VPropPair cols1 (unp (colUnion (VProp cols2) ownReqColumns))
        VPropTriple cols1 cols2 cols3 -> VPropTriple cols1 (unp (colUnion (VProp cols2) ownReqColumns)) cols3
    R3               ->
      case childReqColumns of
        VProp _              -> error "ReqColumns.R3/1"
        VPropPair _ _        -> error "ReqColumns.R3/2"
        VPropTriple cols1 cols2 cols3 -> VPropTriple cols1 cols2 (unp (colUnion (VProp cols3) ownReqColumns))

    Only -> undefined
    Singleton -> undefined

allCols :: BottomUpProps -> VectorProp ReqCols
{'fraction_non_alphanumeric': 0.08895178408976044, 'fraction_numerical': 0.02041847771151319, 'mean_word_length': 2.4862579281183934, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3873295', 'n_tokens_mistral': 3316, 'n_tokens_neox': 3238, 'n_words': 864}