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

The following examples show how to use io.prestosql.spi.type.Type#isComparable() . 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: StatementAnalyzer.java    From presto with Apache License 2.0 6 votes vote down vote up
private void analyzeSelectSingleColumn(
        SingleColumn singleColumn,
        QuerySpecification node,
        Scope scope,
        ImmutableList.Builder<Expression> outputExpressionBuilder,
        ImmutableList.Builder<SelectExpression> selectExpressionBuilder)
{
    Expression expression = singleColumn.getExpression();
    ExpressionAnalysis expressionAnalysis = analyzeExpression(expression, scope);
    analysis.recordSubqueries(node, expressionAnalysis);
    outputExpressionBuilder.add(expression);
    selectExpressionBuilder.add(new SelectExpression(expression, Optional.empty()));

    Type type = expressionAnalysis.getType(expression);
    if (node.getSelect().isDistinct() && !type.isComparable()) {
        throw semanticException(
                TYPE_MISMATCH, node.getSelect(),
                "DISTINCT can only be applied to comparable types (actual: %s): %s",
                type,
                expression);
    }
}
 
Example 2
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 3
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 4
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 5
Source File: OptimizeMixedDistinctAggregations.java    From presto with Apache License 2.0 5 votes vote down vote up
private boolean checkAllEquatableTypes(AggregateInfo aggregateInfo)
{
    for (Symbol symbol : aggregateInfo.getOriginalNonDistinctAggregateArgs()) {
        Type type = symbolAllocator.getTypes().get(symbol);
        if (!type.isComparable()) {
            return false;
        }
    }

    if (!symbolAllocator.getTypes().get(aggregateInfo.getMask()).isComparable()) {
        return false;
    }

    return true;
}
 
Example 6
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 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: OrcTester.java    From presto with Apache License 2.0 4 votes vote down vote up
public void testRoundTrip(Type type, List<?> readValues)
        throws Exception
{
    // just the values
    testRoundTripType(type, readValues);

    // all nulls
    if (nullTestsEnabled) {
        assertRoundTrip(
                type,
                readValues.stream()
                        .map(value -> null)
                        .collect(toList()));
    }

    // values wrapped in struct
    if (structTestsEnabled) {
        testStructRoundTrip(type, readValues);
    }

    // values wrapped in a struct wrapped in a struct
    if (complexStructuralTestsEnabled) {
        testStructRoundTrip(
                rowType(type, type, type),
                readValues.stream()
                        .map(OrcTester::toHiveStruct)
                        .collect(toList()));
    }

    // values wrapped in map
    if (mapTestsEnabled && type.isComparable()) {
        testMapRoundTrip(type, readValues);
    }

    // values wrapped in list
    if (listTestsEnabled) {
        testListRoundTrip(type, readValues);
    }

    // values wrapped in a list wrapped in a list
    if (complexStructuralTestsEnabled) {
        testListRoundTrip(
                arrayType(type),
                readValues.stream()
                        .map(OrcTester::toHiveList)
                        .collect(toList()));
    }
}
 
Example 12
Source File: StatementAnalyzer.java    From presto with Apache License 2.0 4 votes vote down vote up
private void analyzeAllColumnsFromTable(
        List<Field> fields,
        AllColumns allColumns,
        QuerySpecification node,
        Scope scope,
        ImmutableList.Builder<Expression> outputExpressionBuilder,
        ImmutableList.Builder<SelectExpression> selectExpressionBuilder,
        RelationType relationType,
        boolean local)
{
    if (!allColumns.getAliases().isEmpty()) {
        validateColumnAliasesCount(allColumns.getAliases(), fields.size());
    }

    ImmutableList.Builder<Field> itemOutputFieldBuilder = ImmutableList.builder();

    for (int i = 0; i < fields.size(); i++) {
        Field field = fields.get(i);
        Expression fieldExpression;
        if (local) {
            fieldExpression = new FieldReference(relationType.indexOf(field));
        }
        else {
            if (field.getName().isEmpty()) {
                throw semanticException(NOT_SUPPORTED, node.getSelect(), "SELECT * from outer scope table not supported with anonymous columns");
            }
            checkState(field.getRelationAlias().isPresent(), "missing relation alias");
            fieldExpression = new DereferenceExpression(DereferenceExpression.from(field.getRelationAlias().get()), new Identifier(field.getName().get()));
        }
        analyzeExpression(fieldExpression, scope);
        outputExpressionBuilder.add(fieldExpression);
        selectExpressionBuilder.add(new SelectExpression(fieldExpression, Optional.empty()));

        Optional<String> alias = field.getName();
        if (!allColumns.getAliases().isEmpty()) {
            alias = Optional.of((allColumns.getAliases().get(i)).getValue());
        }

        itemOutputFieldBuilder.add(new Field(
                field.getRelationAlias(),
                alias,
                field.getType(),
                false,
                field.getOriginTable(),
                field.getOriginColumnName(),
                !allColumns.getAliases().isEmpty() || field.isAliased()));

        Type type = field.getType();
        if (node.getSelect().isDistinct() && !type.isComparable()) {
            throw semanticException(TYPE_MISMATCH, node.getSelect(), "DISTINCT can only be applied to comparable types (actual: %s)", type);
        }
    }
    analysis.setSelectAllResultFields(allColumns, itemOutputFieldBuilder.build());
}
 
Example 13
Source File: StatementAnalyzer.java    From presto with Apache License 2.0 4 votes vote down vote up
private void analyzeAllFieldsFromRowTypeExpression(
        Expression expression,
        AllColumns allColumns,
        QuerySpecification node,
        Scope scope,
        ImmutableList.Builder<Expression> outputExpressionBuilder,
        ImmutableList.Builder<SelectExpression> selectExpressionBuilder)
{
    ImmutableList.Builder<Field> itemOutputFieldBuilder = ImmutableList.builder();

    ExpressionAnalysis expressionAnalysis = analyzeExpression(expression, scope);
    Type type = expressionAnalysis.getType(expression);
    if (!(type instanceof RowType)) {
        throw semanticException(TYPE_MISMATCH, node.getSelect(), "expected expression of type Row");
    }
    int referencedFieldsCount = ((RowType) type).getFields().size();
    if (!allColumns.getAliases().isEmpty()) {
        validateColumnAliasesCount(allColumns.getAliases(), referencedFieldsCount);
    }
    analysis.recordSubqueries(node, expressionAnalysis);

    ImmutableList.Builder<Expression> unfoldedExpressionsBuilder = ImmutableList.builder();
    for (int i = 0; i < referencedFieldsCount; i++) {
        Expression outputExpression = new SubscriptExpression(expression, new LongLiteral("" + (i + 1)));
        outputExpressionBuilder.add(outputExpression);
        analyzeExpression(outputExpression, scope);
        unfoldedExpressionsBuilder.add(outputExpression);

        Type outputExpressionType = type.getTypeParameters().get(i);
        if (node.getSelect().isDistinct() && !(outputExpressionType.isComparable())) {
            throw semanticException(TYPE_MISMATCH, node.getSelect(), "DISTINCT can only be applied to comparable types (actual: %s)", type.getTypeParameters().get(i));
        }

        Optional<String> name = ((RowType) type).getFields().get(i).getName();
        if (!allColumns.getAliases().isEmpty()) {
            name = Optional.of((allColumns.getAliases().get(i)).getValue());
        }
        itemOutputFieldBuilder.add(Field.newUnqualified(name, outputExpressionType));
    }
    selectExpressionBuilder.add(new SelectExpression(expression, Optional.of(unfoldedExpressionsBuilder.build())));
    analysis.setSelectAllResultFields(allColumns, itemOutputFieldBuilder.build());
}