protected PhoenixResultSet executeQuery(final CompilableStatement stmt, final QueryLogger queryLogger) throws SQLException { return executeQuery(stmt, true, queryLogger); } private PhoenixResultSet executeQuery(final CompilableStatement stmt,
@Override public ResultSet executeQuery(String sql) throws SQLException { if (logger.isDebugEnabled()) { logger.debug(LogUtil.addCustomAnnotations("Execute query: " + sql, connection)); } CompilableStatement stmt = parseStatement(sql); if (stmt.getOperation().isMutation()) { throw new ExecuteQueryNotApplicableException(sql); } return executeQuery(stmt,createQueryLogger(stmt,sql)); }
@Test public void testMinMaxQualifierRangeWithNoOrderBy() throws Exception { Connection conn = DriverManager.getConnection(getUrl()); String tableName = "testMintestMinMaxQualifierRange".toUpperCase(); conn.createStatement().execute("CREATE TABLE " + tableName + " (k INTEGER NOT NULL PRIMARY KEY, v1 INTEGER, v2 VARCHAR) COLUMN_ENCODED_BYTES=4"); PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class); ResultSet rs = stmt.executeQuery("SELECT K from " + tableName); assertQualifierRangesNotPresent(rs); rs = stmt.executeQuery("SELECT V2 from " + tableName); assertQualifierRanges(rs, ENCODED_EMPTY_COLUMN_NAME, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); rs = stmt.executeQuery("SELECT V1 from " + tableName); assertQualifierRanges(rs, ENCODED_EMPTY_COLUMN_NAME, ENCODED_CQ_COUNTER_INITIAL_VALUE); }
@Test public void testMinMaxQualifierRangeWithOrderByOnKVColumn() throws Exception { Connection conn = DriverManager.getConnection(getUrl()); String tableName = "testMintestMinMaxQualifierRange".toUpperCase(); conn.createStatement().execute("CREATE TABLE " + tableName + " (k INTEGER NOT NULL PRIMARY KEY, v1 INTEGER, v2 VARCHAR) COLUMN_ENCODED_BYTES=4"); PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class); ResultSet rs = stmt.executeQuery("SELECT K from " + tableName + " ORDER BY (v1)"); assertQualifierRanges(rs, ENCODED_EMPTY_COLUMN_NAME, ENCODED_CQ_COUNTER_INITIAL_VALUE); rs = stmt.executeQuery("SELECT K from " + tableName + " ORDER BY (v1, v2)"); assertQualifierRanges(rs, ENCODED_EMPTY_COLUMN_NAME, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); rs = stmt.executeQuery("SELECT V2 from " + tableName + " ORDER BY (v1)"); assertQualifierRanges(rs, ENCODED_EMPTY_COLUMN_NAME, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); rs = stmt.executeQuery("SELECT V1 from " + tableName + " ORDER BY (v1, v2)"); assertQualifierRanges(rs, ENCODED_EMPTY_COLUMN_NAME, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1); }
@Override public boolean execute(String sql) throws SQLException { CompilableStatement stmt = parseStatement(sql); if (stmt.getOperation().isMutation()) { if (!batch.isEmpty()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.EXECUTE_UPDATE_WITH_NON_EMPTY_BATCH) .build().buildException(); } executeMutation(stmt); flushIfNecessary(); return false; } executeQuery(stmt,createQueryLogger(stmt,sql)); return true; }
@Test public void testConcatenatingSerialIterators() throws Exception { Connection conn; conn = getConnection(); createTestTable(getUrl(), ddl); initTableValues(conn); String query = "SELECT t_id from " + tableName + " order by t_id desc limit " + 10; PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class); ResultSet rs = stmt.executeQuery(query); int i = 25; while (i >= 16) { assertTrue(rs.next()); assertEquals(strings[i--], rs.getString(1)); } query = "SELECT t_id from " + tableName + " order by t_id limit " + 10; stmt = conn.createStatement().unwrap(PhoenixStatement.class); rs = stmt.executeQuery(query); i = 0; while (i < 10) { assertTrue(rs.next()); assertEquals(strings[i++], rs.getString(1)); } conn.close(); }
private void testHintForIndexOnView(boolean includeColumns) throws Exception { Properties props = new Properties(); Connection conn1 = DriverManager.getConnection(getUrl(), props); conn1.setAutoCommit(true); String tableName = SchemaUtil.getTableName(SCHEMA1, generateUniqueName()); String viewName = SchemaUtil.getTableName(SCHEMA2, generateUniqueName()); String indexName = generateUniqueName(); String fullIndexName = SchemaUtil.getTableName(SCHEMA2, indexName); conn1.createStatement().execute( "CREATE TABLE "+tableName+" (k VARCHAR PRIMARY KEY, v1 VARCHAR, v2 VARCHAR) UPDATE_CACHE_FREQUENCY=1000000"); conn1.createStatement().execute("upsert into "+tableName+" values ('row1', 'value1', 'key1')"); conn1.createStatement().execute( "CREATE VIEW "+viewName+" (v3 VARCHAR, v4 VARCHAR) AS SELECT * FROM "+tableName+" WHERE v1 = 'value1'"); conn1.createStatement().execute("CREATE INDEX " + indexName + " ON " + viewName + "(v3)" + (includeColumns ? " INCLUDE(v4)" : "")); PhoenixStatement stmt = conn1.createStatement().unwrap(PhoenixStatement.class); ResultSet rs = stmt.executeQuery("SELECT /*+ INDEX(" + viewName + " " + fullIndexName + ") */ v1 FROM " + viewName + " WHERE v3 = 'foo' ORDER BY v4"); assertFalse(rs.next()); assertEquals(fullIndexName, stmt.getQueryPlan().getContext().getCurrentTable().getTable().getName().getString()); }
private void assertRowCount(Connection conn, String fullTableName, String fullBaseName, int expectedCount) throws SQLException { PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class); ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + fullTableName); assertTrue(rs.next()); assertEquals(expectedCount, rs.getInt(1)); // Ensure that index is being used rs = stmt.executeQuery("EXPLAIN SELECT COUNT(*) FROM " + fullTableName); if (fullBaseName != null) { // Uses index and finds correct number of rows assertTrue(QueryUtil.getExplainPlan(rs).startsWith("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + Bytes.toString(MetaDataUtil.getViewIndexPhysicalName(Bytes.toBytes(fullBaseName))))); } // Force it not to use index and still finds correct number of rows rs = stmt.executeQuery("SELECT /*+ NO_INDEX */ * FROM " + fullTableName); int count = 0; while (rs.next()) { count++; } assertEquals(expectedCount, count); // Ensure that the table, not index is being used assertEquals(fullTableName, stmt.getQueryPlan().getContext().getCurrentTable().getTable().getName().getString()); }
protected PhoenixResultSet executeQuery(final CompilableStatement stmt, final QueryLogger queryLogger) throws SQLException { return executeQuery(stmt, true, queryLogger); } private PhoenixResultSet executeQuery(final CompilableStatement stmt,
long startTime = System.currentTimeMillis(); try { ResultSet rs = pstmt.executeQuery("SELECT count(*) FROM " + tableName);
protected PhoenixResultSet executeQuery(final CompilableStatement stmt, final QueryLogger queryLogger) throws SQLException { return executeQuery(stmt, true, queryLogger); } private PhoenixResultSet executeQuery(final CompilableStatement stmt,
ResultSet rs = stmt.executeQuery("SELECT v_date, v_time, v_timestamp FROM " + tableName);
stmt.executeQuery(sql); PTable indexTable = stmt.getQueryPlan().getTableRef().getTable(); String tableName = indexTable.getName().getString();
rs = stmt.executeQuery(query); QueryPlan plan = stmt.getQueryPlan(); assertEquals(indexTableName, plan.getContext().getCurrentTable().getTable().getName().getString());
@Test public void testServerNameOnScan() throws Exception { Connection conn = DriverManager.getConnection(getUrl(), TEST_PROPERTIES); byte[][] splits = new byte[][] { K3, K9, KR }; createTable(conn, splits); PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class); ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName + " LIMIT 1"); rs.next(); QueryPlan plan = stmt.getQueryPlan(); List<List<Scan>> nestedScans = plan.getScans(); assertNotNull(nestedScans); for (List<Scan> scans : nestedScans) { for (Scan scan : scans) { byte[] serverNameBytes = scan.getAttribute(BaseScannerRegionObserver.SCAN_REGION_SERVER); assertNotNull(serverNameBytes); ServerName serverName = ServerName.parseVersionedServerName(serverNameBytes); assertNotNull(serverName.getHostname()); } } }
ResultSet rs = stmt.executeQuery("SELECT V2 FROM " + fullTableName + " WHERE V1 = 'a'"); assertTrue(rs.next()); assertEquals("0", rs.getString(1)); clock.time += pendingDisableThreshold + 1000; stmt = conn.createStatement().unwrap(PhoenixStatement.class); rs = stmt.executeQuery("SELECT V2 FROM " + fullTableName + " WHERE V1 = 'a'"); assertTrue(rs.next()); assertEquals("0", rs.getString(1));
ResultSet rs = stmt.executeQuery("SELECT V2 FROM " + fullTableName + " WHERE V1 = 'a'"); assertTrue(rs.next()); assertEquals("0", rs.getString(1));
@Override public ResultSet executeQuery(String sql) throws SQLException { if (logger.isDebugEnabled()) { logger.debug(LogUtil.addCustomAnnotations("Execute query: " + sql, connection)); } CompilableStatement stmt = parseStatement(sql); if (stmt.getOperation().isMutation()) { throw new ExecuteQueryNotApplicableException(sql); } return executeQuery(stmt,createQueryLogger(stmt,sql)); }
@Override public ResultSet executeQuery(String sql) throws SQLException { if (logger.isDebugEnabled()) { logger.debug(LogUtil.addCustomAnnotations("Execute query: " + sql, connection)); } CompilableStatement stmt = parseStatement(sql); if (stmt.getOperation().isMutation()) { throw new ExecuteQueryNotApplicableException(sql); } return executeQuery(stmt,createQueryLogger(stmt,sql)); }
@Override public boolean execute(String sql) throws SQLException { CompilableStatement stmt = parseStatement(sql); if (stmt.getOperation().isMutation()) { if (!batch.isEmpty()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.EXECUTE_UPDATE_WITH_NON_EMPTY_BATCH) .build().buildException(); } executeMutation(stmt); flushIfNecessary(); return false; } executeQuery(stmt,createQueryLogger(stmt,sql)); return true; }