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

[WIP] Support for user-defined enums #14691

Closed
wants to merge 13 commits into from
Closed
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 java.util.Map;

public class StringEnumType
extends VarcharType
implements EnumType<String>
{
private final Map<String, String> entries;

public StringEnumType(String name, Map<String, String> entries)
{
super(VarcharType.MAX_LENGTH, TypeSignature.parseTypeSignature(name));
this.entries = entries;
}

@Override
public Map<String, String> getEntries()
{
return entries;
}

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

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

@Override
public Type getValueType()
{
return VarcharType.VARCHAR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

import static java.util.Collections.singletonList;

public final class VarcharType
public class VarcharType
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably want to refactor VarcharType into AbstractVarcharType and VarcharType (similar to AbstractLongType and BigintType) and keep this as final. That way you also avoid the weird need to provide a type signature for the constructor.

extends AbstractVariableWidthType
{
public static final int UNBOUNDED_LENGTH = Integer.MAX_VALUE;
public static final int MAX_LENGTH = Integer.MAX_VALUE - 1;
public static final VarcharType VARCHAR = new VarcharType(UNBOUNDED_LENGTH);
public static final VarcharType VARCHAR = createVarcharTypeInternal(UNBOUNDED_LENGTH);

public static VarcharType createUnboundedVarcharType()
{
Expand All @@ -41,7 +41,14 @@ public static VarcharType createVarcharType(int length)
// Use createUnboundedVarcharType for unbounded VARCHAR.
throw new IllegalArgumentException("Invalid VARCHAR length " + length);
}
return new VarcharType(length);
return createVarcharTypeInternal(length);
}

private static VarcharType createVarcharTypeInternal(int length)
{
return new VarcharType(length, new TypeSignature(
StandardTypes.VARCHAR,
singletonList(TypeSignatureParameter.of((long) length))));
}

public static TypeSignature getParametrizedVarcharSignature(String param)
Expand All @@ -51,13 +58,9 @@ public static TypeSignature getParametrizedVarcharSignature(String param)

private final int length;

private VarcharType(int length)
protected VarcharType(int length, TypeSignature typeSignature)
{
super(
new TypeSignature(
StandardTypes.VARCHAR,
singletonList(TypeSignatureParameter.of((long) length))),
Slice.class);
super(typeSignature, Slice.class);

if (length < 0) {
throw new IllegalArgumentException("Invalid VARCHAR length " + length);
Expand Down