diff --git a/core/trino-parser/src/main/java/io/trino/sql/tree/LongLiteral.java b/core/trino-parser/src/main/java/io/trino/sql/tree/LongLiteral.java index 0ef8289ccbcd..17591d94ee29 100644 --- a/core/trino-parser/src/main/java/io/trino/sql/tree/LongLiteral.java +++ b/core/trino-parser/src/main/java/io/trino/sql/tree/LongLiteral.java @@ -107,12 +107,21 @@ private static long parse(String value) if (value.startsWith("0x") || value.startsWith("0X")) { return Long.parseLong(value.substring(2), 16); } + else if (value.startsWith("-0x") || value.startsWith("-0X")) { + return Long.parseLong("-" + value.substring(3), 16); + } else if (value.startsWith("0b") || value.startsWith("0B")) { return Long.parseLong(value.substring(2), 2); } + else if (value.startsWith("-0b") || value.startsWith("-0B")) { + return Long.parseLong("-" + value.substring(3), 2); + } else if (value.startsWith("0o") || value.startsWith("0O")) { return Long.parseLong(value.substring(2), 8); } + else if (value.startsWith("-0o") || value.startsWith("-0O")) { + return Long.parseLong("-" + value.substring(3), 8); + } else { return Long.parseLong(value); } diff --git a/core/trino-parser/src/test/java/io/trino/sql/parser/TestSqlParser.java b/core/trino-parser/src/test/java/io/trino/sql/parser/TestSqlParser.java index dcc7e3f4b927..9b5196cf1d8a 100644 --- a/core/trino-parser/src/test/java/io/trino/sql/parser/TestSqlParser.java +++ b/core/trino-parser/src/test/java/io/trino/sql/parser/TestSqlParser.java @@ -486,6 +486,14 @@ public void testNumbers() .isEqualTo(new LongLiteral(new NodeLocation(1, 1), "0X123_ABC_DEF")) .satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(4893429231L)); + assertThat(expression("-0x123_abc_def")) + .isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0x123_abc_def")) + .satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-4893429231L)); + + assertThat(expression("-0X123_ABC_DEF")) + .isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0X123_ABC_DEF")) + .satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-4893429231L)); + assertThatThrownBy(() -> SQL_PARSER.createExpression("0x123_ABC_DEF_")) .isInstanceOf(ParsingException.class); @@ -497,6 +505,14 @@ public void testNumbers() .isEqualTo(new LongLiteral(new NodeLocation(1, 1), "0o012_345")) .satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(5349L)); + assertThat(expression("-0O012_345")) + .isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0O012_345")) + .satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-5349L)); + + assertThat(expression("-0o012_345")) + .isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0o012_345")) + .satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-5349L)); + assertThatThrownBy(() -> SQL_PARSER.createExpression("0o012_345_")) .isInstanceOf(ParsingException.class); @@ -508,6 +524,14 @@ public void testNumbers() .isEqualTo(new LongLiteral(new NodeLocation(1, 1), "0b110_010")) .satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(50L)); + assertThat(expression("-0B110_010")) + .isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0B110_010")) + .satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-50L)); + + assertThat(expression("-0b110_010")) + .isEqualTo(new LongLiteral(new NodeLocation(1, 1), "-0b110_010")) + .satisfies(value -> assertThat(((LongLiteral) value).getParsedValue()).isEqualTo(-50L)); + assertThatThrownBy(() -> SQL_PARSER.createExpression("0b110_010_")) .isInstanceOf(ParsingException.class); }