提交 6ef85054 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #267 from nicolas-f/fix/spatial_index_update_issue266

Fix issue, update key on MVMAP update about #266
...@@ -186,19 +186,18 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -186,19 +186,18 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
* @return Index cost hint * @return Index cost hint
*/ */
public static long getCostRangeIndex(int[] masks, long rowCount, Column[] columns) { public static long getCostRangeIndex(int[] masks, long rowCount, Column[] columns) {
rowCount += Constants.COST_ROW_OFFSET; // Never use spatial tree index without spatial filter
long cost = rowCount; if(columns.length == 0) {
if (masks == null) { return Long.MAX_VALUE;
return cost;
} }
for (Column column : columns) { for (Column column : columns) {
int index = column.getColumnId(); int index = column.getColumnId();
int mask = masks[index]; int mask = masks[index];
if ((mask & IndexCondition.SPATIAL_INTERSECTS) != 0) { if ((mask & IndexCondition.SPATIAL_INTERSECTS) != IndexCondition.SPATIAL_INTERSECTS) {
cost = 3 + rowCount / 4; return Long.MAX_VALUE;
} }
} }
return 10 * cost; return 2;
} }
@Override @Override
......
...@@ -237,6 +237,7 @@ public class MVRTreeMap<V> extends MVMap<SpatialKey, V> { ...@@ -237,6 +237,7 @@ public class MVRTreeMap<V> extends MVMap<SpatialKey, V> {
if (p.isLeaf()) { if (p.isLeaf()) {
for (int i = 0; i < p.getKeyCount(); i++) { for (int i = 0; i < p.getKeyCount(); i++) {
if (keyType.equals(p.getKey(i), key)) { if (keyType.equals(p.getKey(i), key)) {
p.setKey(i, key);
return p.setValue(i, value); return p.setValue(i, value);
} }
} }
......
...@@ -93,6 +93,8 @@ public class TestSpatial extends TestBase { ...@@ -93,6 +93,8 @@ public class TestSpatial extends TestBase {
testNullableGeometryDelete(); testNullableGeometryDelete();
testNullableGeometryInsert(); testNullableGeometryInsert();
testNullableGeometryUpdate(); testNullableGeometryUpdate();
testIndexUpdateNullGeometry();
testInsertNull();
} }
private void testBug1() throws SQLException { private void testBug1() throws SQLException {
...@@ -879,7 +881,8 @@ public class TestSpatial extends TestBase { ...@@ -879,7 +881,8 @@ public class TestSpatial extends TestBase {
stat.execute("create spatial index spatial on test(the_geom)"); stat.execute("create spatial index spatial on test(the_geom)");
ResultSet rs = stat.executeQuery("explain select * from test where _ROWID_ = 5"); ResultSet rs = stat.executeQuery("explain select * from test where _ROWID_ = 5");
assertTrue(rs.next()); assertTrue(rs.next());
assertContains(rs.getString(1), "/* PUBLIC.SPATIAL: _ROWID_ = 5 */"); assertFalse(rs.getString(1).contains("/* PUBLIC.SPATIAL: _ROWID_ = " +
"5 */"));
} finally { } finally {
// Close the database // Close the database
conn.close(); conn.close();
...@@ -1076,4 +1079,122 @@ public class TestSpatial extends TestBase { ...@@ -1076,4 +1079,122 @@ public class TestSpatial extends TestBase {
deleteDb("spatial"); deleteDb("spatial");
} }
public void testIndexUpdateNullGeometry() throws SQLException {
deleteDb("spatial");
Connection conn = getConnection(URL);
Statement stat = conn.createStatement();
stat.execute("drop table if exists DUMMY_11;");
stat.execute("CREATE TABLE PUBLIC.DUMMY_11 (fid serial, GEOM GEOMETRY);");
stat.execute("CREATE SPATIAL INDEX PUBLIC_DUMMY_11_SPATIAL_INDEX on" +
" PUBLIC.DUMMY_11(GEOM);");
stat.execute("insert into PUBLIC.DUMMY_11(geom) values(null);");
stat.execute("update PUBLIC.DUMMY_11 set geom =" +
" 'POLYGON ((1 1, 5 1, 5 5, 1 5, 1 1))';");
ResultSet rs = stat.executeQuery("select fid, GEOM from DUMMY_11 " +
"where GEOM && " +
"'POLYGON" +
"((1 1,5 1,5 5,1 5,1 1))';");
try {
assertTrue(rs.next());
assertEquals("POLYGON ((1 1, 5 1, 5 5, 1 5, 1 1))", rs.getString(2));
} finally {
rs.close();
}
// Update again the geometry elsewhere
stat.execute("update PUBLIC.DUMMY_11 set geom =" +
" 'POLYGON ((10 10, 50 10, 50 50, 10 50, 10 10))';");
rs = stat.executeQuery("select fid, GEOM from DUMMY_11 " +
"where GEOM && " +
"'POLYGON ((10 10, 50 10, 50 50, 10 50, 10 10))';");
try {
assertTrue(rs.next());
assertEquals("POLYGON ((10 10, 50 10, 50 50, 10 50, 10 10))", rs.getString(2));
} finally {
rs.close();
}
conn.close();
deleteDb("spatial");
}
public void testInsertNull() throws SQLException {
deleteDb("spatial");
Connection conn = getConnection(URL);
Statement stat = conn.createStatement();
stat.execute("\n" +
"drop table if exists PUBLIC.DUMMY_12;\n" +
"CREATE TABLE PUBLIC.DUMMY_12 (\n" +
" \"fid\" serial,\n" +
" Z_ID INTEGER,\n" +
" GEOM GEOMETRY,\n" +
" CONSTRAINT CONSTRAINT_DUMMY_12 PRIMARY KEY (\"fid\")\n" +
");\n" +
"CREATE INDEX PRIMARY_KEY_DUMMY_12 ON PUBLIC.DUMMY_12 (\"fid\");\n" +
"CREATE spatial INDEX PUBLIC_DUMMY_12_SPATIAL_INDEX_ ON PUBLIC.DUMMY_12 (GEOM);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (123,3125163,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (124,3125164,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (125,3125173,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (126,3125174,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (127,3125175,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (128,3125176,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (129,3125177,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (130,3125178,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (131,3125179,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (132,3125180,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (133,3125335,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (134,3125336,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (135,3125165,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (136,3125337,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (137,3125338,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (138,3125339,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (139,3125340,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (140,3125341,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (141,3125342,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (142,3125343,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (143,3125344,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (144,3125345,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (145,3125346,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (146,3125166,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (147,3125347,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (148,3125348,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (149,3125349,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (150,3125350,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (151,3125351,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (152,3125352,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (153,3125353,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (154,3125354,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (155,3125355,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (156,3125356,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (157,3125167,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (158,3125357,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (159,3125358,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (160,3125359,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (161,3125360,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (162,3125361,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (163,3125362,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (164,3125363,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (165,3125364,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (166,3125365,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (167,3125366,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (168,3125168,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (169,3125367,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (170,3125368,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (171,3125369,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (172,3125370,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (173,3125169,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (174,3125170,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (175,3125171,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (176,3125172,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (177,-2,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (178,-1,NULL);\n" +
"INSERT INTO PUBLIC.DUMMY_12 (\"fid\",Z_ID,GEOM) VALUES (179," +
"-1,NULL);");
ResultSet rs = stat.executeQuery("select * from DUMMY_12");
try {
assertTrue(rs.next());
} finally {
rs.close();
}
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论