datafusion_geo/function/
as_text.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::geo::GeometryArray;
use crate::DFResult;
use arrow_array::cast::AsArray;
use arrow_array::{GenericBinaryArray, LargeStringArray, OffsetSizeTrait, StringArray};
use arrow_schema::DataType;
use datafusion_common::DataFusionError;
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, TypeSignature, Volatility};
use geozero::ToWkt;
use std::any::Any;
use std::sync::Arc;

#[derive(Debug)]
pub struct AsTextUdf {
    signature: Signature,
    aliases: Vec<String>,
}

impl AsTextUdf {
    pub fn new() -> Self {
        Self {
            signature: Signature::one_of(
                vec![
                    TypeSignature::Exact(vec![DataType::Binary]),
                    TypeSignature::Exact(vec![DataType::LargeBinary]),
                ],
                Volatility::Immutable,
            ),
            aliases: vec!["st_astext".to_string()],
        }
    }
}

impl ScalarUDFImpl for AsTextUdf {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn name(&self) -> &str {
        "ST_AsText"
    }

    fn signature(&self) -> &Signature {
        &self.signature
    }

    fn return_type(&self, arg_types: &[DataType]) -> datafusion_common::Result<DataType> {
        match arg_types[0] {
            DataType::Binary => Ok(DataType::Utf8),
            DataType::LargeBinary => Ok(DataType::LargeUtf8),
            _ => unreachable!(),
        }
    }

    fn invoke(&self, args: &[ColumnarValue]) -> datafusion_common::Result<ColumnarValue> {
        let arr = args[0].clone().into_array(1)?;
        match args[0].data_type() {
            DataType::Binary => {
                let wkb_arr = arr.as_binary::<i32>();

                let mut wkt_vec = vec![];
                for i in 0..wkb_arr.geom_len() {
                    wkt_vec.push(to_wkt::<i32>(wkb_arr, i)?);
                }

                Ok(ColumnarValue::Array(Arc::new(StringArray::from(wkt_vec))))
            }
            DataType::LargeBinary => {
                let wkb_arr = arr.as_binary::<i64>();

                let mut wkt_vec = vec![];
                for i in 0..wkb_arr.geom_len() {
                    wkt_vec.push(to_wkt::<i64>(wkb_arr, i)?);
                }

                Ok(ColumnarValue::Array(Arc::new(LargeStringArray::from(
                    wkt_vec,
                ))))
            }
            _ => unreachable!(),
        }
    }

    fn aliases(&self) -> &[String] {
        &self.aliases
    }
}

fn to_wkt<O: OffsetSizeTrait>(
    wkb_arr: &GenericBinaryArray<O>,
    geom_index: usize,
) -> DFResult<Option<String>> {
    let geom = {
        #[cfg(feature = "geos")]
        {
            wkb_arr.geos_value(geom_index)?
        }
        #[cfg(not(feature = "geos"))]
        {
            wkb_arr.geo_value(geom_index)?
        }
    };
    let wkt = match geom {
        Some(geom) => Some(geom.to_wkt().map_err(|_| {
            DataFusionError::Internal("Failed to convert geometry to wkt".to_string())
        })?),
        None => None,
    };
    Ok(wkt)
}

impl Default for AsTextUdf {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use crate::function::{AsTextUdf, GeomFromWktUdf};
    use arrow::util::pretty::pretty_format_batches;
    use datafusion::logical_expr::ScalarUDF;
    use datafusion::prelude::SessionContext;

    #[tokio::test]
    async fn as_text() {
        let ctx = SessionContext::new();
        ctx.register_udf(ScalarUDF::from(GeomFromWktUdf::new()));
        ctx.register_udf(ScalarUDF::from(AsTextUdf::new()));
        let df = ctx
            .sql("select ST_AsText(ST_GeomFromText('POINT(-71.064544 42.28787)'))")
            .await
            .unwrap();
        assert_eq!(
            pretty_format_batches(&df.collect().await.unwrap())
                .unwrap()
                .to_string(),
            "+----------------------------------------------------------------+
| ST_AsText(ST_GeomFromText(Utf8(\"POINT(-71.064544 42.28787)\"))) |
+----------------------------------------------------------------+
| POINT(-71.064544 42.28787)                                     |
+----------------------------------------------------------------+"
        );
    }
}