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

Make MapType Comparable #2469

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
package com.facebook.presto.type;

import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.StandardErrorCode;
import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.block.BlockBuilder;
import com.facebook.presto.spi.block.BlockBuilderStatus;
Expand All @@ -33,6 +31,7 @@

import static com.facebook.presto.type.TypeUtils.appendToBlockBuilder;
import static com.facebook.presto.type.TypeUtils.buildStructuralSlice;
import static com.facebook.presto.type.TypeUtils.checkElementNotNull;
import static com.facebook.presto.type.TypeUtils.parameterizedTypeName;
import static com.facebook.presto.type.TypeUtils.readStructuralBlock;
import static com.google.common.base.Preconditions.checkNotNull;
Expand All @@ -41,6 +40,7 @@ public class ArrayType
extends AbstractVariableWidthType
{
private final Type elementType;
private static final String ARRAY_NULL_ELEMENT_MSG = "ARRAY comparison not supported for arrays with null elements";

public ArrayType(Type elementType)
{
Expand Down Expand Up @@ -90,7 +90,7 @@ public int hash(Block block, int position)
Block array = readStructuralBlock(value);
List<Integer> hashArray = new ArrayList<>(array.getPositionCount());
for (int i = 0; i < array.getPositionCount(); i++) {
checkElementNotNull(array.isNull(i));
checkElementNotNull(array.isNull(i), ARRAY_NULL_ELEMENT_MSG);
hashArray.add(elementType.hash(array, i));
}
return Objects.hash(hashArray);
Expand All @@ -107,8 +107,8 @@ public int compareTo(Block leftBlock, int leftPosition, Block rightBlock, int ri
int len = Math.min(leftArray.getPositionCount(), rightArray.getPositionCount());
int index = 0;
while (index < len) {
checkElementNotNull(leftArray.isNull(index));
checkElementNotNull(rightArray.isNull(index));
checkElementNotNull(leftArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
checkElementNotNull(rightArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
int comparison = elementType.compareTo(leftArray, index, rightArray, index);
if (comparison != 0) {
return comparison;
Expand All @@ -123,13 +123,6 @@ public int compareTo(Block leftBlock, int leftPosition, Block rightBlock, int ri
return 0;
}

private static void checkElementNotNull(boolean isNull)
{
if (isNull) {
throw new PrestoException(StandardErrorCode.NOT_SUPPORTED, "ARRAY comparison not supported for arrays with null elements");
}
}

@Override
public Object getObjectValue(ConnectorSession session, Block block, int position)
{
Expand Down
97 changes: 97 additions & 0 deletions presto-main/src/main/java/com/facebook/presto/type/MapType.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import java.util.Map;

import static com.facebook.presto.type.TypeUtils.appendToBlockBuilder;
import static com.facebook.presto.type.TypeUtils.hashPosition;
import static com.facebook.presto.type.TypeUtils.buildStructuralSlice;
import static com.facebook.presto.type.TypeUtils.checkElementNotNull;
import static com.facebook.presto.type.TypeUtils.parameterizedTypeName;
import static com.facebook.presto.type.TypeUtils.readStructuralBlock;
import static com.google.common.base.Preconditions.checkArgument;
Expand All @@ -39,6 +41,7 @@ public class MapType
{
private final Type keyType;
private final Type valueType;
private static final String MAP_NULL_ELEMENT_MSG = "MAP comparison not supported for null value elements";

public MapType(Type keyType, Type valueType)
{
Expand Down Expand Up @@ -68,6 +71,100 @@ public Type getValueType()
return valueType;
}

@Override
public boolean isComparable()
{
return valueType.isComparable();
}

@Override
public int hash(Block block, int position)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to implement equalTo

{
Slice mapSlice = getSlice(block, position);
Block mapBlock = readStructuralBlock(mapSlice);
int result = 0;

for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
result += hashPosition(keyType, mapBlock, i);
result += hashPosition(valueType, mapBlock, i + 1);
}
return result;
}

@Override
public boolean equalTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition)
{
Slice leftSlice = getSlice(leftBlock, leftPosition);
Slice rightSlice = getSlice(rightBlock, rightPosition);
Block leftMapBlock = readStructuralBlock(leftSlice);
Block rightMapBlock = readStructuralBlock(rightSlice);

if (leftMapBlock.getPositionCount() != rightMapBlock.getPositionCount()) {
return false;
}

Map<KeyWrapper, Integer> wrappedLeftMap = new HashMap<>();
for (int position = 0; position < leftMapBlock.getPositionCount(); position += 2) {
wrappedLeftMap.put(new KeyWrapper(keyType, leftMapBlock, position), position + 1);
}

for (int position = 0; position < rightMapBlock.getPositionCount(); position += 2) {
KeyWrapper key = new KeyWrapper(keyType, rightMapBlock, position);
Integer leftValuePosition = wrappedLeftMap.get(key);
if (leftValuePosition == null) {
return false;
}
int rightValuePosition = position + 1;
checkElementNotNull(leftMapBlock.isNull(leftValuePosition), MAP_NULL_ELEMENT_MSG);
checkElementNotNull(rightMapBlock.isNull(rightValuePosition), MAP_NULL_ELEMENT_MSG);

if (!valueType.equalTo(leftMapBlock, leftValuePosition, rightMapBlock, rightValuePosition)) {
return false;
}
}
return true;
}

private static final class KeyWrapper
{
private final Type type;
private final Block block;
private final int position;

public KeyWrapper(Type type, Block block, int position)
{
this.type = type;
this.block = block;
this.position = position;
}

public Block getBlock()
{
return this.block;
}

public int getPosition()
{
return this.position;
}

@Override
public int hashCode()
{
return type.hash(block, position);
}

@Override
public boolean equals(Object obj)
{
if (obj == null || !getClass().equals(obj.getClass())) {
return false;
}
KeyWrapper other = (KeyWrapper) obj;
return type.equalTo(this.block, this.position, other.getBlock(), other.getPosition());
}
}

@Override
public Object getObjectValue(ConnectorSession session, Block block, int position)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Map;

import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -227,4 +228,11 @@ public static Block readStructuralBlock(Slice slice)
{
return new VariableWidthBlockEncoding().readBlock(slice.getInput());
}

public static void checkElementNotNull(boolean isNull, String errorMsg)
{
if (isNull) {
throw new PrestoException(NOT_SUPPORTED, errorMsg);
}
}
}