public static void setSessionProperty(Connection connection, String key, String value) throws SQLException { if (usingPrestoJdbcDriver(connection)) { PrestoConnection prestoConnection = connection.unwrap(PrestoConnection.class); prestoConnection.setSessionProperty(key, value); } else if (usingTeradataJdbcDriver(connection)) { try (Statement statement = connection.createStatement()) { if (shouldValueBeQuoted(value)) { value = "'" + value + "'"; } statement.execute(String.format("set session %s=%s", key, value)); } } else { throw new IllegalStateException(); } }
try (Connection connection = DriverManager.getConnection(url, username, password)) { trySetConnectionProperties(query, connection); for (Map.Entry<String, String> entry : sessionProperties.entrySet()) { connection.unwrap(PrestoConnection.class).setSessionProperty(entry.getKey(), entry.getValue()); try (Statement statement = connection.createStatement()) { Stopwatch stopwatch = Stopwatch.createStarted(); Statement limitedStatement = limiter.newProxy(statement, Statement.class, timeout.toMillis(), MILLISECONDS); ProgressMonitor progressMonitor = new ProgressMonitor(); prestoStatement.setProgressMonitor(progressMonitor); boolean isSelectQuery = limitedStatement.execute(sql);
Statement statement = null; try { connection = DriverManager.getConnection("jdbc:calcite:", properties); final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); rootSchema.setCacheEnabled(false); statement = connection.createStatement();
@Test public void testDegenerateRowKeyFilter() throws SQLException { String keyPrefix = "foobar"; String query = "select * from atable where substr(entity_id,1,3)=?"; List<Object> binds = Arrays.<Object>asList(keyPrefix); PhoenixConnection pconn = DriverManager.getConnection(getUrl(), TEST_PROPERTIES).unwrap(PhoenixConnection.class); PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query); bindParams(pstmt, binds); QueryPlan plan = pstmt.optimizeQuery(); // Degenerate b/c "foobar" is more than 3 characters assertDegenerate(plan.getContext()); }
@After public void assertNoUnfreedMemory() throws SQLException { Connection conn = DriverManager.getConnection(getUrl()); try { long unfreedBytes = conn.unwrap(PhoenixConnection.class).getQueryServices().clearCache(); assertEquals(0,unfreedBytes); } finally { conn.close(); } }
private void validatePropertyOnViewIndex(String viewName, String viewIndexName, boolean useStats, Connection conn, Connection tenantConn) throws SQLException, TableNotFoundException { // fetch the latest view ptable PhoenixRuntime.getTableNoCache(tenantConn, viewName); PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class); PTable viewIndex = phxConn.getTable(new PTableKey(phxConn.getTenantId(), viewIndexName)); assertEquals("USE_STATS_FOR_PARALLELIZATION property set incorrectly", useStats, PhoenixConfigurationUtil .getStatsForParallelizationProp(tenantConn.unwrap(PhoenixConnection.class), viewIndex)); }
@Test public void testUnwrapConnection() throws SQLException, NoSuchMethodException, SecurityException { Connection connection = new TestConnection(); Connection wrapper = new WrapperConnection(connection); assertTrue(wrapper.isWrapperFor(Connection.class)); Connection unwrap = wrapper.unwrap(Connection.class); assertSame(connection, unwrap); UnWrapper unwrapper = new GenericUnWrapper(); assertFalse(unwrapper.canUnwrap(wrapper)); try { assertNull(unwrapper.unwrap(wrapper)); fail("Cannot unwrap yet"); } catch (Exception expected) { } GenericUnWrapper.CONNECTION_METHODS.put( WrapperConnection.class, WrapperConnection.class.getMethod("getUnderlyingConnection", null)); assertTrue(unwrapper.canUnwrap(wrapper)); assertSame(connection, unwrapper.unwrap(wrapper)); }
private void assertDurability(Connection conn, Durability durability) throws SQLException { PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class); Iterator<Pair<byte[], List<Mutation>>> it = pconn.getMutationState().toMutations(); assertTrue(it.hasNext()); while (it.hasNext()) { Pair<byte[], List<Mutation>> pair = it.next(); assertFalse(pair.getSecond().isEmpty()); for (Mutation m : pair.getSecond()) { assertEquals(durability, m.getDurability()); } } }
private static void makeImmutableAndDeleteData(String tableName, String fullTableName) throws Exception { Connection conn = getConnection(); try { conn.setAutoCommit(true); conn.createStatement().execute("DELETE FROM " + fullTableName); conn.createStatement().execute("ALTER TABLE " + fullTableName + " SET IMMUTABLE_ROWS=true"); conn.createStatement().executeQuery("SELECT COUNT(*) FROM " + fullTableName).next(); PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class); assertTrue(pconn.getTable(new PTableKey(pconn.getTenantId(), fullTableName)).isImmutableRows()); } finally { conn.close(); } }
@Before // We need to clean up phoenix metadata to ensure tests don't step on each other public void deleteMetadata() throws Exception { try (Connection conn = DriverManager.getConnection(getUrl())) { String delete = "DELETE FROM SYSTEM.CATALOG WHERE TABLE_SCHEM IS NULL OR TABLE_SCHEM = '' OR TABLE_SCHEM != 'SYSTEM'"; conn.createStatement().executeUpdate(delete); conn.commit(); delete = "DELETE FROM \"SYSTEM\".\"SEQUENCE\""; conn.createStatement().executeUpdate(delete); conn.commit(); conn.unwrap(PhoenixConnection.class).getQueryServices().clearCache(); } }
@After public void cleanup() throws SQLException { PhoenixConnection pconn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class); pconn.createStatement().execute("DROP TABLE T"); }
final Connection connection = DriverManager.getConnection("jdbc:calcite:", properties); final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); return calciteConnection; } catch (final Exception e) {
@Test public void testColumnDisplaySize() throws Exception { Connection conn = DriverManager.getConnection(getUrl()); conn.createStatement().execute( "CREATE TABLE T (pk1 CHAR(15) not null, pk2 VARCHAR not null, v1 VARCHAR(15), v2 DATE, v3 VARCHAR " + "CONSTRAINT pk PRIMARY KEY (pk1, pk2)) "); ResultSet rs = conn.createStatement().executeQuery("SELECT pk1, pk2, v1, v2, CAST(null AS varchar) FROM T"); assertEquals(15, rs.getMetaData().getColumnDisplaySize(1)); assertEquals(PhoenixResultSetMetaData.DEFAULT_DISPLAY_WIDTH, rs.getMetaData().getColumnDisplaySize(2)); assertEquals(15, rs.getMetaData().getColumnDisplaySize(3)); assertEquals(conn.unwrap(PhoenixConnection.class).getDatePattern().length(), rs.getMetaData().getColumnDisplaySize(4)); assertEquals(40, rs.getMetaData().getColumnDisplaySize(5)); } }
@Test public void testFalseFilter() throws SQLException { String tenantId = "000000000000001"; String query = "select * from atable where organization_id='" + tenantId + "' and 2=3"; PhoenixConnection pconn = DriverManager.getConnection(getUrl(), TEST_PROPERTIES).unwrap(PhoenixConnection.class); PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query); QueryPlan plan = pstmt.optimizeQuery(); assertDegenerate(plan.getContext()); }
private static QueryPlan compileStatement(String query, List<Object> binds, Integer limit) throws SQLException { PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class); PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query); TestUtil.bindParams(pstmt, binds); QueryPlan plan = pstmt.compileQuery(); assertEquals(limit, plan.getLimit()); return plan; }
private void assertColumnEncodingMetadata(QualifierEncodingScheme expectedEncodingScheme, ImmutableStorageScheme expectedStorageScheme, String tableName, Connection conn) throws Exception { PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class); PTable table = phxConn.getTable(new PTableKey(null, tableName)); assertEquals(expectedEncodingScheme, table.getEncodingScheme()); assertEquals(expectedStorageScheme, table.getImmutableStorageScheme()); }
@Test public void testAcquiringAndReleasingUpgradeMutex() throws Exception { ConnectionQueryServices services = null; try (Connection conn = getConnection(false, null)) { services = conn.unwrap(PhoenixConnection.class).getQueryServices(); assertTrue(((ConnectionQueryServicesImpl)services) .acquireUpgradeMutex(MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_7_0)); try { ((ConnectionQueryServicesImpl)services) .acquireUpgradeMutex(MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_7_0); fail(); } catch (UpgradeInProgressException expected) { } ((ConnectionQueryServicesImpl)services).releaseUpgradeMutex(); } }
public static void createSchema(String url, String tableName, Long ts) throws SQLException { String schema = SchemaUtil.getSchemaNameFromFullName(tableName); if (!schema.equals("")) { Properties props = new Properties(); if (ts != null) { props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts)); } try (Connection conn = DriverManager.getConnection(url, props);) { if (SchemaUtil.isNamespaceMappingEnabled(null, conn.unwrap(PhoenixConnection.class).getQueryServices().getProps())) { conn.createStatement().executeUpdate("CREATE SCHEMA IF NOT EXISTS " + schema); } } } }
public KeyRangeClipTest(String tableDef, List<Object> startValues, List<Object> endValues, int clipTo, KeyRange expectedOutput) throws SQLException { PhoenixConnection pconn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class); pconn.createStatement().execute("CREATE TABLE T(" + tableDef+ ")"); PTable table = pconn.getMetaDataCache().getTableRef(new PTableKey(null,"T")).getTable(); this.schema = table.getRowKeySchema(); byte[] lowerRange = getRange(pconn, startValues); byte[] upperRange = getRange(pconn, endValues); this.input = KeyRange.getKeyRange(lowerRange, upperRange); this.expectedOutput = expectedOutput; this.clipTo = clipTo; }
@Test public void testUpdateConsistency() throws Exception { Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); try (Connection conn = DriverManager.getConnection(getUrl(), props)) { Statement st = conn.createStatement(); st.execute("alter session set Consistency = 'timeline'"); ResultSet rs = st.executeQuery("explain select * from " + tableName); assertEquals(Consistency.TIMELINE, conn.unwrap(PhoenixConnection.class).getConsistency()); String queryPlan = QueryUtil.getExplainPlan(rs); assertTrue(queryPlan.indexOf("TIMELINE") > 0); // turn off timeline read consistency st.execute("alter session set Consistency = 'strong'"); rs = st.executeQuery("explain select * from " + tableName); queryPlan = QueryUtil.getExplainPlan(rs); assertTrue(queryPlan.indexOf("TIMELINE") < 0); } }