Skip to content

Commit

Permalink
implement GeoPointColumnReference
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasender committed Jul 1, 2014
1 parent 044245d commit f200b5b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate licenses
* this file to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/

package io.crate.operation.reference.doc.lucene;

import io.crate.exceptions.GroupByOnArrayUnsupportedException;
import io.crate.types.DataType;
import io.crate.types.DataTypes;
import org.apache.lucene.index.AtomicReaderContext;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.fielddata.GeoPointValues;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;

public class GeoPointColumnReference extends FieldCacheExpression<IndexGeoPointFieldData, Double[]> {

private GeoPointValues values;

public GeoPointColumnReference(String columnName) {
super(columnName);
}

@Override
public Double[] value() {
switch (values.setDocument(docId)) {
case 0:
return null;
case 1:
GeoPoint gp = values.nextValue();
return new Double[] { gp.lon(), gp.lat() };
default:
throw new GroupByOnArrayUnsupportedException(columnName());
}
}

@Override
public void setNextReader(AtomicReaderContext context) {
super.setNextReader(context);
values = indexFieldData.load(context).getGeoPointValues();
}

@Override
public DataType returnType() {
return DataTypes.GEO_POINT;
}

@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (!(obj instanceof GeoPointColumnReference))
return false;
return columnName.equals(((GeoPointColumnReference) obj).columnName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public LuceneCollectorExpression<?> getImplementation(ReferenceInfo referenceInf
return new LongColumnReference(colName);
case IntegerType.ID:
return new IntegerColumnReference(colName);
case GeoPointType.ID:
return new GeoPointColumnReference(colName);
default:
throw new UnhandledServerException(String.format("unsupported type '%s'", referenceInfo.type().getName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4126,14 +4126,16 @@ public void testAlterTableAddObjectColumnToExistingObject() throws Exception {

@Test
public void testGeoTypeQueries() throws Exception {
execute("create table t (id int primary key, p geo_point) " +
// setup
execute("create table t (id int primary key, i int, p geo_point) " +
"clustered into 1 shards " +
"with (number_of_replicas=0)");
ensureGreen();
execute("insert into t (id, p) values (1, 'POINT (10 20)')");
execute("insert into t (id, p) values (2, 'POINT (11 21)')");
execute("insert into t (id, i, p) values (1, 1, 'POINT (10 20)')");
execute("insert into t (id, i, p) values (2, 1, 'POINT (11 21)')");
refresh();

// order by
execute("select distance(p, 'POINT (11 21)') from t order by 1");
assertThat(response.rowCount(), is(2L));

Expand All @@ -4143,16 +4145,26 @@ public void testGeoTypeQueries() throws Exception {
assertThat(result1, is(0.0d));
assertThat(result2, is(152462.70754934277));

String stmt = "SELECT id " +
String stmtOrderBy = "SELECT id " +
"FROM t " +
"ORDER BY distance(p, 'POINT(30.0 30.0)')";
execute(stmt);
execute(stmtOrderBy);
assertThat(response.rowCount(), is(2L));
String expected =
String expectedOrderBy =
"2\n" +
"1\n";
assertEquals(expected, TestingHelpers.printedTable(response.rows()));
assertEquals(expectedOrderBy, TestingHelpers.printedTable(response.rows()));

// aggregation (max())
String stmtAggregate = "SELECT i, max(distance(p, 'POINT(30.0 30.0)')) " +
"FROM t " +
"GROUP BY i";
execute(stmtAggregate);
assertThat(response.rowCount(), is(1L));
String expectedAggregate = "1| 2297790.338709135\n";
assertEquals(expectedAggregate, TestingHelpers.printedTable(response.rows()));

// queries
execute("select p from t where distance(p, 'POINT (11 21)') > 0.0");
List<Double> row = (List<Double>) response.rows()[0][0];
assertThat(row.get(0), is(10.0d));
Expand Down

0 comments on commit f200b5b

Please sign in to comment.