Skip to content

Commit

Permalink
Add long and varchar enum types
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-ohayon authored and Rongrong Zhong committed Aug 31, 2020
1 parent 9bde821 commit 7ced455
Show file tree
Hide file tree
Showing 14 changed files with 894 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
package com.facebook.presto.client;

import com.facebook.airlift.json.ObjectMapperProvider;
import com.facebook.presto.common.type.LongEnumType.LongEnumMap;
import com.facebook.presto.common.type.NamedTypeSignature;
import com.facebook.presto.common.type.ParameterKind;
import com.facebook.presto.common.type.TypeSignatureParameter;
import com.facebook.presto.common.type.VarcharEnumType.VarcharEnumMap;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
Expand Down Expand Up @@ -53,6 +55,12 @@ public ClientTypeSignatureParameter(TypeSignatureParameter typeParameterSignatur
case NAMED_TYPE:
value = typeParameterSignature.getNamedTypeSignature();
break;
case LONG_ENUM:
value = typeParameterSignature.getLongEnumMap();
break;
case VARCHAR_ENUM:
value = typeParameterSignature.getVarcharEnumMap();
break;
default:
throw new UnsupportedOperationException(format("Unknown kind [%s]", kind));
}
Expand Down Expand Up @@ -153,6 +161,12 @@ public ClientTypeSignatureParameter deserialize(JsonParser jp, DeserializationCo
case LONG:
value = MAPPER.readValue(jsonValue, Long.class);
break;
case LONG_ENUM:
value = MAPPER.readValue(jsonValue, LongEnumMap.class);
break;
case VARCHAR_ENUM:
value = MAPPER.readValue(jsonValue, VarcharEnumMap.class);
break;
default:
throw new UnsupportedOperationException(format("Unsupported kind [%s]", kind));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.common.type;

import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.block.BlockBuilder;
import com.facebook.presto.common.function.SqlFunctionProperties;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;

import java.util.Objects;

public class AbstractVarcharType
extends AbstractVariableWidthType
{
public static final int UNBOUNDED_LENGTH = Integer.MAX_VALUE;
public static final int MAX_LENGTH = Integer.MAX_VALUE - 1;

private final int length;

AbstractVarcharType(int length, TypeSignature typeSignature)
{
super(typeSignature, Slice.class);

if (length < 0) {
throw new IllegalArgumentException("Invalid VARCHAR length " + length);
}
this.length = length;
}

@Deprecated
public int getLength()
{
return length;
}

public int getLengthSafe()
{
if (isUnbounded()) {
throw new IllegalStateException("Cannot get size of unbounded VARCHAR.");
}
return length;
}

public boolean isUnbounded()
{
return length == UNBOUNDED_LENGTH;
}

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

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

@Override
public Object getObjectValue(SqlFunctionProperties properties, Block block, int position)
{
if (block.isNull(position)) {
return null;
}

return block.getSlice(position, 0, block.getSliceLength(position)).toStringUtf8();
}

@Override
public boolean equalTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition)
{
int leftLength = leftBlock.getSliceLength(leftPosition);
int rightLength = rightBlock.getSliceLength(rightPosition);
if (leftLength != rightLength) {
return false;
}
return leftBlock.equals(leftPosition, 0, rightBlock, rightPosition, 0, leftLength);
}

@Override
public long hash(Block block, int position)
{
return block.hash(position, 0, block.getSliceLength(position));
}

@Override
public int compareTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition)
{
int leftLength = leftBlock.getSliceLength(leftPosition);
int rightLength = rightBlock.getSliceLength(rightPosition);
return leftBlock.compareTo(leftPosition, 0, leftLength, rightBlock, rightPosition, 0, rightLength);
}

@Override
public void appendTo(Block block, int position, BlockBuilder blockBuilder)
{
if (block.isNull(position)) {
blockBuilder.appendNull();
}
else {
block.writeBytesTo(position, 0, block.getSliceLength(position), blockBuilder);
blockBuilder.closeEntry();
}
}

@Override
public Slice getSlice(Block block, int position)
{
return block.getSlice(position, 0, block.getSliceLength(position));
}

@Override
public Slice getSliceUnchecked(Block block, int internalPosition)
{
return block.getSliceUnchecked(internalPosition, 0, block.getSliceLengthUnchecked(internalPosition));
}

public void writeString(BlockBuilder blockBuilder, String value)
{
writeSlice(blockBuilder, Slices.utf8Slice(value));
}

@Override
public void writeSlice(BlockBuilder blockBuilder, Slice value)
{
writeSlice(blockBuilder, value, 0, value.length());
}

@Override
public void writeSlice(BlockBuilder blockBuilder, Slice value, int offset, int length)
{
blockBuilder.writeBytes(value, offset, length).closeEntry();
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

AbstractVarcharType other = (AbstractVarcharType) o;

return Objects.equals(this.length, other.length);
}

@Override
public int hashCode()
{
return Objects.hash(length);
}

@Override
public String getDisplayName()
{
if (length == UNBOUNDED_LENGTH) {
return getTypeSignature().getBase();
}

return getTypeSignature().toString();
}

@Override
public String toString()
{
return getDisplayName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.common.type;

import java.util.Map;

public interface EnumType<T>
extends Type
{
Map<String, T> getEnumMap();

Type getValueType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.common.type;

import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.function.SqlFunctionProperties;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.facebook.presto.common.type.BigintType.BIGINT;
import static com.facebook.presto.common.type.TypeUtils.normalizeEnumMap;
import static com.facebook.presto.common.type.TypeUtils.validateEnumMap;
import static java.lang.String.format;

public class LongEnumType
extends AbstractLongType
implements EnumType<Long>
{
private final LongEnumMap enumMap;

public LongEnumType(String name, LongEnumMap enumMap)
{
super(new TypeSignature(name, TypeSignatureParameter.of(enumMap)));
this.enumMap = enumMap;
}

@Override
public Map<String, Long> getEnumMap()
{
return enumMap.getEnumMap();
}

@Override
public Object getObjectValue(SqlFunctionProperties properties, Block block, int position)
{
if (block.isNull(position)) {
return null;
}

return block.getLong(position);
}

@Override
public Type getValueType()
{
return BIGINT;
}

@Override
public String getDisplayName()
{
return getTypeSignature().getBase();
}

public static class LongEnumMap
{
private final Map<String, Long> enumMap;

@JsonCreator
public LongEnumMap(@JsonProperty("enumMap") Map<String, Long> enumMap)
{
validateEnumMap(enumMap);
this.enumMap = normalizeEnumMap(enumMap);
}

@JsonProperty
public Map<String, Long> getEnumMap()
{
return enumMap;
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

LongEnumMap other = (LongEnumMap) o;

return Objects.equals(this.enumMap, other.enumMap);
}

@Override
public String toString()
{
return "enum:bigint{"
+ enumMap.entrySet()
.stream()
.sorted(Comparator.comparing(Map.Entry::getKey))
.map(e -> format("\"%s\": %d", e.getKey().replaceAll("\"", "\"\""), e.getValue()))
.collect(Collectors.joining(", "))
+ "}";
}

@Override
public int hashCode()
{
return Objects.hash(enumMap);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public enum ParameterKind
TYPE(Optional.of("TYPE_SIGNATURE")),
NAMED_TYPE(Optional.of("NAMED_TYPE_SIGNATURE")),
LONG(Optional.of("LONG_LITERAL")),
VARIABLE(Optional.empty());
VARIABLE(Optional.empty()),
LONG_ENUM(Optional.of("LONG_ENUM")),
VARCHAR_ENUM(Optional.of("VARCHAR_ENUM"));

// TODO: drop special serialization code as soon as all clients
// migrate to version which can deserialize new format.
Expand Down
Loading

0 comments on commit 7ced455

Please sign in to comment.