Skip to content

Commit 9d60fcf

Browse files
committed
introduce BinaryExpression
1 parent 36f8004 commit 9d60fcf

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

sql/src/main/scala/app/softnetwork/elastic/sql/SQLGroupBy.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,16 @@ object BucketSelectorScript {
9595
extractBucketsPath(left) ++ extractBucketsPath(right)
9696
case relation: ElasticRelation => extractBucketsPath(relation.criteria)
9797
case _: SQLMatch => Map.empty //MATCH is not supported in bucket_selector
98-
case SQLComparisonDateMath(identifier, _, _, _, _, _) if identifier.aggregation =>
99-
Map(identifier.aliasOrName -> identifier.aliasOrName)
98+
case b: BinaryExpression =>
99+
import b._
100+
if (left.aggregation && right.aggregation)
101+
Map(left.aliasOrName -> left.aliasOrName, right.aliasOrName -> right.aliasOrName)
102+
else if (left.aggregation)
103+
Map(left.aliasOrName -> left.aliasOrName)
104+
else if (right.aggregation)
105+
Map(right.aliasOrName -> right.aliasOrName)
106+
else
107+
Map.empty
100108
case e: Expression if e.aggregation =>
101109
import e._
102110
Map(identifier.aliasOrName -> identifier.aliasOrName)
@@ -129,9 +137,9 @@ object BucketSelectorScript {
129137

130138
// build the RHS as a Painless ZonedDateTime (apply +/- interval using TimeInterval.painless)
131139
val rightBase = (arithOp, interval) match {
132-
case (Some(Add), Some(i)) => s"$now.plus(${i.painless})"
140+
case (Some(Add), Some(i)) => s"$now.plus(${i.painless})"
133141
case (Some(Subtract), Some(i)) => s"$now.minus(${i.painless})"
134-
case _ => now
142+
case _ => now
135143
}
136144

137145
val rightZdt = dateFunc match {

sql/src/main/scala/app/softnetwork/elastic/sql/SQLWhere.scala

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,33 @@ case class SQLExpression(
178178
override def asFilter(currentQuery: Option[ElasticBoolQuery]): ElasticFilter = this
179179
}
180180

181+
sealed trait BinaryExpression extends Expression {
182+
def left: SQLIdentifier
183+
def right: SQLIdentifier
184+
override def identifier: SQLIdentifier = left
185+
override def maybeValue: Option[SQLToken] = Some(right)
186+
override lazy val aggregation: Boolean = left.aggregation || right.aggregation
187+
}
188+
189+
case class SQLBinaryExpression(
190+
left: SQLIdentifier,
191+
operator: SQLComparisonOperator, // Gt, Ge, Lt, Le, Eq, ...
192+
right: SQLIdentifier,
193+
maybeNot: Option[Not.type] = None
194+
) extends BinaryExpression
195+
with PainlessScript {
196+
override def update(request: SQLSearchRequest): SQLCriteria =
197+
this.copy(left = left.update(request), right = right.update(request))
198+
199+
override def asFilter(currentQuery: Option[ElasticBoolQuery]): ElasticFilter = this
200+
201+
override def painless: String = {
202+
val painlessOp = (if (maybeNot.isDefined) operator.not else operator).painless
203+
s"${left.painless} $painlessOp ${right.painless}"
204+
}
205+
206+
}
207+
181208
case class SQLIsNull(identifier: SQLIdentifier) extends Expression {
182209
override val operator: SQLOperator = IsNull
183210

@@ -355,9 +382,9 @@ case class SQLComparisonDateMath(
355382
val base = s"${dateTimeFunction.script}"
356383
val dateMath =
357384
(arithmeticOperator, interval) match {
358-
case (Some(Add), Some(i)) => s"$base+${i.script}"
385+
case (Some(Add), Some(i)) => s"$base+${i.script}"
359386
case (Some(Subtract), Some(i)) => s"$base-${i.script}"
360-
case _ => base
387+
case _ => base
361388
}
362389
dateTimeFunction match {
363390
case _: CurrentDateFunction => s"$dateMath/d"

0 commit comments

Comments
 (0)