use of io.prestosql.spi.function.ScalarFunction in project hetu-core by openlookeng.
the class TimestampWithTimeZoneOperators method castToDate.
@ScalarFunction("date")
@ScalarOperator(CAST)
@SqlType(StandardTypes.DATE)
public static long castToDate(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value) {
// round down the current timestamp to days
ISOChronology chronology = unpackChronology(value);
long date = chronology.dayOfYear().roundFloor(unpackMillisUtc(value));
// date is currently midnight in timezone of the original value
// convert to UTC
long millis = date + chronology.getZone().getOffset(date);
return TimeUnit.MILLISECONDS.toDays(millis);
}
use of io.prestosql.spi.function.ScalarFunction in project hetu-core by openlookeng.
the class SetDigestFunctions method hashCounts.
@ScalarFunction
@SqlType("map(bigint,smallint)")
public static Block hashCounts(@TypeParameter("map<bigint,smallint>") Type mapType, @SqlType(SetDigestType.NAME) Slice slice) {
SetDigest digest = SetDigest.newInstance(slice);
// Maybe use static BlockBuilderStatus in order avoid `new`?
BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 1);
BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry();
for (Map.Entry<Long, Short> entry : digest.getHashCounts().entrySet()) {
BIGINT.writeLong(singleMapBlockBuilder, entry.getKey());
SMALLINT.writeLong(singleMapBlockBuilder, entry.getValue());
}
blockBuilder.closeEntry();
return (Block) mapType.getObject(blockBuilder, 0);
}
use of io.prestosql.spi.function.ScalarFunction in project hetu-core by openlookeng.
the class BenchmarkArrayDistinct method oldArrayDistinct.
@ScalarFunction
@SqlType("array(varchar)")
public static Block oldArrayDistinct(@SqlType("array(varchar)") Block array) {
if (array.getPositionCount() == 0) {
return array;
}
TypedSet typedSet = new TypedSet(VARCHAR, array.getPositionCount(), "old_array_distinct");
BlockBuilder distinctElementBlockBuilder = VARCHAR.createBlockBuilder(null, array.getPositionCount());
for (int i = 0; i < array.getPositionCount(); i++) {
if (!typedSet.contains(array, i)) {
typedSet.add(array, i);
VARCHAR.appendTo(array, i, distinctElementBlockBuilder);
}
}
return distinctElementBlockBuilder.build();
}
use of io.prestosql.spi.function.ScalarFunction in project hetu-core by openlookeng.
the class BenchmarkArraySort method oldArraySort.
@ScalarFunction
@SqlType("array(varchar)")
public static Block oldArraySort(@SqlType("array(varchar)") Block block) {
List<Integer> positions = Ints.asList(new int[block.getPositionCount()]);
for (int i = 0; i < block.getPositionCount(); i++) {
positions.set(i, i);
}
positions.sort((p1, p2) -> {
// TODO: This could be quite slow, it should use parametric equals
return VARCHAR.compareTo(block, p1, block, p2);
});
BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, block.getPositionCount());
for (int position : positions) {
VARCHAR.appendTo(block, position, blockBuilder);
}
return blockBuilder.build();
}
use of io.prestosql.spi.function.ScalarFunction in project hetu-core by openlookeng.
the class GeoFunctions method invalidReason.
@Description("Returns the reason for why the input geometry is not valid. Returns null if the input is valid.")
@ScalarFunction("geometry_invalid_reason")
@SqlType(VARCHAR)
@SqlNullable
public static Slice invalidReason(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
GeometryCursor cursor = deserialize(input).getEsriGeometryCursor();
NonSimpleResult result = new NonSimpleResult();
while (true) {
com.esri.core.geometry.Geometry geometry = cursor.next();
if (geometry == null) {
return null;
}
if (!OperatorSimplifyOGC.local().isSimpleOGC(geometry, null, true, result, null)) {
String reasonText = NON_SIMPLE_REASONS.getOrDefault(result.m_reason, result.m_reason.name());
if (!(geometry instanceof MultiVertexGeometry)) {
return utf8Slice(reasonText);
}
MultiVertexGeometry multiVertexGeometry = (MultiVertexGeometry) geometry;
if (result.m_vertexIndex1 >= 0 && result.m_vertexIndex2 >= 0) {
Point point1 = multiVertexGeometry.getPoint(result.m_vertexIndex1);
Point point2 = multiVertexGeometry.getPoint(result.m_vertexIndex2);
return utf8Slice(format("%s at or near (%s %s) and (%s %s)", reasonText, point1.getX(), point1.getY(), point2.getX(), point2.getY()));
}
if (result.m_vertexIndex1 >= 0) {
Point point = multiVertexGeometry.getPoint(result.m_vertexIndex1);
return utf8Slice(format("%s at or near (%s %s)", reasonText, point.getX(), point.getY()));
}
return utf8Slice(reasonText);
}
}
}
Aggregations