9595import org .elasticsearch .xpack .sql .parser .SqlBaseParser .MatchQueryContext ;
9696import org .elasticsearch .xpack .sql .parser .SqlBaseParser .MatchQueryOptionsContext ;
9797import org .elasticsearch .xpack .sql .parser .SqlBaseParser .MultiMatchQueryContext ;
98+ import org .elasticsearch .xpack .sql .parser .SqlBaseParser .NamedValueExpressionContext ;
9899import org .elasticsearch .xpack .sql .parser .SqlBaseParser .NullLiteralContext ;
99100import org .elasticsearch .xpack .sql .parser .SqlBaseParser .NumberContext ;
100101import org .elasticsearch .xpack .sql .parser .SqlBaseParser .OrderByContext ;
@@ -856,24 +857,19 @@ public Literal visitGuidEscapedLiteral(GuidEscapedLiteralContext ctx) {
856857 */
857858 private static Tuple <Source , String > withMinus (NumberContext ctx ) {
858859 String string = ctx .getText ();
859- Source source = minusAwareSource (ctx );
860-
861- if (source != null ) {
862- string = "-" + string ;
863- } else {
864- source = source (ctx );
865- }
866-
867- return new Tuple <>(source , string );
860+ Tuple <Source , Boolean > tuple = minusAwareSource (ctx );
861+ return new Tuple <>(tuple .v1 (), (tuple .v2 () ? "-" : "" ) + string );
868862 }
869863
870864 /**
871- * Checks the presence of MINUS (-) in the parent and if found,
872- * returns the parent source or null otherwise.
865+ * Checks the presence of MINUS (-) in the parent and if found, returns the parent source
866+ * along with a boolean denoting if there is an odd number of MINUSes.
867+ * If not found, returns the original source.
868+ *
873869 * Parsing of the value should not depend on the returned source
874870 * as it might contain extra spaces.
875871 */
876- private static Source minusAwareSource (SqlBaseParser .NumberContext ctx ) {
872+ private static Tuple < Source , Boolean > minusAwareSource (SqlBaseParser .NumberContext ctx ) {
877873 ParserRuleContext parentCtx = ctx .getParent ();
878874 if (parentCtx != null ) {
879875 if (parentCtx instanceof SqlBaseParser .NumericLiteralContext ) {
@@ -883,40 +879,42 @@ private static Source minusAwareSource(SqlBaseParser.NumberContext ctx) {
883879 if (parentCtx instanceof ValueExpressionDefaultContext ) {
884880 parentCtx = parentCtx .getParent ();
885881
886- // Skip parentheses, e.g.: - (( (2.15) ) )
887- while (parentCtx instanceof PredicatedContext ) {
888- parentCtx = parentCtx .getParent ();
889- if (parentCtx instanceof SqlBaseParser .BooleanDefaultContext ) {
890- parentCtx = parentCtx .getParent ();
882+ ParserRuleContext returnCtx = null ;
883+ boolean minus = false ;
884+ while (parentCtx != null ) {
885+ // Stop when we meet a higher level context to avoid unnecessary looping
886+ if (parentCtx instanceof ArithmeticBinaryContext
887+ || parentCtx instanceof ExtractTemplateContext
888+ || parentCtx instanceof NamedValueExpressionContext
889+ || parentCtx instanceof PredicateContext // Not PredicatedContext !!
890+ || parentCtx instanceof ComparisonContext ) {
891+ break ;
891892 }
892- if (parentCtx instanceof SqlBaseParser .ExpressionContext ) {
893- parentCtx = parentCtx .getParent ();
894- }
895- if (parentCtx instanceof SqlBaseParser .ParenthesizedExpressionContext ) {
896- parentCtx = parentCtx .getParent ();
897- }
898- if (parentCtx instanceof ValueExpressionDefaultContext ) {
899- parentCtx = parentCtx .getParent ();
893+ if (parentCtx instanceof ArithmeticUnaryContext ) {
894+ returnCtx = parentCtx ;
895+ if (((ArithmeticUnaryContext ) parentCtx ).MINUS () != null ) {
896+ minus ^= true ;
897+ }
900898 }
899+ parentCtx = parentCtx .getParent ();
901900 }
902- if (parentCtx instanceof ArithmeticUnaryContext ) {
903- if (((ArithmeticUnaryContext ) parentCtx ).MINUS () != null ) {
904- return source (parentCtx );
905- }
901+ if (returnCtx != null ) {
902+ return new Tuple <>(source (returnCtx ), minus );
906903 }
907904 }
908905 }
909- } else if (parentCtx instanceof SqlBaseParser .IntervalContext ) {
906+ // Intervals and SysTypes can only have a single "-" as parentheses are not allowed there
907+ } else if (parentCtx instanceof IntervalContext ) {
910908 IntervalContext ic = (IntervalContext ) parentCtx ;
911909 if (ic .sign != null && ic .sign .getType () == SqlBaseParser .MINUS ) {
912- return source (ic );
910+ return new Tuple <>( source (ic ), true );
913911 }
914- } else if (parentCtx instanceof SqlBaseParser . SysTypesContext ) {
912+ } else if (parentCtx instanceof SysTypesContext ) {
915913 if (((SysTypesContext ) parentCtx ).MINUS () != null ) {
916- return source (parentCtx );
914+ return new Tuple <>( source (parentCtx ), true );
917915 }
918916 }
919917 }
920- return null ;
918+ return new Tuple <>( source ( ctx ), false ) ;
921919 }
922920}
0 commit comments