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
4 changes: 2 additions & 2 deletions src/compute-types/tests/snapshots/func_registry.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/compute-types/tests/snapshots/func_registry_source.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/expr/src/scalar/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func_name! {
<E> CastList1ToList2<E> => "cast_list1_to_list2",
<E> CastListToJsonb<E> => "cast_list_to_jsonb",
CastListToString => "cast_list_to_string",
CastMapToString => "cast_map_to_string",
<E> CastRecord1ToRecord2<E> => "cast_record1_to_record2",
CastRecordToString => "cast_record_to_string",
<E> CastStringToArray<E> => "cast_string_to_array",
Expand All @@ -168,7 +167,6 @@ func_name! {
IsLikeMatch => "is_like_match",
IsRegexpMatch => "is_regexp_match",
ListLengthMax => "list_length_max",
MapBuildFromRecordList => "map_build_from_record_list",
RecordGet => "record_get",
RegexpMatch => "regexp_match",
RegexpReplace => "regexp_replace",
Expand Down
150 changes: 34 additions & 116 deletions src/expr/src/scalar/func/impls/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use std::fmt;

use itertools::Itertools;
use mz_expr_derive::sqlfunc;
use mz_repr::{Datum, DatumMap, RowArena, SqlColumnType, SqlScalarType};
use mz_repr::{Datum, DatumList, DatumMap, ExcludeNull, RowArena, SqlScalarType};
use serde::{Deserialize, Serialize};

use crate::scalar::func::{LazyUnaryFunc, stringify_datum};
use crate::{Eval, EvalError};
use crate::EvalError;
use crate::scalar::func::stringify_datum;

#[derive(
Ord,
Expand All @@ -32,56 +30,12 @@ pub struct CastMapToString {
pub ty: SqlScalarType,
}

impl LazyUnaryFunc for CastMapToString {
fn eval<'a>(
&'a self,
datums: &[Datum<'a>],
temp_storage: &'a RowArena,
a: &'a impl Eval,
) -> Result<Datum<'a>, EvalError> {
let a = a.eval(datums, temp_storage)?;
if a.is_null() {
return Ok(Datum::Null);
}
let mut buf = String::new();
stringify_datum(&mut buf, a, &self.ty)?;
Ok(Datum::String(temp_storage.push_string(buf)))
}

fn output_sql_type(&self, input_type: SqlColumnType) -> SqlColumnType {
SqlScalarType::String.nullable(input_type.nullable)
}

fn propagates_nulls(&self) -> bool {
true
}

fn introduces_nulls(&self) -> bool {
false
}

fn preserves_uniqueness(&self) -> bool {
true
}

fn inverse(&self) -> Option<crate::UnaryFunc> {
// TODO? If we moved typeconv into expr, we could evaluate this
None
}

fn is_monotone(&self) -> bool {
false
}

fn is_eliminable_cast(&self) -> bool {
false
}
}

impl fmt::Display for CastMapToString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("maptostr")
}
// TODO? If we moved typeconv into expr, we could evaluate the inverse of this cast
#[sqlfunc(CastMapToString, sqlname = "maptostr", preserves_uniqueness = true)]
fn cast_map_to_string<'a>(&self, a: ExcludeNull<Datum<'a>>) -> Result<String, EvalError> {
let mut buf = String::new();
stringify_datum(&mut buf, *a, &self.ty)?;
Ok(buf)
}

#[sqlfunc(sqlname = "map_length")]
Expand All @@ -107,72 +61,36 @@ pub struct MapBuildFromRecordList {
pub value_type: SqlScalarType,
}

impl LazyUnaryFunc for MapBuildFromRecordList {
fn eval<'a>(
&'a self,
datums: &[Datum<'a>],
temp_storage: &'a RowArena,
a: &'a impl Eval,
) -> Result<Datum<'a>, EvalError> {
let a = a.eval(datums, temp_storage)?;
if a.is_null() {
return Ok(Datum::Null);
#[sqlfunc(
MapBuildFromRecordList,
sqlname = "map_build",
output_type_expr = SqlScalarType::Map {
value_type: Box::new(self.value_type.clone()),
custom_id: None,
}
.nullable(false)
)]
fn map_build_from_record_list<'a>(
&self,
a: DatumList<'a>,
temp_storage: &'a RowArena,
) -> DatumMap<'a> {
let mut map = std::collections::BTreeMap::new();

for i in a.iter() {
if i.is_null() {
continue;
}
let list = a.unwrap_list();
let mut map = std::collections::BTreeMap::new();

for i in list.iter() {
if i.is_null() {
for (k, v) in i.unwrap_list().iter().tuples() {
if k.is_null() {
continue;
}

for (k, v) in i.unwrap_list().iter().tuples() {
if k.is_null() {
continue;
}
map.insert(k.unwrap_str(), v);
}
map.insert(k.unwrap_str(), v);
}

let map = temp_storage.make_datum(|packer| packer.push_dict(map));
Ok(map)
}

fn output_sql_type(&self, _input_type: SqlColumnType) -> SqlColumnType {
SqlScalarType::Map {
value_type: Box::new(self.value_type.clone()),
custom_id: None,
}
.nullable(true)
}

fn propagates_nulls(&self) -> bool {
true
}

fn introduces_nulls(&self) -> bool {
true
}

fn preserves_uniqueness(&self) -> bool {
false
}

fn inverse(&self) -> Option<crate::UnaryFunc> {
None
}

fn is_monotone(&self) -> bool {
false
}

fn is_eliminable_cast(&self) -> bool {
false
}
}

impl fmt::Display for MapBuildFromRecordList {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("map_build")
}
temp_storage
.make_datum(|packer| packer.push_dict(map))
.unwrap_map()
}
Loading