-
Notifications
You must be signed in to change notification settings - Fork 90
/
PredicateModel.scala
172 lines (142 loc) · 4.84 KB
/
PredicateModel.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
/*
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package za.co.absa.spline.harvester.postprocessing.metadata
import org.apache.spark.SparkConf
import org.apache.spark.internal.Logging
import org.apache.spark.sql.RuntimeConfig
import za.co.absa.spline.commons.reflect.ReflectionUtils
import za.co.absa.spline.harvester.postprocessing.metadata.ComparisonSymbol._
import scala.annotation.tailrec
import scala.util.{Failure, Success, Try}
case class Predicate(expr: Expr) extends Logging {
def eval(bindings: Map[String, Any]): Boolean = {
Try(expr.eval(bindings).asInstanceOf[Boolean]) match {
case Success(value) => value
case Failure(exception) =>
logWarning("predicate evaluation failed, " +
"filter will not be applied.", exception)
false
}
}
}
sealed trait Expr {
def eval(bindings: Map[String, Any]): Any
}
case class Bool(value: Boolean) extends Expr {
override def eval(bindings: Map[String, Any]): Any = value
}
case class IntNum(value: Int) extends Expr {
override def eval(bindings: Map[String, Any]): Any = value
}
case class LongNum(value: Long) extends Expr {
override def eval(bindings: Map[String, Any]): Any = value
}
case class Text(value: String) extends Expr {
override def eval(bindings: Map[String, Any]): Any = value
}
case class Not(expr: Expr) extends Expr {
override def eval(bindings: Map[String, Any]): Any = expr.eval(bindings) match {
case b: Boolean => !b
case _ => throw new UnsupportedOperationException()
}
}
case class Or(exprs: Expr*) extends Expr {
override def eval(bindings: Map[String, Any]): Any =
exprs.exists(_.eval(bindings) == true)
}
case class And(exprs: Expr*) extends Expr {
override def eval(bindings: Map[String, Any]): Any =
exprs.forall(_.eval(bindings) == true)
}
case class Path(segments: PathSegment*) extends Expr {
override def eval(bindings: Map[String, Any]): Any = resolvePath(segments, bindings)
@tailrec
private def resolvePath(segments: Seq[PathSegment], obj: Any): Any =
if (segments.isEmpty) obj
else resolvePath(segments.tail, resolvePath(segments.head, obj))
@tailrec
private def resolvePath(segment: PathSegment, obj: Any): Any = obj match {
case Some(x) => resolvePath(segment, x)
case x => segment.resolve(x)
}
}
sealed trait PathSegment {
def resolve(obj: Any): Any
}
case class PropertyPS(name: String) extends PathSegment {
override def resolve(obj: Any): Any = obj match {
case m: Map[_, _] => m.asInstanceOf[Map[String, Any]](name)
case rc: RuntimeConfig => rc.get(name)
case sc: SparkConf => sc.get(name)
case ar: AnyRef => ReflectionUtils.extractValue[Any](ar, name)
}
}
case class ArrayPS(index: Int) extends PathSegment {
override def resolve(obj: Any): Any = obj match {
case a: Array[_] => a(index)
case s: Seq[_] => s(index)
}
}
case class MapPS(key: String) extends PathSegment {
override def resolve(obj: Any): Any = obj match {
case m: Map[_, _] => m.asInstanceOf[Map[String, Any]](key)
case rc: RuntimeConfig => rc.get(key)
case sc: SparkConf => sc.get(key)
}
}
case class Eq(l: Expr, r: Expr) extends Expr {
override def eval(bindings: Map[String, Any]): Any = {
l.eval(bindings) == r.eval(bindings)
}
}
case class Neq(l: Expr, r: Expr) extends Expr {
override def eval(bindings: Map[String, Any]): Any = {
l.eval(bindings) != r.eval(bindings)
}
}
case class Comparison(l: Expr, r: Expr, symbol: String) extends Expr {
import Ordering.Implicits._
override def eval(bindings: Map[String, Any]): Any = {
val left = l.eval(bindings)
val right = r.eval(bindings)
(left, right) match {
case (n1: Int, n2: Int) => compare(n1, symbol, n2)
case (n1: Int, n2: Long) => compare(n1.toLong, symbol, n2)
case (n1: Long, n2: Int) => compare(n1, symbol, n2.toLong)
case (n1: Long, n2: Long) => compare(n1, symbol, n2)
}
}
private def compare[T: Ordering](a: T, op: String, b: T): Boolean = op match {
case LT => a < b
case LTE => a <= b
case GT => a > b
case GTE => a >= b
}
}
object ComparisonSymbol {
val LT = "<"
val LTE = "<="
val GT = ">"
val GTE = ">="
}
object BaseNodeName {
type Type = String
val ExecutionPlan = "executionPlan"
val ExecutionEvent = "executionEvent"
val Operation = "operation"
val Read = "read"
val Write = "write"
}