use of io.prestosql.spi.function.OperatorType in project hetu-core by openlookeng.
the class TestFunctionRegistry method testExactMatchBeforeCoercion.
@Test
public void testExactMatchBeforeCoercion() {
Metadata metadata = createTestMetadataManager();
boolean foundOperator = false;
for (SqlFunction function : listOperators(metadata)) {
OperatorType operatorType = unmangleOperator(function.getSignature().getName().getObjectName());
if (operatorType == OperatorType.CAST || operatorType == OperatorType.SATURATED_FLOOR_CAST) {
continue;
}
if (!function.getSignature().getTypeVariableConstraints().isEmpty()) {
continue;
}
if (function.getSignature().getArgumentTypes().stream().anyMatch(TypeSignature::isCalculated)) {
continue;
}
List<Type> argumentTypes = function.getSignature().getArgumentTypes().stream().map(metadata::getType).collect(toImmutableList());
Signature exactOperator = metadata.getFunctionAndTypeManager().resolveBuiltInFunction(QualifiedName.of(mangleOperatorName(operatorType)), fromTypes(argumentTypes));
assertEquals(exactOperator, function.getSignature());
foundOperator = true;
}
assertTrue(foundOperator);
}
use of io.prestosql.spi.function.OperatorType in project hetu-core by openlookeng.
the class DecimalOperators method decimalSubtractOperator.
private static SqlScalarFunction decimalSubtractOperator() {
TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
Signature signature = Signature.builder().kind(SCALAR).operatorType(SUBTRACT).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
return SqlScalarFunction.builder(DecimalOperators.class, SUBTRACT).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("subtractShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("subtractShortShortLong", "subtractLongLongLong", "subtractShortLongLong", "subtractLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters))).build();
}
use of io.prestosql.spi.function.OperatorType in project hetu-core by openlookeng.
the class BTreeIndex method lookUp.
@Override
public Iterator<String> lookUp(Object expression) throws IndexLookUpException {
Collection<String> lookUpResults = Collections.emptyList();
if (expression instanceof CallExpression) {
CallExpression callExp = (CallExpression) expression;
Object key = extractValueFromRowExpression(callExp.getArguments().get(1));
BuiltInFunctionHandle builtInFunctionHandle;
if (callExp.getFunctionHandle() instanceof BuiltInFunctionHandle) {
builtInFunctionHandle = (BuiltInFunctionHandle) callExp.getFunctionHandle();
} else {
throw new UnsupportedOperationException("Unsupported function: " + callExp.getDisplayName());
}
Optional<OperatorType> operatorOptional = Signature.getOperatorType(builtInFunctionHandle.getSignature().getNameSuffix());
if (operatorOptional.isPresent()) {
OperatorType operator = operatorOptional.get();
switch(operator) {
case EQUAL:
if (dataMap.containsKey(key)) {
lookUpResults = Collections.singleton(dataMap.get(key));
}
break;
case LESS_THAN:
lookUpResults = rangeLookUp(dataMap.firstKey(), true, key, false);
break;
case LESS_THAN_OR_EQUAL:
lookUpResults = rangeLookUp(dataMap.firstKey(), true, key, true);
break;
case GREATER_THAN:
lookUpResults = rangeLookUp(key, false, dataMap.lastKey(), true);
break;
case GREATER_THAN_OR_EQUAL:
lookUpResults = rangeLookUp(key, true, dataMap.lastKey(), true);
break;
default:
throw new UnsupportedOperationException("Expression not supported");
}
}
} else if (expression instanceof SpecialForm) {
SpecialForm specialForm = (SpecialForm) expression;
switch(specialForm.getForm()) {
case BETWEEN:
Object left = extractValueFromRowExpression(specialForm.getArguments().get(1));
Object right = extractValueFromRowExpression(specialForm.getArguments().get(2));
lookUpResults = rangeLookUp(left, true, right, true);
break;
case IN:
lookUpResults = new ArrayList<>();
for (RowExpression exp : specialForm.getArguments().subList(1, specialForm.getArguments().size())) {
Object key = extractValueFromRowExpression(exp);
if (dataMap.containsKey(key)) {
lookUpResults.add(dataMap.get(key));
}
}
break;
default:
throw new UnsupportedOperationException("Expression not supported");
}
} else {
throw new UnsupportedOperationException("Expression not supported");
}
Set<String> symbolSet = new HashSet<>();
// break lookUp results to symbols and keep in a set. e.g. ["1,2,2,4","2,3"] -> set{"1", "2", "3", "4"}
for (String res : lookUpResults) {
Collections.addAll(symbolSet, res.split(","));
}
// translate the symbols to actual data values
List<String> translated = new ArrayList<>(symbolSet.size());
for (String sym : symbolSet) {
translated.add(symbolTable != null ? symbolTable.get(sym) : sym);
}
translated.sort(String::compareTo);
return translated.iterator();
}
use of io.prestosql.spi.function.OperatorType in project hetu-core by openlookeng.
the class HanaRowExpressionConverter method handleOperatorFunction.
private String handleOperatorFunction(CallExpression call, FunctionMetadata functionMetadata, JdbcConverterContext context) {
Optional<OperatorType> operatorTypeOptional = functionMetadata.getOperatorType();
OperatorType type = operatorTypeOptional.get();
if (type.equals(OperatorType.CAST)) {
return handleCastOperator(call.getArguments().get(0), call.getType(), context);
}
List<String> argumentList = call.getArguments().stream().map(expr -> expr.accept(this, context)).collect(Collectors.toList());
if (type.isArithmeticOperator()) {
if (type.equals(OperatorType.MODULUS)) {
return format("MOD(%s, %s)", argumentList.get(0), argumentList.get(1));
} else {
return format("(%s %s %s)", argumentList.get(0), type.getOperator(), argumentList.get(1));
}
}
if (type.isComparisonOperator()) {
final String[] hanaCompareOperators = new String[] { "=", ">", "<", ">=", "<=", "!=", "<>" };
if (Arrays.asList(hanaCompareOperators).contains(type.getOperator())) {
return format("(%s %s %s)", argumentList.get(0), type.getOperator(), argumentList.get(1));
} else {
String exceptionInfo = "Hana Connector does not support comparison operator " + type.getOperator();
throw new PrestoException(NOT_SUPPORTED, exceptionInfo);
}
}
if (type.equals(OperatorType.SUBSCRIPT)) {
if (call.getArguments().size() == 2) {
return format("MEMBER_AT(%s, %s)", argumentList.get(0), argumentList.get(1));
}
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal argument num of function " + type.getOperator());
}
if (call.getArguments().size() == 1 && type.equals(OperatorType.NEGATION)) {
String value = argumentList.get(0);
String separator = value.startsWith("-") ? " " : "";
return format("-%s%s", separator, value);
}
throw new PrestoException(NOT_SUPPORTED, String.format("Unknown operator %s in push down", type.getOperator()));
}
use of io.prestosql.spi.function.OperatorType 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;
}
Aggregations