Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions common/utils/src/test/scala/org/apache/spark/util/MaybeNull.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.
*/

package org.apache.spark.util

/* The MaybeNull class is a utility that introduces controlled nullability into a sequence
* of invocations. It is designed to return a ~null~ value at a specified interval while returning
* the provided value for all other invocations.
*/
case class MaybeNull(interval: Int) {
assert(interval > 1)
private var invocations = 0
def apply[T](value: T): T = {
val result = if (invocations % interval == 0) {
null.asInstanceOf[T]
} else {
value
}
invocations += 1
result
}
}
77 changes: 77 additions & 0 deletions sql/api/src/main/scala/org/apache/spark/sql/util/ArrowUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,43 @@ private[sql] object ArrowUtils {
largeVarTypes)).asJava)
case udt: UserDefinedType[_] =>
toArrowField(name, udt.sqlType, nullable, timeZoneId, largeVarTypes)
case g: GeometryType =>
val fieldType =
new FieldType(nullable, ArrowType.Struct.INSTANCE, null)

// WKB field is tagged with additional metadata so we can identify that the arrow
// struct actually represents a geometry schema.
val wkbFieldType = new FieldType(
false,
toArrowType(BinaryType, timeZoneId, largeVarTypes),
null,
Map("geometry" -> "true", "srid" -> g.srid.toString).asJava)

new Field(
name,
fieldType,
Seq(
toArrowField("srid", IntegerType, false, timeZoneId, largeVarTypes),
new Field("wkb", wkbFieldType, Seq.empty[Field].asJava)).asJava)

case g: GeographyType =>
val fieldType =
new FieldType(nullable, ArrowType.Struct.INSTANCE, null, null)

// WKB field is tagged with additional metadata so we can identify that the arrow
// struct actually represents a geography schema.
val wkbFieldType = new FieldType(
false,
toArrowType(BinaryType, timeZoneId, largeVarTypes),
null,
Map("geography" -> "true", "srid" -> g.srid.toString).asJava)

new Field(
name,
fieldType,
Seq(
toArrowField("srid", IntegerType, false, timeZoneId, largeVarTypes),
new Field("wkb", wkbFieldType, Seq.empty[Field].asJava)).asJava)
case _: VariantType =>
val fieldType = new FieldType(nullable, ArrowType.Struct.INSTANCE, null)
// The metadata field is tagged with additional metadata so we can identify that the arrow
Expand Down Expand Up @@ -175,6 +212,26 @@ private[sql] object ArrowUtils {
}
}

def isGeometryField(field: Field): Boolean = {
assert(field.getType.isInstanceOf[ArrowType.Struct])
field.getChildren.asScala
.map(_.getName)
.asJava
.containsAll(Seq("wkb", "srid").asJava) && field.getChildren.asScala.exists { child =>
child.getName == "wkb" && child.getMetadata.getOrDefault("geometry", "false") == "true"
}
}

def isGeographyField(field: Field): Boolean = {
assert(field.getType.isInstanceOf[ArrowType.Struct])
field.getChildren.asScala
.map(_.getName)
.asJava
.containsAll(Seq("wkb", "srid").asJava) && field.getChildren.asScala.exists { child =>
child.getName == "wkb" && child.getMetadata.getOrDefault("geography", "false") == "true"
}
}

def fromArrowField(field: Field): DataType = {
field.getType match {
case _: ArrowType.Map =>
Expand All @@ -188,6 +245,26 @@ private[sql] object ArrowUtils {
ArrayType(elementType, containsNull = elementField.isNullable)
case ArrowType.Struct.INSTANCE if isVariantField(field) =>
VariantType
case ArrowType.Struct.INSTANCE if isGeometryField(field) =>
// We expect that type metadata is associated with wkb field.
val metadataField =
field.getChildren.asScala.filter { child => child.getName == "wkb" }.head
val srid = metadataField.getMetadata.get("srid").toInt
if (srid == GeometryType.MIXED_SRID) {
GeometryType("ANY")
} else {
GeometryType(srid)
}
case ArrowType.Struct.INSTANCE if isGeographyField(field) =>
// We expect that type metadata is associated with wkb field.
val metadataField =
field.getChildren.asScala.filter { child => child.getName == "wkb" }.head
val srid = metadataField.getMetadata.get("srid").toInt
if (srid == GeographyType.MIXED_SRID) {
GeographyType("ANY")
} else {
GeographyType(srid)
}
case ArrowType.Struct.INSTANCE =>
val fields = field.getChildren().asScala.map { child =>
val dt = fromArrowField(child)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public static GeometryVal stGeomFromWKB(byte[] wkb) {
return toPhysVal(Geometry.fromWkb(wkb));
}

public static GeometryVal stGeomFromWKB(byte[] wkb, int srid) {
return toPhysVal(Geometry.fromWkb(wkb, srid));
}

// ST_SetSrid
public static GeographyVal stSetSrid(GeographyVal geo, int srid) {
// We only allow setting the SRID to geographic values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@

import org.apache.spark.SparkUnsupportedOperationException;
import org.apache.spark.annotation.DeveloperApi;
import org.apache.spark.sql.catalyst.util.STUtils;
import org.apache.spark.sql.util.ArrowUtils;
import org.apache.spark.sql.types.*;
import org.apache.spark.unsafe.types.CalendarInterval;
import org.apache.spark.unsafe.types.GeographyVal;
import org.apache.spark.unsafe.types.GeometryVal;
import org.apache.spark.unsafe.types.UTF8String;

/**
Expand Down Expand Up @@ -146,6 +149,30 @@ public ColumnarMap getMap(int rowId) {
super(type);
}

@Override
public GeographyVal getGeography(int rowId) {
if (isNullAt(rowId)) return null;

GeographyType gt = (GeographyType) this.type;
int srid = getChild(0).getInt(rowId);
byte[] bytes = getChild(1).getBinary(rowId);
gt.assertSridAllowedForType(srid);
// TODO(GEO-602): Geog still does not support different SRIDs, once it does,
// we need to update this.
return (bytes == null) ? null : STUtils.stGeogFromWKB(bytes);
}

@Override
public GeometryVal getGeometry(int rowId) {
if (isNullAt(rowId)) return null;

GeometryType gt = (GeometryType) this.type;
int srid = getChild(0).getInt(rowId);
byte[] bytes = getChild(1).getBinary(rowId);
gt.assertSridAllowedForType(srid);
return (bytes == null) ? null : STUtils.stGeomFromWKB(bytes, srid);
}

public ArrowColumnVector(ValueVector vector) {
this(ArrowUtils.fromArrowField(vector.getField()));
initAccessor(vector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.apache.arrow.vector.complex._

import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.SpecializedGetters
import org.apache.spark.sql.catalyst.util.STUtils
import org.apache.spark.sql.errors.ExecutionErrors
import org.apache.spark.sql.types._
import org.apache.spark.sql.util.ArrowUtils
Expand Down Expand Up @@ -92,6 +93,16 @@ object ArrowWriter {
createFieldWriter(vector.getChildByOrdinal(ordinal))
}
new StructWriter(vector, children.toArray)
case (dt: GeometryType, vector: StructVector) =>
val children = (0 until vector.size()).map { ordinal =>
createFieldWriter(vector.getChildByOrdinal(ordinal))
}
new GeometryWriter(dt, vector, children.toArray)
case (dt: GeographyType, vector: StructVector) =>
val children = (0 until vector.size()).map { ordinal =>
createFieldWriter(vector.getChildByOrdinal(ordinal))
}
new GeographyWriter(dt, vector, children.toArray)
case (dt, _) =>
throw ExecutionErrors.unsupportedDataTypeError(dt)
}
Expand Down Expand Up @@ -446,6 +457,42 @@ private[arrow] class StructWriter(
}
}

private[arrow] class GeographyWriter(
dt: GeographyType,
valueVector: StructVector,
children: Array[ArrowFieldWriter]) extends StructWriter(valueVector, children) {

override def setValue(input: SpecializedGetters, ordinal: Int): Unit = {
valueVector.setIndexDefined(count)

val geom = STUtils.deserializeGeog(input.getGeography(ordinal), dt)
val bytes = geom.getBytes
val srid = geom.getSrid

val row = InternalRow(srid, bytes)
children(0).write(row, 0)
children(1).write(row, 1)
}
}

private[arrow] class GeometryWriter(
dt: GeometryType,
valueVector: StructVector,
children: Array[ArrowFieldWriter]) extends StructWriter(valueVector, children) {

override def setValue(input: SpecializedGetters, ordinal: Int): Unit = {
valueVector.setIndexDefined(count)

val geom = STUtils.deserializeGeom(input.getGeometry(ordinal), dt)
val bytes = geom.getBytes
val srid = geom.getSrid

val row = InternalRow(srid, bytes)
children(0).write(row, 0)
children(1).write(row, 1)
}
}

private[arrow] class MapWriter(
val valueVector: MapVector,
val structVector: StructVector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class ArrowUtilsSuite extends SparkFunSuite {
roundtrip(BinaryType)
roundtrip(DecimalType.SYSTEM_DEFAULT)
roundtrip(DateType)
roundtrip(GeometryType("ANY"))
roundtrip(GeometryType(4326))
roundtrip(GeographyType("ANY"))
roundtrip(GeographyType(4326))
roundtrip(YearMonthIntervalType())
roundtrip(DayTimeIntervalType())
checkError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import org.apache.spark.sql.connect.client.arrow.FooEnum.FooEnum
import org.apache.spark.sql.connect.test.ConnectFunSuite
import org.apache.spark.sql.types.{ArrayType, DataType, DayTimeIntervalType, Decimal, DecimalType, IntegerType, Metadata, SQLUserDefinedType, StringType, StructType, UserDefinedType, YearMonthIntervalType}
import org.apache.spark.unsafe.types.VariantVal
import org.apache.spark.util.SparkStringUtils
import org.apache.spark.util.{MaybeNull, SparkStringUtils}

/**
* Tests for encoding external data to and from arrow.
Expand Down Expand Up @@ -218,20 +218,6 @@ class ArrowEncoderSuite extends ConnectFunSuite with BeforeAndAfterAll {
}
}

private case class MaybeNull(interval: Int) {
assert(interval > 1)
private var invocations = 0
def apply[T](value: T): T = {
val result = if (invocations % interval == 0) {
null.asInstanceOf[T]
} else {
value
}
invocations += 1
result
}
}

private def javaBigDecimal(i: Int): java.math.BigDecimal = {
javaBigDecimal(i, DecimalType.DEFAULT_SCALE)
}
Expand Down
7 changes: 7 additions & 0 deletions sql/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
<artifactId>spark-sketch_${scala.binary.version}</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-common-utils_${scala.binary.version}</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.binary.version}</artifactId>
Expand Down
Loading