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);
}
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());
}
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());
}
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);
}
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);
});
}
Aggregations