Skip to content

Fix sqlancer#1209: UNARY_PLUS operator now converts values to DOUBLE PRECISION #1220

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

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
Fix #1209: UNARY_PLUS operator now converts values to DOUBLE PRECISION
  • Loading branch information
guptapratykshh committed Apr 8, 2025
commit d731fa6946058370056884f8e67ad351c62bbcf8
32 changes: 32 additions & 0 deletions src/sqlancer/materialize/ast/MaterializeConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ public String getUnquotedTextRepresentation() {
return getTextRepresentation();
}

@Override
public double asDouble() {
return (double) val;
}
}

public static MaterializeConstant createNullConstant() {
Expand Down Expand Up @@ -363,10 +367,18 @@ public long asInt() {
throw new UnsupportedOperationException(this.toString());
}

public double asDouble() {
throw new UnsupportedOperationException(this.toString());
}

public boolean isBoolean() {
return false;
}

public boolean isFloat() {
return false;
}

public abstract MaterializeConstant isEquals(MaterializeConstant rightVal);

public boolean isInt() {
Expand Down Expand Up @@ -451,6 +463,16 @@ public MaterializeDataType getExpressionType() {
return MaterializeDataType.FLOAT;
}

@Override
public boolean isFloat() {
return true;
}

@Override
public double asDouble() {
return val;
}

}

public static class DoubleConstant extends MaterializeConstantBase {
Expand All @@ -475,6 +497,16 @@ public MaterializeDataType getExpressionType() {
return MaterializeDataType.FLOAT;
}

@Override
public double asDouble() {
return val;
}

@Override
public boolean isFloat() {
return true;
}

}

public static class BitConstant extends MaterializeConstantBase {
Expand Down
32 changes: 27 additions & 5 deletions src/sqlancer/materialize/ast/MaterializePrefixOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,35 @@ protected MaterializeConstant getExpectedValue(MaterializeConstant expectedValue

@Override
public MaterializeDataType getExpressionType() {
return MaterializeDataType.INT;
return MaterializeDataType.FLOAT;
}

@Override
protected MaterializeConstant getExpectedValue(MaterializeConstant expectedValue) {
// TODO: actual converts to double precision
return expectedValue;
if (expectedValue.isNull()) {
return MaterializeConstant.createNullConstant();
}
double doubleValue;
if (expectedValue.isInt()) {
doubleValue = expectedValue.asDouble();
} else if (expectedValue.isBoolean()) {
doubleValue = expectedValue.asBoolean() ? 1.0 : 0.0;
} else if (expectedValue.isString()) {
try {
doubleValue = Double.parseDouble(expectedValue.asString());
} catch (NumberFormatException e) {
return MaterializeConstant.createNullConstant();
}
} else if (expectedValue.isFloat()) {
doubleValue = expectedValue.asDouble();
} else {
try {
doubleValue = expectedValue.asDouble();
} catch (UnsupportedOperationException e) {
return MaterializeConstant.createNullConstant();
}
}
return MaterializeConstant.createDoubleConstant(doubleValue);
}

},
Expand Down Expand Up @@ -63,8 +85,8 @@ protected MaterializeConstant getExpectedValue(MaterializeConstant expectedValue

};

private String textRepresentation;
private MaterializeDataType[] dataTypes;
private final String textRepresentation;
private final MaterializeDataType[] dataTypes;

PrefixOperator(String textRepresentation, MaterializeDataType... dataTypes) {
this.textRepresentation = textRepresentation;
Expand Down
53 changes: 53 additions & 0 deletions test/sqlancer/qpg/materialize/TestMaterializeUnaryPlus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package sqlancer.qpg.materialize;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

import sqlancer.materialize.MaterializeSchema.MaterializeDataType;
import sqlancer.materialize.ast.MaterializeConstant;
import sqlancer.materialize.ast.MaterializeExpression;
import sqlancer.materialize.ast.MaterializePrefixOperation;
import sqlancer.materialize.ast.MaterializePrefixOperation.PrefixOperator;

public class TestMaterializeUnaryPlus {

@Test
public void testUnaryPlusConvertToDouble() {
// Test UNARY_PLUS on int
MaterializeConstant intConstant = MaterializeConstant.createIntConstant(42);
MaterializeExpression unaryPlusInt = new MaterializePrefixOperation(intConstant, PrefixOperator.UNARY_PLUS);
MaterializeConstant result = unaryPlusInt.getExpectedValue();

// Verify result type is FLOAT (double precision)
assertEquals(MaterializeDataType.FLOAT, unaryPlusInt.getExpressionType());

// Verify value is converted to double
assertTrue(result.isFloat());
assertEquals(42.0, result.asDouble());

// Test UNARY_PLUS on boolean
MaterializeConstant boolConstant = MaterializeConstant.createBooleanConstant(true);
MaterializeExpression unaryPlusBool = new MaterializePrefixOperation(boolConstant, PrefixOperator.UNARY_PLUS);
result = unaryPlusBool.getExpectedValue();

// Verify result type is FLOAT
assertEquals(MaterializeDataType.FLOAT, unaryPlusBool.getExpressionType());

// Verify value is converted to double (true becomes 1.0)
assertTrue(result.isFloat());
assertEquals(1.0, result.asDouble());

// Test UNARY_PLUS on string
MaterializeConstant stringConstant = MaterializeConstant.createTextConstant("123.45");
MaterializeExpression unaryPlusString = new MaterializePrefixOperation(stringConstant, PrefixOperator.UNARY_PLUS);
result = unaryPlusString.getExpectedValue();

// Verify result type is FLOAT
assertEquals(MaterializeDataType.FLOAT, unaryPlusString.getExpressionType());

// Verify value is converted to double
assertTrue(result.isFloat());
assertEquals(123.45, result.asDouble());
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy