Last active
March 29, 2021 03:25
-
-
Save karthikch53/bc6f0a9fe79db19f8a073ced3aa07674 to your computer and use it in GitHub Desktop.
All substack gists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static class RecordingProgressMonitor implements Consumer < QueryStats > { | |
private final ImmutableList.Builder < QueryStats > builder = ImmutableList.builder(); | |
private boolean finished; | |
@Override | |
public synchronized void accept(QueryStats queryStats) { | |
checkState(!finished); | |
builder.add(queryStats); | |
} | |
public synchronized List < QueryStats > finish() { | |
finished = true; | |
return builder.build(); | |
} | |
} | |
// usage | |
try (Connection connection = createConnection()) { | |
try (Statement statement = connection.createStatement()) { | |
PrestoStatement prestoStatement = statement.unwrap(PrestoStatement.class); | |
RecordingProgressMonitor progressMonitor = new RecordingProgressMonitor(); | |
prestoStatement.setProgressMonitor(progressMonitor); | |
try (ResultSet rs = statement.executeQuery("<< sample query >>")) { | |
ResultSetMetaData metadata = rs.getMetaData(); | |
} | |
prestoStatement.clearProgressMonitor(); | |
// access progress monitor | |
List<QueryStats> queryStatsList = progressMonitor.finish(); | |
} | |
} | |
// results | |
// Query Stats= QueryStats[completedSplits=21,cpuTimeMillis=92,elapsedTimeMillis=44,nodes=1,peakMemoryBytes=0, | |
// processedBytes=0,processedRows=21802,queryId=20200507_054120_00481_5nch8,queued=false,queuedSplits=0, queuedTimeMillis=1, | |
// rootStage=Optional[io.prestosql.jdbc.StageStats@625732],runningSplits=0,scheduled=true,state=FINISHED, | |
// totalSplits=21,wallTimeMillis=100] | |
try (Connection newConnection = DriverManager.getConnection("jdbc:presto://localhost:5050/", "admin", ""); | |
Statement selectStatement = newConnection.createStatement();) { | |
// unwrap | |
PrestoConnection prestoConnection = newConnection.unwrap(PrestoConnection.class); | |
// override | |
prestoConnection.setSessionProperty("query_max_run_time", "1 s"); | |
String sql = "select * from tpch.sf100.orders"; | |
ResultSet resultSet = selectStatement.executeQuery(sql); | |
while (resultSet.next()) {} | |
} | |
// running this should throw an exception. | |
// java.sql.SQLException: Query failed (#20200507_055827_00492_5nch8): | |
// Query exceeded maximum time limit of 1.00s at io.prestosql.jdbc.PrestoResultSet.resultsException(PrestoResultSet.java:1894) | |
// at |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment