@Test public void testCompile() throws Exception { try (Connection conn = DriverManager.getConnection(getUrl())) { setupTables(conn); PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class); PTable pDataTable = pConn.getTable(new PTableKey(null, "T")); PostIndexDDLCompiler compiler = new PostIndexDDLCompiler(pConn, new TableRef(pDataTable)); MutationPlan plan = compiler.compile(pConn.getTable(new PTableKey(null, "IDX"))); assertEquals("T", plan.getQueryPlan().getTableRef().getTable().getTableName().getString()); } }
public MutationPlan compile(final PTable indexTable) throws SQLException { StringBuilder indexColumns = new StringBuilder(); StringBuilder dataColumns = new StringBuilder(); List<PColumn> indexPKColumns = indexTable.getPKColumns(); int nIndexPKColumns = indexTable.getPKColumns().size(); boolean isSalted = indexTable.getBucketNum() != null; boolean isMultiTenant = connection.getTenantId() != null && indexTable.isMultiTenant(); boolean isViewIndex = indexTable.getViewIndexId()!=null; int posOffset = (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isViewIndex ? 1 : 0); for (int i = posOffset; i < nIndexPKColumns; i++) { PColumn col = indexPKColumns.get(i); String indexColName = col.getName().getString(); String dataColName = StringUtil.escapeBackslash(col.getExpressionStr()); dataColumns.append(dataColName).append(","); indexColumns.append('"').append(indexColName).append("\","); indexColumnNames.add(indexColName); dataColumnNames.add(dataColName); } for (PColumnFamily family : indexTable.getColumnFamilies()) { for (PColumn col : family.getColumns()) { if (col.getViewConstant() == null) { String indexColName = col.getName().getString(); String dataFamilyName = IndexUtil.getDataColumnFamilyName(indexColName); String dataColumnName = IndexUtil.getDataColumnName(indexColName); if (!dataFamilyName.equals("")) { dataColumns.append('"').append(dataFamilyName).append("\"."); } dataColumns.append('"').append(dataColumnName).append("\","); indexColumns.append('"').append(indexColName).append("\","); indexColumnNames.add(indexColName); dataColumnNames.add(dataColumnName); } } } final PTable dataTable = dataTableRef.getTable(); dataColumns.setLength(dataColumns.length()-1); indexColumns.setLength(indexColumns.length()-1); String schemaName = dataTable.getSchemaName().getString(); String tableName = indexTable.getTableName().getString(); StringBuilder updateStmtStr = new StringBuilder(); updateStmtStr.append("UPSERT INTO ").append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(tableName).append("\"(") .append(indexColumns).append(") "); final StringBuilder selectQueryBuilder = new StringBuilder(); selectQueryBuilder.append(" SELECT ").append(dataColumns).append(" FROM ") .append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(dataTable.getTableName().getString()).append('"'); this.selectQuery = selectQueryBuilder.toString(); updateStmtStr.append(this.selectQuery); try (final PhoenixStatement statement = new PhoenixStatement(connection)) { DelegateMutationPlan delegate = new DelegateMutationPlan(statement.compileMutation(updateStmtStr.toString())) { @Override public MutationState execute() throws SQLException { connection.getMutationState().commitDDLFence(dataTable); return super.execute(); } }; return delegate; } }
PostIndexDDLCompiler { public MutationPlan compile(final PTable indexTable) throws SQLException { StringBuilder indexColumns = new StringBuilder(); StringBuilder dataColumns = new StringBuilder(); List<PColumn> indexPKColumns = indexTable.getPKColumns(); int nIndexPKColumns = indexTable.getPKColumns().size(); boolean isSalted = indexTable.getBucketNum() != null; boolean isMultiTenant = connection.getTenantId() != null && indexTable.isMultiTenant(); boolean isViewIndex = indexTable.getViewIndexId()!=null; int posOffset = (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isViewIndex ? 1 : 0); for (int i = posOffset; i < nIndexPKColumns; i++) { PColumn col = indexPKColumns.get(i); String indexColName = col.getName().getString(); String dataColName = StringUtil.escapeBackslash(col.getExpressionStr()); dataColumns.append(dataColName).append(","); indexColumns.append('"').append(indexColName).append("\","); indexColumnNames.add(indexColName); dataColumnNames.add(dataColName); } for (PColumnFamily family : indexTable.getColumnFamilies()) { for (PColumn col : family.getColumns()) { if (col.getViewConstant() == null) { String indexColName = col.getName().getString(); String dataFamilyName = IndexUtil.getDataColumnFamilyName(indexColName); String dataColumnName = IndexUtil.getDataColumnName(indexColName); if (!dataFamilyName.equals("")) { dataColumns.append('"').append(dataFamilyName).append("\"."); } dataColumns.append('"').append(dataColumnName).append("\","); indexColumns.append('"').append(indexColName).append("\","); indexColumnNames.add(indexColName); dataColumnNames.add(dataColumnName); } } } final PTable dataTable = dataTableRef.getTable(); dataColumns.setLength(dataColumns.length()-1); indexColumns.setLength(indexColumns.length()-1); String schemaName = dataTable.getSchemaName().getString(); String tableName = indexTable.getTableName().getString(); StringBuilder updateStmtStr = new StringBuilder(); updateStmtStr.append("UPSERT INTO ").append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(tableName).append("\"(") .append(indexColumns).append(") "); final StringBuilder selectQueryBuilder = new StringBuilder(); selectQueryBuilder.append(" SELECT ").append(dataColumns).append(" FROM ") .append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(dataTable.getTableName().getString()).append('"'); this.selectQuery = selectQueryBuilder.toString(); updateStmtStr.append(this.selectQuery); try (final PhoenixStatement statement = new PhoenixStatement(connection)) { DelegateMutationPlan delegate = new DelegateMutationPlan(statement.compileMutation(updateStmtStr.toString())) { @Override public MutationState execute() throws SQLException { connection.getMutationState().commitDDLFence(dataTable); return super.execute(); } }; return delegate; } } }
PostIndexDDLCompiler { public MutationPlan compile(final PTable indexTable) throws SQLException { StringBuilder indexColumns = new StringBuilder(); StringBuilder dataColumns = new StringBuilder(); List<PColumn> indexPKColumns = indexTable.getPKColumns(); int nIndexPKColumns = indexTable.getPKColumns().size(); boolean isSalted = indexTable.getBucketNum() != null; boolean isMultiTenant = connection.getTenantId() != null && indexTable.isMultiTenant(); boolean isViewIndex = indexTable.getViewIndexId()!=null; int posOffset = (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isViewIndex ? 1 : 0); for (int i = posOffset; i < nIndexPKColumns; i++) { PColumn col = indexPKColumns.get(i); String indexColName = col.getName().getString(); String dataColName = StringUtil.escapeBackslash(col.getExpressionStr()); dataColumns.append(dataColName).append(","); indexColumns.append('"').append(indexColName).append("\","); indexColumnNames.add(indexColName); dataColumnNames.add(dataColName); } for (PColumnFamily family : indexTable.getColumnFamilies()) { for (PColumn col : family.getColumns()) { if (col.getViewConstant() == null) { String indexColName = col.getName().getString(); String dataFamilyName = IndexUtil.getDataColumnFamilyName(indexColName); String dataColumnName = IndexUtil.getDataColumnName(indexColName); if (!dataFamilyName.equals("")) { dataColumns.append('"').append(dataFamilyName).append("\"."); } dataColumns.append('"').append(dataColumnName).append("\","); indexColumns.append('"').append(indexColName).append("\","); indexColumnNames.add(indexColName); dataColumnNames.add(dataColumnName); } } } final PTable dataTable = dataTableRef.getTable(); dataColumns.setLength(dataColumns.length()-1); indexColumns.setLength(indexColumns.length()-1); String schemaName = dataTable.getSchemaName().getString(); String tableName = indexTable.getTableName().getString(); StringBuilder updateStmtStr = new StringBuilder(); updateStmtStr.append("UPSERT INTO ").append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(tableName).append("\"(") .append(indexColumns).append(") "); final StringBuilder selectQueryBuilder = new StringBuilder(); selectQueryBuilder.append(" SELECT ").append(dataColumns).append(" FROM ") .append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(dataTable.getTableName().getString()).append('"'); this.selectQuery = selectQueryBuilder.toString(); updateStmtStr.append(this.selectQuery); try (final PhoenixStatement statement = new PhoenixStatement(connection)) { DelegateMutationPlan delegate = new DelegateMutationPlan(statement.compileMutation(updateStmtStr.toString())) { @Override public MutationState execute() throws SQLException { connection.getMutationState().commitDDLFence(dataTable); return super.execute(); } }; return delegate; } } PostIndexDDLCompiler(PhoenixConnection connection, TableRef dataTableRef); }
PostIndexDDLCompiler { public MutationPlan compile(final PTable indexTable) throws SQLException { StringBuilder indexColumns = new StringBuilder(); StringBuilder dataColumns = new StringBuilder(); List<PColumn> indexPKColumns = indexTable.getPKColumns(); int nIndexPKColumns = indexTable.getPKColumns().size(); boolean isSalted = indexTable.getBucketNum() != null; boolean isMultiTenant = connection.getTenantId() != null && indexTable.isMultiTenant(); boolean isViewIndex = indexTable.getViewIndexId()!=null; int posOffset = (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isViewIndex ? 1 : 0); for (int i = posOffset; i < nIndexPKColumns; i++) { PColumn col = indexPKColumns.get(i); String indexColName = col.getName().getString(); String dataColName = StringUtil.escapeBackslash(col.getExpressionStr()); dataColumns.append(dataColName).append(","); indexColumns.append('"').append(indexColName).append("\","); indexColumnNames.add(indexColName); dataColumnNames.add(dataColName); } for (PColumnFamily family : indexTable.getColumnFamilies()) { for (PColumn col : family.getColumns()) { if (col.getViewConstant() == null) { String indexColName = col.getName().getString(); String dataFamilyName = IndexUtil.getDataColumnFamilyName(indexColName); String dataColumnName = IndexUtil.getDataColumnName(indexColName); if (!dataFamilyName.equals("")) { dataColumns.append('"').append(dataFamilyName).append("\"."); } dataColumns.append('"').append(dataColumnName).append("\","); indexColumns.append('"').append(indexColName).append("\","); indexColumnNames.add(indexColName); dataColumnNames.add(dataColumnName); } } } final PTable dataTable = dataTableRef.getTable(); dataColumns.setLength(dataColumns.length()-1); indexColumns.setLength(indexColumns.length()-1); String schemaName = dataTable.getSchemaName().getString(); String tableName = indexTable.getTableName().getString(); StringBuilder updateStmtStr = new StringBuilder(); updateStmtStr.append("UPSERT INTO ").append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(tableName).append("\"(") .append(indexColumns).append(") "); final StringBuilder selectQueryBuilder = new StringBuilder(); selectQueryBuilder.append(" SELECT ").append(dataColumns).append(" FROM ") .append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(dataTable.getTableName().getString()).append('"'); this.selectQuery = selectQueryBuilder.toString(); updateStmtStr.append(this.selectQuery); try (final PhoenixStatement statement = new PhoenixStatement(connection)) { DelegateMutationPlan delegate = new DelegateMutationPlan(statement.compileMutation(updateStmtStr.toString())) { @Override public MutationState execute() throws SQLException { connection.getMutationState().commitDDLFence(dataTable); return super.execute(); } }; return delegate; } } PostIndexDDLCompiler(PhoenixConnection connection, TableRef dataTableRef); MutationPlan compile(final PTable indexTable); List<String> getIndexColumnNames(); List<String> getDataColumnNames(); String getSelectQuery(); }
PostIndexDDLCompiler { public MutationPlan compile(final PTable indexTable) throws SQLException { StringBuilder indexColumns = new StringBuilder(); StringBuilder dataColumns = new StringBuilder(); List<PColumn> indexPKColumns = indexTable.getPKColumns(); int nIndexPKColumns = indexTable.getPKColumns().size(); boolean isSalted = indexTable.getBucketNum() != null; boolean isMultiTenant = connection.getTenantId() != null && indexTable.isMultiTenant(); boolean isViewIndex = indexTable.getViewIndexId()!=null; int posOffset = (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isViewIndex ? 1 : 0); for (int i = posOffset; i < nIndexPKColumns; i++) { PColumn col = indexPKColumns.get(i); String indexColName = col.getName().getString(); String dataColName = StringUtil.escapeBackslash(col.getExpressionStr()); dataColumns.append(dataColName).append(","); indexColumns.append('"').append(indexColName).append("\","); indexColumnNames.add(indexColName); dataColumnNames.add(dataColName); } for (PColumnFamily family : indexTable.getColumnFamilies()) { for (PColumn col : family.getColumns()) { if (col.getViewConstant() == null) { String indexColName = col.getName().getString(); String dataFamilyName = IndexUtil.getDataColumnFamilyName(indexColName); String dataColumnName = IndexUtil.getDataColumnName(indexColName); if (!dataFamilyName.equals("")) { dataColumns.append('"').append(dataFamilyName).append("\"."); } dataColumns.append('"').append(dataColumnName).append("\","); indexColumns.append('"').append(indexColName).append("\","); indexColumnNames.add(indexColName); dataColumnNames.add(dataColumnName); } } } final PTable dataTable = dataTableRef.getTable(); dataColumns.setLength(dataColumns.length()-1); indexColumns.setLength(indexColumns.length()-1); String schemaName = dataTable.getSchemaName().getString(); String tableName = indexTable.getTableName().getString(); StringBuilder updateStmtStr = new StringBuilder(); updateStmtStr.append("UPSERT INTO ").append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(tableName).append("\"(") .append(indexColumns).append(") "); final StringBuilder selectQueryBuilder = new StringBuilder(); selectQueryBuilder.append(" SELECT ").append(dataColumns).append(" FROM ") .append(schemaName.length() == 0 ? "" : '"' + schemaName + "\".").append('"').append(dataTable.getTableName().getString()).append('"'); this.selectQuery = selectQueryBuilder.toString(); updateStmtStr.append(this.selectQuery); try (final PhoenixStatement statement = new PhoenixStatement(connection)) { DelegateMutationPlan delegate = new DelegateMutationPlan(statement.compileMutation(updateStmtStr.toString())) { @Override public MutationState execute() throws SQLException { connection.getMutationState().commitDDLFence(dataTable); return super.execute(); } }; return delegate; } } PostIndexDDLCompiler(PhoenixConnection connection, TableRef dataTableRef); MutationPlan compile(final PTable indexTable); List<String> getIndexColumnNames(); List<String> getDataColumnNames(); String getSelectQuery();  }