private static void propagateIfUnhandled(PrestoException e) throws PrestoException { int errorCode = e.getErrorCode().getCode(); if (errorCode == DIVISION_BY_ZERO.toErrorCode().getCode() || errorCode == INVALID_CAST_ARGUMENT.toErrorCode().getCode() || errorCode == INVALID_FUNCTION_ARGUMENT.toErrorCode().getCode() || errorCode == NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode().getCode()) { return; } throw e; } }
public boolean isRegistered(Signature signature) { try { // TODO: this is hacky, but until the magic literal and row field reference hacks are cleaned up it's difficult to implement this. getScalarFunctionImplementation(signature); return true; } catch (PrestoException e) { if (e.getErrorCode().getCode() == FUNCTION_IMPLEMENTATION_MISSING.toErrorCode().getCode()) { return false; } throw e; } }
public Signature getCoercion(TypeSignature fromType, TypeSignature toType) { Signature signature = internalOperator(OperatorType.CAST.name(), toType, ImmutableList.of(fromType)); try { getScalarFunctionImplementation(signature); } catch (PrestoException e) { if (e.getErrorCode().getCode() == FUNCTION_IMPLEMENTATION_MISSING.toErrorCode().getCode()) { throw new OperatorNotFoundException(OperatorType.CAST, ImmutableList.of(fromType), toType); } throw e; } return signature; }
public static Signature resolveFunction(FunctionCall node, List<TypeSignatureProvider> argumentTypes, FunctionRegistry functionRegistry) { try { return functionRegistry.resolveFunction(node.getName(), argumentTypes); } catch (PrestoException e) { if (e.getErrorCode().getCode() == StandardErrorCode.FUNCTION_NOT_FOUND.toErrorCode().getCode()) { throw new SemanticException(SemanticErrorCode.FUNCTION_NOT_FOUND, node, e.getMessage()); } if (e.getErrorCode().getCode() == StandardErrorCode.AMBIGUOUS_FUNCTION_CALL.toErrorCode().getCode()) { throw new SemanticException(SemanticErrorCode.AMBIGUOUS_FUNCTION_CALL, node, e.getMessage()); } throw e; } }
private static int code(StandardErrorCode error) { return error.toErrorCode().getCode(); } }
public Signature resolveOperator(OperatorType operatorType, List<? extends Type> argumentTypes) throws OperatorNotFoundException { try { return resolveFunction(QualifiedName.of(mangleOperatorName(operatorType)), fromTypes(argumentTypes)); } catch (PrestoException e) { if (e.getErrorCode().getCode() == FUNCTION_NOT_FOUND.toErrorCode().getCode()) { throw new OperatorNotFoundException( operatorType, argumentTypes.stream() .map(Type::getTypeSignature) .collect(toImmutableList())); } else { throw e; } } }
private Type getOperator(StackableAstVisitorContext<Context> context, Expression node, OperatorType operatorType, Expression... arguments) { ImmutableList.Builder<Type> argumentTypes = ImmutableList.builder(); for (Expression expression : arguments) { argumentTypes.add(process(expression, context)); } Signature operatorSignature; try { operatorSignature = functionRegistry.resolveOperator(operatorType, argumentTypes.build()); } catch (OperatorNotFoundException e) { throw new SemanticException(TYPE_MISMATCH, node, "%s", e.getMessage()); } catch (PrestoException e) { if (e.getErrorCode().getCode() == StandardErrorCode.AMBIGUOUS_FUNCTION_CALL.toErrorCode().getCode()) { throw new SemanticException(SemanticErrorCode.AMBIGUOUS_FUNCTION_CALL, node, e.getMessage()); } throw e; } for (int i = 0; i < arguments.length; i++) { Expression expression = arguments[i]; Type type = typeManager.getType(operatorSignature.getArgumentTypes().get(i)); coerceType(context, expression, type, format("Operator %s argument %d", operatorSignature, i)); } Type type = typeManager.getType(operatorSignature.getReturnType()); return setExpressionType(node, type); }
private void waitForQueryToBeKilled(DistributedQueryRunner queryRunner) throws InterruptedException { while (true) { for (BasicQueryInfo info : queryRunner.getCoordinator().getQueryManager().getQueries()) { if (info.getState().isDone()) { assertNotNull(info.getErrorCode()); assertEquals(info.getErrorCode().getCode(), CLUSTER_OUT_OF_MEMORY.toErrorCode().getCode()); return; } } MILLISECONDS.sleep(10); } }
@Test public void testCorrelatedScalarSubqueryInSelect() { assertDistributedPlan("SELECT name, (SELECT name FROM region WHERE regionkey = nation.regionkey) FROM nation", anyTree( filter(format("CASE \"is_distinct\" WHEN true THEN true ELSE CAST(fail(%s, 'Scalar sub-query has returned multiple rows') AS boolean) END", SUBQUERY_MULTIPLE_ROWS.toErrorCode().getCode()), markDistinct("is_distinct", ImmutableList.of("unique"), join(LEFT, ImmutableList.of(equiJoinClause("n_regionkey", "r_regionkey")), assignUniqueId("unique", exchange(REMOTE, REPARTITION, anyTree(tableScan("nation", ImmutableMap.of("n_regionkey", "regionkey"))))), anyTree( tableScan("region", ImmutableMap.of("r_regionkey", "regionkey")))))))); }
@Description("Throws an exception with a given error code and message") @ScalarFunction(value = "fail", hidden = true) @SqlType("unknown") public static boolean fail( @SqlType(StandardTypes.INTEGER) long errorCode, @SqlType(StandardTypes.VARCHAR) Slice message) { for (StandardErrorCode standardErrorCode : StandardErrorCode.values()) { if (standardErrorCode.toErrorCode().getCode() == errorCode) { throw new PrestoException(standardErrorCode, message.toStringUtf8()); } } throw new PrestoException(StandardErrorCode.GENERIC_INTERNAL_ERROR, "Unable to find error for code: " + errorCode); } }
firstNonNull(failure.getMessage(), "Internal error"), null, errorCode.getCode(), errorCode.getName(), errorCode.getType().toString(),
@Test public void testNoTransactionSupport() { Request request = preparePost() .setUri(uriFor("/v1/statement")) .setBodyGenerator(createStaticBodyGenerator("start transaction", UTF_8)) .setHeader(PRESTO_USER, "user") .setHeader(PRESTO_SOURCE, "source") .build(); QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC)); while (queryResults.getNextUri() != null) { queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_CODEC)); } assertNotNull(queryResults.getError()); assertEquals(queryResults.getError().getErrorCode(), INCOMPATIBLE_CLIENT.toErrorCode().getCode()); }
@Test public void testInvalidSessionError() { String invalidTimeZone = "this_is_an_invalid_time_zone"; Request request = preparePost().setHeader(PRESTO_USER, "user") .setUri(uriFor("/v1/statement")) .setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)) .setHeader(PRESTO_SOURCE, "source") .setHeader(PRESTO_CATALOG, "catalog") .setHeader(PRESTO_SCHEMA, "schema") .setHeader(PRESTO_PATH, "path") .setHeader(PRESTO_TIME_ZONE, invalidTimeZone) .build(); QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC)); while (queryResults.getNextUri() != null) { queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_CODEC)); } QueryError queryError = queryResults.getError(); assertNotNull(queryError); TimeZoneNotSupportedException expected = new TimeZoneNotSupportedException(invalidTimeZone); assertEquals(queryError.getErrorCode(), expected.getErrorCode().getCode()); assertEquals(queryError.getErrorName(), expected.getErrorCode().getName()); assertEquals(queryError.getErrorType(), expected.getErrorCode().getType().name()); assertEquals(queryError.getMessage(), expected.getMessage()); }
QualifiedName.of("fail"), ImmutableList.of( new LongLiteral(Integer.toString(SUBQUERY_MULTIPLE_ROWS.toErrorCode().getCode())), new StringLiteral("Scalar sub-query has returned multiple rows"))), BOOLEAN))));
@EventField public Integer getErrorCode() { return errorCode == null ? null : errorCode.getCode(); }
public static Object tryExpressionExceptionHandler(PrestoException e) throws PrestoException { int errorCode = e.getErrorCode().getCode(); if (errorCode == DIVISION_BY_ZERO.toErrorCode().getCode() || errorCode == INVALID_CAST_ARGUMENT.toErrorCode().getCode() || errorCode == INVALID_FUNCTION_ARGUMENT.toErrorCode().getCode() || errorCode == NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode().getCode()) { return null; } throw e; } }
public boolean isRegistered(Signature signature) { try { // TODO: this is hacky, but until the magic literal and row field reference hacks are cleaned up it's difficult to implement this. getScalarFunctionImplementation(signature); return true; } catch (PrestoException e) { if (e.getErrorCode().getCode() == FUNCTION_IMPLEMENTATION_MISSING.toErrorCode().getCode()) { return false; } throw e; } }
public Signature getCoercion(TypeSignature fromType, TypeSignature toType) { Signature signature = internalOperator(OperatorType.CAST.name(), toType, ImmutableList.of(fromType)); try { getScalarFunctionImplementation(signature); } catch (PrestoException e) { if (e.getErrorCode().getCode() == FUNCTION_IMPLEMENTATION_MISSING.toErrorCode().getCode()) { throw new OperatorNotFoundException(OperatorType.CAST, ImmutableList.of(fromType), toType); } throw e; } return signature; }
private void waitForQueryToBeKilled(DistributedQueryRunner queryRunner) throws InterruptedException { while (true) { for (BasicQueryInfo info : queryRunner.getCoordinator().getQueryManager().getQueries()) { if (info.getState().isDone()) { assertNotNull(info.getErrorCode()); assertEquals(info.getErrorCode().getCode(), CLUSTER_OUT_OF_MEMORY.toErrorCode().getCode()); return; } } MILLISECONDS.sleep(10); } }