Java Code Examples for io.prestosql.spi.type.Type#isOrderable()

The following examples show how to use io.prestosql.spi.type.Type#isOrderable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestTypeRegistry.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testOperatorsImplemented()
{
    for (Type type : metadata.getTypes()) {
        if (type.isComparable()) {
            metadata.resolveOperator(EQUAL, ImmutableList.of(type, type));
            metadata.resolveOperator(NOT_EQUAL, ImmutableList.of(type, type));
            metadata.resolveOperator(IS_DISTINCT_FROM, ImmutableList.of(type, type));
            metadata.resolveOperator(HASH_CODE, ImmutableList.of(type));
        }
        if (type.isOrderable()) {
            metadata.resolveOperator(LESS_THAN, ImmutableList.of(type, type));
            metadata.resolveOperator(LESS_THAN_OR_EQUAL, ImmutableList.of(type, type));
            metadata.resolveOperator(GREATER_THAN_OR_EQUAL, ImmutableList.of(type, type));
            metadata.resolveOperator(GREATER_THAN, ImmutableList.of(type, type));
        }
    }
}
 
Example 2
Source File: EquatableValueSet.java    From presto with Apache License 2.0 6 votes vote down vote up
@JsonCreator
public EquatableValueSet(
        @JsonProperty("type") Type type,
        @JsonProperty("whiteList") boolean whiteList,
        @JsonProperty("entries") Set<ValueEntry> entries)
{
    requireNonNull(type, "type is null");
    requireNonNull(entries, "entries is null");

    if (!type.isComparable()) {
        throw new IllegalArgumentException("Type is not comparable: " + type);
    }
    if (type.isOrderable()) {
        throw new IllegalArgumentException("Use SortedRangeSet instead");
    }
    this.type = type;
    this.whiteList = whiteList;
    this.entries = unmodifiableSet(new LinkedHashSet<>(entries));
}
 
Example 3
Source File: SignatureBinder.java    From presto with Apache License 2.0 5 votes vote down vote up
private boolean satisfiesConstraints(Type type)
{
    if (comparableRequired && !type.isComparable()) {
        return false;
    }
    if (orderableRequired && !type.isOrderable()) {
        return false;
    }
    if (requiredBaseName.isPresent() && !UNKNOWN.equals(type) && !requiredBaseName.get().equalsIgnoreCase(type.getBaseName())) {
        // TODO: the case below should be properly handled:
        // * `type` does not have the `requiredBaseName` but can be coerced to some type that has the `requiredBaseName`.
        return false;
    }
    return true;
}
 
Example 4
Source File: ExpressionAnalyzer.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Type visitQuantifiedComparisonExpression(QuantifiedComparisonExpression node, StackableAstVisitorContext<Context> context)
{
    Expression value = node.getValue();
    process(value, context);

    Expression subquery = node.getSubquery();
    process(subquery, context);

    Type comparisonType = coerceToSingleType(context, node, "Value expression and result of subquery must be of the same type for quantified comparison: %s vs %s", value, subquery);

    switch (node.getOperator()) {
        case LESS_THAN:
        case LESS_THAN_OR_EQUAL:
        case GREATER_THAN:
        case GREATER_THAN_OR_EQUAL:
            if (!comparisonType.isOrderable()) {
                throw semanticException(TYPE_MISMATCH, node, "Type [%s] must be orderable in order to be used in quantified comparison", comparisonType);
            }
            break;
        case EQUAL:
        case NOT_EQUAL:
            if (!comparisonType.isComparable()) {
                throw semanticException(TYPE_MISMATCH, node, "Type [%s] must be comparable in order to be used in quantified comparison", comparisonType);
            }
            break;
        default:
            throw new IllegalStateException(format("Unexpected comparison type: %s", node.getOperator()));
    }

    return setExpressionType(node, BOOLEAN);
}
 
Example 5
Source File: StatementAnalyzer.java    From presto with Apache License 2.0 5 votes vote down vote up
private List<Expression> analyzeOrderBy(Node node, List<SortItem> sortItems, Scope orderByScope)
{
    ImmutableList.Builder<Expression> orderByFieldsBuilder = ImmutableList.builder();

    for (SortItem item : sortItems) {
        Expression expression = item.getSortKey();

        if (expression instanceof LongLiteral) {
            // this is an ordinal in the output tuple

            long ordinal = ((LongLiteral) expression).getValue();
            if (ordinal < 1 || ordinal > orderByScope.getRelationType().getVisibleFieldCount()) {
                throw semanticException(INVALID_COLUMN_REFERENCE, expression, "ORDER BY position %s is not in select list", ordinal);
            }

            expression = new FieldReference(toIntExact(ordinal - 1));
        }

        ExpressionAnalysis expressionAnalysis = ExpressionAnalyzer.analyzeExpression(session,
                metadata,
                accessControl,
                sqlParser,
                orderByScope,
                analysis,
                expression,
                WarningCollector.NOOP,
                correlationSupport);
        analysis.recordSubqueries(node, expressionAnalysis);

        Type type = analysis.getType(expression);
        if (!type.isOrderable()) {
            throw semanticException(TYPE_MISMATCH, node, "Type %s is not orderable, and therefore cannot be used in ORDER BY: %s", type, expression);
        }

        orderByFieldsBuilder.add(expression);
    }

    return orderByFieldsBuilder.build();
}
 
Example 6
Source File: Marker.java    From presto with Apache License 2.0 5 votes vote down vote up
/**
 * LOWER UNBOUNDED is specified with an empty value and a ABOVE bound
 * UPPER UNBOUNDED is specified with an empty value and a BELOW bound
 */
@JsonCreator
public Marker(
        @JsonProperty("type") Type type,
        @JsonProperty("valueBlock") Optional<Block> valueBlock,
        @JsonProperty("bound") Bound bound)
{
    requireNonNull(type, "type is null");
    requireNonNull(valueBlock, "valueBlock is null");
    requireNonNull(bound, "bound is null");

    if (!type.isOrderable()) {
        throw new IllegalArgumentException("type must be orderable");
    }
    if (valueBlock.isEmpty() && bound == Bound.EXACTLY) {
        throw new IllegalArgumentException("Cannot be equal to unbounded");
    }
    if (valueBlock.isPresent() && valueBlock.get().getPositionCount() != 1) {
        throw new IllegalArgumentException("value block should only have one position");
    }
    if (type instanceof RealType && valueBlock.isPresent() && Float.isNaN(intBitsToFloat(toIntExact((long) blockToNativeValue(type, valueBlock.get()))))) {
        throw new IllegalArgumentException("cannot use Real NaN as range bound");
    }
    if (type instanceof DoubleType && valueBlock.isPresent() && Double.isNaN((double) blockToNativeValue(type, valueBlock.get()))) {
        throw new IllegalArgumentException("cannot use Double NaN as range bound");
    }
    this.type = type;
    this.valueBlock = valueBlock;
    this.bound = bound;
}
 
Example 7
Source File: ValueSet.java    From presto with Apache License 2.0 5 votes vote down vote up
static ValueSet none(Type type)
{
    if (type.isOrderable()) {
        return SortedRangeSet.none(type);
    }
    if (type.isComparable()) {
        return EquatableValueSet.none(type);
    }
    return AllOrNoneValueSet.none(type);
}
 
Example 8
Source File: ValueSet.java    From presto with Apache License 2.0 5 votes vote down vote up
static ValueSet all(Type type)
{
    if (type.isOrderable()) {
        return SortedRangeSet.all(type);
    }
    if (type.isComparable()) {
        return EquatableValueSet.all(type);
    }
    return AllOrNoneValueSet.all(type);
}
 
Example 9
Source File: ValueSet.java    From presto with Apache License 2.0 5 votes vote down vote up
static ValueSet of(Type type, Object first, Object... rest)
{
    if (type.isOrderable()) {
        return SortedRangeSet.of(type, first, rest);
    }
    if (type.isComparable()) {
        return EquatableValueSet.of(type, first, rest);
    }
    throw new IllegalArgumentException("Cannot create discrete ValueSet with non-comparable type: " + type);
}
 
Example 10
Source File: ValueSet.java    From presto with Apache License 2.0 5 votes vote down vote up
static ValueSet copyOf(Type type, Collection<Object> values)
{
    if (type.isOrderable()) {
        return SortedRangeSet.copyOf(type, values.stream()
                .map(value -> Range.equal(type, value))
                .collect(toList()));
    }
    if (type.isComparable()) {
        return EquatableValueSet.copyOf(type, values);
    }
    throw new IllegalArgumentException("Cannot create discrete ValueSet with non-comparable type: " + type);
}
 
Example 11
Source File: SortedRangeSet.java    From presto with Apache License 2.0 5 votes vote down vote up
private SortedRangeSet(Type type, NavigableMap<Marker, Range> lowIndexedRanges)
{
    requireNonNull(type, "type is null");
    requireNonNull(lowIndexedRanges, "lowIndexedRanges is null");

    if (!type.isOrderable()) {
        throw new IllegalArgumentException("Type is not orderable: " + type);
    }
    this.type = type;
    this.lowIndexedRanges = lowIndexedRanges;
}
 
Example 12
Source File: SortedRangeSet.java    From presto with Apache License 2.0 5 votes vote down vote up
Builder(Type type)
{
    requireNonNull(type, "type is null");

    if (!type.isOrderable()) {
        throw new IllegalArgumentException("Type is not orderable: " + type);
    }
    this.type = type;
}