-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sql checks for select statements with group by clause (#1371)
- Loading branch information
Showing
4 changed files
with
42 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module HStream.SQL.Internal.Check where | ||
|
||
import qualified Data.Map.Strict as Map | ||
import GHC.Stack (HasCallStack) | ||
|
||
import HStream.SQL.AST | ||
import HStream.SQL.Exception (SomeSQLException (..), | ||
buildSQLException) | ||
|
||
class Check a where | ||
check :: HasCallStack => a -> Either SomeSQLException () | ||
|
||
instance Check RSelect where | ||
check (RSelect (RSel sels) _RFrom _RWhere (RGroupBy groupBys _) _RHaving) = mapM_ (checkGroupBySel groupBys) sels | ||
check _ = Right () | ||
|
||
checkGroupBySel gbs (RSelectItemProject expr _) = checkGroupBySelExpr gbs expr | ||
checkGroupBySel gbs RSelectProjectQualifiedAll{} = Left $ buildSQLException ParseException Nothing "GROUP BY clause does not allow wildcards" | ||
checkGroupBySel gbs RSelectProjectAll = Left $ buildSQLException ParseException Nothing "GROUP BY clause does not allow wildcards" | ||
|
||
checkGroupBySelExpr gbs (RExprCast _ expr _RDataType) = checkGroupBySelExpr gbs expr | ||
checkGroupBySelExpr gbs (RExprArray _ exprs) = mapM_ (checkGroupBySelExpr gbs) exprs | ||
checkGroupBySelExpr gbs (RExprMap _ eMap) = mapM_ (checkGroupBySelExpr gbs) (Map.keys eMap) | ||
checkGroupBySelExpr gbs (RExprAccessMap _ expr _expr) = checkGroupBySelExpr gbs expr | ||
checkGroupBySelExpr gbs (RExprAccessArray _ expr _RArrayAccessRhs) = checkGroupBySelExpr gbs expr | ||
checkGroupBySelExpr gbs (RExprCol _ maybeStreamName name) = | ||
if (maybeStreamName,name) `elem` gbs then Right () else Left $ buildSQLException ParseException Nothing "Select item does not appear in the GROUP BY clause" | ||
checkGroupBySelExpr gbs (RExprAggregate _ _) = Right () | ||
checkGroupBySelExpr gbs (RExprAccessJson _ _JsonOp expr _expr) = checkGroupBySelExpr gbs expr | ||
checkGroupBySelExpr gbs (RExprBinOp _ _BinaryOp expr1 expr2) = checkGroupBySelExpr gbs expr1 >> checkGroupBySelExpr gbs expr2 | ||
checkGroupBySelExpr gbs (RExprUnaryOp _ _UnaryOp expr) = checkGroupBySelExpr gbs expr | ||
checkGroupBySelExpr gbs (RExprConst _ _) = Right () | ||
|
||
instance Check RSQL where | ||
check (RQSelect rselect) = check rselect | ||
check _ = Right () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters