Skip to content

Commit

Permalink
Use length of VarcharType and CharType to calculate EXPECTED_ENTRY_SIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
raunaqmorarka authored and sopel39 committed Jul 30, 2020
1 parent deb9c13 commit 547eb1a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
12 changes: 12 additions & 0 deletions presto-main/src/main/java/io/prestosql/type/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import io.prestosql.spi.block.Block;
import io.prestosql.spi.block.BlockBuilder;
import io.prestosql.spi.type.ArrayType;
import io.prestosql.spi.type.CharType;
import io.prestosql.spi.type.FixedWidthType;
import io.prestosql.spi.type.MapType;
import io.prestosql.spi.type.RowType;
import io.prestosql.spi.type.StandardTypes;
import io.prestosql.spi.type.TimestampType;
import io.prestosql.spi.type.TimestampWithTimeZoneType;
import io.prestosql.spi.type.Type;
import io.prestosql.spi.type.VarcharType;

import java.lang.invoke.MethodHandle;
import java.util.List;
Expand Down Expand Up @@ -57,6 +59,16 @@ public static int expectedValueSize(Type type, int defaultSize)
if (type instanceof FixedWidthType) {
return ((FixedWidthType) type).getFixedSize();
}
// If bound on length of varchar or char is smaller than defaultSize, use that as expected size
// The data can take up to 4 bytes per character due to UTF-8 encoding, but we assume it is ASCII and only needs one byte.
if (type instanceof VarcharType) {
return ((VarcharType) type).getLength()
.map(length -> Math.min(length, defaultSize))
.orElse(defaultSize);
}
if (type instanceof CharType) {
return Math.min(((CharType) type).getLength(), defaultSize);
}
return defaultSize;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class AbstractVariableWidthType
extends AbstractType
implements VariableWidthType
{
private static final int EXPECTED_BYTES_PER_ENTRY = 32;
protected static final int EXPECTED_BYTES_PER_ENTRY = 32;

protected AbstractVariableWidthType(TypeSignature signature, Class<?> javaType)
{
Expand Down
9 changes: 9 additions & 0 deletions presto-spi/src/main/java/io/prestosql/spi/type/CharType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.block.Block;
import io.prestosql.spi.block.BlockBuilder;
import io.prestosql.spi.block.BlockBuilderStatus;
import io.prestosql.spi.connector.ConnectorSession;

import java.util.Objects;
Expand Down Expand Up @@ -108,6 +109,14 @@ public int compareTo(Block leftBlock, int leftPosition, Block rightBlock, int ri
return compareChars(leftSlice, rightSlice);
}

@Override
public BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
// If bound on length of char is smaller than EXPECTED_BYTES_PER_ENTRY, use that as expectedBytesPerEntry
// The data can take up to 4 bytes per character due to UTF-8 encoding, but we assume it is ASCII and only needs one byte.
return createBlockBuilder(blockBuilderStatus, expectedEntries, Math.min(length, EXPECTED_BYTES_PER_ENTRY));
}

@Override
public void appendTo(Block block, int position, BlockBuilder blockBuilder)
{
Expand Down
14 changes: 14 additions & 0 deletions presto-spi/src/main/java/io/prestosql/spi/type/VarcharType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.airlift.slice.Slices;
import io.prestosql.spi.block.Block;
import io.prestosql.spi.block.BlockBuilder;
import io.prestosql.spi.block.BlockBuilderStatus;
import io.prestosql.spi.connector.ConnectorSession;

import java.util.Objects;
Expand Down Expand Up @@ -136,6 +137,19 @@ public int compareTo(Block leftBlock, int leftPosition, Block rightBlock, int ri
return leftBlock.compareTo(leftPosition, 0, leftLength, rightBlock, rightPosition, 0, rightLength);
}

@Override
public BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
return createBlockBuilder(
blockBuilderStatus,
expectedEntries,
getLength()
// If bound on length is smaller than EXPECTED_BYTES_PER_ENTRY, use that as expectedBytesPerEntry
// The data can take up to 4 bytes per character due to UTF-8 encoding, but we assume it is ASCII and only needs one byte.
.map(length -> Math.min(length, EXPECTED_BYTES_PER_ENTRY))
.orElse(EXPECTED_BYTES_PER_ENTRY));
}

@Override
public Optional<Range> getRange()
{
Expand Down

0 comments on commit 547eb1a

Please sign in to comment.