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));
}
}
Aggregations