use of io.trino.sql.parser.SqlParser in project trino by trinodb.
the class CreateViewTask method execute.
@Override
public ListenableFuture<Void> execute(CreateView statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
Session session = stateMachine.getSession();
QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
accessControl.checkCanCreateView(session.toSecurityContext(), name);
if (metadata.isMaterializedView(session, name)) {
throw semanticException(TABLE_ALREADY_EXISTS, statement, "Materialized view already exists: '%s'", name);
}
if (metadata.isView(session, name)) {
if (!statement.isReplace()) {
throw semanticException(TABLE_ALREADY_EXISTS, statement, "View already exists: '%s'", name);
}
} else if (metadata.getTableHandle(session, name).isPresent()) {
throw semanticException(TABLE_ALREADY_EXISTS, statement, "Table already exists: '%s'", name);
}
String sql = getFormattedSql(statement.getQuery(), sqlParser);
Analysis analysis = analyzerFactory.createAnalyzer(session, parameters, parameterExtractor(statement, parameters), stateMachine.getWarningCollector()).analyze(statement);
List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery()).getVisibleFields().stream().map(field -> new ViewColumn(field.getName().get(), field.getType().getTypeId())).collect(toImmutableList());
// use DEFINER security by default
Optional<Identity> owner = Optional.of(session.getIdentity());
if (statement.getSecurity().orElse(null) == INVOKER) {
owner = Optional.empty();
}
ViewDefinition definition = new ViewDefinition(sql, session.getCatalog(), session.getSchema(), columns, statement.getComment(), owner);
metadata.createView(session, name, definition, statement.isReplace());
stateMachine.setOutput(analysis.getTarget());
stateMachine.setReferencedTables(analysis.getReferencedTables());
return immediateVoidFuture();
}
use of io.trino.sql.parser.SqlParser in project trino by trinodb.
the class VerifyCommand method run.
@Override
public void run() {
if (configFilename != null) {
// Read
System.setProperty("config", configFilename);
}
ImmutableList.Builder<Module> builder = ImmutableList.<Module>builder().add(new PrestoVerifierModule()).addAll(getAdditionalModules());
Bootstrap app = new Bootstrap(builder.build());
Injector injector;
try {
injector = app.initialize();
} catch (Exception e) {
throwIfUnchecked(e);
throw new RuntimeException(e);
}
try {
VerifierConfig config = injector.getInstance(VerifierConfig.class);
injector.injectMembers(this);
Set<String> supportedEventClients = injector.getInstance(Key.get(new TypeLiteral<Set<String>>() {
}, SupportedEventClients.class));
for (String clientType : config.getEventClients()) {
checkArgument(supportedEventClients.contains(clientType), "Unsupported event client: %s", clientType);
}
Set<EventClient> eventClients = injector.getInstance(Key.get(new TypeLiteral<Set<EventClient>>() {
}));
VerifierDao dao = Jdbi.create(getQueryDatabase(injector)).installPlugin(new SqlObjectPlugin()).onDemand(VerifierDao.class);
ImmutableList.Builder<QueryPair> queriesBuilder = ImmutableList.builder();
for (String suite : config.getSuites()) {
queriesBuilder.addAll(dao.getQueriesBySuite(suite, config.getMaxQueries()));
}
List<QueryPair> queries = queriesBuilder.build();
queries = applyOverrides(config, queries);
queries = filterQueryTypes(new SqlParser(), config, queries);
queries = filterQueries(queries);
if (config.getShadowWrites()) {
Sets.SetView<QueryType> allowedTypes = Sets.union(config.getTestQueryTypes(), config.getControlQueryTypes());
checkArgument(!Sets.intersection(allowedTypes, ImmutableSet.of(CREATE, MODIFY)).isEmpty(), "CREATE or MODIFY queries must be allowed in test or control to use write shadowing");
queries = rewriteQueries(new SqlParser(), config, queries);
}
// Load jdbc drivers if needed
if (config.getAdditionalJdbcDriverPath() != null) {
List<URL> urlList = getUrls(config.getAdditionalJdbcDriverPath());
URL[] urls = new URL[urlList.size()];
urlList.toArray(urls);
if (config.getTestJdbcDriverName() != null) {
loadJdbcDriver(urls, config.getTestJdbcDriverName());
}
if (config.getControlJdbcDriverName() != null) {
loadJdbcDriver(urls, config.getControlJdbcDriverName());
}
}
// TODO: construct this with Guice
int numFailedQueries = new Verifier(System.out, config, eventClients).run(queries);
System.exit((numFailedQueries > 0) ? 1 : 0);
} catch (InterruptedException | MalformedURLException e) {
throw new RuntimeException(e);
} finally {
injector.getInstance(LifeCycleManager.class).stop();
}
}
use of io.trino.sql.parser.SqlParser in project trino by trinodb.
the class TestShadowing method testCreateTableAsSelect.
@Test
public void testCreateTableAsSelect() throws Exception {
handle.execute("CREATE TABLE \"my_test_table\" (column1 BIGINT, column2 DOUBLE)");
SqlParser parser = new SqlParser();
Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "CREATE TABLE my_test_table AS SELECT 1 column1, CAST('2.0' AS DOUBLE) column2 LIMIT 1", ImmutableList.of(), null, null, ImmutableMap.of());
QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS));
Query rewrittenQuery = rewriter.shadowQuery(query);
assertEquals(rewrittenQuery.getPreQueries().size(), 1);
assertEquals(rewrittenQuery.getPostQueries().size(), 1);
CreateTableAsSelect createTableAs = (CreateTableAsSelect) parser.createStatement(rewrittenQuery.getPreQueries().get(0), PARSING_OPTIONS);
assertEquals(createTableAs.getName().getParts().size(), 1);
assertTrue(createTableAs.getName().getSuffix().startsWith("tmp_"));
assertFalse(createTableAs.getName().getSuffix().contains("my_test_table"));
assertEquals(statementToQueryType(parser, rewrittenQuery.getQuery()), READ);
Table table = new Table(createTableAs.getName());
SingleColumn column1 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("COLUMN1"))));
SingleColumn column2 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("COLUMN2"), new LongLiteral("1"))))));
assertEquals(parser.createStatement(rewrittenQuery.getQuery(), PARSING_OPTIONS), simpleQuery(selectList(column1, column2), table));
assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0), PARSING_OPTIONS), new DropTable(createTableAs.getName(), true));
}
use of io.trino.sql.parser.SqlParser in project trino by trinodb.
the class TestShadowing method testInsert.
@Test
public void testInsert() throws Exception {
handle.execute("CREATE TABLE \"test_insert_table\" (a BIGINT, b DOUBLE, c VARCHAR)");
SqlParser parser = new SqlParser();
Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "INSERT INTO test_insert_table (b, a, c) values (1.1, 1, 'a'), (2.0, 2, 'b'), (3.1, 3, 'c')", ImmutableList.of(), null, null, ImmutableMap.of());
QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("other_catalog", "other_schema", "tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS));
Query rewrittenQuery = rewriter.shadowQuery(query);
assertEquals(rewrittenQuery.getPreQueries().size(), 2);
CreateTable createTable = (CreateTable) parser.createStatement(rewrittenQuery.getPreQueries().get(0), PARSING_OPTIONS);
assertEquals(createTable.getName().getParts().size(), 3);
assertEquals(createTable.getName().getPrefix().get(), QualifiedName.of("other_catalog", "other_schema"));
assertTrue(createTable.getName().getSuffix().startsWith("tmp_"));
assertFalse(createTable.getName().getSuffix().contains("test_insert_table"));
Insert insert = (Insert) parser.createStatement(rewrittenQuery.getPreQueries().get(1), PARSING_OPTIONS);
assertEquals(insert.getTarget(), createTable.getName());
assertEquals(insert.getColumns(), Optional.of(ImmutableList.of(identifier("b"), identifier("a"), identifier("c"))));
Table table = new Table(createTable.getName());
SingleColumn columnA = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("A"))));
SingleColumn columnB = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("B"), new LongLiteral("1"))))));
SingleColumn columnC = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("C"))));
assertEquals(parser.createStatement(rewrittenQuery.getQuery(), PARSING_OPTIONS), simpleQuery(selectList(columnA, columnB, columnC), table));
assertEquals(rewrittenQuery.getPostQueries().size(), 1);
assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0), PARSING_OPTIONS), new DropTable(createTable.getName(), true));
}
Aggregations