use of io.prestosql.spi.relation.SpecialForm in project boostkit-bigdata by kunpengcompute.
the class DataIoAdapter method reverseExpressionTree.
private RowExpression reverseExpressionTree(Expression filterExpression) {
RowExpression resRowExpression = null;
if (filterExpression == null) {
return resRowExpression;
}
List<RowExpression> tempRowExpression = new ArrayList<>();
if (filterExpression instanceof Or) {
RowExpression a1 = getExpression(((Or) filterExpression).left());
RowExpression a2 = getExpression(((Or) filterExpression).right());
tempRowExpression.add(a1);
tempRowExpression.add(a2);
resRowExpression = new SpecialForm(SpecialForm.Form.valueOf("OR"), BOOLEAN, tempRowExpression);
} else if (filterExpression instanceof And) {
RowExpression a1 = getExpression(((And) filterExpression).left());
RowExpression a2 = getExpression(((And) filterExpression).right());
tempRowExpression.add(a2);
tempRowExpression.add(a1);
resRowExpression = new SpecialForm(SpecialForm.Form.valueOf("AND"), BOOLEAN, tempRowExpression);
} else {
resRowExpression = getExpression(filterExpression);
}
return resRowExpression;
}
use of io.prestosql.spi.relation.SpecialForm in project boostkit-bigdata by kunpengcompute.
the class OmniRowExpressionUtil method getWhenExpr.
/**
* Generates a RowExpression for when-then expression special case
*
* @param staticExpr when-then expression
* @param translatedExpr RowExpression from staticFilter conversion
* @return new Optional<RowExpression> checked for possible like expression
* nested within
* note: refer to
* SqlToRowExpressionTranslator.visitSearchedCaseExpression to see how
* when-then expressions
* are being translated. This method involves some assumptions so some
* comments have been provided
*/
private static Optional<RowExpression> getWhenExpr(SearchedCaseExpression staticExpr, SpecialForm translatedExpr) {
// generates a list of the expr involved in the nested when-then expression and
// sends it to helper
List<Expression> exprList = new ArrayList<Expression>();
for (WhenClause clause : staticExpr.getWhenClauses()) {
exprList.add(clause.getOperand());
exprList.add(clause.getResult());
}
boolean defaultValueExist = (((SearchedCaseExpression) staticExpr).getDefaultValue()).isPresent();
exprList.add(defaultValueExist ? (((SearchedCaseExpression) staticExpr).getDefaultValue()).get() : null);
List<RowExpression> newArguments = new ArrayList<RowExpression>();
LinkedList<Type> typeList = new LinkedList<Type>();
SpecialForm originalExpr = translatedExpr;
int i = 0;
// expressions
while (true) {
typeList.addFirst(translatedExpr.getType());
// layer.
if (i >= exprList.size() - 3) {
for (int j = 0; j < translatedExpr.getArguments().size(); i++, j++) {
newArguments.add((generateOmniExpr(exprList.get(i), translatedExpr.getArguments().get(j))).get());
}
break;
} else {
for (int j = 0; j < translatedExpr.getArguments().size() - 1; i++, j++) {
newArguments.add((generateOmniExpr(exprList.get(i), translatedExpr.getArguments().get(j))).get());
}
translatedExpr = (SpecialForm) translatedExpr.getArguments().get(2);
}
}
// Recreating the SpecialFrom RowExpression in the same recursive format has it
// was done in
// SqlToRowExpressionTranslator.visitSearchedCaseExpression
Collections.reverse(newArguments);
RowExpression expression = new SpecialForm(originalExpr.getForm(), typeList.removeFirst(), newArguments.get(2), newArguments.get(1), newArguments.get(0));
for (int j = 3; j < newArguments.size(); j += 2) {
expression = new SpecialForm(originalExpr.getForm(), typeList.removeFirst(), newArguments.get(j + 1), newArguments.get(j), expression);
}
return Optional.of(expression);
}
use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.
the class PushAggregationThroughOuterJoin method coalesceWithNullAggregation.
// When the aggregation is done after the join, there will be a null value that gets aggregated over
// where rows did not exist in the inner table. For some aggregate functions, such as count, the result
// of an aggregation over a single null row is one or zero rather than null. In order to ensure correct results,
// we add a coalesce function with the output of the new outer join and the agggregation performed over a single
// null row.
private Optional<PlanNode> coalesceWithNullAggregation(AggregationNode aggregationNode, PlanNode outerJoin, PlanSymbolAllocator planSymbolAllocator, PlanNodeIdAllocator idAllocator, Lookup lookup) {
// Create an aggregation node over a row of nulls.
Optional<MappedAggregationInfo> aggregationOverNullInfoResultNode = createAggregationOverNull(aggregationNode, planSymbolAllocator, idAllocator, lookup);
if (!aggregationOverNullInfoResultNode.isPresent()) {
return Optional.empty();
}
MappedAggregationInfo aggregationOverNullInfo = aggregationOverNullInfoResultNode.get();
AggregationNode aggregationOverNull = aggregationOverNullInfo.getAggregation();
Map<Symbol, Symbol> sourceAggregationToOverNullMapping = aggregationOverNullInfo.getSymbolMapping();
// Do a cross join with the aggregation over null
JoinNode crossJoin = new JoinNode(idAllocator.getNextId(), JoinNode.Type.INNER, outerJoin, aggregationOverNull, ImmutableList.of(), ImmutableList.<Symbol>builder().addAll(outerJoin.getOutputSymbols()).addAll(aggregationOverNull.getOutputSymbols()).build(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of());
// Add coalesce expressions for all aggregation functions
Assignments.Builder assignmentsBuilder = Assignments.builder();
for (Symbol symbol : outerJoin.getOutputSymbols()) {
if (aggregationNode.getAggregations().containsKey(symbol)) {
assignmentsBuilder.put(symbol, new SpecialForm(COALESCE, planSymbolAllocator.getTypes().get(symbol), ImmutableList.of(toVariableReference(symbol, planSymbolAllocator.getTypes()), toVariableReference(sourceAggregationToOverNullMapping.get(symbol), planSymbolAllocator.getTypes()))));
} else {
assignmentsBuilder.put(symbol, toVariableReference(symbol, planSymbolAllocator.getTypes()));
}
}
return Optional.of(new ProjectNode(idAllocator.getNextId(), crossJoin, assignmentsBuilder.build()));
}
use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.
the class SplitFiltering method isSupportedExpression.
private static boolean isSupportedExpression(RowExpression predicate) {
if (predicate instanceof SpecialForm) {
SpecialForm specialForm = (SpecialForm) predicate;
switch(specialForm.getForm()) {
case BETWEEN:
case IN:
return true;
case AND:
case OR:
return isSupportedExpression(specialForm.getArguments().get(0)) && isSupportedExpression(specialForm.getArguments().get(1));
default:
return false;
}
}
if (predicate instanceof CallExpression) {
CallExpression call = (CallExpression) predicate;
FunctionHandle builtInFunctionHandle = call.getFunctionHandle();
if (builtInFunctionHandle instanceof BuiltInFunctionHandle) {
Signature signature = ((BuiltInFunctionHandle) builtInFunctionHandle).getSignature();
if (signature.getName().getObjectName().equals("not")) {
return true;
}
try {
OperatorType operatorType = Signature.unmangleOperator(signature.getName().getObjectName());
if (operatorType.isComparisonOperator() && operatorType != IS_DISTINCT_FROM) {
return true;
}
return false;
} catch (IllegalArgumentException e) {
return false;
}
}
}
return false;
}
use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.
the class SplitFiltering method getAllColumns.
public static void getAllColumns(RowExpression expression, Set<String> columns, Map<Symbol, ColumnHandle> assignments) {
if (expression instanceof SpecialForm) {
SpecialForm specialForm = (SpecialForm) expression;
RowExpression left;
switch(specialForm.getForm()) {
case BETWEEN:
case IN:
left = extractExpression(specialForm.getArguments().get(0));
break;
case AND:
case OR:
getAllColumns(specialForm.getArguments().get(0), columns, assignments);
getAllColumns(specialForm.getArguments().get(1), columns, assignments);
return;
default:
return;
}
if (!(left instanceof VariableReferenceExpression)) {
LOG.warn("Invalid Left of expression %s, should be an VariableReferenceExpression", left.toString());
return;
}
String columnName = ((VariableReferenceExpression) left).getName();
Symbol columnSymbol = new Symbol(columnName);
if (assignments.containsKey(columnSymbol)) {
columnName = assignments.get(columnSymbol).getColumnName();
}
columns.add(columnName);
return;
}
if (expression instanceof CallExpression) {
CallExpression call = (CallExpression) expression;
try {
FunctionHandle builtInFunctionHandle = call.getFunctionHandle();
Signature signature;
if (builtInFunctionHandle instanceof BuiltInFunctionHandle) {
signature = ((BuiltInFunctionHandle) builtInFunctionHandle).getSignature();
} else {
return;
}
OperatorType operatorType = Signature.unmangleOperator(signature.getName().getObjectName());
if (!operatorType.isComparisonOperator()) {
return;
}
RowExpression left = extractExpression(call.getArguments().get(0));
if (!(left instanceof VariableReferenceExpression)) {
LOG.warn("Invalid Left of expression %s, should be an VariableReferenceExpression", left.toString());
return;
}
String columnName = ((VariableReferenceExpression) left).getName();
Symbol columnSymbol = new Symbol(columnName);
if (assignments.containsKey(columnSymbol)) {
columnName = assignments.get(columnSymbol).getColumnName();
}
columns.add(columnName);
return;
} catch (IllegalArgumentException e) {
return;
}
}
return;
}
Aggregations