@@ -286,6 +286,9 @@ public Object visitArithmeticUnary(ArithmeticUnaryContext ctx) {
286286 case SqlBaseParser .PLUS :
287287 return value ;
288288 case SqlBaseParser .MINUS :
289+ if (value instanceof Literal ) { // Minus already processed together with literal number
290+ return value ;
291+ }
289292 return new Neg (source (ctx .operator ), value );
290293 default :
291294 throw new ParsingException (loc , "Unknown arithemtic {}" , ctx .operator .getText ());
@@ -483,38 +486,40 @@ public Expression visitStringLiteral(StringLiteralContext ctx) {
483486
484487 @ Override
485488 public Literal visitDecimalLiteral (DecimalLiteralContext ctx ) {
489+ String ctxText = (hasMinusFromParent (ctx ) ? "-" : "" ) + ctx .getText ();
486490 double value ;
487491 try {
488- value = Double .parseDouble (ctx . getText () );
492+ value = Double .parseDouble (ctxText );
489493 } catch (NumberFormatException nfe ) {
490- throw new ParsingException (source (ctx ), "Cannot parse number [{}]" , ctx . getText () );
494+ throw new ParsingException (source (ctx ), "Cannot parse number [{}]" , ctxText );
491495 }
492496 if (Double .isInfinite (value )) {
493- throw new ParsingException (source (ctx ), "Number [{}] is too large" , ctx . getText () );
497+ throw new ParsingException (source (ctx ), "Number [{}] is too large" , ctxText );
494498 }
495499 if (Double .isNaN (value )) {
496- throw new ParsingException (source (ctx ), "[{}] cannot be parsed as a number (NaN)" , ctx . getText () );
500+ throw new ParsingException (source (ctx ), "[{}] cannot be parsed as a number (NaN)" , ctxText );
497501 }
498502 return new Literal (source (ctx ), Double .valueOf (value ), DataType .DOUBLE );
499503 }
500504
501505 @ Override
502506 public Literal visitIntegerLiteral (IntegerLiteralContext ctx ) {
507+ String ctxText = (hasMinusFromParent (ctx ) ? "-" : "" ) + ctx .getText ();
503508 long value ;
504509 try {
505- value = Long .parseLong (ctx . getText () );
510+ value = Long .parseLong (ctxText );
506511 } catch (NumberFormatException nfe ) {
507512 try {
508- BigInteger bi = new BigInteger (ctx . getText () );
513+ BigInteger bi = new BigInteger (ctxText );
509514 try {
510515 bi .longValueExact ();
511516 } catch (ArithmeticException ae ) {
512- throw new ParsingException (source (ctx ), "Number [{}] is too large" , ctx . getText () );
517+ throw new ParsingException (source (ctx ), "Number [{}] is too large" , ctxText );
513518 }
514519 } catch (NumberFormatException ex ) {
515520 // parsing fails, go through
516521 }
517- throw new ParsingException (source (ctx ), "Cannot parse number [{}]" , ctx . getText () );
522+ throw new ParsingException (source (ctx ), "Cannot parse number [{}]" , ctxText );
518523 }
519524
520525 DataType type = DataType .LONG ;
@@ -681,4 +686,21 @@ public Literal visitGuidEscapedLiteral(GuidEscapedLiteralContext ctx) {
681686
682687 return new Literal (source (ctx ), string , DataType .KEYWORD );
683688 }
689+
690+ private boolean hasMinusFromParent (SqlBaseParser .NumberContext ctx ) {
691+ ParserRuleContext parentCtx = ctx .getParent ();
692+ if (parentCtx != null && parentCtx instanceof SqlBaseParser .NumericLiteralContext ) {
693+ parentCtx = parentCtx .getParent ();
694+ if (parentCtx != null && parentCtx instanceof SqlBaseParser .ConstantDefaultContext ) {
695+ parentCtx = parentCtx .getParent ();
696+ if (parentCtx != null && parentCtx instanceof SqlBaseParser .ValueExpressionDefaultContext ) {
697+ parentCtx = parentCtx .getParent ();
698+ if (parentCtx != null && parentCtx instanceof SqlBaseParser .ArithmeticUnaryContext ) {
699+ return ((ArithmeticUnaryContext ) parentCtx ).MINUS () != null ;
700+ }
701+ }
702+ }
703+ }
704+ return false ;
705+ }
684706}
0 commit comments