<commit_before>{-# LANGUAGE TemplateHaskell #-}

module Database.DSH.Optimizer.VL.Properties.Empty where

import Control.Monad
  
import Database.DSH.VL.Lang

import Database.DSH.Optimizer.VL.Properties.Types
import Database.DSH.Optimizer.VL.Properties.Common
  
unp :: Show a => VectorProp a -> Either String a
unp = unpack "Properties.Empty"
                   
mapUnp :: Show a => VectorProp a
          -> VectorProp a 
          -> (a -> a -> VectorProp a) 
          -> Either String (VectorProp a)
mapUnp = mapUnpack "Properties.Empty"  

inferEmptyNullOp :: NullOp -> Either String (VectorProp Bool)
inferEmptyNullOp op =
  case op of
    SingletonDescr     -> Right $ VProp False
    Lit (_, _, [])     -> Right $ VProp True
    Lit (_, _, _)      -> Right $ VProp False
    TableRef (_, _, _) -> Right $ VProp False
    
inferEmptyUnOp :: VectorProp Bool -> UnOp -> Either String (VectorProp Bool)
inferEmptyUnOp e op =
  case op of
    WinFun _       -> Right e
    UniqueS         -> Right e
    Aggr _          -> Right $ VProp False
    AggrNonEmpty _  -> Right $ VProp False
    UnboxRename     -> Right e
    Segment         -> Right e
    Unsegment       -> Right e
    Reverse         -> let ue = unp e in liftM2 VPropPair ue ue
    ReverseS        -> let ue = unp e in liftM2 VPropPair ue ue
    Project _       -> Right e
    Select _        -> let ue = unp e in liftM2 VPropPair ue ue
    SortS _         -> let ue = unp e in liftM2 VPropPair ue ue
    GroupS _        -> let ue = unp e in liftM2 VPropPair ue ue

    -- FIXME this documents the current implementation behaviour, not
    -- what _should_ happen!
    ReshapeS _ -> let ue = unp e in liftM2 VPropPair ue ue
    Reshape _ -> let ue = unp e in liftM2 VPropPair ue ue
    Transpose -> let ue = unp e in liftM2 VPropPair ue ue

    SelectPos1{} -> let ue = unp e in liftM3 VPropTriple ue ue ue
    SelectPos1S{} -> let ue = unp e in liftM3 VPropTriple ue ue ue
    -- FIXME think about it: what happens if we feed an empty vector into the aggr operator?
    GroupAggr (_, _) -> Right $ VProp False
    Number -> Right e
    NumberS -> Right e
    AggrNonEmptyS _ -> return $ VProp False
  
    R1 -> 
      case e of
        VProp _           -> Left "Properties.Empty: not a pair/triple"
        VPropPair b _     -> Right $ VProp b
        VPropTriple b _ _ -> Right $ VProp b
    R2 ->
      case e of
        VProp _           -> Left "Properties.Empty: not a pair/triple"
        VPropPair _ b     -> Right $ VProp b
        VPropTriple _ b _ -> Right $ VProp b
    R3 ->
      case e of
        VPropTriple _ _ b -> Right $ VProp b
        p                 -> Left ("Properties.Empty: not a triple" ++ show p)

    <commit_msg>Bugfix in VL Empty property inference.<commit_after>{-# LANGUAGE TemplateHaskell #-}

module Database.DSH.Optimizer.VL.Properties.Empty where

import Control.Monad
  
import Database.DSH.VL.Lang

import Database.DSH.Optimizer.VL.Properties.Types
import Database.DSH.Optimizer.VL.Properties.Common
  
unp :: Show a => VectorProp a -> Either String a
unp = unpack "Properties.Empty"
                   
mapUnp :: Show a => VectorProp a
          -> VectorProp a 
          -> (a -> a -> VectorProp a) 
          -> Either String (VectorProp a)
mapUnp = mapUnpack "Properties.Empty"  

inferEmptyNullOp :: NullOp -> Either String (VectorProp Bool)
inferEmptyNullOp op =
  case op of
    SingletonDescr     -> Right $ VProp False
    Lit (_, _, [])     -> Right $ VProp True
    Lit (_, _, _)      -> Right $ VProp False
    TableRef (_, _, _) -> Right $ VProp False
    
inferEmptyUnOp :: VectorProp Bool -> UnOp -> Either String (VectorProp Bool)
inferEmptyUnOp e op =
  case op of
    WinFun _       -> Right e
    UniqueS         -> Right e
    Aggr _          -> Right $ VProp False
    AggrNonEmpty _  -> Right $ VProp False
    UnboxRename     -> Right e
    Segment         -> Right e
    Unsegment       -> Right e
    Reverse         -> let ue = unp e in liftM2 VPropPair ue ue
    ReverseS        -> let ue = unp e in liftM2 VPropPair ue ue
    Project _       -> Right e
    Select _        -> let ue = unp e in liftM2 VPropPair ue ue
    SortS _         -> let ue = unp e in liftM2 VPropPair ue ue
    GroupS _        -> let ue = unp e in liftM3 VPropTriple ue ue ue

    -- FIXME this documents the current implementation behaviour, not
    -- what _should_ happen!
    ReshapeS _ -> let ue = unp e in liftM2 VPropPair ue ue
    Reshape _ -> let ue = unp e in liftM2 VPropPair ue ue
    Transpose -> let ue = unp e in liftM2 VPropPair ue ue

    SelectPos1{} -> let ue = unp e in liftM3 VPropTriple ue ue ue
    SelectPos1S{} -> let ue = unp e in liftM3 VPropTriple ue ue ue
    -- FIXME think about it: what happens if we feed an empty vector into the aggr operator?
    GroupAggr (_, _) -> Right $ VProp False
    Number -> Right e
    NumberS -> Right e
    AggrNonEmptyS _ -> return $ VProp False
  
    R1 -> 
      case e of
        VProp _           -> Left "Properties.Empty: not a pair/triple"
        VPropPair b _     -> Right $ VProp b
        VPropTriple b _ _ -> Right $ VProp b
    R2 ->
      case e of
        VProp _           -> Left "Properties.Empty: not a pair/triple"
        VPropPair _ b     -> Right $ VProp b
        VPropTriple _ b _ -> Right $ VProp b
    R3 ->
      case e of
        VPropTriple _ _ b -> Right $ VProp b
        p                 -> Left ("Properties.Empty: not a triple" ++ show p)

    
{'fraction_non_alphanumeric': 0.08147198828268033, 'fraction_numerical': 0.005492493592090809, 'mean_word_length': 1.8904761904761904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4036578', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1761, 'n_words': 626}