-
Notifications
You must be signed in to change notification settings - Fork 26
/
predicate.scala
283 lines (228 loc) · 8.77 KB
/
predicate.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (c) 2016-2020 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause
package edu.gemini.grackle
import scala.annotation.tailrec
import scala.util.matching.Regex
import cats.Apply
import cats.data.Ior
import cats.kernel.{ Eq, Order }
import cats.implicits._
import QueryInterpreter.mkErrorResult
/**
* A reified function over a `Cursor`.
*
* Query interpreters will typically need to introspect predicates (eg. in the doobie module
* we need to be able to construct where clauses from predicates over fields/attributes), so
* these cannot be arbitrary functions `Cursor => Boolean`.
*/
trait Term[T] extends Product with Serializable { // fun fact: making this covariant crashes Scala 3
def apply(c: Cursor): Result[T]
def children: List[Term[_]]
def fold[Acc](acc: Acc)(f: (Acc, Term[_]) => Acc): Acc =
children.foldLeft(f(acc, this)) { case (acc, child) => child.fold(acc)(f) }
def exists(f: Term[_] => Boolean): Boolean =
f(this) || children.exists(_.exists(f))
def forall(f: Term[_] => Boolean): Boolean =
f(this) && children.forall(_.forall(f))
}
object Term extends TermLow {
implicit def path2Term[A](p: Path): Term[A] =
p.asTerm[A]
}
trait TermLow {
implicit def path2ListTerm[A](p: Path): Term[List[A]] =
p.asTerm[List[A]]
}
/** A path starting from some root type. */
case class Path private (root: Type, path: List[String]) {
def asTerm[A]: Term[A] =
if (isList) PathTerm.ListPath(path)
else PathTerm.UniquePath(path)
lazy val isList: Boolean = root.pathIsList(path)
lazy val tpe: Option[Type] = root.path(path)
def prepend(prefix: List[String]): Path =
copy(path = prefix ++ path)
def /(elem: String): Path =
copy(path = path :+ elem)
}
object Path {
/** Construct an empty `Path` rooted at the given type. */
def from(tpe: Type): Path =
Path(tpe, Nil)
}
sealed trait PathTerm {
def children: List[Term[_]] = Nil
def path: List[String]
}
object PathTerm {
case class ListPath[A](path: List[String]) extends Term[A] with PathTerm {
def apply(c: Cursor): Result[A] =
c.flatListPath(path).map(_.map {
case Predicate.ScalarFocus(f) => f
case _ => sys.error("impossible")
} .asInstanceOf[A])
}
case class UniquePath[A](path: List[String]) extends Term[A] with PathTerm {
def apply(c: Cursor): Result[A] =
c.listPath(path) match {
case Ior.Right(List(Predicate.ScalarFocus(a: A @unchecked))) => a.rightIor
case other => mkErrorResult(s"Expected exactly one element for path $path found $other")
}
}
}
trait Predicate extends Term[Boolean]
object Predicate {
object ScalarFocus {
def unapply(c: Cursor): Option[Any] =
if (c.isLeaf) Some(c.focus)
else if (c.isNullable && c.tpe.nonNull.isLeaf)
c.asNullable match {
case Ior.Right(Some(c)) => unapply(c).map(Some(_))
case _ => Some(None)
}
else None
}
case object True extends Predicate {
def apply(c: Cursor): Result[Boolean] = true.rightIor
def children = Nil
}
case object False extends Predicate {
def apply(c: Cursor): Result[Boolean] = false.rightIor
def children = Nil
}
def and(props: List[Predicate]): Predicate = {
@tailrec
def loop(props: List[Predicate], acc: Predicate): Predicate =
props match {
case Nil => acc
case False :: _ => False
case True :: tl => loop(tl, acc)
case hd :: tl if acc == True => loop(tl, hd)
case hd :: tl => loop(tl, And(hd, acc))
}
loop(props, True)
}
def or(props: List[Predicate]): Predicate = {
@tailrec
def loop(props: List[Predicate], acc: Predicate): Predicate =
props match {
case Nil => acc
case True :: _ => True
case False :: tl => loop(tl, acc)
case hd :: tl if acc == False => loop(tl, hd)
case hd :: tl => loop(tl, Or(hd, acc))
}
loop(props, False)
}
case class Const[T](v: T) extends Term[T] {
def apply(c: Cursor): Result[T] = v.rightIor
def children = Nil
}
case class And(x: Predicate, y: Predicate) extends Predicate {
def apply(c: Cursor): Result[Boolean] = Apply[Result].map2(x(c), y(c))(_ && _)
def children = List(x, y)
}
object And {
def combineAll(preds: List[Predicate]): Predicate =
preds match {
case Nil => True
case List(pred) => pred
case hd :: tail => tail.foldRight(hd)((elem, acc) => And(elem, acc))
}
}
case class Or(x: Predicate, y: Predicate) extends Predicate {
def apply(c: Cursor): Result[Boolean] = Apply[Result].map2(x(c), y(c))(_ || _)
def children = List(x, y)
}
case class Not(x: Predicate) extends Predicate {
def apply(c: Cursor): Result[Boolean] = x(c).map(! _)
def children = List(x)
}
case class Eql[T: Eq](x: Term[T], y: Term[T]) extends Predicate {
def apply(c: Cursor): Result[Boolean] = Apply[Result].map2(x(c), y(c))(_ === _)
def eqInstance: Eq[T] = implicitly[Eq[T]]
def children = List(x, y)
def subst(x: Term[T], y: Term[T]): Eql[T] = copy(x = x, y = y)
}
case class NEql[T: Eq](x: Term[T], y: Term[T]) extends Predicate {
def apply(c: Cursor): Result[Boolean] = Apply[Result].map2(x(c), y(c))(_ =!= _)
def children = List(x, y)
def subst(x: Term[T], y: Term[T]): NEql[T] = copy(x = x, y = y)
}
case class Contains[T: Eq](x: Term[List[T]], y: Term[T]) extends Predicate {
def apply(c: Cursor): Result[Boolean] = Apply[Result].map2(x(c), y(c))((xs, y0) => xs.exists(_ === y0))
def children = List(x, y)
def subst(x: Term[List[T]], y: Term[T]): Contains[T] = copy(x = x, y = y)
}
case class Lt[T: Order](x: Term[T], y: Term[T]) extends Predicate {
def apply(c: Cursor): Result[Boolean] = Apply[Result].map2(x(c), y(c))(_.compare(_) < 0)
def children = List(x, y)
def subst(x: Term[T], y: Term[T]): Lt[T] = copy(x = x, y = y)
}
case class LtEql[T: Order](x: Term[T], y: Term[T]) extends Predicate {
def apply(c: Cursor): Result[Boolean] = Apply[Result].map2(x(c), y(c))(_.compare(_) <= 0)
def children = List(x, y)
def subst(x: Term[T], y: Term[T]): LtEql[T] = copy(x = x, y = y)
}
case class Gt[T: Order](x: Term[T], y: Term[T]) extends Predicate {
def apply(c: Cursor): Result[Boolean] = Apply[Result].map2(x(c), y(c))(_.compare(_) > 0)
def children = List(x, y)
def subst(x: Term[T], y: Term[T]): Gt[T] = copy(x = x, y = y)
}
case class GtEql[T: Order](x: Term[T], y: Term[T]) extends Predicate {
def apply(c: Cursor): Result[Boolean] = Apply[Result].map2(x(c), y(c))(_.compare(_) >= 0)
def children = List(x, y)
def subst(x: Term[T], y: Term[T]): GtEql[T] = copy(x = x, y = y)
}
case class IsNull[T](x: Term[Option[T]], isNull: Boolean) extends Predicate {
def apply(c: Cursor): Result[Boolean] = x(c).map(_.isEmpty == isNull)
def children = List(x)
}
case class In[T: Eq](x: Term[T], y: List[T]) extends Predicate {
def apply(c: Cursor): Result[Boolean] = x(c).map(y.contains_(_))
def children = List(x)
def subst(x: Term[T]): In[T] = copy(x = x)
}
object In {
def fromEqls[T](eqls: List[Eql[T]]): Option[In[T]] = {
val paths = eqls.map(_.x).distinct
val consts = eqls.collect { case Eql(_, Const(c)) => c }
if (eqls.map(_.x).distinct.sizeCompare(1) == 0 && consts.sizeCompare(eqls) == 0)
Some(In(paths.head, consts)(eqls.head.eqInstance))
else
None
}
}
case class AndB(x: Term[Int], y: Term[Int]) extends Term[Int] {
def apply(c: Cursor): Result[Int] = Apply[Result].map2(x(c), y(c))(_ & _)
def children = List(x, y)
}
case class OrB(x: Term[Int], y: Term[Int]) extends Term[Int] {
def apply(c: Cursor): Result[Int] = Apply[Result].map2(x(c), y(c))(_ | _)
def children = List(x, y)
}
case class XorB(x: Term[Int], y: Term[Int]) extends Term[Int] {
def apply(c: Cursor): Result[Int] = Apply[Result].map2(x(c), y(c))(_ ^ _)
def children = List(x, y)
}
case class NotB(x: Term[Int]) extends Term[Int] {
def apply(c: Cursor): Result[Int] = x(c).map(~ _)
def children = List(x)
}
case class Matches(x: Term[String], r: Regex) extends Predicate {
def apply(c: Cursor): Result[Boolean] = x(c).map(r.matches(_))
def children = List(x)
}
case class StartsWith(x: Term[String], prefix: String) extends Predicate {
def apply(c: Cursor): Result[Boolean] = x(c).map(_.startsWith(prefix))
def children = List(x)
}
case class ToUpperCase(x: Term[String]) extends Term[String] {
def apply(c: Cursor): Result[String] = x(c).map(_.toUpperCase)
def children = List(x)
}
case class ToLowerCase(x: Term[String]) extends Term[String] {
def apply(c: Cursor): Result[String] = x(c).map(_.toLowerCase)
def children = List(x)
}
}