use of io.trino.sql.tree.GenericLiteral in project trino by trinodb.
the class LogicalPlanner method noTruncationCast.
/*
According to the standard, for the purpose of store assignment (INSERT),
no non-space characters of a character string, and no non-zero octets
of a binary string must be lost when the inserted value is truncated to
fit in the target column type.
The following method returns a cast from source type to target type
with a guarantee of no illegal truncation.
TODO Once BINARY and parametric VARBINARY types are supported, they should be handled here.
TODO This workaround is insufficient to handle structural types
*/
private Expression noTruncationCast(Expression expression, Type fromType, Type toType) {
if (fromType instanceof UnknownType || (!(toType instanceof VarcharType) && !(toType instanceof CharType))) {
return new Cast(expression, toSqlType(toType));
}
int targetLength;
if (toType instanceof VarcharType) {
if (((VarcharType) toType).isUnbounded()) {
return new Cast(expression, toSqlType(toType));
}
targetLength = ((VarcharType) toType).getBoundedLength();
} else {
targetLength = ((CharType) toType).getLength();
}
checkState(fromType instanceof VarcharType || fromType instanceof CharType, "inserting non-character value to column of character type");
ResolvedFunction spaceTrimmedLength = metadata.resolveFunction(session, QualifiedName.of("$space_trimmed_length"), fromTypes(VARCHAR));
ResolvedFunction fail = metadata.resolveFunction(session, 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 CoalesceExpression(new FunctionCall(spaceTrimmedLength.toQualifiedName(), ImmutableList.of(new Cast(expression, toSqlType(VARCHAR)))), new GenericLiteral("BIGINT", "0"))), new Cast(expression, toSqlType(toType)), new Cast(new FunctionCall(fail.toQualifiedName(), ImmutableList.of(new Cast(new StringLiteral(format("Cannot truncate non-space characters when casting from %s to %s on INSERT", fromType.getDisplayName(), toType.getDisplayName())), toSqlType(VARCHAR)))), toSqlType(toType)));
}
use of io.trino.sql.tree.GenericLiteral in project trino by trinodb.
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.trino.sql.tree.GenericLiteral in project trino by trinodb.
the class TestAddExchangesPlans method testRepartitionForUnionAllBeforeHashJoin.
@Test
public void testRepartitionForUnionAllBeforeHashJoin() {
Session session = Session.builder(getQueryRunner().getDefaultSession()).setSystemProperty(JOIN_DISTRIBUTION_TYPE, DistributionType.PARTITIONED.name()).setSystemProperty(JOIN_REORDERING_STRATEGY, ELIMINATE_CROSS_JOINS.name()).build();
assertDistributedPlan("SELECT * FROM (SELECT nationkey FROM nation UNION ALL select nationkey from nation) n join region r on n.nationkey = r.regionkey", session, anyTree(join(INNER, ImmutableList.of(equiJoinClause("nationkey", "regionkey")), anyTree(exchange(REMOTE, REPARTITION, anyTree(tableScan("nation", ImmutableMap.of("nationkey", "nationkey")))), exchange(REMOTE, REPARTITION, anyTree(tableScan("nation")))), anyTree(exchange(REMOTE, REPARTITION, anyTree(tableScan("region", ImmutableMap.of("regionkey", "regionkey"))))))));
assertDistributedPlan("SELECT * FROM (SELECT nationkey FROM nation UNION ALL select 1) n join region r on n.nationkey = r.regionkey", session, anyTree(join(INNER, ImmutableList.of(equiJoinClause("nationkey", "regionkey")), anyTree(exchange(REMOTE, REPARTITION, anyTree(tableScan("nation", ImmutableMap.of("nationkey", "nationkey")))), exchange(REMOTE, REPARTITION, project(values(ImmutableList.of("expr"), ImmutableList.of(ImmutableList.of(new GenericLiteral("BIGINT", "1"))))))), anyTree(exchange(REMOTE, REPARTITION, anyTree(tableScan("region", ImmutableMap.of("regionkey", "regionkey"))))))));
}
use of io.trino.sql.tree.GenericLiteral in project trino by trinodb.
the class TestPatternRecognitionNodeSerialization method testMeasureRoundtrip.
@Test
public void testMeasureRoundtrip() {
ObjectMapperProvider provider = new ObjectMapperProvider();
provider.setJsonSerializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionSerializer()));
provider.setJsonDeserializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionDeserializer(new SqlParser()), Type.class, new TypeDeserializer(TESTING_TYPE_MANAGER)));
JsonCodec<Measure> codec = new JsonCodecFactory(provider).jsonCodec(Measure.class);
assertJsonRoundTrip(codec, new Measure(new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of()), BOOLEAN));
assertJsonRoundTrip(codec, new Measure(new ExpressionAndValuePointers(new IfExpression(new ComparisonExpression(GREATER_THAN, new SymbolReference("match_number"), new SymbolReference("x")), new GenericLiteral("BIGINT", "10"), new ArithmeticUnaryExpression(MINUS, new SymbolReference("y"))), ImmutableList.of(new Symbol("match_number"), new Symbol("x"), new Symbol("y")), ImmutableList.of(new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(), true, true, 0, 0), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("A")), false, true, 1, -1), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("B")), false, true, 1, -1), new Symbol("input_symbol_b"))), ImmutableSet.of(), ImmutableSet.of(new Symbol("match_number"))), BIGINT));
}
use of io.trino.sql.tree.GenericLiteral in project trino by trinodb.
the class ImplementExceptAll method apply.
@Override
public Result apply(ExceptNode node, Captures captures, Context context) {
SetOperationNodeTranslator translator = new SetOperationNodeTranslator(context.getSession(), metadata, context.getSymbolAllocator(), context.getIdAllocator());
SetOperationNodeTranslator.TranslationResult result = translator.makeSetContainmentPlanForAll(node);
// compute expected multiplicity for every row
checkState(result.getCountSymbols().size() > 0, "ExceptNode translation result has no count symbols");
ResolvedFunction greatest = metadata.resolveFunction(context.getSession(), QualifiedName.of("greatest"), fromTypes(BIGINT, BIGINT));
Expression count = result.getCountSymbols().get(0).toSymbolReference();
for (int i = 1; i < result.getCountSymbols().size(); i++) {
count = new FunctionCall(greatest.toQualifiedName(), ImmutableList.of(new ArithmeticBinaryExpression(SUBTRACT, count, result.getCountSymbols().get(i).toSymbolReference()), new GenericLiteral("BIGINT", "0")));
}
// filter rows so that expected number of rows remains
Expression removeExtraRows = new ComparisonExpression(LESS_THAN_OR_EQUAL, result.getRowNumberSymbol().toSymbolReference(), count);
FilterNode filter = new FilterNode(context.getIdAllocator().getNextId(), result.getPlanNode(), removeExtraRows);
// prune helper symbols
ProjectNode project = new ProjectNode(context.getIdAllocator().getNextId(), filter, Assignments.identity(node.getOutputSymbols()));
return Result.ofPlanNode(project);
}
Aggregations