Skip to content
Merged
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
16 changes: 16 additions & 0 deletions doc/user/data/sql_funcs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,22 @@
- signature: 'reverse(s: str) -> str'
description: Reverse the characters in `s`.

- signature: 'string_to_array(s: str, ) -> str[]'
description: |
Splits the string at occurrences of delimiter and returns a text array of
the split segments.

If `delimiter` is NULL, each character in the string will become a
separate element in the array.

If `delimiter` is an empty string, then the string is treated as a single
field.

If `null_string` is supplied and is not NULL, fields matching that string
are replaced by NULL.

For example: `string_to_array('xx~~yy~~zz', '~~', 'yy')` → `{xx,NULL,zz}`

- type: Scalar
description: Scalar functions take a list of scalar expressions
functions:
Expand Down
4 changes: 2 additions & 2 deletions src/environmentd/tests/testdata/http/ws

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/expr/src/scalar.proto
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ message ProtoVariadicFunc {
google.protobuf.Empty regexp_split_to_array = 39;
google.protobuf.Empty regexp_replace = 40;
mz_repr.relation_and_scalar.ProtoScalarType map_build = 41;
google.protobuf.Empty string_to_array = 42;
}
}

Expand Down
97 changes: 97 additions & 0 deletions src/expr/src/scalar/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,87 @@ fn parse_ident<'a>(
})?)
}

fn string_to_array<'a>(
string_datum: Datum<'a>,
delimiter: Datum<'a>,
null_string: Datum<'a>,
temp_storage: &'a RowArena,
) -> Result<Datum<'a>, EvalError> {
if string_datum.is_null() {
return Ok(Datum::Null);
}

let string = string_datum.unwrap_str();

if string.is_empty() {
let mut row = Row::default();
let mut packer = row.packer();
packer.try_push_array(&[], std::iter::empty::<Datum>())?;

return Ok(temp_storage.push_unary_row(row));
}

if delimiter.is_null() {
let split_all_chars_delimiter = "";
return string_to_array_impl(string, split_all_chars_delimiter, null_string, temp_storage);
}

let delimiter = delimiter.unwrap_str();
if delimiter.is_empty() {
let mut row = Row::default();
let mut packer = row.packer();
packer.try_push_array(
&[ArrayDimension {
lower_bound: 1,
length: 1,
}],
vec![string].into_iter().map(Datum::String),
)?;

Ok(temp_storage.push_unary_row(row))
} else {
string_to_array_impl(string, delimiter, null_string, temp_storage)
}
}

fn string_to_array_impl<'a>(
string: &str,
delimiter: &str,
null_string: Datum<'a>,
temp_storage: &'a RowArena,
) -> Result<Datum<'a>, EvalError> {
let mut row = Row::default();
let mut packer = row.packer();

let result = string.split(delimiter);
let found: Vec<&str> = if delimiter.is_empty() {
result.filter(|s| !s.is_empty()).collect()
} else {
result.collect()
};
let array_dimensions = [ArrayDimension {
lower_bound: 1,
length: found.len(),
}];

if null_string.is_null() {
packer.try_push_array(&array_dimensions, found.into_iter().map(Datum::String))?;
} else {
let null_string = null_string.unwrap_str();
let found_datums = found.into_iter().map(|chunk| {
if chunk.eq(null_string) {
Datum::Null
} else {
Datum::String(chunk)
}
});

packer.try_push_array(&array_dimensions, found_datums)?;
}

Ok(temp_storage.push_unary_row(row))
}

fn regexp_split_to_array<'a>(
text: Datum<'a>,
regexp: Datum<'a>,
Expand Down Expand Up @@ -7845,6 +7926,7 @@ pub enum VariadicFunc {
ArrayFill {
elem_type: ScalarType,
},
StringToArray,
TimezoneTime,
RegexpSplitToArray,
RegexpReplace,
Expand Down Expand Up @@ -7951,6 +8033,11 @@ impl VariadicFunc {
regexp_split_to_array(ds[0], ds[1], flags, temp_storage)
}
VariadicFunc::RegexpReplace => regexp_replace_dynamic(&ds, temp_storage),
VariadicFunc::StringToArray => {
let null_string = if ds.len() == 2 { Datum::Null } else { ds[2] };

string_to_array(ds[0], ds[1], null_string, temp_storage)
}
}
}

Expand Down Expand Up @@ -7997,6 +8084,7 @@ impl VariadicFunc {
| VariadicFunc::ArrayFill { .. }
| VariadicFunc::TimezoneTime
| VariadicFunc::RegexpSplitToArray
| VariadicFunc::StringToArray
| VariadicFunc::RegexpReplace => false,
}
}
Expand Down Expand Up @@ -8102,6 +8190,9 @@ impl VariadicFunc {
ScalarType::Array(Box::new(ScalarType::String)).nullable(in_nullable)
}
RegexpReplace => ScalarType::String.nullable(in_nullable),
StringToArray => {
ScalarType::Array(Box::new(ScalarType::String)).nullable(input_types[0].nullable)
}
}
}

Expand Down Expand Up @@ -8129,6 +8220,7 @@ impl VariadicFunc {
| VariadicFunc::RangeCreate { .. }
| VariadicFunc::ArrayPosition
| VariadicFunc::ArrayFill { .. }
| VariadicFunc::StringToArray
)
}

Expand Down Expand Up @@ -8173,6 +8265,7 @@ impl VariadicFunc {
| ArrayFill { .. }
| TimezoneTime
| RegexpSplitToArray
| StringToArray
| RegexpReplace => false,
Coalesce
| Greatest
Expand Down Expand Up @@ -8286,6 +8379,7 @@ impl VariadicFunc {
| VariadicFunc::DateDiffTime
| VariadicFunc::TimezoneTime
| VariadicFunc::RegexpSplitToArray
| VariadicFunc::StringToArray
| VariadicFunc::RegexpReplace => false,
}
}
Expand Down Expand Up @@ -8344,6 +8438,7 @@ impl fmt::Display for VariadicFunc {
VariadicFunc::TimezoneTime => f.write_str("timezonet"),
VariadicFunc::RegexpSplitToArray => f.write_str("regexp_split_to_array"),
VariadicFunc::RegexpReplace => f.write_str("regexp_replace"),
VariadicFunc::StringToArray => f.write_str("string_to_array"),
}
}
}
Expand Down Expand Up @@ -8466,6 +8561,7 @@ impl RustType<ProtoVariadicFunc> for VariadicFunc {
VariadicFunc::TimezoneTime => TimezoneTime(()),
VariadicFunc::RegexpSplitToArray => RegexpSplitToArray(()),
VariadicFunc::RegexpReplace => RegexpReplace(()),
VariadicFunc::StringToArray => StringToArray(()),
};
ProtoVariadicFunc { kind: Some(kind) }
}
Expand Down Expand Up @@ -8532,6 +8628,7 @@ impl RustType<ProtoVariadicFunc> for VariadicFunc {
TimezoneTime(()) => Ok(VariadicFunc::TimezoneTime),
RegexpSplitToArray(()) => Ok(VariadicFunc::RegexpSplitToArray),
RegexpReplace(()) => Ok(VariadicFunc::RegexpReplace),
StringToArray(()) => Ok(VariadicFunc::StringToArray),
}
} else {
Err(TryFromProtoError::missing_field(
Expand Down
4 changes: 4 additions & 0 deletions src/sql/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3161,6 +3161,10 @@ pub static PG_CATALOG_BUILTINS: LazyLock<BTreeMap<&'static str, Func>> = LazyLoc
}) => String, 3538;
params!(Bytes, Bytes) => Operation::binary(|_ecx, _l, _r| bail_unsupported!("string_agg on BYTEA")) => Bytes, 3545;
},
"string_to_array" => Scalar {
params!(String, Any) => VariadicFunc::StringToArray => ScalarType::Array(Box::new(ScalarType::String)), 376;
params!(String, Any, String) => VariadicFunc::StringToArray => ScalarType::Array(Box::new(ScalarType::String)), 394;
},
"sum" => Aggregate {
params!(Int16) => AggregateFunc::SumInt16 => Int64, 2109;
params!(Int32) => AggregateFunc::SumInt32 => Int64, 2108;
Expand Down
Loading