Skip to content

Commit af84669

Browse files
committed
fix sql for round mathematical function
1 parent 7478f8e commit af84669

File tree

4 files changed

+6
-10
lines changed

4 files changed

+6
-10
lines changed

es6/sql-bridge/src/test/scala/app/softnetwork/elastic/sql/SQLQuerySpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2219,7 +2219,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
22192219
| "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.pow(arg0, 3))"
22202220
| }
22212221
| },
2222-
| "round_identifier_0": {
2222+
| "round_identifier": {
22232223
| "script": {
22242224
| "lang": "painless",
22252225
| "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : (def p = Math.pow(10, 0); Math.round((arg0 * p) / p)))"

sql/bridge/src/test/scala/app/softnetwork/elastic/sql/SQLQuerySpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,7 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
22082208
| "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : Math.pow(arg0, 3))"
22092209
| }
22102210
| },
2211-
| "round_identifier_0": {
2211+
| "round_identifier": {
22122212
| "script": {
22132213
| "lang": "painless",
22142214
| "source": "(def arg0 = (!doc.containsKey('identifier') || doc['identifier'].empty ? null : doc['identifier'].value); (arg0 == null) ? null : (def p = Math.pow(10, 0); Math.round((arg0 * p) / p)))"

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -994,18 +994,14 @@ case class SQLPow(arg: PainlessScript, exponent: Int) extends MathematicalFuncti
994994
override def nullable: Boolean = arg.nullable
995995
}
996996

997-
case class SQLRound(arg: PainlessScript, scale: Int = 0) extends MathematicalFunction {
997+
case class SQLRound(arg: PainlessScript, scale: Option[Int]) extends MathematicalFunction {
998998
override def operator: UnaryArithmeticOperator = Round
999999

10001000
override def args: List[PainlessScript] =
1001-
List(arg) :+ SQLIntValue(scale)
1001+
List(arg) ++ scale.map(SQLIntValue(_)).toList
10021002

10031003
override def toPainlessCall(callArgs: List[String]): String =
1004-
callArgs match {
1005-
case List(arg0, arg1) =>
1006-
s"(def p = ${SQLPow(SQLIntValue(10), arg1.toInt).painless}; ${operator.painless}(($arg0 * p) / p))"
1007-
case _ => throw new IllegalArgumentException("ROUND requires one or two arguments")
1008-
}
1004+
s"(def p = ${SQLPow(SQLIntValue(10), scale.getOrElse(0)).painless}; ${operator.painless}((${callArgs.head} * p) / p))"
10091005
}
10101006

10111007
case class SQLSign(arg: PainlessScript) extends MathematicalFunction {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ trait SQLParser extends RegexParsers with PackratParsers { _: SQLWhereParser =>
465465

466466
def roundFunction: PackratParser[MathematicalFunction] =
467467
round ~ start ~ valueExpr ~ separator.? ~ long.? ~ end ^^ { case _ ~ _ ~ v ~ _ ~ s ~ _ =>
468-
SQLRound(v, s.map(_.value.toInt).getOrElse(0))
468+
SQLRound(v, s.map(_.value.toInt))
469469
}
470470

471471
private[this] def pow: PackratParser[UnaryArithmeticOperator] = Pow.regex ^^ (_ => Pow)

0 commit comments

Comments
 (0)