@Test public void testParametersNonPreparedStatement() { try { computeActual("SELECT ?, 1"); fail("parameters not in prepared statements should fail"); } catch (SemanticException e) { assertEquals(e.getCode(), INVALID_PARAMETER_USAGE); } catch (RuntimeException e) { assertEquals(e.getMessage(), "line 1:1: Incorrect number of parameters: expected 1 but found 0"); } }
@Override public void assertInvalidFunction(String projection, SemanticErrorCode errorCode) { try { assertFunction(projection, UNKNOWN, null); fail("Expected error " + errorCode + " from " + projection); } catch (SemanticException e) { assertEquals(e.getCode(), errorCode); } }
public void assertInvalidFunction(String projection, SemanticErrorCode expectedErrorCode) { try { evaluateInvalid(projection); fail(format("Expected to throw %s exception", expectedErrorCode)); } catch (SemanticException e) { try { assertEquals(e.getCode(), expectedErrorCode); } catch (Throwable failure) { failure.addSuppressed(e); throw failure; } } }
@Test public void testExecuteWithParametersInGroupBy() { try { String query = "SELECT a + ?, count(1) FROM (VALUES 1, 2, 3, 2) t(a) GROUP BY a + ?"; Session session = Session.builder(getSession()) .addPreparedStatement("my_query", query) .build(); computeActual(session, "EXECUTE my_query USING 1, 1"); fail("parameters in GROUP BY and SELECT should fail"); } catch (SemanticException e) { assertEquals(e.getCode(), MUST_BE_AGGREGATE_OR_GROUP_BY); } catch (RuntimeException e) { assertEquals(e.getMessage(), "line 1:10: '(a + ?)' must be an aggregate expression or appear in GROUP BY clause"); } }
public void assertInvalidFunction(String projection, SemanticErrorCode expectedErrorCode, String message) { try { evaluateInvalid(projection); fail(format("Expected to throw %s exception", expectedErrorCode)); } catch (SemanticException e) { try { assertEquals(e.getCode(), expectedErrorCode); assertEquals(e.getMessage(), message); } catch (Throwable failure) { failure.addSuppressed(e); throw failure; } } }
private void assertFails(Session session, SemanticErrorCode error, String message, @Language("SQL") String query) { try { analyze(session, query); fail(format("Expected error %s, but analysis succeeded", error)); } catch (SemanticException e) { if (e.getCode() != error) { fail(format("Expected error %s, but found %s: %s", error, e.getCode(), e.getMessage()), e); } if (!e.getMessage().matches(message)) { fail(format("Expected error '%s', but got '%s'", message, e.getMessage()), e); } } }
@Test public void testTooFewParameters() { try { Session session = testSessionBuilder() .addPreparedStatement("my_query", "SELECT ? FROM foo where col1 = ?") .build(); QUERY_PREPARER.prepareQuery(session, "EXECUTE my_query USING 1"); fail("expected exception"); } catch (SemanticException e) { assertEquals(e.getCode(), INVALID_PARAMETER_USAGE); } } }
@Test public void testTooManyParameters() { try { Session session = testSessionBuilder() .addPreparedStatement("my_query", "SELECT * FROM foo where col1 = ?") .build(); QUERY_PREPARER.prepareQuery(session, "EXECUTE my_query USING 1,2"); fail("expected exception"); } catch (SemanticException e) { assertEquals(e.getCode(), INVALID_PARAMETER_USAGE); } }
private void assertFails(Session session, SemanticErrorCode error, Optional<NodeLocation> location, @Language("SQL") String query) { try { analyze(session, query); fail(format("Expected error %s, but analysis succeeded", error)); } catch (SemanticException e) { if (e.getCode() != error) { fail(format("Expected error %s, but found %s: %s", error, e.getCode(), e.getMessage()), e); } if (location.isPresent()) { NodeLocation expected = location.get(); NodeLocation actual = e.getNode().getLocation().get(); if (expected.getLineNumber() != actual.getLineNumber() || expected.getColumnNumber() != actual.getColumnNumber()) { fail(format( "Expected error '%s' to occur at line %s, offset %s, but was: line %s, offset %s", e.getCode(), expected.getLineNumber(), expected.getColumnNumber(), actual.getLineNumber(), actual.getColumnNumber())); } } } }
@Test public void testStartTransactionTooManyIsolationLevels() { Session session = sessionBuilder() .setClientTransactionSupport() .build(); TransactionManager transactionManager = createTestTransactionManager(); QueryStateMachine stateMachine = createQueryStateMachine("START TRANSACTION", session, transactionManager); assertFalse(stateMachine.getSession().getTransactionId().isPresent()); try { getFutureValue(new StartTransactionTask().execute( new StartTransaction(ImmutableList.of(new Isolation(Isolation.Level.READ_COMMITTED), new Isolation(Isolation.Level.READ_COMMITTED))), transactionManager, metadata, new AllowAllAccessControl(), stateMachine, emptyList())); fail(); } catch (SemanticException e) { assertEquals(e.getCode(), INVALID_TRANSACTION_MODE); } assertTrue(transactionManager.getAllTransactionInfos().isEmpty()); assertFalse(stateMachine.getQueryInfo(Optional.empty()).isClearTransactionId()); assertFalse(stateMachine.getQueryInfo(Optional.empty()).getStartedTransactionId().isPresent()); }
@Test public void testStartTransactionTooManyAccessModes() { Session session = sessionBuilder() .setClientTransactionSupport() .build(); TransactionManager transactionManager = createTestTransactionManager(); QueryStateMachine stateMachine = createQueryStateMachine("START TRANSACTION", session, transactionManager); assertFalse(stateMachine.getSession().getTransactionId().isPresent()); try { getFutureValue(new StartTransactionTask().execute( new StartTransaction(ImmutableList.of(new TransactionAccessMode(true), new TransactionAccessMode(true))), transactionManager, metadata, new AllowAllAccessControl(), stateMachine, emptyList())); fail(); } catch (SemanticException e) { assertEquals(e.getCode(), INVALID_TRANSACTION_MODE); } assertTrue(transactionManager.getAllTransactionInfos().isEmpty()); assertFalse(stateMachine.getQueryInfo(Optional.empty()).isClearTransactionId()); assertFalse(stateMachine.getQueryInfo(Optional.empty()).getStartedTransactionId().isPresent()); }
assertTrue(e.getCode() == TYPE_MISMATCH);
@Test public void testParametersNonPreparedStatement() { try { computeActual("SELECT ?, 1"); fail("parameters not in prepared statements should fail"); } catch (SemanticException e) { assertEquals(e.getCode(), INVALID_PARAMETER_USAGE); } catch (RuntimeException e) { assertEquals(e.getMessage(), "line 1:1: Incorrect number of parameters: expected 1 but found 0"); } }
protected void assertInvalidFunction(String projection, SemanticErrorCode expectedErrorCode) { try { evaluateInvalid(projection); fail(format("Expected to throw %s exception", expectedErrorCode)); } catch (SemanticException e) { assertEquals(e.getCode(), expectedErrorCode); } }
public void assertInvalidFunction(String projection, SemanticErrorCode errorCode) { try { assertFunction(projection, UNKNOWN, null); fail("Expected error " + errorCode + " from " + projection); } catch (SemanticException e) { assertEquals(e.getCode(), errorCode); } }
private void assertFailsWithoutExperimentalSyntax(SemanticErrorCode error, @Language("SQL") String query) { try { analyzeWithoutExperimentalSyntax(query); fail(format("Expected error %s, but analysis succeeded", error)); } catch (SemanticException e) { if (e.getCode() != error) { fail(format("Expected error %s, but found %s: %s", error, e.getCode(), e.getMessage()), e); } } }
private void assertFails(Session session, SemanticErrorCode error, @Language("SQL") String query) { try { analyze(session, query); fail(format("Expected error %s, but analysis succeeded", error)); } catch (SemanticException e) { if (e.getCode() != error) { fail(format("Expected error %s, but found %s: %s", error, e.getCode(), e.getMessage()), e); } } }
@Test public void testExecuteWithParametersInGroupBy() { try { String query = "SELECT a + ?, count(1) FROM (VALUES 1, 2, 3, 2) t(a) GROUP BY a + ?"; Session session = Session.builder(getSession()) .addPreparedStatement("my_query", query) .build(); computeActual(session, "EXECUTE my_query USING 1, 1"); fail("parameters in GROUP BY and SELECT should fail"); } catch (SemanticException e) { assertEquals(e.getCode(), MUST_BE_AGGREGATE_OR_GROUP_BY); } catch (RuntimeException e) { assertEquals(e.getMessage(), "line 1:10: '(a + ?)' must be an aggregate expression or appear in GROUP BY clause"); } }
private void assertMissingInformationSchema(Session session, @Language("SQL") String query) { try { analyze(session, query); fail("expected exception"); } catch (SemanticException e) { assertEquals(e.getCode(), MISSING_SCHEMA); assertEquals(e.getMessage(), "Schema information_schema does not exist"); } }
@Test public void testStartTransactionTooManyIsolationLevels() throws Exception { Session session = sessionBuilder() .setClientTransactionSupport() .build(); TransactionManager transactionManager = createTestTransactionManager(); QueryStateMachine stateMachine = QueryStateMachine.begin(new QueryId("query"), "START TRANSACTION", session, URI.create("fake://uri"), true, transactionManager, executor); assertFalse(stateMachine.getSession().getTransactionId().isPresent()); try { try { new StartTransactionTask().execute(new StartTransaction(ImmutableList.of(new Isolation(Isolation.Level.READ_COMMITTED), new Isolation(Isolation.Level.READ_COMMITTED))), transactionManager, metadata, new AllowAllAccessControl(), stateMachine).join(); fail(); } catch (CompletionException e) { throw Throwables.propagate(e.getCause()); } } catch (SemanticException e) { assertEquals(e.getCode(), INVALID_TRANSACTION_MODE); } assertTrue(transactionManager.getAllTransactionInfos().isEmpty()); assertFalse(stateMachine.getQueryInfoWithoutDetails().isClearTransactionId()); assertFalse(stateMachine.getQueryInfoWithoutDetails().getStartedTransactionId().isPresent()); }