jinggou commented on code in PR #1844: URL: https://github.com/apache/phoenix/pull/1844#discussion_r1681904422
########## phoenix-core-client/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java: ########## @@ -209,6 +250,151 @@ public MutationPlan compile(CreateTableStatement create) throws SQLException { viewStatement, viewType, viewColumnConstants, isViewColumnReferenced, connection); } + /** + * Restrict view to be UPDATABLE if the view specification (view statement): + * 1. uses only the PK columns + * 2. starts from the first PK column if the parent table is not multi tenant; otherwise, + * starts from the second PK column (the first column will be TENANT_ID) + * 3. PK columns should be in the order they are defined + * 4. uses the same set of PK columns as its sibling views' specification + * Otherwise, mark the view as READ_ONLY. + * + * @param connection The client connection + * @param parentToBe To be parent for given view + * @param pkColumnsInWhere Set of primary key in where clause + * @param nonPkColumnsInWhere Set of non-primary key columns in where clause + * @throws IOException thrown if there is an error finding sibling views + * @throws SQLException + */ + private ViewType setViewTypeToBe(final PhoenixConnection connection, final PTable parentToBe, + final Set<PColumn> pkColumnsInWhere, + final Set<PColumn> nonPkColumnsInWhere) + throws IOException, SQLException { + // 1. Check the view specification WHERE clause uses only the PK columns + if (!nonPkColumnsInWhere.isEmpty()) { + LOGGER.info("Setting the view type as READ_ONLY because the statement contains non-PK" + + " columns"); + return ViewType.READ_ONLY; + } + if (pkColumnsInWhere.isEmpty()) { + return ViewType.UPDATABLE; + } + + List<Integer> tablePkPositions = new ArrayList<>(); + List<Integer> pkPositions = new ArrayList<>(); + parentToBe.getPKColumns().forEach(tablePkColumn -> + tablePkPositions.add(tablePkColumn.getPosition())); + pkColumnsInWhere.forEach(pkColumn -> pkPositions.add(pkColumn.getPosition())); + Collections.sort(pkPositions); + + // 2. If not multi tenant, view specification WHERE clause should start from the first PK + // column; otherwise, start from the second PK column + boolean isMultiTenant = parentToBe.isMultiTenant(); + int firstPkPosition = pkPositions.get(0); Review Comment: You are right. I tested with SALTED table, the first PK column would be _SALT, so the first PK defined in view would be in the second position in the table, just like the multi-tenant table. What do you think we should do here? @jpisaac -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@phoenix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org