Skip to content
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

IGNITE-23873 Sql. Fix processing Infinite and NaN values after calcite version was bumped #5060

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;

/**
Expand Down Expand Up @@ -129,6 +130,34 @@ public void exactWithApproxComparison() {
assertQuery("SELECT '1.1'::float > 2").returns(false).check();
}

@ParameterizedTest
@CsvSource({"FLOAT", "REAL", "DOUBLE"})
public void nonFiniteNumerics(String type) {
Number positiveInfinity;
Number negativeInfinity;
Number nan;

if ("DOUBLE".equals(type)) {
positiveInfinity = Double.POSITIVE_INFINITY;
negativeInfinity = Double.NEGATIVE_INFINITY;
nan = Double.NaN;
} else {
positiveInfinity = Float.POSITIVE_INFINITY;
negativeInfinity = Float.NEGATIVE_INFINITY;
nan = Float.NaN;
}

assertQuery(format("select CAST('+Infinity' AS {})", type)).returns(positiveInfinity).check();
assertQuery(format("select CAST('Infinity' AS {})", type)).returns(positiveInfinity).check();
assertQuery(format("select CAST('-Infinity' AS {})", type)).returns(negativeInfinity).check();
assertQuery(format("select CAST('NaN' AS {})", type)).returns(nan).check();

assertQuery(format("SELECT * FROM (VALUES(0)) AS t(k) WHERE k < CAST('+Infinity' AS REAL)", type)).returns(0).check();
assertQuery(format("SELECT * FROM (VALUES(0)) AS t(k) WHERE k < CAST('Infinity' AS REAL)", type)).returns(0).check();
assertQuery(format("SELECT * FROM (VALUES(0)) AS t(k) WHERE k > CAST('-Infinity' AS REAL)", type)).returns(0).check();
assertQuery(format("SELECT * FROM (VALUES(0)) AS t(k) WHERE k <> CAST('NaN' AS REAL)", type)).returns(0).check();
}

@WithSystemProperty(key = "IMPLICIT_PK_ENABLED", value = "true")
@ParameterizedTest()
@MethodSource("exactDecimalTypes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,12 +881,6 @@ RexNode toRex(RelInput relInput, Object o) {
literal = new BigDecimal(((Number) literal).longValue());
}

// Stub, it need to be fixed https://issues.apache.org/jira/browse/IGNITE-23873
if (type.getSqlTypeName() == SqlTypeName.DOUBLE && literal instanceof String
&& Double.isNaN(Double.parseDouble(literal.toString()))) {
literal = Double.NaN;
}

if (literal instanceof BigInteger) {
// If the literal is a BigInteger, RexBuilder assumes it represents a long value
// within the valid range and converts it without checking the bounds. If the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,37 @@ public RexNode makeLiteral(@Nullable Object value, RelDataType type, boolean all
// IgniteCustomType: Not comparable types are not supported.
assert value instanceof Comparable : "Not comparable IgniteCustomType:" + type + ". value: " + value;
return makeLiteral((Comparable<?>) value, type, type.getSqlTypeName());
} else if (value != null && type.getSqlTypeName() == SqlTypeName.CHAR) {
if (type.isNullable()) {
RelDataType typeNotNull =
typeFactory.createTypeWithNullability(type, false);
if (allowCast) {
RexNode literalNotNull = makeLiteral(value, typeNotNull, allowCast);
return makeAbstractCast(type, literalNotNull, false);
}

if (value != null) {
if (type.getSqlTypeName() == SqlTypeName.CHAR) {
if (type.isNullable()) {
RelDataType typeNotNull =
typeFactory.createTypeWithNullability(type, false);
if (allowCast) {
RexNode literalNotNull = makeLiteral(value, typeNotNull, allowCast);
return makeAbstractCast(type, literalNotNull, false);
}
}
}

NlsString string;
if (value instanceof NlsString) {
string = (NlsString) value;
} else {
assert type.getCharset() != null : type + ".getCharset() must not be null";
string = new NlsString((String) value, type.getCharset().name(), type.getCollation());
}
NlsString string;
if (value instanceof NlsString) {
string = (NlsString) value;
} else {
assert type.getCharset() != null : type + ".getCharset() must not be null";
string = new NlsString((String) value, type.getCharset().name(), type.getCollation());
}

return makeCharLiteral(string);
} else {
return super.makeLiteral(value, type, allowCast, trim);
return makeCharLiteral(string);
} else if (value instanceof String) {
if (type.getSqlTypeName() == SqlTypeName.DOUBLE) {
value = Double.parseDouble((String) value);
} else if (type.getSqlTypeName() == SqlTypeName.REAL || type.getSqlTypeName() == SqlTypeName.FLOAT) {
value = Float.parseFloat((String) value);
}
}
}

return super.makeLiteral(value, type, allowCast, trim);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@ public static float convertToFloatExact(Number x) {
return value.floatValue();
} else {
double v = x.doubleValue();

if (!Double.isFinite(v)) {
return x.floatValue();
}

if (v > UPPER_FLOAT_DOUBLE || v < LOWER_FLOAT_DOUBLE) {
throw outOfRangeForTypeException(SqlTypeName.REAL);
}
Expand Down