@@ -904,19 +904,48 @@ public SearchLiteral visitSearchLiteral(OpenSearchPPLParser.SearchLiteralContext
904904 return new SearchLiteral (new Literal (ctx .getText (), DataType .STRING ), false );
905905 }
906906
907+ @ Override
908+ public UnresolvedExpression visitTimeModifierValue (
909+ OpenSearchPPLParser .TimeModifierValueContext ctx ) {
910+ String osDateMathExpression ;
911+ // Convert unix timestamp from seconds to milliseconds for decimal and integer
912+ // as OpenSearch time range accepts unix milliseconds in place of timestamp values
913+ if (ctx .DECIMAL_LITERAL () != null ) {
914+ String decimal = ctx .DECIMAL_LITERAL ().getText ();
915+ BigDecimal unixSecondDecimal = new BigDecimal (decimal );
916+ BigDecimal unixMilliDecimal =
917+ unixSecondDecimal .multiply (BigDecimal .valueOf (1000 )).stripTrailingZeros ();
918+ osDateMathExpression = unixMilliDecimal .toString ();
919+ } else if (ctx .INTEGER_LITERAL () != null ) {
920+ String integer = ctx .INTEGER_LITERAL ().getText ();
921+ osDateMathExpression = String .valueOf (Long .parseLong (integer ) * 1000 );
922+ } else if (ctx .NOW () != null ) { // Converts both NOW and NOW()
923+ // OpenSearch time range accepts "now" as a reference to the current time
924+ osDateMathExpression = ctx .NOW ().getText ().toLowerCase (Locale .ROOT );
925+ } else {
926+ // Process absolute and relative time modifier values
927+ String pplTimeModifier =
928+ ctx .stringLiteral () != null
929+ ? (String ) ((Literal ) visit (ctx .stringLiteral ())).getValue ()
930+ : ctx .getText ().strip ();
931+ // Parse a PPL time modifier to OpenSearch date math expression
932+ osDateMathExpression = DateTimeUtils .parseRelativeTime (pplTimeModifier );
933+ }
934+ return AstDSL .stringLiteral (osDateMathExpression );
935+ }
936+
907937 /**
908938 * Process time range expressions (EARLIEST='value' or LATEST='value') It creates a Comparison
909939 * filter like @timestamp >= timeModifierValue
910940 */
911941 @ Override
912942 public UnresolvedExpression visitTimeModifierExpression (
913943 OpenSearchPPLParser .TimeModifierExpressionContext ctx ) {
914- String pplTimeModifierExpression =
915- stripSingleQuote (ctx .timeModifier ().timeModifierValue ().getText ().strip ());
916944
917- String osDateMathExpression = DateTimeUtils .parseRelativeTime (pplTimeModifierExpression );
918- SearchLiteral osDateMathLiteral =
919- new SearchLiteral (AstDSL .stringLiteral (osDateMathExpression ), false );
945+ Literal timeModifierValue =
946+ (Literal ) visitTimeModifierValue (ctx .timeModifier ().timeModifierValue ());
947+
948+ SearchLiteral osDateMathLiteral = new SearchLiteral (timeModifierValue , false );
920949
921950 Field implicitTimestampField =
922951 new Field (new QualifiedName (OpenSearchConstants .IMPLICIT_FIELD_TIMESTAMP ), List .of ());
@@ -926,11 +955,4 @@ public UnresolvedExpression visitTimeModifierExpression(
926955 : SearchComparison .Operator .LESS_OR_EQUAL ;
927956 return new SearchComparison (implicitTimestampField , operator , osDateMathLiteral );
928957 }
929-
930- private static String stripSingleQuote (String text ) {
931- if (text .length () >= 2 && text .startsWith ("'" ) && text .endsWith ("'" )) {
932- return text .substring (1 , text .length () - 1 );
933- }
934- return text ;
935- }
936958}
0 commit comments