module Spark.Core.Internal.TypesFunctions(
isNullable,
iInnerStrictType,
columnType,
unsafeCastType,
intType,
arrayType,
arrayType',
frameTypeFromCol,
colTypeFromFrame,
canNull,
structField,
structType,
iSingleField,
) where
import Data.Text as T
import qualified Data.Vector as V
import Spark.Core.Internal.TypesStructures
import Spark.Core.StructuresInternal
unsafeCastType :: SQLType a -> SQLType b
unsafeCastType (SQLType dt) = SQLType dt
columnType :: SQLType a -> DataType
columnType (SQLType dt) = dt
isNullable :: DataType -> Bool
isNullable (StrictType _) = False
isNullable (NullableType _) = True
frameTypeFromCol :: DataType -> StructType
frameTypeFromCol (StrictType (Struct struct)) = struct
frameTypeFromCol dt = _structFromUnfields [("value", dt)]
colTypeFromFrame :: StructType -> DataType
colTypeFromFrame st @ (StructType fs) = case V.toList fs of
[StructField {
structFieldName = fname,
structFieldType = (StrictType dt)}] | fname == "value" ->
StrictType dt
_ -> StrictType (Struct st)
intType :: DataType
intType = StrictType IntType
structField :: T.Text -> DataType -> StructField
structField txt = StructField (FieldName txt)
structType :: [StructField] -> DataType
structType = StrictType . Struct . StructType . V.fromList
arrayType' :: DataType -> DataType
arrayType' = StrictType . ArrayType
canNull :: DataType -> DataType
canNull = NullableType . iInnerStrictType
arrayType :: SQLType a -> SQLType [a]
arrayType (SQLType dt) = SQLType (arrayType' dt)
iInnerStrictType :: DataType -> StrictDataType
iInnerStrictType (StrictType st) = st
iInnerStrictType (NullableType st) = st
iSingleField :: DataType -> Maybe DataType
iSingleField (StrictType (Struct (StructType fields))) = case V.toList fields of
[StructField _ dt] -> Just dt
_ -> Nothing
iSingleField _ = Nothing
_structFromUnfields :: [(T.Text, DataType)] -> StructType
_structFromUnfields l = StructType . V.fromList $ x where
x = [StructField (FieldName name) dt | (name, dt) <- l]