2020-10-22 23:42:27 +03:00
|
|
|
{-# LANGUAGE PartialTypeSignatures #-}
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2022-02-08 12:24:34 +03:00
|
|
|
-- | Postgres Translate BoolExp
|
|
|
|
--
|
|
|
|
-- Convert IR boolean expressions to Postgres-specific SQL expressions.
|
2021-09-24 01:56:37 +03:00
|
|
|
module Hasura.Backends.Postgres.Translate.BoolExp
|
|
|
|
( toSQLBoolExp,
|
|
|
|
annBoolExp,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.HashMap.Strict qualified as M
|
2022-05-25 13:24:41 +03:00
|
|
|
import Data.Text.Extended (ToTxt)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.DML qualified as S
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types hiding (TableName)
|
|
|
|
import Hasura.Backends.Postgres.Types.BoolExp
|
2022-05-25 13:24:41 +03:00
|
|
|
import Hasura.Backends.Postgres.Types.Function (onArgumentExp)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.Prelude
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.IR.BoolExp
|
|
|
|
import Hasura.RQL.Types.Backend
|
2022-05-25 13:24:41 +03:00
|
|
|
import Hasura.RQL.Types.BoolExp
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Common
|
2022-05-25 13:24:41 +03:00
|
|
|
import Hasura.RQL.Types.Function
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Metadata.Backend
|
|
|
|
import Hasura.RQL.Types.Relationships.Local
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
|
|
|
import Hasura.RQL.Types.Table
|
|
|
|
import Hasura.SQL.Backend
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.SQL.Types
|
2021-05-11 18:18:31 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
-- This convoluted expression instead of col = val
|
|
|
|
-- to handle the case of col : null
|
2021-04-22 00:44:37 +03:00
|
|
|
equalsBoolExpBuilder :: SQLExpression ('Postgres pgKind) -> SQLExpression ('Postgres pgKind) -> S.BoolExp
|
2018-06-27 16:11:32 +03:00
|
|
|
equalsBoolExpBuilder qualColExp rhsExp =
|
2021-09-24 01:56:37 +03:00
|
|
|
S.BEBin
|
|
|
|
S.OrOp
|
|
|
|
(S.BECompare S.SEQ qualColExp rhsExp)
|
|
|
|
( S.BEBin
|
|
|
|
S.AndOp
|
|
|
|
(S.BENull qualColExp)
|
|
|
|
(S.BENull rhsExp)
|
|
|
|
)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
notEqualsBoolExpBuilder :: SQLExpression ('Postgres pgKind) -> SQLExpression ('Postgres pgKind) -> S.BoolExp
|
2018-06-27 16:11:32 +03:00
|
|
|
notEqualsBoolExpBuilder qualColExp rhsExp =
|
2021-09-24 01:56:37 +03:00
|
|
|
S.BEBin
|
|
|
|
S.OrOp
|
|
|
|
(S.BECompare S.SNE qualColExp rhsExp)
|
|
|
|
( S.BEBin
|
|
|
|
S.AndOp
|
|
|
|
(S.BENotNull qualColExp)
|
|
|
|
(S.BENull rhsExp)
|
|
|
|
)
|
|
|
|
|
|
|
|
annBoolExp ::
|
|
|
|
(QErrM m, TableCoreInfoRM b m, BackendMetadata b) =>
|
|
|
|
BoolExpRHSParser b m v ->
|
|
|
|
TableName b ->
|
|
|
|
FieldInfoMap (FieldInfo b) ->
|
|
|
|
GBoolExp b ColExp ->
|
|
|
|
m (AnnBoolExp b v)
|
2021-04-19 15:16:10 +03:00
|
|
|
annBoolExp rhsParser rootTable fim boolExp =
|
2019-09-05 10:34:53 +03:00
|
|
|
case boolExp of
|
|
|
|
BoolAnd exps -> BoolAnd <$> procExps exps
|
2021-09-24 01:56:37 +03:00
|
|
|
BoolOr exps -> BoolOr <$> procExps exps
|
|
|
|
BoolNot e -> BoolNot <$> annBoolExp rhsParser rootTable fim e
|
2019-09-05 10:34:53 +03:00
|
|
|
BoolExists (GExists refqt whereExp) ->
|
|
|
|
withPathK "_exists" $ do
|
2020-12-28 15:56:00 +03:00
|
|
|
refFields <- withPathK "_table" $ askFieldInfoMapSource refqt
|
2021-07-28 11:09:32 +03:00
|
|
|
annWhereExp <- withPathK "_where" $ annBoolExp rhsParser rootTable refFields whereExp
|
2019-09-05 10:34:53 +03:00
|
|
|
return $ BoolExists $ GExists refqt annWhereExp
|
2022-07-12 12:25:22 +03:00
|
|
|
BoolField fld -> BoolField <$> annColExp rhsParser rootTable fim fld
|
2019-09-05 10:34:53 +03:00
|
|
|
where
|
2021-04-19 15:16:10 +03:00
|
|
|
procExps = mapM (annBoolExp rhsParser rootTable fim)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
annColExp ::
|
|
|
|
(QErrM m, TableCoreInfoRM b m, BackendMetadata b) =>
|
|
|
|
BoolExpRHSParser b m v ->
|
|
|
|
TableName b ->
|
|
|
|
FieldInfoMap (FieldInfo b) ->
|
|
|
|
ColExp ->
|
|
|
|
m (AnnBoolExpFld b v)
|
2021-04-19 15:16:10 +03:00
|
|
|
annColExp rhsParser rootTable colInfoMap (ColExp fieldName colVal) = do
|
2018-06-27 16:11:32 +03:00
|
|
|
colInfo <- askFieldInfo colInfoMap fieldName
|
|
|
|
case colInfo of
|
2021-07-28 11:09:32 +03:00
|
|
|
FIColumn pgi -> AVColumn pgi <$> parseBoolExpOperations (_berpValueParser rhsParser) rootTable colInfoMap (ColumnReferenceColumn pgi) colVal
|
2018-06-27 16:11:32 +03:00
|
|
|
FIRelationship relInfo -> do
|
2021-09-24 01:56:37 +03:00
|
|
|
relBoolExp <- decodeValue colVal
|
2020-12-28 15:56:00 +03:00
|
|
|
relFieldInfoMap <- askFieldInfoMapSource $ riRTable relInfo
|
2021-09-24 01:56:37 +03:00
|
|
|
annRelBoolExp <- annBoolExp rhsParser rootTable relFieldInfoMap $ unBoolExp relBoolExp
|
2021-07-07 14:58:37 +03:00
|
|
|
return $ AVRelationship relInfo annRelBoolExp
|
2022-05-25 13:24:41 +03:00
|
|
|
FIComputedField computedFieldInfo ->
|
|
|
|
AVComputedField <$> buildComputedFieldBooleanExp (BoolExpResolver annBoolExp) rhsParser rootTable colInfoMap computedFieldInfo colVal
|
|
|
|
-- Using remote fields in the boolean expression is not supported.
|
2021-09-24 01:56:37 +03:00
|
|
|
FIRemoteRelationship {} ->
|
2020-05-27 18:02:58 +03:00
|
|
|
throw400 UnexpectedPayload "remote field unsupported"
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-10-18 12:16:38 +03:00
|
|
|
-- | Translate an IR boolean expression to an SQL boolean expression. References
|
|
|
|
-- to columns etc are relative to the given 'rootReference'.
|
2021-09-24 01:56:37 +03:00
|
|
|
toSQLBoolExp ::
|
|
|
|
forall pgKind.
|
|
|
|
Backend ('Postgres pgKind) =>
|
2021-10-18 12:16:38 +03:00
|
|
|
-- | The name of the tabular value in query scope that the boolean expression
|
|
|
|
-- applies to
|
2021-09-24 01:56:37 +03:00
|
|
|
S.Qual ->
|
2021-10-18 12:16:38 +03:00
|
|
|
-- | The boolean expression to translate
|
2021-09-24 01:56:37 +03:00
|
|
|
AnnBoolExpSQL ('Postgres pgKind) ->
|
|
|
|
S.BoolExp
|
2021-10-18 12:16:38 +03:00
|
|
|
toSQLBoolExp rootReference e =
|
|
|
|
evalState
|
|
|
|
( runReaderT
|
|
|
|
(unBoolExpM (translateBoolExp e))
|
|
|
|
initialCtx
|
|
|
|
)
|
|
|
|
0
|
2018-11-16 15:40:23 +03:00
|
|
|
where
|
2021-10-18 12:16:38 +03:00
|
|
|
initialCtx =
|
|
|
|
BoolExpCtx
|
|
|
|
{ currTableReference = rootReference,
|
|
|
|
rootReference = rootReference
|
|
|
|
}
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-10-18 12:16:38 +03:00
|
|
|
-- | The table context of boolean expression translation. This is used to
|
|
|
|
-- resolve references to fields, as those may refer to the so-called 'root
|
|
|
|
-- table' (identified by a '$'-sign in the expression input syntax) or the
|
|
|
|
-- 'current' table.
|
|
|
|
data BoolExpCtx = BoolExpCtx
|
|
|
|
{ -- | Reference to the current tabular value.
|
|
|
|
currTableReference :: S.Qual,
|
|
|
|
-- | Reference to the root tabular value.
|
|
|
|
rootReference :: S.Qual
|
|
|
|
}
|
2019-09-05 10:34:53 +03:00
|
|
|
|
2021-10-18 12:16:38 +03:00
|
|
|
-- | The monad that carries the translation of boolean expressions. This
|
|
|
|
-- supports the generation of fresh names for aliasing sub-expressions and
|
|
|
|
-- maintains the table context of the expressions being translated.
|
|
|
|
newtype BoolExpM a = BoolExpM {unBoolExpM :: ReaderT BoolExpCtx (State Word64) a}
|
|
|
|
deriving (Functor, Applicative, Monad, MonadReader BoolExpCtx, MonadState Word64)
|
|
|
|
|
|
|
|
-- | Translate a 'GBoolExp' with annotated SQLExpressions in the leaves into a
|
|
|
|
-- bare SQL Boolean Expression.
|
|
|
|
translateBoolExp ::
|
2021-09-24 01:56:37 +03:00
|
|
|
forall pgKind.
|
2021-10-18 12:16:38 +03:00
|
|
|
(Backend ('Postgres pgKind)) =>
|
2021-09-24 01:56:37 +03:00
|
|
|
AnnBoolExpSQL ('Postgres pgKind) ->
|
2021-10-18 12:16:38 +03:00
|
|
|
BoolExpM S.BoolExp
|
|
|
|
translateBoolExp = \case
|
2021-09-24 01:56:37 +03:00
|
|
|
BoolAnd bes -> do
|
2021-10-18 12:16:38 +03:00
|
|
|
sqlBExps <- mapM translateBoolExp bes
|
2019-09-05 10:34:53 +03:00
|
|
|
return $ foldr (S.BEBin S.AndOp) (S.BELit True) sqlBExps
|
2021-09-24 01:56:37 +03:00
|
|
|
BoolOr bes -> do
|
2021-10-18 12:16:38 +03:00
|
|
|
sqlBExps <- mapM translateBoolExp bes
|
2019-09-05 10:34:53 +03:00
|
|
|
return $ foldr (S.BEBin S.OrOp) (S.BELit False) sqlBExps
|
2021-10-18 12:16:38 +03:00
|
|
|
BoolNot notExp -> S.BENot <$> translateBoolExp notExp
|
|
|
|
BoolExists (GExists currTableReference wh) -> do
|
|
|
|
whereExp <- recCurrentTable (S.QualTable currTableReference) wh
|
|
|
|
return $ S.mkExists (S.FISimple currTableReference Nothing) whereExp
|
2022-07-12 12:25:22 +03:00
|
|
|
BoolField boolExp -> case boolExp of
|
2021-10-18 12:16:38 +03:00
|
|
|
AVColumn colInfo opExps -> do
|
|
|
|
BoolExpCtx {rootReference, currTableReference} <- ask
|
2022-01-19 11:37:50 +03:00
|
|
|
let colFld = fromCol @('Postgres pgKind) $ ciColumn colInfo
|
2021-10-18 12:16:38 +03:00
|
|
|
bExps = map (mkFieldCompExp rootReference currTableReference $ LColumn colFld) opExps
|
|
|
|
return $ foldr (S.BEBin S.AndOp) (S.BELit True) bExps
|
|
|
|
AVRelationship (RelInfo _ _ colMapping relTN _ _) nesAnn -> do
|
|
|
|
-- Convert the where clause on the relationship
|
|
|
|
aliasRelTN <- freshIdentifier relTN
|
|
|
|
annRelBoolExp <- recCurrentTable (S.QualifiedIdentifier aliasRelTN Nothing) nesAnn
|
|
|
|
BoolExpCtx {currTableReference} <- ask
|
|
|
|
let backCompExp = foldr (S.BEBin S.AndOp) (S.BELit True) $
|
|
|
|
flip map (M.toList colMapping) $ \(lCol, rCol) ->
|
|
|
|
S.BECompare
|
|
|
|
S.SEQ
|
|
|
|
(mkQCol (S.QualifiedIdentifier aliasRelTN Nothing) rCol)
|
|
|
|
(mkQCol currTableReference lCol)
|
|
|
|
innerBoolExp = S.BEBin S.AndOp backCompExp annRelBoolExp
|
|
|
|
return $ S.mkExists (S.FISimple relTN $ Just $ S.Alias aliasRelTN) innerBoolExp
|
|
|
|
AVComputedField (AnnComputedFieldBoolExp _ _ function sessionArgPresence cfBoolExp) -> do
|
|
|
|
case cfBoolExp of
|
|
|
|
CFBEScalar opExps -> do
|
|
|
|
BoolExpCtx {rootReference, currTableReference} <- ask
|
|
|
|
-- Convert the where clause on scalar computed field
|
|
|
|
let bExps = map (mkFieldCompExp rootReference currTableReference $ LComputedField function sessionArgPresence) opExps
|
|
|
|
pure $ foldr (S.BEBin S.AndOp) (S.BELit True) bExps
|
|
|
|
CFBETable _ be -> do
|
|
|
|
-- Convert the where clause on table computed field
|
|
|
|
BoolExpCtx {currTableReference} <- ask
|
|
|
|
aliasFunction <- freshIdentifier function
|
|
|
|
let functionExp =
|
|
|
|
mkComputedFieldFunctionExp currTableReference function sessionArgPresence $
|
|
|
|
Just $ S.Alias aliasFunction
|
|
|
|
S.mkExists (S.FIFunc functionExp) <$> recCurrentTable (S.QualifiedIdentifier aliasFunction Nothing) be
|
|
|
|
where
|
|
|
|
mkQCol :: forall a. IsIdentifier a => S.Qual -> a -> S.SQLExp
|
|
|
|
mkQCol q = S.SEQIdentifier . S.QIdentifier q . toIdentifier
|
|
|
|
|
|
|
|
-- Draw a fresh identifier intended to alias the given object.
|
|
|
|
freshIdentifier :: forall a. ToTxt a => QualifiedObject a -> BoolExpM Identifier
|
|
|
|
freshIdentifier obj = do
|
|
|
|
curVarNum <- get
|
|
|
|
put $ curVarNum + 1
|
|
|
|
let newIdentifier =
|
|
|
|
Identifier $
|
|
|
|
"_be_" <> tshow curVarNum <> "_"
|
|
|
|
<> snakeCaseQualifiedObject obj
|
|
|
|
|
|
|
|
return newIdentifier
|
|
|
|
|
|
|
|
-- Call recursively using the given identifier for the 'current' table.
|
|
|
|
recCurrentTable :: S.Qual -> AnnBoolExpSQL ('Postgres pgKind) -> BoolExpM S.BoolExp
|
|
|
|
recCurrentTable curr = local (\e -> e {currTableReference = curr}) . translateBoolExp
|
2019-09-05 10:34:53 +03:00
|
|
|
|
2021-07-07 14:58:37 +03:00
|
|
|
data LHSField b
|
|
|
|
= LColumn !FieldName
|
2022-05-25 13:24:41 +03:00
|
|
|
| LComputedField !QualifiedFunction !(FunctionArgsExp b (SQLExpression b))
|
2021-07-07 14:58:37 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
mkComputedFieldFunctionExp ::
|
|
|
|
S.Qual ->
|
|
|
|
QualifiedFunction ->
|
2022-05-25 13:24:41 +03:00
|
|
|
FunctionArgsExp ('Postgres pgKind) (SQLExpression ('Postgres pgKind)) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
Maybe S.Alias ->
|
|
|
|
S.FunctionExp
|
2022-05-25 13:24:41 +03:00
|
|
|
mkComputedFieldFunctionExp qual function functionArgs alias =
|
2021-07-07 14:58:37 +03:00
|
|
|
-- "function_schema"."function_name"("qual".*)
|
|
|
|
let tableRowInput = S.SEStar $ Just qual
|
2022-05-25 13:24:41 +03:00
|
|
|
resolvedFunctionArgs =
|
|
|
|
let FunctionArgsExp {..} = fmap (onArgumentExp tableRowInput (S.SEIdentifier . Identifier)) functionArgs
|
|
|
|
in S.FunctionArgs _faePositional _faeNamed
|
|
|
|
in S.FunctionExp function resolvedFunctionArgs $ flip S.FunctionAlias Nothing <$> alias
|
2021-07-07 14:58:37 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
mkFieldCompExp ::
|
2021-10-18 12:16:38 +03:00
|
|
|
S.Qual -> S.Qual -> LHSField ('Postgres pgKind) -> OpExpG ('Postgres pgKind) S.SQLExp -> S.BoolExp
|
|
|
|
mkFieldCompExp rootReference currTableReference lhsField = mkCompExp qLhsField
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2021-07-07 14:58:37 +03:00
|
|
|
qLhsField = case lhsField of
|
|
|
|
LColumn fieldName ->
|
|
|
|
-- "qual"."column" =
|
2021-10-18 12:16:38 +03:00
|
|
|
S.SEQIdentifier $ S.QIdentifier currTableReference $ Identifier $ getFieldNameTxt fieldName
|
2021-07-07 14:58:37 +03:00
|
|
|
LComputedField function sessionArgPresence ->
|
|
|
|
-- "function_schema"."function_name"("qual".*) =
|
2021-10-18 12:16:38 +03:00
|
|
|
S.SEFunction $ mkComputedFieldFunctionExp currTableReference function sessionArgPresence Nothing
|
2021-07-07 14:58:37 +03:00
|
|
|
|
2021-10-18 12:16:38 +03:00
|
|
|
mkQCol :: RootOrCurrentColumn ('Postgres pgKind) -> S.SQLExp
|
|
|
|
mkQCol (RootOrCurrentColumn IsRoot col) = S.SEQIdentifier $ S.QIdentifier rootReference $ toIdentifier col
|
|
|
|
mkQCol (RootOrCurrentColumn IsCurrent col) = S.SEQIdentifier $ S.QIdentifier currTableReference $ toIdentifier col
|
2019-01-17 09:21:38 +03:00
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
mkCompExp :: SQLExpression ('Postgres pgKind) -> OpExpG ('Postgres pgKind) (SQLExpression ('Postgres pgKind)) -> S.BoolExp
|
2019-07-15 11:52:45 +03:00
|
|
|
mkCompExp lhs = \case
|
2021-09-24 01:56:37 +03:00
|
|
|
ACast casts -> mkCastsExp casts
|
|
|
|
AEQ False val -> equalsBoolExpBuilder lhs val
|
|
|
|
AEQ True val -> S.BECompare S.SEQ lhs val
|
|
|
|
ANE False val -> notEqualsBoolExpBuilder lhs val
|
|
|
|
ANE True val -> S.BECompare S.SNE lhs val
|
|
|
|
AIN val -> S.BECompareAny S.SEQ lhs val
|
|
|
|
ANIN val -> S.BENot $ S.BECompareAny S.SEQ lhs val
|
|
|
|
AGT val -> S.BECompare S.SGT lhs val
|
|
|
|
ALT val -> S.BECompare S.SLT lhs val
|
|
|
|
AGTE val -> S.BECompare S.SGTE lhs val
|
|
|
|
ALTE val -> S.BECompare S.SLTE lhs val
|
|
|
|
ALIKE val -> S.BECompare S.SLIKE lhs val
|
|
|
|
ANLIKE val -> S.BECompare S.SNLIKE lhs val
|
|
|
|
CEQ rhsCol -> S.BECompare S.SEQ lhs $ mkQCol rhsCol
|
|
|
|
CNE rhsCol -> S.BECompare S.SNE lhs $ mkQCol rhsCol
|
|
|
|
CGT rhsCol -> S.BECompare S.SGT lhs $ mkQCol rhsCol
|
|
|
|
CLT rhsCol -> S.BECompare S.SLT lhs $ mkQCol rhsCol
|
|
|
|
CGTE rhsCol -> S.BECompare S.SGTE lhs $ mkQCol rhsCol
|
|
|
|
CLTE rhsCol -> S.BECompare S.SLTE lhs $ mkQCol rhsCol
|
|
|
|
ANISNULL -> S.BENull lhs
|
|
|
|
ANISNOTNULL -> S.BENotNull lhs
|
2021-04-14 16:02:16 +03:00
|
|
|
ABackendSpecific op -> case op of
|
2021-09-24 01:56:37 +03:00
|
|
|
AILIKE val -> S.BECompare S.SILIKE lhs val
|
|
|
|
ANILIKE val -> S.BECompare S.SNILIKE lhs val
|
|
|
|
ASIMILAR val -> S.BECompare S.SSIMILAR lhs val
|
|
|
|
ANSIMILAR val -> S.BECompare S.SNSIMILAR lhs val
|
|
|
|
AREGEX val -> S.BECompare S.SREGEX lhs val
|
|
|
|
AIREGEX val -> S.BECompare S.SIREGEX lhs val
|
|
|
|
ANREGEX val -> S.BECompare S.SNREGEX lhs val
|
|
|
|
ANIREGEX val -> S.BECompare S.SNIREGEX lhs val
|
|
|
|
AContains val -> S.BECompare S.SContains lhs val
|
|
|
|
AContainedIn val -> S.BECompare S.SContainedIn lhs val
|
|
|
|
AHasKey val -> S.BECompare S.SHasKey lhs val
|
|
|
|
AHasKeysAny val -> S.BECompare S.SHasKeysAny lhs val
|
|
|
|
AHasKeysAll val -> S.BECompare S.SHasKeysAll lhs val
|
|
|
|
AAncestor val -> S.BECompare S.SContains lhs val
|
|
|
|
AAncestorAny val -> S.BECompare S.SContains lhs val
|
|
|
|
ADescendant val -> S.BECompare S.SContainedIn lhs val
|
|
|
|
ADescendantAny val -> S.BECompare S.SContainedIn lhs val
|
|
|
|
AMatches val -> S.BECompare S.SREGEX lhs val
|
|
|
|
AMatchesAny val -> S.BECompare S.SHasKey lhs val
|
2021-04-14 16:02:16 +03:00
|
|
|
AMatchesFulltext val -> S.BECompare S.SMatchesFulltext lhs val
|
2021-09-24 01:56:37 +03:00
|
|
|
ASTContains val -> mkGeomOpBe "ST_Contains" val
|
|
|
|
ASTCrosses val -> mkGeomOpBe "ST_Crosses" val
|
|
|
|
ASTEquals val -> mkGeomOpBe "ST_Equals" val
|
|
|
|
ASTIntersects val -> mkGeomOpBe "ST_Intersects" val
|
|
|
|
AST3DIntersects val -> mkGeomOpBe "ST_3DIntersects" val
|
|
|
|
ASTOverlaps val -> mkGeomOpBe "ST_Overlaps" val
|
|
|
|
ASTTouches val -> mkGeomOpBe "ST_Touches" val
|
|
|
|
ASTWithin val -> mkGeomOpBe "ST_Within" val
|
|
|
|
AST3DDWithinGeom (DWithinGeomOp r val) -> applySQLFn "ST_3DDWithin" [lhs, val, r]
|
|
|
|
ASTDWithinGeom (DWithinGeomOp r val) -> applySQLFn "ST_DWithin" [lhs, val, r]
|
2021-04-14 16:02:16 +03:00
|
|
|
ASTDWithinGeog (DWithinGeogOp r val sph) -> applySQLFn "ST_DWithin" [lhs, val, r, sph]
|
2021-09-24 01:56:37 +03:00
|
|
|
ASTIntersectsRast val -> applySTIntersects [lhs, val]
|
|
|
|
ASTIntersectsNbandGeom (STIntersectsNbandGeommin nband geommin) -> applySTIntersects [lhs, nband, geommin]
|
2021-04-14 16:02:16 +03:00
|
|
|
ASTIntersectsGeomNband (STIntersectsGeomminNband geommin mNband) -> applySTIntersects [lhs, geommin, withSQLNull mNband]
|
2019-07-15 11:52:45 +03:00
|
|
|
where
|
|
|
|
mkGeomOpBe fn v = applySQLFn fn [lhs, v]
|
|
|
|
|
|
|
|
applySQLFn f exps = S.BEExp $ S.SEFnApp f exps Nothing
|
|
|
|
|
2019-08-29 16:07:05 +03:00
|
|
|
applySTIntersects = applySQLFn "ST_Intersects"
|
|
|
|
|
|
|
|
withSQLNull = fromMaybe S.SENull
|
|
|
|
|
2019-07-15 11:52:45 +03:00
|
|
|
mkCastsExp casts =
|
|
|
|
sqlAll . flip map (M.toList casts) $ \(targetType, operations) ->
|
2020-12-01 18:50:18 +03:00
|
|
|
let targetAnn = S.mkTypeAnn $ CollectableTypeScalar targetType
|
2021-09-24 01:56:37 +03:00
|
|
|
in sqlAll $ map (mkCompExp (S.SETyAnn lhs targetAnn)) operations
|
2019-07-15 11:52:45 +03:00
|
|
|
|
|
|
|
sqlAll = foldr (S.BEBin S.AndOp) (S.BELit True)
|