use of io.trino.sql.parser.ParsingOptions in project trino by trinodb.
the class SqlFormatterUtil method getFormattedSql.
public static String getFormattedSql(Statement statement, SqlParser sqlParser) {
String sql = SqlFormatter.formatSql(statement);
// verify round-trip
Statement parsed;
try {
ParsingOptions parsingOptions = new ParsingOptions(REJECT);
parsed = sqlParser.createStatement(sql, parsingOptions);
} catch (ParsingException e) {
throw formattingFailure(e, "Formatted query does not parse", statement, sql);
}
if (!statement.equals(parsed)) {
throw formattingFailure(null, "Query does not round-trip", statement, sql);
}
return sql;
}
use of io.trino.sql.parser.ParsingOptions in project trino by trinodb.
the class HttpRequestSessionContextFactory method parsePreparedStatementsHeaders.
private Map<String, String> parsePreparedStatementsHeaders(ProtocolHeaders protocolHeaders, MultivaluedMap<String, String> headers) {
ImmutableMap.Builder<String, String> preparedStatements = ImmutableMap.builder();
parseProperty(headers, protocolHeaders.requestPreparedStatement()).forEach((key, value) -> {
String statementName;
try {
statementName = urlDecode(key);
} catch (IllegalArgumentException e) {
throw badRequest(format("Invalid %s header: %s", protocolHeaders.requestPreparedStatement(), e.getMessage()));
}
String sqlString = preparedStatementEncoder.decodePreparedStatementFromHeader(value);
// Validate statement
SqlParser sqlParser = new SqlParser();
try {
sqlParser.createStatement(sqlString, new ParsingOptions(AS_DOUBLE));
} catch (ParsingException e) {
throw badRequest(format("Invalid %s header: %s", protocolHeaders.requestPreparedStatement(), e.getMessage()));
}
preparedStatements.put(statementName, sqlString);
});
return preparedStatements.buildOrThrow();
}
use of io.trino.sql.parser.ParsingOptions in project trino by trinodb.
the class TestSetRoleTask method executeSetRole.
private QueryStateMachine executeSetRole(String statement) {
SetRole setRole = (SetRole) parser.createStatement(statement, new ParsingOptions());
QueryStateMachine stateMachine = QueryStateMachine.begin(Optional.empty(), statement, Optional.empty(), testSessionBuilder().setIdentity(Identity.ofUser(USER_NAME)).build(), URI.create("fake://uri"), new ResourceGroupId("test"), false, transactionManager, accessControl, executor, metadata, WarningCollector.NOOP, Optional.empty());
new SetRoleTask(metadata, accessControl).execute(setRole, stateMachine, ImmutableList.of(), WarningCollector.NOOP);
return stateMachine;
}
use of io.trino.sql.parser.ParsingOptions in project trino by trinodb.
the class TestSimplifyExpressions method assertSimplifiesNumericTypes.
private static void assertSimplifiesNumericTypes(String expression, String expected) {
ParsingOptions parsingOptions = new ParsingOptions();
Expression actualExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expression, parsingOptions));
Expression expectedExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expected, parsingOptions));
Expression rewritten = rewrite(actualExpression, TEST_SESSION, new SymbolAllocator(numericAndBooleanSymbolTypeMapFor(actualExpression)), PLANNER_CONTEXT, createTestingTypeAnalyzer(PLANNER_CONTEXT));
assertEquals(normalize(rewritten), normalize(expectedExpression));
}
use of io.trino.sql.parser.ParsingOptions in project trino by trinodb.
the class TestParameterExtractor method testShowStats.
@Test
public void testShowStats() {
Statement statement = sqlParser.createStatement("SHOW STATS FOR (SELECT c1, c2 FROM test_table WHERE c1 = ? AND c2 > ?)", new ParsingOptions());
assertThat(ParameterExtractor.getParameters(statement)).containsExactly(new Parameter(new NodeLocation(1, 57), 0), new Parameter(new NodeLocation(1, 68), 1));
assertThat(ParameterExtractor.getParameterCount(statement)).isEqualTo(2);
}
Aggregations