&() in where

30 views
Skip to first unread message

Andrew Charles

unread,
Oct 25, 2012, 12:04:35 PM10/25/12
to squ...@googlegroups.com
Is &() in a where clause supported?  

There appears to be no workaround for what I want.  After all, tuples can not be used as a subquery, so that's out.

I tried a query where this is used and it throws a NullPointerException.

If this is supposed to work, I'll be happy to provide a test case of the issue.

Peter Brant

unread,
Oct 25, 2012, 12:15:06 PM10/25/12
to squ...@googlegroups.com
What are you trying to do?  (Using & in a where clause is a bit of a non-sequitur since it will all be turned into SQL anyway)

Pete

Andrew Charles

unread,
Oct 25, 2012, 12:22:48 PM10/25/12
to squ...@googlegroups.com
I have a table that I need to update, and I would like to do it in a batch.  There are two fields which need to be matched with a set of I'm getting from an external source:

val externalSet = List((1,2), (3,4)...etc

I have an entity like this

case class Entity(  var id1:Int, var id2:Int)

and I want to update the entity table with the list from externalSet

val toUpdate = from(entities)(e=>
where (&(e.id1 * 1000 + e.id2) in externalSet.map(s=>s._1 * 1000 + s._2))
select(e)
).map(various changes)

entities.update(toUpdate)

Maxime Lévesque

unread,
Oct 25, 2012, 12:24:18 PM10/25/12
to squ...@googlegroups.com
> Is &() in a where clause supported?

No, &() is only for use in a select clause, and only needed for outermost
query (when there is nesting).

> There appears to be no workaround for what I want. After all, tuples can
> not be used as a subquery, so that's out.

They can't be used in a from clause if the tuple is yielded by a select,
if it is yielded by a compute and/or groupBy, then it can.

I'd be curious to see the query, and the use case for a &() in the where clause.

Andrew Charles

unread,
Oct 26, 2012, 9:00:07 AM10/26/12
to squ...@googlegroups.com
I'd be curious to see the query, and the use case for a &() in the where clause. 

Please see my post directly above yours. 

Maxime Lévesque

unread,
Oct 26, 2012, 10:17:23 AM10/26/12
to squ...@googlegroups.com
try this :

val toUpdate = from(entities)(e=>
where ((e.id1 times 1000 plus e.id2) in externalSet.map(s=>s._1
* 1000 + s._2))
select(e)
)


2012/10/26 Andrew Charles <acha...@gmail.com>:

Andrew Charles

unread,
Oct 26, 2012, 2:03:32 PM10/26/12
to squ...@googlegroups.com
Whoa.  That worked!

I tried this, it also did the trick:

where ((e.id1.~ * 1000 + e.id2) in (...etc))

Seems just that one method call of ~ bypasses scala's number handling and allows Squeryl to "pull" the AST

Kinda hacky, but given what we have right now it certainly works. 

[Speaking of which, how will the scala macros feature affect the design of squeryl in the future?  Will there be a redesign around it?  Cause unless I'm reading something wrong, using them would eliminate ambiguities like this.]

Thanks!

Maxime Lévesque

unread,
Oct 26, 2012, 2:14:30 PM10/26/12
to squ...@googlegroups.com
> [Speaking of which, how will the scala macros feature affect the design of
> squeryl in the future? Will there be a redesign around it? Cause unless
> I'm reading something wrong, using them would eliminate ambiguities like
> this.]

Absolutely, macros can solve rough edges like these,
The need for ~ and &() should go away.
I played with macros a while ago, they kept changing, but they
seem to have stabilized even though they are still an experimental feature.
It's probably time to revisit them.
Reply all
Reply to author
Forward
0 new messages