Search in sources :

Example 56 with PhoenixStatement

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;
}
Also used : Function(com.google.common.base.Function) SQLException(java.sql.SQLException) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ColumnInfo(org.apache.phoenix.util.ColumnInfo) QueryPlan(org.apache.phoenix.compile.QueryPlan) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) ColumnProjector(org.apache.phoenix.compile.ColumnProjector)

Example 57 with PhoenixStatement

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());
}
Also used : Connection(java.sql.Connection) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 58 with PhoenixStatement

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());
}
Also used : Connection(java.sql.Connection) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 59 with PhoenixStatement

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());
}
Also used : Connection(java.sql.Connection) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 60 with PhoenixStatement

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());
}
Also used : Connection(java.sql.Connection) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Aggregations

PhoenixStatement (org.apache.phoenix.jdbc.PhoenixStatement)64 Connection (java.sql.Connection)47 Test (org.junit.Test)42 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)34 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)13 ResultSet (java.sql.ResultSet)10 StatementContext (org.apache.phoenix.compile.StatementContext)10 QueryPlan (org.apache.phoenix.compile.QueryPlan)8 PTable (org.apache.phoenix.schema.PTable)8 SQLException (java.sql.SQLException)7 Statement (java.sql.Statement)7 PhoenixResultSet (org.apache.phoenix.jdbc.PhoenixResultSet)7 PColumn (org.apache.phoenix.schema.PColumn)7 ArrayList (java.util.ArrayList)6 Properties (java.util.Properties)6 MutationState (org.apache.phoenix.execute.MutationState)6 PreparedStatement (java.sql.PreparedStatement)5 List (java.util.List)5 Scan (org.apache.hadoop.hbase.client.Scan)5 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)5