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 Sep 29, 2020
1 parent 2dfb7cd commit 5e20973
Show file tree
Hide file tree
Showing 16 changed files with 979 additions and 175 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
6 changes: 6 additions & 0 deletions presto-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
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,53 @@
/*
* 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.type.LongEnumType.LongEnumMap;

import java.util.List;

import static com.facebook.presto.common.type.StatisticalDigestParametricType.checkArgument;

public final class LongEnumParametricType
implements ParametricType
{
private final String name;
private final LongEnumMap enumMap;

public LongEnumParametricType(String name, LongEnumMap enumMap)
{
this.name = name;
this.enumMap = enumMap;
}

@Override
public String getName()
{
return name;
}

@Override
public Type createType(TypeManager typeManager, List<TypeParameter> parameters)
{
if (parameters.isEmpty()) {
return new LongEnumType(name, enumMap);
}
checkArgument(parameters.size() == 1, "Enum type expects exactly one parameter, got %s", parameters);
checkArgument(
parameters.get(0).getKind() == ParameterKind.LONG_ENUM,
"Enum definition expected, got %s",
parameters);
return new LongEnumType(name, parameters.get(0).getLongEnumMap());
}
}
Loading

0 comments on commit 5e20973

Please sign in to comment.