kadirozde commented on code in PR #1701:
URL: https://github.com/apache/phoenix/pull/1701#discussion_r1348168855


##########
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;

Review Comment:
   We should break when it is not null



##########
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:
   Yes



##########
phoenix-core/src/main/java/org/apache/phoenix/expression/ComparisonExpression.java:
##########
@@ -352,7 +356,139 @@ public boolean evaluate(Tuple tuple, 
ImmutableBytesWritable ptr) {
         ptr.set(ByteUtil.compare(op, comparisonResult) ? PDataType.TRUE_BYTES 
: PDataType.FALSE_BYTES);
         return true;
     }
-    
+
+    @Override
+    public boolean contains (Expression other) {
+        if (!(other instanceof ComparisonExpression || other instanceof 
IsNullExpression)) {
+                return false;
+        }
+        if (other instanceof IsNullExpression) {
+            return !((IsNullExpression) other).isNegate();
+        }
+
+        BaseTerminalExpression lhsA = 
WhereCompiler.getBaseTerminalExpression(this.getChildren().get(0));
+        BaseTerminalExpression lhsB = 
WhereCompiler.getBaseTerminalExpression(other.getChildren().get(0));
+        if (!lhsA.equals(lhsB)) {
+            return false;
+        }
+        CompareOperator opA = this.getFilterOp();
+        CompareOperator opB = ((ComparisonExpression) other).getFilterOp();
+        BaseTerminalExpression rhs = WhereCompiler.getBaseTerminalExpression(
+                this.getChildren().get(1));
+        if (rhs instanceof ColumnExpression) {
+            BaseTerminalExpression rhsB = 
WhereCompiler.getBaseTerminalExpression(
+                    other.getChildren().get(1));
+            if (!rhs.equals(rhsB)) {
+                return false;
+            }
+            switch (opA) {

Review Comment:
   Yes we can



##########
phoenix-core/src/main/java/org/apache/phoenix/coprocessor/GlobalIndexRegionScanner.java:
##########
@@ -1330,24 +1345,43 @@ public static List<Mutation> 
prepareIndexMutationsForRebuild(IndexMaintainer ind
                 applyDeleteOnPut(deleteToApply, currentDataRowState);
                 Put nextDataRowState = currentDataRowState;
                 if (nextDataRowState.getFamilyCellMap().size() == 0) {
-                    Mutation del = 
indexMaintainer.buildRowDeleteMutation(indexRowKeyForCurrentDataRow,
-                            IndexMaintainer.DeleteType.ALL_VERSIONS, ts);
-                    indexMutations.add(del);
+                    if (indexRowKeyForCurrentDataRow != null) {
+                        Mutation
+                                del =
+                                
indexMaintainer.buildRowDeleteMutation(indexRowKeyForCurrentDataRow,
+                                        
IndexMaintainer.DeleteType.ALL_VERSIONS, ts);
+                        indexMutations.add(del);
+                    }
                     currentDataRowState = null;
                     indexRowKeyForCurrentDataRow = null;
-                } else {
+                } else if (indexRowKeyForCurrentDataRow != null) {
+                    if 
(!IndexRegionObserver.shouldPrepareIndexMutations(indexMaintainer,
+                            nextDataRowState)) {
+                        currentDataRowState = nextDataRowState;
+                        Mutation del = indexMaintainer.buildRowDeleteMutation(
+                                indexRowKeyForCurrentDataRow,
+                                IndexMaintainer.DeleteType.ALL_VERSIONS, ts);
+                        indexMutations.add(del);
+                        indexRowKeyForCurrentDataRow = null;
+                        continue;
+                    }
                     ValueGetter nextDataRowVG = new 
IndexUtil.SimpleValueGetter(nextDataRowState);
-                    Put indexPut = prepareIndexPutForRebuid(indexMaintainer, 
rowKeyPtr, nextDataRowVG, ts);
+                    Put indexPut = prepareIndexPutForRebuid(indexMaintainer, 
rowKeyPtr,

Review Comment:
   Yes



-- 
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]

Reply via email to