agentm / project-m36

Project: M36 Relational Algebra Engine
The Unlicense
895 stars 48 forks source link

sum() problem #208

Closed YuMingLiao closed 6 years ago

YuMingLiao commented 6 years ago

I encounter problems when using sum() Did I use it wrong?

TutorialD (master/main): :importexample date
TutorialD (master/main): :showexpr s
┌──────────┬────────┬───────────┬───────────────┐
│city::Text│s#::Text│sname::Text│status::Integer│
├──────────┼────────┼───────────┼───────────────┤
│"Paris"   │"S2"    │"Jones"    │10             │
│"Paris"   │"S3"    │"Blake"    │30             │
│"London"  │"S4"    │"Clark"    │20             │
│"London"  │"S1"    │"Smith"    │20             │
│"Athens"  │"S5"    │"Adams"    │30             │
└──────────┴────────┴───────────┴───────────────┘
TutorialD (master/main): :showexpr s:{status2:=sum(@status)}
ERR: AtomFunctionTypeError "sum" 1 (RelationAtomType [Attribute "_" IntegerAtomType]) IntegerAtomType
YuMingLiao commented 6 years ago

And lt gt didn't complain when the attribute is Int, not Integer. They produce unexpected result.

TutorialD (master/main): :showexpr orders
┌───────────┬─────────────┬─────────────┬────────────┐
│amount::Int│product::Text│quantity::Int│region::Text│
├───────────┼─────────────┼─────────────┼────────────┤
│5          │"QQQ"        │-26          │"JJJ"       │
│-1         │"WWW"        │7            │"JJJ"       │
│9          │"BBB"        │20           │"OOO"       │
│12         │"XXX"        │-30          │"PPP"       │
│11         │"ZZZ"        │1            │"CCC"       │
│10         │"EEE"        │21           │"BBB"       │
│-7         │"III"        │-24          │"HHH"       │
│-23        │"III"        │29           │"SSS"       │
│-30        │"YYY"        │-15          │"EEE"       │
│-9         │"CCC"        │19           │"PPP"       │
│-29        │"WWW"        │30           │"GGG"       │
└───────────┴─────────────┴─────────────┴────────────┘
TutorialD (master/main): :showexpr orders where ^gt(@amount,5)
┌───────────┬─────────────┬─────────────┬────────────┐
│amount::Int│product::Text│quantity::Int│region::Text│
├───────────┼─────────────┼─────────────┼────────────┤
│12         │"XXX"        │-30          │"PPP"       │
│-23        │"III"        │29           │"SSS"       │
│5          │"QQQ"        │-26          │"JJJ"       │
│10         │"EEE"        │21           │"BBB"       │
│-30        │"YYY"        │-15          │"EEE"       │
│-9         │"CCC"        │19           │"PPP"       │
│-1         │"WWW"        │7            │"JJJ"       │
│-7         │"III"        │-24          │"HHH"       │
│-29        │"WWW"        │30           │"GGG"       │
│11         │"ZZZ"        │1            │"CCC"       │
│9          │"BBB"        │20           │"OOO"       │
└───────────┴─────────────┴─────────────┴────────────┘
TutorialD (master/main): :showexpr orders where ^lt(@amount,5)
┌───────────┬─────────────┬─────────────┬────────────┐
│amount::Int│product::Text│quantity::Int│region::Text│
└───────────┴─────────────┴─────────────┴────────────┘
agentm commented 6 years ago

Aggregate functions such as sum only work on relation valued attributes, so it's different from what you are used to in SQL where aggregate functions switch the query into a different mode. Take a look at this example.

I'll examine the numeric function issue.

agentm commented 6 years ago

I can reproduce this issue. It's definitely related to broken type resolution:

TutorialD (master/main): :showexpr relation{tuple{a int(5)}} where ^gt(@a,-10)
┌──────┐
│a::Int│
├──────┤
│5     │
└──────┘
TutorialD (master/main): :showexpr relation{tuple{a int(5)}} where ^gt(@a,10)
┌──────┐
│a::Int│
├──────┤
│5     │
└──────┘

but this is fine:

TutorialD (master/main): :showexpr relation{tuple{a int(-5)}} : {x:=gt(@a,5)}
ERR: AtomFunctionTypeError "gt" 1 IntegerAtomType IntAtomType
YuMingLiao commented 6 years ago

I traced the first case :showexpr relation{tuple{a int(5)}} where ^gt(@a,-10)

In evalAtomExpr tupIn (FunctionAtomExpr funcName arguments ()), safeInit (x:xs) = safeInit xs causes safeInit (zip (atomFuncType func) argTypes)) to be [] So it atomTypeVerify nothing.

However, if I change it to safeInit (x:xs) = x : safeInit xs, then there is an error.

Couldn't match type `Either RelationalError AtomType' with `AtomType'
      Expected type: [(AtomType, AtomType)]
        Actual type: [(AtomType, Either RelationalError AtomType)]
    * In the second argument of `mapM_', namely
        `(traceShowId $ safeInit (zip (atomFuncType func) argTypes))'

so argTypes <- mapM (typeFromAtomExpr (tupleAttributes tupIn)) arguments need a case of lefts argTypes...

Moreover, I don't why it needs a init here...

agentm commented 6 years ago

Fixed in 0d4a061a451fa0d9b283d6518d25917f540e4a34.