[enhancement](nereids) add syntax support for fractional literal (#12444)

Just as legacy planner, Nereids parse all fractional literal to decimal.
In the future, we will add more syntax for user to control the fractional literal type.
This commit is contained in:
Kikyou1997
2022-09-08 15:54:20 +08:00
committed by GitHub
parent 7c7ac86fe8
commit d1ab6b1db2
4 changed files with 24 additions and 10 deletions

View File

@ -432,16 +432,6 @@ DECIMAL_VALUE
: DECIMAL_DIGITS {isValidDecimal()}?
;
FLOAT_LITERAL
: DIGIT+ EXPONENT? 'F'
| DECIMAL_DIGITS EXPONENT? 'F' {isValidDecimal()}?
;
DOUBLE_LITERAL
: DIGIT+ EXPONENT? 'D'
| DECIMAL_DIGITS EXPONENT? 'D' {isValidDecimal()}?
;
BIGDECIMAL_LITERAL
: DIGIT+ EXPONENT? 'BD'
| DECIMAL_DIGITS EXPONENT? 'BD' {isValidDecimal()}?

View File

@ -294,6 +294,7 @@ quotedIdentifier
number
: MINUS? INTEGER_VALUE #integerLiteral
| MINUS? (EXPONENT_VALUE | DECIMAL_VALUE) #decimalLiteral
;

View File

@ -27,6 +27,7 @@ import org.apache.doris.nereids.DorisParser.ArithmeticUnaryContext;
import org.apache.doris.nereids.DorisParser.BooleanLiteralContext;
import org.apache.doris.nereids.DorisParser.ColumnReferenceContext;
import org.apache.doris.nereids.DorisParser.ComparisonContext;
import org.apache.doris.nereids.DorisParser.DecimalLiteralContext;
import org.apache.doris.nereids.DorisParser.DereferenceContext;
import org.apache.doris.nereids.DorisParser.ExistContext;
import org.apache.doris.nereids.DorisParser.ExplainContext;
@ -113,6 +114,7 @@ import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DateLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DateTimeLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DecimalLiteral;
import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
import org.apache.doris.nereids.trees.expressions.literal.IntervalLiteral;
import org.apache.doris.nereids.trees.expressions.literal.LargeIntLiteral;
@ -147,6 +149,7 @@ import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.RuleNode;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
@ -975,4 +978,9 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {
public List<Expression> withInList(PredicateContext ctx) {
return ctx.expression().stream().map(this::getExpression).collect(ImmutableList.toImmutableList());
}
@Override
public DecimalLiteral visitDecimalLiteral(DecimalLiteralContext ctx) {
return new DecimalLiteral(new BigDecimal(ctx.getText()));
}
}

View File

@ -21,6 +21,7 @@ import org.apache.doris.analysis.ExplainOptions;
import org.apache.doris.analysis.StatementBase;
import org.apache.doris.nereids.exceptions.ParseException;
import org.apache.doris.nereids.glue.LogicalPlanAdapter;
import org.apache.doris.nereids.trees.expressions.literal.DecimalLiteral;
import org.apache.doris.nereids.trees.plans.JoinType;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand;
@ -33,6 +34,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Set;
public class NereidsParserTest extends ParserTestBase {
@ -187,4 +189,17 @@ public class NereidsParserTest extends ParserTestBase {
logicalJoin = (LogicalJoin) logicalPlan.child(0);
Assertions.assertEquals(JoinType.CROSS_JOIN, logicalJoin.getJoinType());
}
@Test
public void parseDecimal() {
String f1 = "SELECT col1 * 0.267081789095306 FROM t";
NereidsParser nereidsParser = new NereidsParser();
LogicalPlan logicalPlan = nereidsParser.parseSingle(f1);
long doubleCount = logicalPlan
.getExpressions()
.stream()
.mapToLong(e -> e.<Set<DecimalLiteral>>collect(DecimalLiteral.class::isInstance).size())
.sum();
Assertions.assertEquals(doubleCount, 1);
}
}