Search in sources :

Example 16 with StringLiteral

use of io.prestosql.sql.tree.StringLiteral in project hetu-core by openlookeng.

the class TestExpressionInterpreter method assertLike.

private static void assertLike(byte[] value, String pattern, boolean expected) {
    Expression predicate = new LikePredicate(rawStringLiteral(Slices.wrappedBuffer(value)), new StringLiteral(pattern), Optional.empty());
    assertEquals(evaluate(predicate), expected);
}
Also used : StringLiteral(io.prestosql.sql.tree.StringLiteral) ExpressionFormatter.formatExpression(io.prestosql.sql.ExpressionFormatter.formatExpression) Expression(io.prestosql.sql.tree.Expression) LikePredicate(io.prestosql.sql.tree.LikePredicate)

Example 17 with StringLiteral

use of io.prestosql.sql.tree.StringLiteral in project hetu-core by openlookeng.

the class LogicalPlanner method noTruncationCast.

private Expression noTruncationCast(Expression expression, Type fromType, Type toType) {
    // cast larger size varchar to small size varchar
    if ((fromType instanceof VarcharType || fromType instanceof CharType) && (toType instanceof VarcharType || toType instanceof CharType)) {
        int targetLength;
        if (toType instanceof VarcharType) {
            if (((VarcharType) toType).isUnbounded()) {
                return new Cast(expression, toType.getTypeSignature().toString());
            }
            targetLength = ((VarcharType) toType).getBoundedLength();
        } else {
            targetLength = ((CharType) toType).getLength();
        }
        Signature spaceTrimmedLength = metadata.getFunctionAndTypeManager().resolveBuiltInFunction(QualifiedName.of("$space_trimmed_length"), fromTypes(VARCHAR));
        Signature fail = metadata.getFunctionAndTypeManager().resolveBuiltInFunction(QualifiedName.of("fail"), fromTypes(VARCHAR));
        return new IfExpression(// check if the trimmed value fits in the target type
        new ComparisonExpression(GREATER_THAN_OR_EQUAL, new GenericLiteral("BIGINT", Integer.toString(targetLength)), new FunctionCall(QualifiedName.of("$space_trimmed_length"), ImmutableList.of(new Cast(expression, VARCHAR.getTypeSignature().toString())))), new Cast(expression, toType.getTypeSignature().toString()), new Cast(new FunctionCall(QualifiedName.of("fail"), ImmutableList.of(new Cast(new StringLiteral(format("Out of range for insert query type: Table: %s, Query: %s", toType.toString(), fromType.toString())), VARCHAR.getTypeSignature().toString()))), toType.getTypeSignature().toString()));
    }
    return new Cast(expression, toType.getTypeSignature().toString());
}
Also used : Cast(io.prestosql.sql.tree.Cast) IfExpression(io.prestosql.sql.tree.IfExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) StringLiteral(io.prestosql.sql.tree.StringLiteral) VarcharType(io.prestosql.spi.type.VarcharType) Signature(io.prestosql.spi.function.Signature) CharType(io.prestosql.spi.type.CharType) FunctionCall(io.prestosql.sql.tree.FunctionCall) GenericLiteral(io.prestosql.sql.tree.GenericLiteral)

Example 18 with StringLiteral

use of io.prestosql.sql.tree.StringLiteral in project hetu-core by openlookeng.

the class ExpressionInterpreter method createFailureFunction.

private static Expression createFailureFunction(RuntimeException exception, Type type, Metadata metadata) {
    requireNonNull(exception, "Exception is null");
    String failureInfo = JsonCodec.jsonCodec(FailureInfo.class).toJson(Failures.toFailure(exception).toFailureInfo());
    FunctionCall jsonParse = new FunctionCallBuilder(metadata).setName(QualifiedName.of("json_parse")).addArgument(VARCHAR, new StringLiteral(failureInfo)).build();
    FunctionCall failureFunction = new FunctionCallBuilder(metadata).setName(QualifiedName.of("fail")).addArgument(JSON, jsonParse).build();
    return new Cast(failureFunction, type.getTypeSignature().toString());
}
Also used : Cast(io.prestosql.sql.tree.Cast) StringLiteral(io.prestosql.sql.tree.StringLiteral) FailureInfo(io.prestosql.client.FailureInfo) FunctionCall(io.prestosql.sql.tree.FunctionCall)

Example 19 with StringLiteral

use of io.prestosql.sql.tree.StringLiteral in project hetu-core by openlookeng.

the class TestScalarStatsCalculator method testLiteral.

@Test
public void testLiteral() {
    assertCalculate(new GenericLiteral("TINYINT", "7")).distinctValuesCount(1.0).lowValue(7).highValue(7).nullsFraction(0.0);
    assertCalculate(new GenericLiteral("SMALLINT", "8")).distinctValuesCount(1.0).lowValue(8).highValue(8).nullsFraction(0.0);
    assertCalculate(new GenericLiteral("INTEGER", "9")).distinctValuesCount(1.0).lowValue(9).highValue(9).nullsFraction(0.0);
    assertCalculate(new GenericLiteral("BIGINT", Long.toString(Long.MAX_VALUE))).distinctValuesCount(1.0).lowValue(Long.MAX_VALUE).highValue(Long.MAX_VALUE).nullsFraction(0.0);
    assertCalculate(new DoubleLiteral("7.5")).distinctValuesCount(1.0).lowValue(7.5).highValue(7.5).nullsFraction(0.0);
    assertCalculate(new DecimalLiteral("75.5")).distinctValuesCount(1.0).lowValue(75.5).highValue(75.5).nullsFraction(0.0);
    assertCalculate(new StringLiteral("blah")).distinctValuesCount(1.0).lowValueUnknown().highValueUnknown().nullsFraction(0.0);
    assertCalculate(new NullLiteral()).distinctValuesCount(0.0).lowValueUnknown().highValueUnknown().nullsFraction(1.0);
}
Also used : StringLiteral(io.prestosql.sql.tree.StringLiteral) DecimalLiteral(io.prestosql.sql.tree.DecimalLiteral) DoubleLiteral(io.prestosql.sql.tree.DoubleLiteral) NullLiteral(io.prestosql.sql.tree.NullLiteral) GenericLiteral(io.prestosql.sql.tree.GenericLiteral) Test(org.testng.annotations.Test)

Example 20 with StringLiteral

use of io.prestosql.sql.tree.StringLiteral in project hetu-core by openlookeng.

the class TestComparisonStatsCalculator method symbolToLiteralNotEqualStats.

@Test
public void symbolToLiteralNotEqualStats() {
    // Simple case
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("y"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls multiplied by ((distinct values - 1) / distinct values)
    475.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(19.0).lowValue(0.0).highValue(5.0).nullsFraction(0.0);
    });
    // Literal on the edge of symbol range
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("x"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls multiplied by ((distinct values - 1) / distinct values)
    731.25).symbolStats("x", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(39.0).lowValue(-10.0).highValue(10.0).nullsFraction(0.0);
    });
    // Literal out of symbol range
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("y"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls
    500.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(19.0).lowValue(0.0).highValue(5.0).nullsFraction(0.0);
    });
    // Literal in left open range
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("leftOpen"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls multiplied by ((distinct values - 1) / distinct values)
    882.0).symbolStats("leftOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(49.0).lowValueUnknown().highValue(15.0).nullsFraction(0.0);
    });
    // Literal in right open range
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("rightOpen"), new DoubleLiteral("-2.5"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    882.0).symbolStats("rightOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(49.0).lowValue(-15.0).highValueUnknown().nullsFraction(0.0);
    });
    // Literal in unknown range
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    882.0).symbolStats("unknownRange", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(49.0).lowValueUnknown().highValueUnknown().nullsFraction(0.0);
    });
    // Literal in empty range
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("emptyRange"), new DoubleLiteral("0.0"))).outputRowsCount(0.0).symbolStats("emptyRange", equalTo(emptyRangeStats));
    // Column with values not representable as double (unknown range)
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("varchar"), new StringLiteral("blah"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    882.0).symbolStats("varchar", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(49.0).lowValueUnknown().highValueUnknown().nullsFraction(0.0);
    });
}
Also used : ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) StringLiteral(io.prestosql.sql.tree.StringLiteral) SymbolReference(io.prestosql.sql.tree.SymbolReference) DoubleLiteral(io.prestosql.sql.tree.DoubleLiteral) Test(org.testng.annotations.Test)

Aggregations

StringLiteral (io.prestosql.sql.tree.StringLiteral)46 Test (org.testng.annotations.Test)23 LongLiteral (io.prestosql.sql.tree.LongLiteral)20 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)15 FunctionCall (io.prestosql.sql.tree.FunctionCall)15 Identifier (io.prestosql.sql.tree.Identifier)15 Expression (io.prestosql.sql.tree.Expression)13 Property (io.prestosql.sql.tree.Property)12 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)9 ArithmeticUnaryExpression (io.prestosql.sql.tree.ArithmeticUnaryExpression)9 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)9 DoubleLiteral (io.prestosql.sql.tree.DoubleLiteral)9 IfExpression (io.prestosql.sql.tree.IfExpression)9 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)9 NotExpression (io.prestosql.sql.tree.NotExpression)9 QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)9 SimpleCaseExpression (io.prestosql.sql.tree.SimpleCaseExpression)9 SubqueryExpression (io.prestosql.sql.tree.SubqueryExpression)9 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)8 CreateTable (io.prestosql.sql.tree.CreateTable)8