Support Questions
Find answers, ask questions, and share your expertise

Phoenix IndexOutOfBoundsException, Expression 'AND' with 'RVC'

Phoenix IndexOutOfBoundsException, Expression 'AND' with 'RVC'

New Contributor

CDH 5.14.2

Phoenix Affects Version 4.14.1-cdh5.14.2 

 

Bug in WhereOptimizer$KeyExpressionVisitor.intersectTrailing included in PHOENIX-3383(https://issues.apache.org/jira/browse/PHOENIX-3383) patch

 

Using AndExpression with RVCExpression in a query results in an exception.

 

Test Query

 

 

 

CREATE TABLE TEST.TABLE1 (CREATE TABLE TEST.TABLE1 ( ID1 BIGINT NOT NULL, YYYYMMDD INTEGER NOT NULL, ID2 VARCHAR NOT NULL, ID3 VARCHAR NOT NULL, CONSTRAINT PK PRIMARY KEY (ID1,YYYYMMDD,ID2,ID3));

SELECT ID1, YYYYMMDD, ID2, ID3FROM TEST.TABLE1WHERE    (ID1 = 100200300 AND YYYYMMDD = 20200101 AND ID2 = 'LENGTH_8_BYTE_OVER' AND ID3 = 'ABCDEFGHIJK')   AND (ID1, YYYYMMDD, ID2, ID3)   IN ((100200300, 20200101,'LENGTH_8_BYTE_OVER', 'ABCDEFGHIJK'),(200200300, 20200201,'ABC', 'ABCDEFGHIJK'));

 

 

 

 Exception

 

 

 

at org.apache.phoenix.compile.WhereOptimizer$KeyExpressionVisitor.intersectTrailing(WhereOptimizer.java:1362)
    at org.apache.phoenix.compile.WhereOptimizer$KeyExpressionVisitor.andKeySlots(WhereOptimizer.java:974)
    at org.apache.phoenix.compile.WhereOptimizer$KeyExpressionVisitor.visitLeave(WhereOptimizer.java:1501)
    at org.apache.phoenix.compile.WhereOptimizer$KeyExpressionVisitor.visitLeave(WhereOptimizer.java:512)
    at org.apache.phoenix.expression.AndExpression.accept(AndExpression.java:100)
    at org.apache.phoenix.compile.WhereOptimizer.pushKeyExpressionsToScan(WhereOptimizer.java:142)
    at org.apache.phoenix.compile.WhereCompiler.compile(WhereCompiler.java:157)
    at org.apache.phoenix.execute.HashJoinPlan.iterator(HashJoinPlan.java:232)
    at org.apache.phoenix.execute.DelegateQueryPlan.iterator(DelegateQueryPlan.java:144)
    at org.apache.phoenix.execute.DelegateQueryPlan.iterator(DelegateQueryPlan.java:139)
    at org.apache.phoenix.jdbc.PhoenixStatement$1.call(PhoenixStatement.java:312)
    at org.apache.phoenix.jdbc.PhoenixStatement$1.call(PhoenixStatement.java:291)
    at org.apache.phoenix.call.CallRunner.run(CallRunner.java:53)
    at org.apache.phoenix.jdbc.PhoenixStatement.executeQuery(PhoenixStatement.java:290)
    at org.apache.phoenix.jdbc.PhoenixStatement.executeQuery(PhoenixStatement.java:283)
    at org.apache.phoenix.jdbc.PhoenixPreparedStatement.executeQuery(PhoenixPreparedStatement.java:194)

 

 

 

 

Exception occurs when the value of the ID2 column is 8 bytes or more.

This is because of the way bytes are read in the function below.
org.apache.phoenix.compile.WhereOptimizer$KeyExpressionVisitor.intersectTrailing 

 

My Custom Log

 

 

andExpression=((ID1 = 100200300 AND YYYYMMDD = 20200101 AND ID2 = 'LENGTH_8_BYTE_OVER' AND ID3 = 'ABCDEFGHIJK') AND (ID1, YYYYMMDD, ID2, ID3) IN ([128,0,0,0,5,248,239,108,129,52,58,165,76,69,78,71,84,72,95,56,95,66,89,84,69,95,79,86,69,82,0,65,66,67,68,69,70,71,72,73,74,75],[128,0,0,0,11,238,208,108,129,52,59,9,65,66,67,0,65,66,67,68,69,70,71,72,73,74,75],...))

 

 

 

 

 

bytes, 8byte(bigint) + 4byte(int) + LENGTH_8_BYTE_OVER + delimeter(\0) + ABCDEFGHIJK

 

 

 

128,0,0,0,5,248,239,108,129,52,58,165,76,69,78,71,84,72,95,56,95,66,89,84,69,95,79,86,69,82,0,65,66,67,68,69,70,71,72,73,74,75

 

 

 

intersectTrailing function reads byte : 8byte > 8byte > 4byte > remain

If the value of the ID2 column is more than 8 bytes, the delimeter is encountered when reading the 'remain' byte, and the loop is executed once more.

 

2020-09-23 17:27:36,907 compile.WhereOptimizer$KeyExpressionVisitor(1358) - MYLOG intersectTrailing : result=\x80\x00\x00\x00\x05\xF8\xEFl\x814:\xA5LENGTH_8_BYTE_OVER\x00ABCDEFGHIJK, pkPos=0, otherRange=\x814:\xA5, otherPKPos=1
2020-09-23 17:27:36,907 compile.WhereOptimizer$KeyExpressionVisitor(1429) - MYLOG intersectTrailing : pos=1, getOffset=8, getLength=4, ptr=[-127, 52, 58, -91, 76, 69, 78, 71, 84, 72, 95, 56, 95, 66, 89, 84, 69, 95, 79, 86, 69, 82, 0, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75], str=�4:�LENGTH_8_BYTE_OVERABCDEFGHIJK
2020-09-23 17:27:36,908 compile.WhereOptimizer$KeyExpressionVisitor(1429) - MYLOG intersectTrailing : pos=2, getOffset=12, getLength=10, ptr=[-127, 52, 58, -91, 76, 69, 78, 71, 84, 72, 95, 56, 95, 66, 89, 84, 69, 95, 79, 86, 69, 82, 0, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75], str=�4:�LENGTH_8_BYTE_OVERABCDEFGHIJK
2020-09-23 17:27:36,908 compile.WhereOptimizer$KeyExpressionVisitor(1429) - MYLOG intersectTrailing : pos=3, getOffset=23, getLength=11, ptr=[-127, 52, 58, -91, 76, 69, 78, 71, 84, 72, 95, 56, 95, 66, 89, 84, 69, 95, 79, 86, 69, 82, 0, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75], str=�4:�LENGTH_8_BYTE_OVERABCDEFGHIJK
2020-09-23 17:27:36,908 compile.WhereOptimizer$KeyExpressionVisitor(1429) - MYLOG intersectTrailing : pos=4, getOffset=34, getLength=0, ptr=[-127, 52, 58, -91, 76, 69, 78, 71, 84, 72, 95, 56, 95, 66, 89, 84, 69, 95, 79, 86, 69, 82, 0, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75], str=�4:�LENGTH_8_BYTE_OVERABCDEFGHIJK
2020-09-23 17:27:36,908 compile.WhereOptimizer$KeyExpressionVisitor(1445) - MYLOG intersectTrailing : error=index (4) must be less than size (4)

 

 

 

Test Code

 


public void testAndWithRVC2() throws Exception {

    String ddl;
    String query;
    StatementContext context;
    Connection conn = DriverManager.getConnection(getUrl());
    ddl = "CREATE TABLE TEST.TABLE1 ( " +
            "ID1 BIGINT NOT NULL, YYYYMMDD INTEGER NOT NULL, " +
            "ID2 VARCHAR NOT NULL, ID3 VARCHAR NOT NULL, " +
            "CONSTRAINT PK PRIMARY KEY (ID1,YYYYMMDD,ID2,ID3) " +
            ")";

    conn.createStatement().execute(ddl);

    query = "SELECT ID1, YYYYMMDD, ID2, ID3 " +
            "FROM TEST.TABLE1 " +
            "WHERE " +
            "(ID1 = 100200300 AND YYYYMMDD = 20200101 AND ID2 = 'LENGTH_8_BYTE_OVER' AND ID3 = 'ABCDEFGHIJK')  AND " +
            "(ID1, YYYYMMDD, ID2, ID3) IN " +
            "((100200300, 20200101,'LENGTH_8_BYTE_OVER', 'ABCDEFGHIJK'), (200200300, 20200201,'ABC', 'ABCDEFGHIJK'))" +
            "";

    context = compileStatement(query, Collections.<Object>emptyList());
    Scan scan = context.getScan();
    conn.close();

}