Skip to content

Commit 7c30aa6

Browse files
committed
add specifications for query with date_diff
1 parent 6c85817 commit 7c30aa6

File tree

4 files changed

+132
-7
lines changed

4 files changed

+132
-7
lines changed

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,4 +1274,67 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12741274
.replaceAll(">", " > ")
12751275
.replaceAll(",LocalDate", ", LocalDate")
12761276
}
1277+
1278+
it should "handle date_diff function as script field" in {
1279+
val select: ElasticSearchRequest =
1280+
SQLQuery(dateDiff)
1281+
val query = select.query
1282+
println(query)
1283+
query shouldBe
1284+
"""{
1285+
| "query": {
1286+
| "match_all": {}
1287+
| },
1288+
| "script_fields": {
1289+
| "diff": {
1290+
| "script": {
1291+
| "lang": "painless",
1292+
| "source": "ChronoUnit.DAYS.between(doc['updatedAt'].value, doc['createdAt'].value)"
1293+
| }
1294+
| }
1295+
| },
1296+
| "_source": {
1297+
| "includes": [
1298+
| "identifier"
1299+
| ]
1300+
| }
1301+
|}""".stripMargin
1302+
.replaceAll("\\s", "")
1303+
.replaceAll(",doc", ", doc")
1304+
}
1305+
1306+
it should "handle aggregation with date_diff function" in {
1307+
val select: ElasticSearchRequest =
1308+
SQLQuery(aggregationWithDateDiff)
1309+
val query = select.query
1310+
println(query)
1311+
query shouldBe
1312+
"""{
1313+
| "query": {
1314+
| "match_all": {}
1315+
| },
1316+
| "size": 0,
1317+
| "_source": true,
1318+
| "aggs": {
1319+
| "identifier": {
1320+
| "terms": {
1321+
| "field": "identifier.keyword"
1322+
| },
1323+
| "aggs": {
1324+
| "max_diff": {
1325+
| "max": {
1326+
| "script": {
1327+
| "lang": "painless",
1328+
| "source": "ChronoUnit.DAYS.between(doc['updatedAt'].value, doc['createdAt'].value)"
1329+
| }
1330+
| }
1331+
| }
1332+
| }
1333+
| }
1334+
| }
1335+
|}""".stripMargin
1336+
.replaceAll("\\s", "")
1337+
.replaceAll(",doc", ", doc")
1338+
}
1339+
12771340
}

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,4 +1271,67 @@ class SQLQuerySpec extends AnyFlatSpec with Matchers {
12711271
.replaceAll(">", " > ")
12721272
.replaceAll(",LocalDate", ", LocalDate")
12731273
}
1274+
1275+
it should "handle date_diff function as script field" in {
1276+
val select: ElasticSearchRequest =
1277+
SQLQuery(dateDiff)
1278+
val query = select.query
1279+
println(query)
1280+
query shouldBe
1281+
"""{
1282+
| "query": {
1283+
| "match_all": {}
1284+
| },
1285+
| "script_fields": {
1286+
| "diff": {
1287+
| "script": {
1288+
| "lang": "painless",
1289+
| "source": "ChronoUnit.DAYS.between(doc['updatedAt'].value, doc['createdAt'].value)"
1290+
| }
1291+
| }
1292+
| },
1293+
| "_source": {
1294+
| "includes": [
1295+
| "identifier"
1296+
| ]
1297+
| }
1298+
|}""".stripMargin
1299+
.replaceAll("\\s", "")
1300+
.replaceAll(",doc", ", doc")
1301+
}
1302+
1303+
it should "handle aggregation with date_diff function" in {
1304+
val select: ElasticSearchRequest =
1305+
SQLQuery(aggregationWithDateDiff)
1306+
val query = select.query
1307+
println(query)
1308+
query shouldBe
1309+
"""{
1310+
| "query": {
1311+
| "match_all": {}
1312+
| },
1313+
| "size": 0,
1314+
| "_source": true,
1315+
| "aggs": {
1316+
| "identifier": {
1317+
| "terms": {
1318+
| "field": "identifier.keyword"
1319+
| },
1320+
| "aggs": {
1321+
| "max_diff": {
1322+
| "max": {
1323+
| "script": {
1324+
| "lang": "painless",
1325+
| "source": "ChronoUnit.DAYS.between(doc['updatedAt'].value, doc['createdAt'].value)"
1326+
| }
1327+
| }
1328+
| }
1329+
| }
1330+
| }
1331+
| }
1332+
|}""".stripMargin
1333+
.replaceAll("\\s", "")
1334+
.replaceAll(",doc", ", doc")
1335+
}
1336+
12741337
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ case object Select extends SQLExpr("select") with SQLRegex
55
sealed trait Field extends Updateable with SQLFunctionChain with PainlessScript {
66
def identifier: Identifier
77
def fieldAlias: Option[SQLAlias]
8-
def isScriptField: Boolean =
9-
identifier.name.isEmpty || (functions.nonEmpty && !aggregation && identifier.bucket.isEmpty)
8+
def isScriptField: Boolean = functions.nonEmpty && !aggregation && identifier.bucket.isEmpty
109
override def sql: String = s"$identifier${asString(fieldAlias)}"
1110
lazy val sourceField: String =
1211
if (identifier.nested) {

sql/src/test/scala/app/softnetwork/elastic/sql/SQLParserSpec.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ object Queries {
114114
.replaceAll("\\( ", "(")
115115
.replaceAll(" \\)", ")")
116116

117-
val dateDiff = "select date_diff(createdAt, updatedAt, day) as diff from Table"
117+
val dateDiff = "select date_diff(createdAt, updatedAt, day) as diff, identifier from Table"
118118

119-
val dateDiffWithAggregation =
120-
"select max(date_diff(parse_datetime('yyyy-MM-ddTHH:mm:ssZ')(createdAt), updatedAt, day)) as max_diff from Table"
119+
val aggregationWithDateDiff =
120+
"select max(date_diff(parse_datetime('yyyy-MM-ddTHH:mm:ssZ')(createdAt), updatedAt, day)) as max_diff from Table group by identifier"
121121
}
122122

123123
/** Created by smanciot on 15/02/17.
@@ -444,9 +444,9 @@ class SQLParserSpec extends AnyFlatSpec with Matchers {
444444
}
445445

446446
it should "parse date_diff function with aggregation" in {
447-
val result = SQLParser(dateDiffWithAggregation)
447+
val result = SQLParser(aggregationWithDateDiff)
448448
result.toOption.flatMap(_.left.toOption.map(_.sql)).getOrElse("") should ===(
449-
dateDiffWithAggregation
449+
aggregationWithDateDiff
450450
)
451451
}
452452
}

0 commit comments

Comments
 (0)