forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for ST_WktToSQL function which accepts a string and parses it as WKT representation of a geoshape. Relates to elastic#29872
- Loading branch information
Showing
12 changed files
with
211 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
.../src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StWkttosql.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.sql.expression.function.scalar.geo; | ||
|
||
import org.elasticsearch.xpack.sql.expression.Expression; | ||
import org.elasticsearch.xpack.sql.expression.Expressions; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction; | ||
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor; | ||
import org.elasticsearch.xpack.sql.expression.gen.script.Scripts; | ||
import org.elasticsearch.xpack.sql.tree.Location; | ||
import org.elasticsearch.xpack.sql.tree.NodeInfo; | ||
import org.elasticsearch.xpack.sql.type.DataType; | ||
|
||
/** | ||
* Constructs geometric objects from their WTK representations | ||
*/ | ||
public class StWkttosql extends UnaryScalarFunction { | ||
|
||
public StWkttosql(Location location, Expression field) { | ||
super(location, field); | ||
} | ||
|
||
@Override | ||
protected StWkttosql replaceChild(Expression newChild) { | ||
return new StWkttosql(location(), newChild); | ||
} | ||
|
||
@Override | ||
protected TypeResolution resolveType() { | ||
if (field().dataType().isString()) { | ||
return TypeResolution.TYPE_RESOLVED; | ||
} | ||
return Expressions.typeMustBeString(field(), functionName(), Expressions.ParamOrdinal.DEFAULT); | ||
} | ||
|
||
@Override | ||
protected Processor makeProcessor() { | ||
return StWkttosqlProcessor.INSTANCE; | ||
} | ||
|
||
@Override | ||
public DataType dataType() { | ||
return DataType.GEO_SHAPE; | ||
} | ||
|
||
@Override | ||
protected NodeInfo<StWkttosql> info() { | ||
return NodeInfo.create(this, StWkttosql::new, field()); | ||
} | ||
|
||
@Override | ||
public String processScript(String script) { | ||
return Scripts.formatTemplate(Scripts.SQL_SCRIPTS + ".wktToSql(" + script + ")"); | ||
} | ||
|
||
@Override | ||
public Object fold() { | ||
return StWkttosqlProcessor.INSTANCE.process(field().fold()); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
.../java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StWkttosqlProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.sql.expression.function.scalar.geo; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; | ||
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor; | ||
|
||
import java.io.IOException; | ||
|
||
public class StWkttosqlProcessor implements Processor { | ||
|
||
static final StWkttosqlProcessor INSTANCE = new StWkttosqlProcessor(); | ||
|
||
public static final String NAME = "geo_wkttosql"; | ||
|
||
private StWkttosqlProcessor() { | ||
} | ||
|
||
public StWkttosqlProcessor(StreamInput in) throws IOException {} | ||
|
||
@Override | ||
public Object process(Object input) { | ||
return StWkttosqlProcessor.apply(input); | ||
} | ||
|
||
|
||
public static GeoShape apply(Object input) { | ||
if (input == null) { | ||
return null; | ||
} | ||
|
||
if ((input instanceof String) == false) { | ||
throw new SqlIllegalArgumentException("A string is required; received {}", input); | ||
} | ||
try { | ||
return new GeoShape(input); | ||
} catch (IOException ex) { | ||
throw new SqlIllegalArgumentException("Cannot parse [{}] as a geo_shape value", input); | ||
} | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters