public void assertInvalidFunction(String projection, StandardErrorCode errorCode, String messagePattern) { try { evaluateInvalid(projection); fail("Expected to throw a PrestoException with message matching " + messagePattern); } catch (PrestoException e) { try { assertEquals(e.getErrorCode(), errorCode.toErrorCode()); assertTrue(e.getMessage().equals(messagePattern) || e.getMessage().matches(messagePattern), format("Error message [%s] doesn't match [%s]", e.getMessage(), messagePattern)); } catch (Throwable failure) { failure.addSuppressed(e); throw failure; } } }
@Test public void testDeallocateNoSuchStatement() { try { executeDeallocate("my_query", "DEALLOCATE PREPARE my_query", TEST_SESSION); fail("expected exception"); } catch (PrestoException e) { assertEquals(e.getErrorCode(), NOT_FOUND.toErrorCode()); assertEquals(e.getMessage(), "Prepared statement not found: my_query"); } }
@Test public void testCreateTableInNotExistSchema() { SchemaTableName schemaTableName = new SchemaTableName("schema1", "test_table"); try { metadata.beginCreateTable(SESSION, new ConnectorTableMetadata(schemaTableName, ImmutableList.of(), tableProperties), Optional.empty()); fail("Should fail because schema does not exist"); } catch (PrestoException ex) { assertEquals(ex.getErrorCode(), NOT_FOUND.toErrorCode()); assertTrue(ex.getMessage().equals("Schema schema1 not found")); } }
@Test(timeOut = 60_000) public void testTimeout() { try { queryRunner.execute("SELECT * FROM disks"); fail(); } catch (PrestoException e) { assertEquals(e.getErrorCode(), ATOP_READ_TIMEOUT.toErrorCode(), e.getMessage()); } }
private static void assertInvalidExtract(String inputJson, String jsonPath, String message) { try { doJsonExtract(inputJson, jsonPath); } catch (PrestoException e) { assertEquals(e.getErrorCode(), INVALID_FUNCTION_ARGUMENT.toErrorCode()); assertEquals(e.getMessage(), message); } } }
@Test public void testMessage() { PrestoException exception = new PrestoException(new TestErrorCode(), "test"); assertEquals(exception.getMessage(), "test"); exception = new PrestoException(new TestErrorCode(), new RuntimeException("test2")); assertEquals(exception.getMessage(), "test2"); exception = new PrestoException(new TestErrorCode(), new RuntimeException()); assertEquals(exception.getMessage(), "test"); }
private void assertInvalidSpatialPartitioning(Session session, String sql, String expectedMessageRegExp) { LocalQueryRunner queryRunner = getQueryRunner(); try { queryRunner.inTransaction(session, transactionSession -> { queryRunner.createPlan(transactionSession, sql, LogicalPlanner.Stage.OPTIMIZED_AND_VALIDATED, false, WarningCollector.NOOP); return null; }); fail(format("Expected query to fail: %s", sql)); } catch (PrestoException ex) { assertEquals(ex.getErrorCode(), INVALID_SPATIAL_PARTITIONING.toErrorCode()); if (!nullToEmpty(ex.getMessage()).matches(expectedMessageRegExp)) { fail(format("Expected exception message '%s' to match '%s' for query: %s", ex.getMessage(), expectedMessageRegExp, sql), ex); } } }
public void assertInvalidCast(String projection, String message) { try { evaluateInvalid(projection); fail("Expected to throw an INVALID_CAST_ARGUMENT exception"); } catch (PrestoException e) { try { assertEquals(e.getErrorCode(), INVALID_CAST_ARGUMENT.toErrorCode()); assertEquals(e.getMessage(), message); } catch (Throwable failure) { failure.addSuppressed(e); throw failure; } } }
@Test public void testInvalidBinaryLength() { try { byte[] invalidLengthBinaryTimestamp = new byte[8]; getTimestampMillis(Binary.fromByteArray(invalidLengthBinaryTimestamp)); } catch (PrestoException e) { assertEquals(e.getErrorCode(), NOT_SUPPORTED.toErrorCode()); assertEquals(e.getMessage(), "Parquet timestamp must be 12 bytes, actual 8"); } }
public void assertNumericOverflow(String projection, String message) { try { evaluateInvalid(projection); fail("Expected to throw an NUMERIC_VALUE_OUT_OF_RANGE exception with message " + message); } catch (PrestoException e) { try { assertEquals(e.getErrorCode(), NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode()); assertEquals(e.getMessage(), message); } catch (Throwable failure) { failure.addSuppressed(e); throw failure; } } }
protected void assertNotSupported(String projection, String message) { try { functionAssertions.executeProjectionWithFullEngine(projection); fail("expected exception"); } catch (PrestoException e) { try { assertEquals(e.getErrorCode(), NOT_SUPPORTED.toErrorCode()); assertEquals(e.getMessage(), message); } catch (Throwable failure) { failure.addSuppressed(e); throw failure; } } }
private void assertInvalidCastWithJsonParse(String json, String castSqlType, String message) { String query = "" + "SELECT CAST(JSON_PARSE(col) AS " + castSqlType + ") " + "FROM (VALUES('" + json + "')) AS t(col)"; try { runner.execute(query); fail("Expected to throw an INVALID_CAST_ARGUMENT exception"); } catch (PrestoException e) { assertEquals(e.getErrorCode(), INVALID_CAST_ARGUMENT.toErrorCode()); assertEquals(e.getMessage(), message); } } }
@Test public void testPrepareInvalidStatement() { Statement statement = new Execute(identifier("foo"), emptyList()); String sqlString = "PREPARE my_query FROM EXECUTE foo"; try { executePrepare("my_query", statement, sqlString, TEST_SESSION); fail("expected exception"); } catch (PrestoException e) { assertEquals(e.getErrorCode(), NOT_SUPPORTED.toErrorCode()); assertEquals(e.getMessage(), "Invalid statement type for prepared statement: EXECUTE"); } }
private void assertFailure( Optional<HivePageSourceFactory> pageSourceFactory, Optional<HiveRecordCursorProvider> cursorProvider, HiveErrorCode expectedErrorCode, String expectedMessage) throws Exception { try { assertRead(pageSourceFactory, cursorProvider); fail("failure is expected"); } catch (PrestoException prestoException) { assertEquals(prestoException.getErrorCode(), expectedErrorCode.toErrorCode()); assertEquals(prestoException.getMessage(), expectedMessage); } } }
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; } }
@Test public void testSetSessionWithValidation() { testSetSessionWithValidation(new LongLiteral("0"), "0"); testSetSessionWithValidation(new LongLiteral("2"), "2"); try { testSetSessionWithValidation(new LongLiteral("-1"), "-1"); fail(); } catch (PrestoException e) { assertEquals(e.getMessage(), MUST_BE_POSITIVE); } }
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 assertInvalidTypeTranslation(String typeName, ErrorCode errorCode, String message) { Type type = TYPE_MANAGER.getType(parseTypeSignature(typeName)); try { HiveType.toHiveType(typeTranslator, type); fail("expected exception"); } catch (PrestoException e) { try { assertEquals(e.getErrorCode(), errorCode); assertContains(e.getMessage(), message); } catch (Throwable failure) { failure.addSuppressed(e); throw failure; } } } }
@Test public void tableAlreadyExists() { assertNoTables(); SchemaTableName test1Table = new SchemaTableName("default", "test1"); SchemaTableName test2Table = new SchemaTableName("default", "test2"); metadata.createTable(SESSION, new ConnectorTableMetadata(test1Table, ImmutableList.of()), false); try { metadata.createTable(SESSION, new ConnectorTableMetadata(test1Table, ImmutableList.of()), false); fail("Should fail because table already exists"); } catch (PrestoException ex) { assertEquals(ex.getErrorCode(), ALREADY_EXISTS.toErrorCode()); assertEquals(ex.getMessage(), "Table [default.test1] already exists"); } ConnectorTableHandle test1TableHandle = metadata.getTableHandle(SESSION, test1Table); metadata.createTable(SESSION, new ConnectorTableMetadata(test2Table, ImmutableList.of()), false); try { metadata.renameTable(SESSION, test1TableHandle, test2Table); fail("Should fail because table already exists"); } catch (PrestoException ex) { assertEquals(ex.getErrorCode(), ALREADY_EXISTS.toErrorCode()); assertEquals(ex.getMessage(), "Table [default.test2] already exists"); } }
@Test public void testOutOfBound() { InternalAggregationFunction function = METADATA.getFunctionRegistry().getAggregateFunctionImplementation( new Signature("max_by", AGGREGATE, parseTypeSignature("array(varchar)"), parseTypeSignature(StandardTypes.VARCHAR), parseTypeSignature(StandardTypes.BIGINT), parseTypeSignature(StandardTypes.BIGINT))); try { groupedAggregation(function, new Page(createStringsBlock("z"), createLongsBlock(0), createLongsBlock(10001))); } catch (PrestoException e) { assertEquals(e.getMessage(), "third argument of max_by/min_by must be less than or equal to 10000; found 10001"); } } }