提交 84bbf5bd authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 609: the spatial index did not support NULL with update and delete operations.

上级 03fc1e6f
...@@ -20,7 +20,8 @@ Change Log ...@@ -20,7 +20,8 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>Pull request #2: Add external metadata type support (table type "external") <ul><li>Issue 609: the spatial index did not support NULL with update and delete operations.
</li><li>Pull request #2: Add external metadata type support (table type "external")
</li><li>MS SQL Server: the CONVERT method did not work in views </li><li>MS SQL Server: the CONVERT method did not work in views
and derrived tables. and derrived tables.
</li><li>Java 8 compatibility for "regexp_replace". </li><li>Java 8 compatibility for "regexp_replace".
......
...@@ -171,6 +171,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -171,6 +171,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
try { try {
Value old = map.remove(key); Value old = map.remove(key);
if (old == null) { if (old == null) {
old = map.remove(key);
throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1,
getSQL() + ": " + row.getKey()); getSQL() + ": " + row.getKey());
} }
...@@ -211,12 +212,9 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -211,12 +212,9 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
} }
private SpatialKey getKey(SearchRow row) { private SpatialKey getKey(SearchRow row) {
if (row == null) {
return null;
}
Value v = row.getValue(columnIds[0]); Value v = row.getValue(columnIds[0]);
if (v == ValueNull.INSTANCE) { if (v == ValueNull.INSTANCE) {
return null; return new SpatialKey(row.getKey());
} }
Geometry g = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getGeometryNoCopy(); Geometry g = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).getGeometryNoCopy();
Envelope env = g.getEnvelopeInternal(); Envelope env = g.getEnvelopeInternal();
......
...@@ -83,11 +83,12 @@ public class SpatialDataType implements DataType { ...@@ -83,11 +83,12 @@ public class SpatialDataType implements DataType {
@Override @Override
public void write(WriteBuffer buff, Object obj) { public void write(WriteBuffer buff, Object obj) {
if (obj == null) { SpatialKey k = (SpatialKey) obj;
if (k.isNull()) {
buff.putVarInt(-1); buff.putVarInt(-1);
buff.putVarLong(k.getId());
return; return;
} }
SpatialKey k = (SpatialKey) obj;
int flags = 0; int flags = 0;
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < dimensions; i++) {
if (k.min(i) == k.max(i)) { if (k.min(i) == k.max(i)) {
...@@ -108,7 +109,8 @@ public class SpatialDataType implements DataType { ...@@ -108,7 +109,8 @@ public class SpatialDataType implements DataType {
public Object read(ByteBuffer buff) { public Object read(ByteBuffer buff) {
int flags = DataUtils.readVarInt(buff); int flags = DataUtils.readVarInt(buff);
if (flags == -1) { if (flags == -1) {
return null; long id = DataUtils.readVarLong(buff);
return new SpatialKey(id);
} }
float[] minMax = new float[dimensions * 2]; float[] minMax = new float[dimensions * 2];
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < dimensions; i++) {
......
...@@ -70,6 +70,10 @@ public class SpatialKey { ...@@ -70,6 +70,10 @@ public class SpatialKey {
return id; return id;
} }
public boolean isNull() {
return minMax.length == 0;
}
@Override @Override
public String toString() { public String toString() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
......
...@@ -90,6 +90,8 @@ public class TestSpatial extends TestBase { ...@@ -90,6 +90,8 @@ public class TestSpatial extends TestBase {
testStoreCorruption(); testStoreCorruption();
testExplainSpatialIndexWithPk(); testExplainSpatialIndexWithPk();
testNullableGeometry(); testNullableGeometry();
testNullableGeometryDelete();
testNullableGeometryUpdate();
} }
private void testHashCode() { private void testHashCode() {
...@@ -920,6 +922,9 @@ public class TestSpatial extends TestBase { ...@@ -920,6 +922,9 @@ public class TestSpatial extends TestBase {
+ "(id int primary key, the_geom geometry)"); + "(id int primary key, the_geom geometry)");
stat.execute("create spatial index on test(the_geom)"); stat.execute("create spatial index on test(the_geom)");
stat.execute("insert into test values(1, null)"); stat.execute("insert into test values(1, null)");
stat.execute("insert into test values(2, null)");
stat.execute("delete from test where the_geom is null");
stat.execute("insert into test values(1, null)");
ResultSet rs = stat.executeQuery("select * from test"); ResultSet rs = stat.executeQuery("select * from test");
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(1, rs.getInt(1)); assertEquals(1, rs.getInt(1));
...@@ -934,7 +939,54 @@ public class TestSpatial extends TestBase { ...@@ -934,7 +939,54 @@ public class TestSpatial extends TestBase {
assertNull(rs.getObject(2)); assertNull(rs.getObject(2));
conn.close(); conn.close();
} }
deleteDb("spatial");
}
private void testNullableGeometryDelete() throws SQLException {
deleteDb("spatial");
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("create memory table test"
+ "(id int primary key, the_geom geometry)");
stat.execute("create spatial index on test(the_geom)");
stat.execute("insert into test values(1, null)");
stat.execute("insert into test values(2, null)");
stat.execute("insert into test values(3, null)");
ResultSet rs = stat.executeQuery("select * from test");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertNull(rs.getObject(2));
stat.execute("delete from test where id = 1");
stat.execute("delete from test where id = 2");
stat.execute("delete from test where id = 3");
stat.execute("insert into test values(4, null)");
stat.execute("insert into test values(5, null)");
stat.execute("insert into test values(6, null)");
stat.execute("delete from test where id = 4");
stat.execute("delete from test where id = 5");
stat.execute("delete from test where id = 6");
conn.close();
deleteDb("spatial");
}
private void testNullableGeometryUpdate() throws SQLException {
deleteDb("spatial");
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("create memory table test"
+ "(id int primary key, the_geom geometry, description varchar2(32))");
stat.execute("create spatial index on test(the_geom)");
stat.execute("insert into test values(1, null, null)");
stat.execute("insert into test values(2, null, null)");
stat.execute("insert into test values(3, null, null)");
ResultSet rs = stat.executeQuery("select * from test");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertNull(rs.getObject(2));
stat.execute("update test set description='DESCRIPTION' where id = 1");
stat.execute("update test set description='DESCRIPTION' where id = 2");
stat.execute("update test set description='DESCRIPTION' where id = 3");
conn.close();
deleteDb("spatial"); deleteDb("spatial");
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论