Skip to content

Commit

Permalink
fix SQL tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agentm committed May 8, 2024
1 parent 9c8d986 commit 41dfaef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
18 changes: 12 additions & 6 deletions src/lib/ProjectM36/SQL/Convert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,12 @@ convertQuery typeF (QueryOp op q1 q2) = do

when (dfType1 /= dfType2) $ throwSQLE (QueryOperatorTypeMismatchError op (attributes dfType1) (attributes dfType2))

pure $ baseDFExpr { convertExpr = Union (convertExpr dfExpr1) (convertExpr dfExpr2) }
let relOp = case op of
UnionQueryOperator -> Union
ExceptQueryOperator -> Difference
IntersectQueryOperator -> Join

pure $ baseDFExpr { convertExpr = relOp (convertExpr dfExpr1) (convertExpr dfExpr2) }

convertSelect :: TypeForRelExprF -> Select -> ConvertM DataFrameExpr
convertSelect typeF sel = do
Expand Down Expand Up @@ -610,7 +615,6 @@ convertProjection typeF selItems groupBys havingExpr = do
-- let fAggregates
-- apply rename
renamesSet <- foldM (\acc (qProjName, (ColumnAlias newName)) -> do
traceShowM ("renamesSet"::String, qProjName, newName)
oldName <- convertColumnProjectionName qProjName
pure $ S.insert (oldName, newName) acc) S.empty (taskRenames task)
let fRenames = if S.null renamesSet then id else Rename renamesSet
Expand Down Expand Up @@ -753,7 +757,7 @@ convertProjectionScalarExpr typeF expr = do
naked (BoolAtom False)
--pure $ ConstructedAtomExpr "False" [] ()
NullLiteral -> pure $ ConstructedAtomExpr "SQLNull" [] ()
Identifier i ->
Identifier i -> do
AttributeAtomExpr <$> convertColumnProjectionName i
BinaryOperator exprA op exprB -> do
a <- convertProjectionScalarExpr typeF exprA
Expand All @@ -762,7 +766,11 @@ convertProjectionScalarExpr typeF expr = do
pure $ f [a,b]
FunctionApplication fname fargs -> do
func <- lookupFunc fname
fargs' <- mapM (convertProjectionScalarExpr typeF) fargs
-- as a special case, count(*) is valid, if non-sensical SQL, so handle it here
fargs' <- if fname == FuncName ["count"] && fargs == [Identifier (ColumnProjectionName [Asterisk])] then
pure [AttributeAtomExpr "_sql_aggregate"]
else
mapM (convertProjectionScalarExpr typeF) fargs
pure (func fargs')
PrefixOperator op sexpr -> do
func <- lookupOperator True op
Expand Down Expand Up @@ -1259,9 +1267,7 @@ convertGroupBy _typeF groupBys mHavingExpr sqlProjection = do
AggGroupByItem pe _gb ->
pure $ info { aggregates = pe : aggregates info }
NonAggGroupByItem (Identifier colName) gb -> do
traceShowM ("convertGroupBy"::String, colName)
aname <- convertColumnProjectionName colName
traceShowM ("convertGroupBy2"::String, "done")
pure $ info { nonAggregates = (aname, gb) : nonAggregates info }
NonAggGroupByItem pe _ -> do
throwSQLE (UnsupportedGroupByProjectionError pe)
Expand Down
8 changes: 5 additions & 3 deletions test/SQL/InterpreterTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,14 @@ testSelect = TestCase $ do
"(s)"
),
-- intersect
("select city from s intersect select 'New York' as city",
"((s{ city }) union ((relation{ }{ tuple{ } }:{city:=\"New York\"}){ city }))",
"(relation{tuple{city \"London\"},tuple{city \"New York\"}, tuple{city \"Athens\"}, tuple{city \"Paris\"}})"
("select city from s intersect select 'London' as city",
"((s{ city }) join ((relation{ }{ tuple{ } }:{city:=\"London\"}){ city }))",
"(relation{tuple{city \"London\"}})"
),
-- except
("select city from s except select 'London' as city",
"((s{city}) minus ((relation{}{tuple{}}:{city:=\"London\"}){city}))",
"(relation{tuple{city \"Athens\"}, tuple{city \"Paris\"}})"
),
-- limit
("SELECT * FROM s LIMIT 10",
Expand Down

0 comments on commit 41dfaef

Please sign in to comment.