@@ -286,6 +286,9 @@ public Object visitArithmeticUnary(ArithmeticUnaryContext ctx) {
286
286
case SqlBaseParser .PLUS :
287
287
return value ;
288
288
case SqlBaseParser .MINUS :
289
+ if (value instanceof Literal ) { // Minus already processed together with literal number
290
+ return value ;
291
+ }
289
292
return new Neg (source (ctx .operator ), value );
290
293
default :
291
294
throw new ParsingException (loc , "Unknown arithemtic {}" , ctx .operator .getText ());
@@ -483,38 +486,40 @@ public Expression visitStringLiteral(StringLiteralContext ctx) {
483
486
484
487
@ Override
485
488
public Literal visitDecimalLiteral (DecimalLiteralContext ctx ) {
489
+ String ctxText = (hasMinusFromParent (ctx ) ? "-" : "" ) + ctx .getText ();
486
490
double value ;
487
491
try {
488
- value = Double .parseDouble (ctx . getText () );
492
+ value = Double .parseDouble (ctxText );
489
493
} catch (NumberFormatException nfe ) {
490
- throw new ParsingException (source (ctx ), "Cannot parse number [{}]" , ctx . getText () );
494
+ throw new ParsingException (source (ctx ), "Cannot parse number [{}]" , ctxText );
491
495
}
492
496
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 );
494
498
}
495
499
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 );
497
501
}
498
502
return new Literal (source (ctx ), Double .valueOf (value ), DataType .DOUBLE );
499
503
}
500
504
501
505
@ Override
502
506
public Literal visitIntegerLiteral (IntegerLiteralContext ctx ) {
507
+ String ctxText = (hasMinusFromParent (ctx ) ? "-" : "" ) + ctx .getText ();
503
508
long value ;
504
509
try {
505
- value = Long .parseLong (ctx . getText () );
510
+ value = Long .parseLong (ctxText );
506
511
} catch (NumberFormatException nfe ) {
507
512
try {
508
- BigInteger bi = new BigInteger (ctx . getText () );
513
+ BigInteger bi = new BigInteger (ctxText );
509
514
try {
510
515
bi .longValueExact ();
511
516
} 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 );
513
518
}
514
519
} catch (NumberFormatException ex ) {
515
520
// parsing fails, go through
516
521
}
517
- throw new ParsingException (source (ctx ), "Cannot parse number [{}]" , ctx . getText () );
522
+ throw new ParsingException (source (ctx ), "Cannot parse number [{}]" , ctxText );
518
523
}
519
524
520
525
DataType type = DataType .LONG ;
@@ -681,4 +686,21 @@ public Literal visitGuidEscapedLiteral(GuidEscapedLiteralContext ctx) {
681
686
682
687
return new Literal (source (ctx ), string , DataType .KEYWORD );
683
688
}
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
+ }
684
706
}
0 commit comments