-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2dfb7cd
commit 5e20973
Showing
16 changed files
with
979 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
presto-common/src/main/java/com/facebook/presto/common/type/AbstractVarcharType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
presto-common/src/main/java/com/facebook/presto/common/type/EnumType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
53 changes: 53 additions & 0 deletions
53
presto-common/src/main/java/com/facebook/presto/common/type/LongEnumParametricType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.