ranganathg commented on code in PR #1701:
URL: https://github.com/apache/phoenix/pull/1701#discussion_r1346828959
##########
phoenix-core/src/main/java/org/apache/phoenix/compile/CreateIndexCompiler.java:
##########
@@ -45,11 +59,92 @@ public CreateIndexCompiler(PhoenixStatement statement,
Operation operation) {
this.operation = operation;
}
+
+ private static class IndexWhereParseNodeVisitor extends
StatelessTraverseAllParseNodeVisitor {
+ private boolean hasSubquery = false;
+
+ @Override
+ public Void visit(SubqueryParseNode node) throws SQLException {
+ hasSubquery = true;
+ return null;
+ }
+ }
+ private String getValue(PDataType type) {
+ if (type instanceof PNumericType || type instanceof PTimestamp ||
+ type instanceof PUnsignedTime) {
+ return "0";
+ } else if (type instanceof PChar || type instanceof PVarchar) {
+ return "'a'";
+ } else if (type instanceof PDate || type instanceof PTime) {
+ return (new Date(System.currentTimeMillis())).toString();
+ } else {
+ return "0x00";
+ }
+ }
+ private void verifyIndexWhere(ParseNode indexWhere, StatementContext
context,
+ TableName dataTableName) throws SQLException {
+ if (indexWhere == null) {
+ return;
+ }
+ PhoenixConnection connection = context.getConnection();
+ IndexWhereParseNodeVisitor indexWhereParseNodeVisitor = new
IndexWhereParseNodeVisitor();
+ indexWhere.accept(indexWhereParseNodeVisitor);
+ if (indexWhereParseNodeVisitor.hasSubquery) {
+ throw new
SQLExceptionInfo.Builder(SQLExceptionCode.INVALID_INDEX_WHERE_WITH_SUBQUERY).
+ build().buildException();
+ }
+ ExpressionCompiler expressionCompiler = new
ExpressionCompiler(context);
+ Expression indexWhereExpression =
indexWhere.accept(expressionCompiler);
+ PTable dataTable = PhoenixRuntime.getTable(connection,
dataTableName.toString());
+
+ boolean autoCommit = connection.getAutoCommit();
+ connection.setAutoCommit(false);
+ StringBuilder stringBuilder = new StringBuilder("UPSERT INTO ");
+ stringBuilder.append(dataTableName);
+ stringBuilder.append(" Values(");
+ int i = dataTable.getBucketNum() != null ? 1 : 0;
+ for (; i < dataTable.getColumns().size() - 1; i++) {
+ PDataType dataType = dataTable.getColumns().get(i).getDataType();
+ stringBuilder.append(getValue(dataType) + ",");
+ }
+ PDataType dataType = dataTable.getColumns().get(i).getDataType();
+ stringBuilder.append(getValue(dataType)+ ")");
+ PreparedStatement ps =
context.getConnection().prepareStatement(stringBuilder.toString());
+ ps.execute();
+ Iterator<Pair<byte[],List<Cell>>> dataTableNameAndMutationIterator =
+ PhoenixRuntime.getUncommittedDataIterator(connection);
+ Pair<byte[], List<Cell>> dataTableNameAndMutation = null;
+ while (dataTableNameAndMutationIterator.hasNext()) {
+ dataTableNameAndMutation = dataTableNameAndMutationIterator.next();
+ if (!java.util.Arrays.equals(dataTableNameAndMutation.getFirst(),
+ dataTableName.toString().getBytes())) {
+ dataTableNameAndMutation = null;
+ }
+ }
+ if (dataTableNameAndMutation == null) {
+ ps.close();
+ connection.setAutoCommit(autoCommit);
+ throw new RuntimeException("Unexpected result from " +
+ "PhoenixRuntime#getUncommittedDataIterator for " +
dataTableName);
+ }
+ ImmutableBytesWritable ptr = new ImmutableBytesWritable();
+ ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
+ MultiKeyValueTuple tuple = new
MultiKeyValueTuple(dataTableNameAndMutation.getSecond());
+ if (!indexWhereExpression.evaluate(tuple, ptr)) {
+ ps.close();
+ connection.setAutoCommit(autoCommit);
+ throw new
SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_EVALUATE_INDEX_WHERE).
+ build().buildException();
+ }
+ ps.close();
+ connection.setAutoCommit(autoCommit);
Review Comment:
would it be good to put this code in a try/finally block?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]