-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQL: Allow long literals #31777
SQL: Allow long literals #31777
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.sql.parser; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xpack.sql.expression.Expression; | ||
import org.elasticsearch.xpack.sql.expression.Literal; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.Neg; | ||
import org.elasticsearch.xpack.sql.type.DataType; | ||
|
||
public class ExpressionTests extends ESTestCase { | ||
|
||
private final SqlParser parser = new SqlParser(); | ||
|
||
public void testLiteralLong() throws Exception { | ||
Expression lt = parser.createExpression(String.valueOf(Long.MAX_VALUE)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to test for things like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a test for it (which uncovered an interesting case) - see below. |
||
assertEquals(Literal.class, lt.getClass()); | ||
Literal l = (Literal) lt; | ||
assertEquals(Long.MAX_VALUE, l.value()); | ||
assertEquals(DataType.LONG, l.dataType()); | ||
} | ||
|
||
public void testLiteralLongNegative() throws Exception { | ||
// Long.MIN_VALUE doesn't work since it is being interpreted as negate positive.long which is 1 higher than Long.MAX_VALUE | ||
Expression lt = parser.createExpression(String.valueOf(-Long.MAX_VALUE)); | ||
assertEquals(Neg.class, lt.getClass()); | ||
Neg n = (Neg) lt; | ||
assertTrue(n.foldable()); | ||
assertEquals(-Long.MAX_VALUE, n.fold()); | ||
assertEquals(DataType.LONG, n.dataType()); | ||
} | ||
|
||
public void testLiteralInteger() throws Exception { | ||
Expression lt = parser.createExpression(String.valueOf(Integer.MAX_VALUE)); | ||
assertEquals(Literal.class, lt.getClass()); | ||
Literal l = (Literal) lt; | ||
assertEquals(Integer.MAX_VALUE, l.value()); | ||
assertEquals(DataType.INTEGER, l.dataType()); | ||
} | ||
|
||
public void testLiteralIntegerWithShortValue() throws Exception { | ||
Expression lt = parser.createExpression(String.valueOf(Short.MAX_VALUE)); | ||
assertEquals(Literal.class, lt.getClass()); | ||
Literal l = (Literal) lt; | ||
assertEquals(Integer.valueOf(Short.MAX_VALUE), l.value()); | ||
assertEquals(DataType.INTEGER, l.dataType()); | ||
} | ||
|
||
public void testLiteralIntegerWithByteValue() throws Exception { | ||
Expression lt = parser.createExpression(String.valueOf(Byte.MAX_VALUE)); | ||
assertEquals(Literal.class, lt.getClass()); | ||
Literal l = (Literal) lt; | ||
assertEquals(Integer.valueOf(Byte.MAX_VALUE), l.value()); | ||
assertEquals(DataType.INTEGER, l.dataType()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My preference would be to move
BOOL_TO_LONG
up and start using it instead ofBOOL_TO_INT
for date conversions (we can't right now since the enum entry is defined after).It's consistent and avoids the int to long conversion.