Skip to content

Commit

Permalink
Add CAST from BOOLEAN to REAL
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr authored and wenleix committed May 5, 2017
1 parent 538ac1a commit fbc8af5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Expand Up @@ -32,6 +32,7 @@
import static com.facebook.presto.spi.function.OperatorType.LESS_THAN;
import static com.facebook.presto.spi.function.OperatorType.LESS_THAN_OR_EQUAL;
import static com.facebook.presto.spi.function.OperatorType.NOT_EQUAL;
import static java.lang.Float.floatToRawIntBits;
import static java.nio.charset.StandardCharsets.US_ASCII;

public final class BooleanOperators
Expand Down Expand Up @@ -99,6 +100,13 @@ public static double castToDouble(@SqlType(StandardTypes.BOOLEAN) boolean value)
return value ? 1 : 0;
}

@ScalarOperator(CAST)
@SqlType(StandardTypes.REAL)
public static long castToReal(@SqlType(StandardTypes.BOOLEAN) boolean value)
{
return value ? floatToRawIntBits(1.0f) : floatToRawIntBits(0.0f);
}

@ScalarOperator(CAST)
@SqlType(StandardTypes.BIGINT)
public static long castToBigint(@SqlType(StandardTypes.BOOLEAN) boolean value)
Expand Down
Expand Up @@ -17,6 +17,7 @@
import org.testng.annotations.Test;

import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
import static com.facebook.presto.spi.type.RealType.REAL;
import static com.facebook.presto.spi.type.VarcharType.VARCHAR;

public class TestBooleanOperators
Expand Down Expand Up @@ -112,6 +113,14 @@ public void testBetween()
assertFunction("false BETWEEN false AND false", BOOLEAN, true);
}

@Test
public void testCastToReal()
throws Exception
{
assertFunction("cast(true as real)", REAL, 1.0f);
assertFunction("cast(false as real)", REAL, 0.0f);
}

@Test
public void testCastToVarchar()
throws Exception
Expand Down

0 comments on commit fbc8af5

Please sign in to comment.