<commit_before>module Optimizer.VL.Properties.Const where

import Data.List

import Optimizer.VL.Properties.Common
import Optimizer.VL.Properties.Types
import Database.Algebra.VL.Data
  
unp :: Show a => VectorProp a -> Either String a
unp = unpack "Properties.Const"

mapUnp :: Show a => VectorProp a
          -> VectorProp a 
          -> (a -> a -> VectorProp a) 
          -> Either String (VectorProp a)
mapUnp = mapUnpack "Properties.Empty"  
         
fromDBV :: ConstVec -> Either String (ConstDescr, [ConstPayload])
fromDBV (DBVConst d ps)   = Right (d, ps)
fromDBV (DescrVecConst d) = Right (d, [])
fromDBV x                 = Left $ "Properties.Const fromDBV " ++ (show x)

fromDBP :: ConstVec -> Either String [ConstPayload]
fromDBP (DBPConst ps) = Right ps
fromDBP _             = Left "Properties.Const fromDBP"
         
fromDescrVec :: ConstVec -> Either String ConstDescr
fromDescrVec (DescrVecConst d) = Right d
fromDescrVec _                 = Left "Properties.Const fromDescrVec"
               
fromRenameVec :: ConstVec -> Either String (SourceConstDescr, TargetConstDescr)
fromRenameVec (RenameVecConst s t) = Right (s, t)
fromRenameVec x                    = Left ("Properties.Const fromRenameVec " ++ (show x))

fromPropVec :: ConstVec -> Either String (SourceConstDescr, TargetConstDescr)
fromPropVec (PropVecConst s t)  = Right (s, t)
fromPropVec _                   = Left "Properties.Const fromPropVec"


inferConstVecNullOp :: NullOp -> Either String (VectorProp ConstVec)
inferConstVecNullOp op = 
  case op of
    SingletonDescr                    -> return $ VProp $ DescrVecConst $ ConstDescr $ N 1
    -- do not include the first two columns in the payload columns because they represent descr and pos.
    ConstructLiteralTable _ rows      -> return $ VProp $ DBVConst (ConstDescr $ N 1) constCols
      where constCols       = map col $ drop 2 $ transpose rows
            col col@(c : _) = if all (c ==) col 
                              then ConstPL c 
                              else NonConstPL
    ConstructLiteralValue _ vals      -> return $ VProp $ DBPConst $ map ConstPL $ drop 2 vals
    TableRef              _ cols _    -> return $ VProp $ DBVConst (ConstDescr $ N 1) $ map (const NonConstPL) cols
  <commit_msg>Fixed stupid bug in Const property inference: Provide information about columns, even if it's an empty LiteralTable.<commit_after>module Optimizer.VL.Properties.Const where

import Data.List

import Optimizer.VL.Properties.Common
import Optimizer.VL.Properties.Types
import Database.Algebra.VL.Data
  
unp :: Show a => VectorProp a -> Either String a
unp = unpack "Properties.Const"

mapUnp :: Show a => VectorProp a
          -> VectorProp a 
          -> (a -> a -> VectorProp a) 
          -> Either String (VectorProp a)
mapUnp = mapUnpack "Properties.Empty"  
         
fromDBV :: ConstVec -> Either String (ConstDescr, [ConstPayload])
fromDBV (DBVConst d ps)   = Right (d, ps)
fromDBV (DescrVecConst d) = Right (d, [])
fromDBV x                 = Left $ "Properties.Const fromDBV " ++ (show x)

fromDBP :: ConstVec -> Either String [ConstPayload]
fromDBP (DBPConst ps) = Right ps
fromDBP _             = Left "Properties.Const fromDBP"
         
fromDescrVec :: ConstVec -> Either String ConstDescr
fromDescrVec (DescrVecConst d) = Right d
fromDescrVec _                 = Left "Properties.Const fromDescrVec"
               
fromRenameVec :: ConstVec -> Either String (SourceConstDescr, TargetConstDescr)
fromRenameVec (RenameVecConst s t) = Right (s, t)
fromRenameVec x                    = Left ("Properties.Const fromRenameVec " ++ (show x))

fromPropVec :: ConstVec -> Either String (SourceConstDescr, TargetConstDescr)
fromPropVec (PropVecConst s t)  = Right (s, t)
fromPropVec _                   = Left "Properties.Const fromPropVec"


inferConstVecNullOp :: NullOp -> Either String (VectorProp ConstVec)
inferConstVecNullOp op = 
  case op of
    SingletonDescr                    -> return $ VProp $ DescrVecConst $ ConstDescr $ N 1
    -- do not include the first two columns in the payload columns because they represent descr and pos.
    ConstructLiteralTable colTypes rows      -> 
      if null rows
      then return $ VProp $ DBVConst NonConstDescr $ map (const NonConstPL) colTypes
      else return $ VProp $ DBVConst (ConstDescr $ N 1) constCols
        where constCols       = map col $ drop 2 $ transpose rows
              col col@(c : _) = if all (c ==) col 
                                then ConstPL c 
                                else NonConstPL
    ConstructLiteralValue colTypes vals      -> 
      if null vals
      then return $ VProp $ DBPConst $ map (const NonConstPL) colTypes
      else return $ VProp $ DBPConst $ map ConstPL $ drop 2 vals
    TableRef              _ cols _    -> return $ VProp $ DBVConst (ConstDescr $ N 1) $ map (const NonConstPL) cols
  
{'fraction_non_alphanumeric': 0.08290580751077366, 'fraction_numerical': 0.0020521239482864766, 'mean_word_length': 2.4939068100358424, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, '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': '2275693', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1393, 'n_words': 473}