Search in sources :

Example 41 with QueryPlan

use of org.apache.phoenix.compile.QueryPlan in project phoenix by apache.

the class PhoenixRuntimeTest method testGetPkColsDataTypes.

@Test
public void testGetPkColsDataTypes() throws Exception {
    Connection conn = DriverManager.getConnection(getUrl(), new Properties());
    int i = 0;
    PDataType[] pTypes = PDataType.values();
    int size = pTypes.length;
    StringBuilder sb = null;
    try {
        for (i = 0; i < size; i++) {
            PDataType pType = pTypes[i];
            String sqlTypeName = pType.getSqlTypeName();
            if (sqlTypeName.equalsIgnoreCase("VARBINARY ARRAY")) {
                // JIRA - https://issues.apache.org/jira/browse/PHOENIX-1329
                continue;
            }
            if (pType.isArrayType() && PDataType.arrayBaseType(pType).isFixedWidth() && PDataType.arrayBaseType(pType).getByteSize() == null) {
                // Need to treat array type whose base type is of fixed width whose byte size is not known as a special case.
                // Cannot just use the sql type name returned by PDataType.getSqlTypeName().
                String baseTypeName = PDataType.arrayBaseType(pType).getSqlTypeName();
                sqlTypeName = baseTypeName + "(15)" + " " + PDataType.ARRAY_TYPE_SUFFIX;
            } else if (pType.isFixedWidth() && pType.getByteSize() == null) {
                sqlTypeName = sqlTypeName + "(15)";
            }
            String columnName = "col" + i;
            String tableName = "t" + i;
            sb = new StringBuilder(100);
            // create a table by using the type name as returned by PDataType
            sb.append("CREATE TABLE " + tableName + " (");
            sb.append(columnName + " " + sqlTypeName + " NOT NULL PRIMARY KEY, V1 VARCHAR)");
            conn.createStatement().execute(sb.toString());
            // generate the optimized query plan by going through the pk of the table.
            PreparedStatement stmt = conn.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnName + " = ?");
            Integer maxLength = pType.isFixedWidth() && pType.getByteSize() == null ? 15 : null;
            stmt.setObject(1, pType.getSampleValue(maxLength));
            QueryPlan plan = PhoenixRuntime.getOptimizedQueryPlan(stmt);
            // now go through the utility method, get column name and type name and
            // try creating another table with the returned info. Use the query plan generated above.
            // If table can be created with the returned sql type name, then great!
            // It would mean "Roundtrip" of column data type name works.
            List<Pair<String, String>> pkCols = new ArrayList<Pair<String, String>>();
            List<String> dataTypes = new ArrayList<String>();
            PhoenixRuntime.getPkColsDataTypesForSql(pkCols, dataTypes, plan, conn, true);
            tableName = "newt" + i;
            columnName = "newCol" + i;
            String roundTripSqlTypeName = dataTypes.get(0);
            // create a table by using the type name as returned by the utility method
            sb = new StringBuilder(100);
            sb.append("CREATE TABLE " + tableName + " (");
            sb.append(columnName + " " + roundTripSqlTypeName + " NOT NULL PRIMARY KEY)");
            conn.createStatement().execute(sb.toString());
        }
    } catch (Exception e) {
        fail("Failed sql: " + sb.toString() + ExceptionUtils.getStackTrace(e));
    }
}
Also used : Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) QueryPlan(org.apache.phoenix.compile.QueryPlan) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) SQLException(java.sql.SQLException) PDataType(org.apache.phoenix.schema.types.PDataType) Pair(org.apache.hadoop.hbase.util.Pair) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Aggregations

QueryPlan (org.apache.phoenix.compile.QueryPlan)41 Connection (java.sql.Connection)21 PhoenixStatement (org.apache.phoenix.jdbc.PhoenixStatement)21 PreparedStatement (java.sql.PreparedStatement)14 ResultSet (java.sql.ResultSet)13 Test (org.junit.Test)13 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)11 SQLException (java.sql.SQLException)10 Date (java.sql.Date)8 Statement (java.sql.Statement)8 Properties (java.util.Properties)7 PTable (org.apache.phoenix.schema.PTable)7 Scan (org.apache.hadoop.hbase.client.Scan)6 BaseQueryPlan (org.apache.phoenix.execute.BaseQueryPlan)6 List (java.util.List)4 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)4 ResultIterator (org.apache.phoenix.iterate.ResultIterator)4 KeyRange (org.apache.phoenix.query.KeyRange)4 PColumn (org.apache.phoenix.schema.PColumn)4 IOException (java.io.IOException)3