Skip to content

Commit

Permalink
Deny wrong null replacement of array type in Phoenix connector
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Mar 8, 2022
1 parent 9aeb65d commit 73c18ac
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@
import org.apache.phoenix.schema.PTable;
import org.apache.phoenix.schema.TableProperty;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PhoenixArray;
import org.apache.phoenix.util.SchemaUtil;

import javax.inject.Inject;

import java.io.IOException;
import java.sql.Array;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.JDBCType;
Expand Down Expand Up @@ -777,8 +777,15 @@ private static ObjectReadFunction arrayReadFunction(ConnectorSession session, Ty
private static ObjectWriteFunction arrayWriteFunction(ConnectorSession session, Type elementType, String elementJdbcTypeName)
{
return ObjectWriteFunction.of(Block.class, (statement, index, block) -> {
Array jdbcArray = statement.getConnection().createArrayOf(elementJdbcTypeName, getJdbcObjectArray(session, elementType, block));
statement.setArray(index, jdbcArray);
Object[] jdbcObjectArray = getJdbcObjectArray(session, elementType, block);
PhoenixArray phoenixArray = (PhoenixArray) statement.getConnection().createArrayOf(elementJdbcTypeName, jdbcObjectArray);
for (int i = 0; i < jdbcObjectArray.length; i++) {
if (jdbcObjectArray[i] == null && phoenixArray.getElement(i) != null) {
// TODO (https://github.com/trinodb/trino/issues/6421) Prevent writing incorrect results due to Phoenix JDBC driver bug
throw new TrinoException(PHOENIX_QUERY_ERROR, format("Phoenix JDBC driver replaced 'null' with '%s' at index %s in %s", phoenixArray.getElement(i), i + 1, phoenixArray));
}
}
statement.setArray(index, phoenixArray);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ public void testInsertArray()
{
assertThatThrownBy(super::testInsertArray)
// TODO (https://github.com/trinodb/trino/issues/6421) array with double null stored as array with 0
.hasMessageContaining("Actual rows (up to 100 of 1 extra rows shown, 2 rows in total):\n" +
" [0.0, null]\n" +
"Expected rows (up to 100 of 1 missing rows shown, 2 rows in total):\n" +
" [null, null]");
.hasMessage("Phoenix JDBC driver replaced 'null' with '0.0' at index 1 in [0.0]");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@
import org.apache.phoenix.schema.PTable;
import org.apache.phoenix.schema.TableProperty;
import org.apache.phoenix.schema.types.PDataType;
import org.apache.phoenix.schema.types.PhoenixArray;
import org.apache.phoenix.util.SchemaUtil;

import javax.inject.Inject;

import java.io.IOException;
import java.sql.Array;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.JDBCType;
Expand Down Expand Up @@ -769,8 +769,15 @@ private static ObjectReadFunction arrayReadFunction(ConnectorSession session, Ty
private static ObjectWriteFunction arrayWriteFunction(ConnectorSession session, Type elementType, String elementJdbcTypeName)
{
return ObjectWriteFunction.of(Block.class, (statement, index, block) -> {
Array jdbcArray = statement.getConnection().createArrayOf(elementJdbcTypeName, getJdbcObjectArray(session, elementType, block));
statement.setArray(index, jdbcArray);
Object[] jdbcObjectArray = getJdbcObjectArray(session, elementType, block);
PhoenixArray phoenixArray = (PhoenixArray) statement.getConnection().createArrayOf(elementJdbcTypeName, jdbcObjectArray);
for (int i = 0; i < jdbcObjectArray.length; i++) {
if (jdbcObjectArray[i] == null && phoenixArray.getElement(i) != null) {
// TODO (https://github.com/trinodb/trino/issues/6421) Prevent writing incorrect results due to Phoenix JDBC driver bug
throw new TrinoException(PHOENIX_QUERY_ERROR, format("Phoenix JDBC driver replaced 'null' with '%s' at index %s in %s", phoenixArray.getElement(i), i + 1, phoenixArray));
}
}
statement.setArray(index, phoenixArray);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,7 @@ public void testInsertArray()
{
assertThatThrownBy(super::testInsertArray)
// TODO (https://github.com/trinodb/trino/issues/6421) array with double null stored as array with 0
.hasMessageContaining("Actual rows (up to 100 of 1 extra rows shown, 2 rows in total):\n" +
" [0.0, null]\n" +
"Expected rows (up to 100 of 1 missing rows shown, 2 rows in total):\n" +
" [null, null]");
.hasMessage("Phoenix JDBC driver replaced 'null' with '0.0' at index 1 in [0.0]");
}

@Override
Expand Down

0 comments on commit 73c18ac

Please sign in to comment.