use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.
the class SqlQueryToColumnInfoFunction method apply.
@Override
public List<ColumnInfo> apply(String sqlQuery) {
Preconditions.checkNotNull(sqlQuery);
Connection connection = null;
List<ColumnInfo> columnInfos = null;
try {
connection = ConnectionUtil.getInputConnection(this.configuration);
final Statement statement = connection.createStatement();
final PhoenixStatement pstmt = statement.unwrap(PhoenixStatement.class);
final QueryPlan queryPlan = pstmt.compileQuery(sqlQuery);
final List<? extends ColumnProjector> projectedColumns = queryPlan.getProjector().getColumnProjectors();
columnInfos = Lists.newArrayListWithCapacity(projectedColumns.size());
columnInfos = Lists.transform(projectedColumns, new Function<ColumnProjector, ColumnInfo>() {
@Override
public ColumnInfo apply(final ColumnProjector columnProjector) {
return new ColumnInfo(columnProjector.getName(), columnProjector.getExpression().getDataType().getSqlType());
}
});
} catch (SQLException e) {
LOG.error(String.format(" Error [%s] parsing SELECT query [%s] ", e.getMessage(), sqlQuery));
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException sqle) {
LOG.error("Error closing connection!!");
throw new RuntimeException(sqle);
}
}
}
return columnInfos;
}
use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.
the class QueryOptimizerTest method testChooseIndexFromDoubleQuotedHint.
@Test
public void testChooseIndexFromDoubleQuotedHint() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
conn.createStatement().execute("CREATE TABLE t (k INTEGER NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR) IMMUTABLE_ROWS=true");
conn.createStatement().execute("CREATE INDEX idx1 ON t(v1) INCLUDE(v2)");
conn.createStatement().execute("CREATE INDEX idx2 ON t(v1,v2)");
PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class);
QueryPlan plan = stmt.optimizeQuery("SELECT /*+ INDEX(t \"IDX1\") INDEX(t idx3) */ k FROM t WHERE v1 = 'foo' AND v2 = 'bar'");
assertEquals("IDX1", plan.getTableRef().getTable().getTableName().getString());
}
use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.
the class QueryOptimizerTest method testRVCForTableWithSecondaryIndexBasic.
@Test
public void testRVCForTableWithSecondaryIndexBasic() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
conn.createStatement().execute("CREATE TABLE T (k VARCHAR NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR)");
conn.createStatement().execute("CREATE INDEX IDX ON T(v1, v2)");
PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class);
String query = "select * from t where (v1, v2) <= ('1', '2')";
QueryPlan plan = stmt.optimizeQuery(query);
assertEquals("IDX", plan.getTableRef().getTable().getTableName().getString());
}
use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.
the class QueryOptimizerTest method testChooseTableFromOrderByAsc.
@Test
public void testChooseTableFromOrderByAsc() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
conn.createStatement().execute("CREATE TABLE t (k INTEGER NOT NULL PRIMARY KEY DESC, v1 VARCHAR, v2 VARCHAR) IMMUTABLE_ROWS=true");
conn.createStatement().execute("CREATE INDEX idx ON t(v1)");
PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class);
QueryPlan plan = stmt.optimizeQuery("SELECT k FROM t WHERE k > 30 ORDER BY v1, k LIMIT 5");
assertEquals("T", plan.getTableRef().getTable().getTableName().getString());
}
use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.
the class QueryOptimizerTest method testChooseIndexEvenWithSelectionStar.
@Test
public void testChooseIndexEvenWithSelectionStar() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
conn.createStatement().execute("CREATE TABLE t (k INTEGER NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR) IMMUTABLE_ROWS=true");
conn.createStatement().execute("CREATE INDEX idx ON t(v1) INCLUDE (v2)");
PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class);
QueryPlan plan = stmt.optimizeQuery("SELECT * FROM t WHERE v1 = 'bar'");
assertEquals("IDX", plan.getTableRef().getTable().getTableName().getString());
}
Aggregations