Skip to content

Commit

Permalink
Add tests for BIGINT near 32-bit boundary in InCodeGenerator
Browse files Browse the repository at this point in the history
And also clean up TestInCodeGenerator.
  • Loading branch information
haozhun committed Nov 13, 2015
1 parent fb0e565 commit f9f529e
Showing 1 changed file with 74 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
package com.facebook.presto.sql.gen;

import com.facebook.presto.metadata.Signature;
import com.facebook.presto.sql.gen.InCodeGenerator.SwitchGenerationCase;
import com.facebook.presto.sql.relational.CallExpression;
import com.facebook.presto.sql.relational.ConstantExpression;
import com.facebook.presto.sql.relational.RowExpression;
import io.airlift.slice.Slices;
import org.testng.annotations.Test;

import java.util.ArrayList;
Expand All @@ -28,6 +28,7 @@
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static com.facebook.presto.spi.type.DateType.DATE;
import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
import static com.facebook.presto.sql.gen.InCodeGenerator.SwitchGenerationCase.DIRECT_SWITCH;
import static com.facebook.presto.sql.gen.InCodeGenerator.SwitchGenerationCase.HASH_SWITCH;
import static com.facebook.presto.sql.gen.InCodeGenerator.SwitchGenerationCase.SET_CONTAINS;
Expand All @@ -38,11 +39,11 @@
public class TestInCodeGenerator
{
@Test
public void testDirectSwitchBigint()
public void testBigintSmall()
{
List<RowExpression> values = new ArrayList<>();
values.add(new ConstantExpression(1L, BIGINT));
values.add(new ConstantExpression(2L, BIGINT));
values.add(new ConstantExpression((long) Integer.MIN_VALUE, BIGINT));
values.add(new ConstantExpression((long) Integer.MAX_VALUE, BIGINT));
values.add(new ConstantExpression(3L, BIGINT));
assertEquals(checkSwitchGenerationCase(BIGINT, values), DIRECT_SWITCH);

Expand All @@ -56,57 +57,108 @@ public void testDirectSwitchBigint()
DOUBLE.getDisplayName()
),
BIGINT,
Collections.singletonList(new ConstantExpression(5.0D, DOUBLE))
Collections.singletonList(new ConstantExpression(12345678901234.0, DOUBLE))
));
assertEquals(checkSwitchGenerationCase(BIGINT, values), DIRECT_SWITCH);

for (int i = 6; i <= 1000; ++i) {
values.add(new ConstantExpression(Long.valueOf(i), BIGINT));
for (long i = 6; i <= 1000; ++i) {
values.add(new ConstantExpression(i, BIGINT));
}
assertEquals(checkSwitchGenerationCase(BIGINT, values), DIRECT_SWITCH);

values.add(new ConstantExpression(1001L, BIGINT));
assertEquals(checkSwitchGenerationCase(BIGINT, values), SET_CONTAINS);
}

@Test
public void testDirectSwitchDate()
public void testBigintLarge()
{
List<RowExpression> values = new ArrayList<>();
values.add(new ConstantExpression(Integer.MAX_VALUE + 1L, BIGINT));
values.add(new ConstantExpression(Integer.MIN_VALUE - 1L, BIGINT));
values.add(new ConstantExpression(3L, BIGINT));
assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);

values.add(new ConstantExpression(null, BIGINT));
assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);
values.add(new CallExpression(
new Signature(
CAST,
SCALAR,
BIGINT.getDisplayName(),
DOUBLE.getDisplayName()
),
BIGINT,
Collections.singletonList(new ConstantExpression(12345678901234.0, DOUBLE))
));
assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);

for (long i = 6; i <= 1000; ++i) {
values.add(new ConstantExpression(i, BIGINT));
}
assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);

values.add(new ConstantExpression(1001L, BIGINT));
assertEquals(checkSwitchGenerationCase(BIGINT, values), SET_CONTAINS);
}

@Test
public void testDate()
{
List<RowExpression> values = new ArrayList<>();
values.add(new ConstantExpression(1L, DATE));
values.add(new ConstantExpression(2L, DATE));
values.add(new ConstantExpression(3L, DATE));
assertEquals(checkSwitchGenerationCase(DATE, values), DIRECT_SWITCH);

for (long i = 4; i <= 1000; ++i) {
values.add(new ConstantExpression(i, DATE));
}
assertEquals(checkSwitchGenerationCase(DATE, values), DIRECT_SWITCH);

values.add(new ConstantExpression(1001L, DATE));
assertEquals(checkSwitchGenerationCase(DATE, values), SET_CONTAINS);
}

@Test
public void testHashSwitch()
public void testDouble()
{
List<RowExpression> values = new ArrayList<>();
values.add(new ConstantExpression(1.5D, DOUBLE));
values.add(new ConstantExpression(2.5D, DOUBLE));
values.add(new ConstantExpression(3.5D, DOUBLE));
values.add(new ConstantExpression(1.5, DOUBLE));
values.add(new ConstantExpression(2.5, DOUBLE));
values.add(new ConstantExpression(3.5, DOUBLE));
assertEquals(checkSwitchGenerationCase(DOUBLE, values), HASH_SWITCH);

values.add(new ConstantExpression(null, DOUBLE));
assertEquals(checkSwitchGenerationCase(DOUBLE, values), HASH_SWITCH);

for (int i = 5; i <= 1000; ++i) {
values.add(new ConstantExpression(Double.valueOf(i + 0.5D), DOUBLE));
values.add(new ConstantExpression(i + 0.5, DOUBLE));
}
assertEquals(checkSwitchGenerationCase(DOUBLE, values), HASH_SWITCH);

values.add(new ConstantExpression(1001.5, DOUBLE));
assertEquals(checkSwitchGenerationCase(DOUBLE, values), SET_CONTAINS);
}

@Test
public void testSetContains()
public void testVarchar()
{
List<RowExpression> values = new ArrayList<>();
for (int i = 1; i <= 1001; ++i) {
values.add(new ConstantExpression(Long.valueOf(i), BIGINT));
}
SwitchGenerationCase switchGenerationCase = checkSwitchGenerationCase(BIGINT, values);
assertEquals(switchGenerationCase, SET_CONTAINS);
values.add(new ConstantExpression(Slices.utf8Slice("1"), DOUBLE));
values.add(new ConstantExpression(Slices.utf8Slice("2"), DOUBLE));
values.add(new ConstantExpression(Slices.utf8Slice("3"), DOUBLE));
assertEquals(checkSwitchGenerationCase(VARCHAR, values), HASH_SWITCH);

for (int i = 1; i <= 1001; ++i) {
values.set(i - 1, new ConstantExpression(Double.valueOf(i + 0.5D), DOUBLE));
values.add(new ConstantExpression(null, VARCHAR));
assertEquals(checkSwitchGenerationCase(VARCHAR, values), HASH_SWITCH);

for (int i = 5; i <= 1000; ++i) {
values.add(new ConstantExpression(Slices.utf8Slice(String.valueOf(i)), VARCHAR));
}
assertEquals(checkSwitchGenerationCase(BIGINT, values), SET_CONTAINS);
assertEquals(checkSwitchGenerationCase(VARCHAR, values), HASH_SWITCH);

values.add(new ConstantExpression(Slices.utf8Slice("1001"), VARCHAR));
assertEquals(checkSwitchGenerationCase(VARCHAR, values), SET_CONTAINS);
}
}

0 comments on commit f9f529e

Please sign in to comment.