use of io.prestosql.spi.relation.ConstantExpression in project hetu-core by openlookeng.
the class LogicalRowExpressions method flipOperatorFunctionWithOneVarOneConstant.
public RowExpression flipOperatorFunctionWithOneVarOneConstant(RowExpression expressions) {
if (expressions instanceof CallExpression) {
CallExpression call = (CallExpression) expressions;
FunctionMetadata functionMetadata = functionMetadataManager.getFunctionMetadata(call.getFunctionHandle());
if (functionMetadata.getOperatorType().isPresent() && functionMetadata.getOperatorType().get().isComparisonOperator()) {
if (call.getArguments().get(1) instanceof VariableReferenceExpression && call.getArguments().get(0) instanceof ConstantExpression) {
OperatorType operator;
switch(functionMetadata.getOperatorType().get()) {
case LESS_THAN:
operator = GREATER_THAN;
break;
case LESS_THAN_OR_EQUAL:
operator = GREATER_THAN_OR_EQUAL;
break;
case GREATER_THAN:
operator = LESS_THAN;
break;
case GREATER_THAN_OR_EQUAL:
operator = LESS_THAN_OR_EQUAL;
break;
case IS_DISTINCT_FROM:
operator = IS_DISTINCT_FROM;
break;
case EQUAL:
case NOT_EQUAL:
operator = functionMetadata.getOperatorType().get();
break;
default:
throw new UnsupportedOperationException(format("Unsupported or is not comparison operator: %s", functionMetadata.getOperatorType().get()));
}
List<RowExpression> arguments = new ArrayList<>();
arguments.add(call.getArguments().get(1));
arguments.add(call.getArguments().get(0));
return new CallExpression(operator.name(), functionResolution.comparisonFunction(operator, call.getArguments().get(1).getType(), call.getArguments().get(0).getType()), call.getType(), arguments, Optional.empty());
}
}
}
return expressions;
}
use of io.prestosql.spi.relation.ConstantExpression in project hetu-core by openlookeng.
the class TypeUtils method extractValueFromRowExpression.
public static Object extractValueFromRowExpression(RowExpression rowExpression) {
if (rowExpression instanceof CallExpression) {
CallExpression callExpression = (CallExpression) rowExpression;
String name = ((BuiltInFunctionHandle) callExpression.getFunctionHandle()).getSignature().getNameSuffix();
if (name.equals(CAST_OPERATOR)) {
Object valueExtracted = extractValueFromRowExpression(callExpression.getArguments().get(0));
String valueTypeName = valueExtracted.getClass().getTypeName().toLowerCase(Locale.ROOT);
String expectedTypeName = rowExpression.getType().getDisplayName().toLowerCase(Locale.ROOT);
if (!valueTypeName.contains(expectedTypeName) && expectedTypeName.equals("real")) {
// A cast is necessary for Real type, so use Float casting
return Float.parseFloat(valueExtracted.toString());
} else if (!valueTypeName.contains(expectedTypeName) && expectedTypeName.equals("date")) {
// A cast is necessary for Date type, so use Long casting
LocalDate dateGiven = LocalDate.parse(valueExtracted.toString());
return dateGiven.toEpochDay();
}
return valueExtracted;
}
} else if (rowExpression instanceof ConstantExpression) {
ConstantExpression constant = (ConstantExpression) rowExpression;
return getActualValue(constant.getType(), constant.getValue());
}
throw new UnsupportedOperationException("Not Implemented Exception: " + rowExpression.toString());
}
use of io.prestosql.spi.relation.ConstantExpression in project boostkit-bigdata by kunpengcompute.
the class OmniRowExpressionUtil method expressionStringify.
/**
* Stringify a RowExpression
*
* @param rowExpression RowExpression from openLooKeng
* @return String representation of the RowExpression
*/
public static String expressionStringify(RowExpression rowExpression) {
if (rowExpression instanceof CallExpression) {
CallExpression callExpression = (CallExpression) rowExpression;
List<String> args = callExpression.getArguments().stream().map(OmniRowExpressionUtil::expressionStringify).collect(Collectors.toList());
return callExpression.getDisplayName() + ":" + typeToDataTypeId(callExpression.getType()) + "(" + Joiner.on(", ").join(args) + ")";
}
if (rowExpression instanceof SpecialForm) {
SpecialForm specialForm = (SpecialForm) rowExpression;
List<String> args = specialForm.getArguments().stream().map(OmniRowExpressionUtil::expressionStringify).collect(Collectors.toList());
return specialForm.getForm().name() + ":" + toDataType(specialForm.getType()).getId().ordinal() + "(" + Joiner.on(", ").join(args) + ")";
}
if (rowExpression instanceof LambdaDefinitionExpression) {
LambdaDefinitionExpression lambdaDefinitionExpression = (LambdaDefinitionExpression) rowExpression;
return "(" + Joiner.on(", ").join(lambdaDefinitionExpression.getArguments()) + ") -> " + lambdaDefinitionExpression.getBody();
}
if (rowExpression instanceof InputReferenceExpression || rowExpression instanceof VariableReferenceExpression) {
return rowExpression.toString();
}
if (rowExpression instanceof ConstantExpression) {
ConstantExpression constantExpression = (ConstantExpression) rowExpression;
Type type = rowExpression.getType();
if (type instanceof UnknownType && constantExpression.getValue() == null) {
return "NULL:0";
}
if ((type instanceof VarcharType || type instanceof CharType) && constantExpression.getValue() instanceof Slice) {
String varcharValue = ((Slice) constantExpression.getValue()).toStringAscii();
return "'" + varcharValue + "':" + typeToDataTypeId(type);
}
if (type instanceof DecimalType && !((DecimalType) type).isShort() && constantExpression.getValue() instanceof Slice) {
return Decimals.decodeUnscaledValue((Slice) constantExpression.getValue()) + ":" + typeToDataTypeId(type);
}
return constantExpression.getValue() + ":" + typeToDataTypeId(type);
}
return rowExpression.toString();
}
use of io.prestosql.spi.relation.ConstantExpression in project boostkit-bigdata by kunpengcompute.
the class OmniRowExpressionUtil method generateLikeExpr.
/**
* Converts SQL like query syntax into regex syntax and recreate return new
* appropriate filter
*
* @param rawString String from OmniLocalExecutionPlanner
* @param translatedExpr Optional<RowExpression> from
* OmniLocalExecutionPlanner
* @return new Optional<RowExpression> with new regex syntax argument
*/
public static Optional<RowExpression> generateLikeExpr(String rawString, Optional<RowExpression> translatedExpr) {
List<RowExpression> newArgs = new LinkedList<RowExpression>();
StringBuilder regexString = new StringBuilder(rawString.length() * 2 + 2);
regexString.append('^');
for (char curChar : rawString.toCharArray()) {
switch(curChar) {
case '%':
regexString.append(".*");
break;
case '_':
regexString.append(".");
break;
case '\\':
case '^':
case '$':
case '.':
case '*':
regexString.append("\\");
regexString.append(curChar);
break;
default:
regexString.append(curChar);
break;
}
}
regexString.append('$');
ConstantExpression regexStringSlice = new ConstantExpression(Slices.utf8Slice(regexString.toString()), VARCHAR);
newArgs.add(((CallExpression) translatedExpr.get()).getArguments().get(0));
newArgs.add(regexStringSlice);
Optional<RowExpression> likeTranslatedFilter = Optional.of(new CallExpression(((CallExpression) translatedExpr.get()).getDisplayName().toUpperCase(Locale.ROOT), ((CallExpression) translatedExpr.get()).getFunctionHandle(), ((CallExpression) translatedExpr.get()).getType(), newArgs));
return likeTranslatedFilter;
}
use of io.prestosql.spi.relation.ConstantExpression in project boostkit-bigdata by kunpengcompute.
the class DataIoAdapter method createAggProjection.
private RowExpression createAggProjection(Expression expression) {
Type prestoType = NdpUtils.transOlkDataType(expression.dataType(), false);
AggExpressionType aggExpressionType = AggExpressionType.valueOf(expression.getClass().getSimpleName());
switch(aggExpressionType) {
case Add:
return createAggBinCall((Add) expression, "Add", prestoType);
case Subtract:
return createAggBinCall((Subtract) expression, "Subtract", prestoType);
case Multiply:
return createAggBinCall((Multiply) expression, "Multiply", prestoType);
case Divide:
return createAggBinCall((Divide) expression, "Divide", prestoType);
case Remainder:
return createAggBinCall((Remainder) expression, "Remainder", prestoType);
case Literal:
Object value = NdpUtils.transData(expression.dataType().toString(), expression.toString());
return new ConstantExpression(value, prestoType);
case AttributeReference:
String aggColumnName = expression.toString().split("#")[0];
int field;
if (null == columnNameMap.get(aggColumnName)) {
field = columnNameMap.size();
columnNameMap.put(aggColumnName, field);
int columnId = NdpUtils.getColumnId(expression.toString()) - columnOffset;
boolean isPartitionKey = partitionColumnName.contains(aggColumnName);
String partitionValue = NdpUtils.getPartitionValue(filePath, aggColumnName);
omnidataColumns.add(new Column(columnId, aggColumnName, prestoType, isPartitionKey, partitionValue));
} else {
field = columnNameMap.get(aggColumnName);
}
return new InputReferenceExpression(field, prestoType);
default:
throw new UnsupportedOperationException("unsupported agg operation type");
}
}
Aggregations