Skip to content

Commit

Permalink
Handle Date type on painless script
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbey committed Dec 1, 2023
1 parent fb28a71 commit 5c2778b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,17 @@ private static Iterator<ObjectNode> processFiltersBucket(AggregateTDSResultVisit

for (Map.Entry<String, FiltersBucket> bucketEntry : buckets.keyed.entrySet())
{
FiltersBucket value = bucketEntry.getValue();

if (value.doc_count.getLiteral() == 0)
{
continue;
}

MutableMap<String, Object> result = Maps.mutable.empty();

result.put(key, bucketEntry.getKey());

FiltersBucket value = bucketEntry.getValue();

for (Map.Entry<String, Aggregate> aggregateEntry : value.__additionalProperties.entrySet())
{
result.put(aggregateEntry.getKey(), ((AggregateBase) aggregateEntry.getValue().unionValue()).accept(aggregateTDSResultVisitor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1535,16 +1535,11 @@ function meta::external::store::elasticsearch::v7::pureToEs::processPainlessBool
]);

let paramScript = $newValue->match([
pv: PlanVarPlaceHolder[1] |
if($pv.type == StrictDate,
|'LocalDate.parse(params[\'%s\']).atTime(0, 0).toInstant(ZoneOffset.UTC).toEpochMilli()',
| if($pv.type == DateTime,
|'ZonedDateTime.parse(params[\'%s\']).toInstant().toEpochMilli()',
| fail('Type not supported on variable - %s:%s'->format([$pv.name, $pv.type->elementToPath()]))->cast(@String)
)
),
any: Any[*] | 'params[\'%s\']'
])->format($param.first);
pv: PlanVarPlaceHolder[1] |
'(params[\'%s\'].length() <= 10 ? LocalDate.parse(params[\'%s\']).atTime(0, 0) : LocalDateTime.parse(params[\'%s\'])).toInstant(ZoneOffset.UTC).toEpochMilli()'
->format([$param.first, $param.first, $param.first]),
any: Any[*] | 'params[\'%s\']'->format($param.first)
]);

let script = '(%s && %s.toInstant().toEpochMilli() %s %s)'->format([painlessIsNotEmpty($tdsESDetail), painlessExtractField($tdsESDetail), $operation, $paramScript]);
let literalOrExpression = $newValue->literalOrExpression(true)->toOne();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,64 @@ meta::external::store::elasticsearch::executionTest::testCase::tds::groupBy::tes
|'Newer',
|'Newest'))), 'ReleaseDate'), col(x: TDSRow[1] | $x.getInteger('Budget'), 'Budget')])->groupBy('ReleaseDate', agg('sumBudget', r | $r.getInteger('Budget'), agg | $agg->sum())));
}

function
<<paramTest.Test>>
{doc.doc = 'Test projection on Elasticsearch with pure nested if expressions'}
meta::external::store::elasticsearch::executionTest::testCase::tds::groupBy::testGroupByIfExpressionOnAdjustTodayFunctions(config:TestConfig[1]):Boolean[1]
{
$config->testTdsExpression(x|$x->extend([col(x: TDSRow[1] |
if($x.getDate('ReleaseDate') < today()->adjust(-15, DurationUnit.YEARS),
|'Oldest',
|if($x.getDate('ReleaseDate') < today()->adjust(-10, DurationUnit.YEARS),
|'Older',
|if($x.getDate('ReleaseDate') < today()->adjust(-5, DurationUnit.YEARS),
|'Newer',
|'Newest'))), 'Bucket')])->groupBy('Bucket', agg('sumBudget', r | $r.getInteger('Budget'), agg | $agg->sum())));
}

function
<<paramTest.Test>>
{doc.doc = 'Test projection on Elasticsearch with pure nested if expressions'}
meta::external::store::elasticsearch::executionTest::testCase::tds::groupBy::testGroupByExpressionOnAdjustTodayFunctions(config:TestConfig[1]):Boolean[1]
{
$config->testTdsExpression(x|$x->extend([col(x: TDSRow[1] |
$x.getDate('ReleaseDate') < today()->adjust(-15, DurationUnit.YEARS),
'Is Old?')])->groupBy('Is Old?', agg('sumBudget', r | $r.getInteger('Budget'), agg | $agg->sum())));
}


function
<<paramTest.Test>>
{doc.doc = 'Test projection on Elasticsearch with pure nested if expressions'}
meta::external::store::elasticsearch::executionTest::testCase::tds::groupBy::testGroupByIfExpressionOnAdjustNowFunctions(config:TestConfig[1]):Boolean[1]
{
$config->testTdsExpression(x|$x->extend([col(x: TDSRow[1] |
if($x.getDate('ReleaseDate') < now()->adjust(-15, DurationUnit.YEARS),
|'Oldest',
|if($x.getDate('ReleaseDate') < now()->adjust(-10, DurationUnit.YEARS),
|'Older',
|if($x.getDate('ReleaseDate') < now()->adjust(-5, DurationUnit.YEARS),
|'Newer',
|'Newest'))), 'Bucket')])->groupBy('Bucket', agg('sumBudget', r | $r.getInteger('Budget'), agg | $agg->sum())));
}

function
<<paramTest.Test>>
{doc.doc = 'Test projection on Elasticsearch with pure nested if expressions'}
meta::external::store::elasticsearch::executionTest::testCase::tds::groupBy::testGroupByExpressionOnAdjustNowFunctions(config:TestConfig[1]):Boolean[1]
{
$config->testTdsExpression(x|$x->extend([col(x: TDSRow[1] |
$x.getDate('ReleaseDate') < now()->adjust(-15, DurationUnit.YEARS),
'Is Old?')])->groupBy('Is Old?', agg('sumBudget', r | $r.getInteger('Budget'), agg | $agg->sum())));
}

function
<<paramTest.Test>>
{doc.doc = 'Test projection on Elasticsearch with pure nested if expressions'}
meta::external::store::elasticsearch::executionTest::testCase::tds::groupBy::testGroupByExpressionOnAdjustNowFunctions2(config:TestConfig[1]):Boolean[1]
{
$config->testTdsExpression(x|$x->extend([col(x: TDSRow[1] |
$x.getDate('ReleaseDate') < now(),
'Is Old?')])->groupBy('Is Old?', agg('sumBudget', r | $r.getInteger('Budget'), agg | $agg->sum())));
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,20 @@ meta::external::store::elasticsearch::executionTest::testCase::tds::filter::date
{
let var = %2010-04-26T00:00:00.200+0000;
$config->testTdsExpression(x | $x->filter(x | $var <= $x.getDate('ReleaseDate')));
}

function
<<paramTest.Test>>
{doc.doc = 'Test less than equal filter on Elasticsearch Integer property mapping with variable'}
meta::external::store::elasticsearch::executionTest::testCase::tds::filter::date::testFilterUsingValueFromAdjustNowFunctions(config:TestConfig[1]):Boolean[1]
{
$config->testTdsExpression(x | $x->filter(x | $x.getDate('ReleaseDate') >= now()->adjust(-15, DurationUnit.YEARS)));
}

function
<<paramTest.Test>>
{doc.doc = 'Test less than equal filter on Elasticsearch Integer property mapping with variable'}
meta::external::store::elasticsearch::executionTest::testCase::tds::filter::date::testFilterUsingValueFromAdjustTodayFunctions(config:TestConfig[1]):Boolean[1]
{
$config->testTdsExpression(x | $x->filter(x | $x.getDate('ReleaseDate') >= today()->adjust(-15, DurationUnit.YEARS)));
}

0 comments on commit 5c2778b

Please sign in to comment.