use of io.prestosql.spi.relation.ConstantExpression in project hetu-core by openlookeng.
the class MySqlRowExpressionConverter method visitCall.
@Override
public String visitCall(CallExpression call, JdbcConverterContext context) {
// remote udf verify
FunctionHandle functionHandle = call.getFunctionHandle();
if (!isDefaultFunction(call)) {
Optional<String> result = mySqlApplyRemoteFunctionPushDown.rewriteRemoteFunction(call, this, context);
if (result.isPresent()) {
return result.get();
}
throw new PrestoException(NOT_SUPPORTED, String.format("MySql connector does not support remote function: %s.%s", call.getDisplayName(), call.getFunctionHandle().getFunctionNamespace()));
}
if (standardFunctionResolution.isArrayConstructor(functionHandle)) {
throw new PrestoException(NOT_SUPPORTED, "MySql connector does not support array constructor");
}
if (standardFunctionResolution.isSubscriptFunction(functionHandle)) {
throw new PrestoException(NOT_SUPPORTED, "MySql connector does not support subscript expression");
}
if (standardFunctionResolution.isCastFunction(functionHandle)) {
// deal with literal, when generic literal expression translate to rowExpression, it will be
// translated to a 'CAST' rowExpression with a varchar type 'CONSTANT' rowExpression, in some
// case, 'CAST' is superfluous
RowExpression argument = call.getArguments().get(0);
Type type = call.getType();
if (argument instanceof ConstantExpression && argument.getType().equals(VARCHAR)) {
String value = argument.accept(this, context);
if (type instanceof VarcharType || type instanceof CharType || type instanceof VarbinaryType || type instanceof DecimalType || type instanceof RealType || type instanceof DoubleType) {
return value;
}
}
if (call.getType().getDisplayName().equals(LIKE_PATTERN_NAME)) {
return call.getArguments().get(0).accept(this, context);
}
return getCastExpression(call.getArguments().get(0).accept(this, context), call.getType());
}
return super.visitCall(call, context);
}
use of io.prestosql.spi.relation.ConstantExpression in project hetu-core by openlookeng.
the class ValuesMatcher method detailMatches.
@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
ValuesNode valuesNode = (ValuesNode) node;
if (!expectedRows.map(rows -> rows.equals(valuesNode.getRows().stream().map(rowExpressions -> rowExpressions.stream().map(rowExpression -> {
if (isExpression(rowExpression)) {
return castToExpression(rowExpression);
}
ConstantExpression expression = (ConstantExpression) rowExpression;
if (expression.getType().getJavaType() == boolean.class) {
return new BooleanLiteral(String.valueOf(expression.getValue()));
}
if (expression.getType().getJavaType() == long.class) {
return new LongLiteral(String.valueOf(expression.getValue()));
}
if (expression.getType().getJavaType() == double.class) {
return new DoubleLiteral(String.valueOf(expression.getValue()));
}
if (expression.getType().getJavaType() == Slice.class) {
return new StringLiteral(String.valueOf(expression.getValue()));
}
return new GenericLiteral(expression.getType().toString(), String.valueOf(expression.getValue()));
}).collect(toImmutableList())).collect(toImmutableList()))).orElse(true)) {
return NO_MATCH;
}
return match(SymbolAliases.builder().putAll(Maps.transformValues(outputSymbolAliases, index -> toSymbolReference(valuesNode.getOutputSymbols().get(index)))).build());
}
use of io.prestosql.spi.relation.ConstantExpression in project hetu-core by openlookeng.
the class RowExpressionVerifier method visitDereferenceExpression.
@Override
protected Boolean visitDereferenceExpression(DereferenceExpression expected, RowExpression actual) {
if (!(actual instanceof SpecialForm) || !(((SpecialForm) actual).getForm().equals(DEREFERENCE))) {
return false;
}
SpecialForm actualDereference = (SpecialForm) actual;
if (actualDereference.getArguments().size() == 2 && actualDereference.getArguments().get(0).getType() instanceof RowType && actualDereference.getArguments().get(1) instanceof ConstantExpression) {
RowType rowType = (RowType) actualDereference.getArguments().get(0).getType();
Object value = LiteralInterpreter.evaluate((ConstantExpression) actualDereference.getArguments().get(1));
checkState(value instanceof Long);
long index = (Long) value;
checkState(index >= 0 && index < rowType.getFields().size());
RowType.Field field = rowType.getFields().get(toIntExact(index));
checkState(field.getName().isPresent());
return expected.getField().getValue().equals(field.getName().get()) && process(expected.getBase(), actualDereference.getArguments().get(0));
}
return false;
}
use of io.prestosql.spi.relation.ConstantExpression in project hetu-core by openlookeng.
the class RowExpressionVerifier method visitCast.
@Override
protected Boolean visitCast(Cast expected, RowExpression actual) {
// TODO: clean up cast path
if (actual instanceof ConstantExpression && expected.getExpression() instanceof Literal && expected.getType().equals(actual.getType().toString())) {
Literal literal = (Literal) expected.getExpression();
if (literal instanceof StringLiteral) {
Object value = LiteralInterpreter.evaluate((ConstantExpression) actual);
String actualString = value instanceof Slice ? ((Slice) value).toStringUtf8() : String.valueOf(value);
return ((StringLiteral) literal).getValue().equals(actualString);
}
return getValueFromLiteral(literal).equals(String.valueOf(LiteralInterpreter.evaluate((ConstantExpression) actual)));
}
if (actual instanceof VariableReferenceExpression && expected.getExpression() instanceof SymbolReference && expected.getType().equals(actual.getType().toString())) {
return visitSymbolReference((SymbolReference) expected.getExpression(), actual);
}
if (!(actual instanceof CallExpression) || !functionResolution.isCastFunction(((CallExpression) actual).getFunctionHandle())) {
return false;
}
if (!expected.getType().equalsIgnoreCase(actual.getType().toString()) && !(expected.getType().toLowerCase(ENGLISH).equals(VARCHAR) && actual.getType().getTypeSignature().getBase().equals(VARCHAR))) {
return false;
}
return process(expected.getExpression(), ((CallExpression) actual).getArguments().get(0));
}
use of io.prestosql.spi.relation.ConstantExpression in project hetu-core by openlookeng.
the class TestExpressionOptimizer method testCastWithJsonParseOptimization.
@Test
public void testCastWithJsonParseOptimization() {
FunctionHandle jsonParseFunctionHandle = functionAndTypeManager.lookupFunction("json_parse", fromTypes(VARCHAR));
// constant
FunctionHandle jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("array(integer)"));
RowExpression jsonCastExpression = new CallExpression(CAST.name(), jsonCastFunctionHandle, new ArrayType(INTEGER), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, constant(utf8Slice("[1, 2]"), VARCHAR))), Optional.empty());
RowExpression resultExpression = optimizer.optimize(jsonCastExpression);
assertInstanceOf(resultExpression, ConstantExpression.class);
Object resultValue = ((ConstantExpression) resultExpression).getValue();
assertInstanceOf(resultValue, IntArrayBlock.class);
assertEquals(toValues(INTEGER, (IntArrayBlock) resultValue), ImmutableList.of(1, 2));
// varchar to array
jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("array(varchar)"));
jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, new ArrayType(VARCHAR), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
resultExpression = optimizer.optimize(jsonCastExpression);
assertEquals(resultExpression, call(JSON_TO_ARRAY_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_ARRAY_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("array(varchar)")), new ArrayType(VARCHAR), field(1, VARCHAR)));
// varchar to row
jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("row(varchar,bigint)"));
jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
resultExpression = optimizer.optimize(jsonCastExpression);
assertEquals(resultExpression, call(JSON_TO_ROW_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_ROW_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("row(varchar,bigint)")), RowType.anonymous(ImmutableList.of(VARCHAR, BIGINT)), field(1, VARCHAR)));
// varchar to map
jsonCastFunctionHandle = functionAndTypeManager.lookupCast(CAST, JSON.getTypeSignature(), parseTypeSignature("map(integer,varchar)"));
jsonCastExpression = call(CAST.name(), jsonCastFunctionHandle, mapType(INTEGER, VARCHAR), ImmutableList.of(call("json_parse", jsonParseFunctionHandle, JSON, field(1, VARCHAR))));
resultExpression = optimizer.optimize(jsonCastExpression);
assertEquals(resultExpression, call(JSON_TO_MAP_CAST.name(), functionAndTypeManager.lookupCast(JSON_TO_MAP_CAST, VARCHAR.getTypeSignature(), parseTypeSignature("map(integer, varchar)")), mapType(INTEGER, VARCHAR), field(1, VARCHAR)));
}
Aggregations