-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(expr): optimize to_char function with constant template (#5130)
* perf(expr): optimize to_char function with constant template Signed-off-by: TennyZhuang <[email protected]> * fmt Signed-off-by: TennyZhuang <[email protected]> * Update src/expr/src/expr/expr_to_char_const_tmpl.rs Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update src/expr/src/expr/expr_to_char_const_tmpl.rs Co-authored-by: Tao Wu <[email protected]> * Update src/expr/src/expr/build_expr_from_prost.rs Co-authored-by: Bugen Zhao <[email protected]> * fmt Signed-off-by: TennyZhuang <[email protected]> * clippy Signed-off-by: TennyZhuang <[email protected]> Signed-off-by: TennyZhuang <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tao Wu <[email protected]> Co-authored-by: Bugen Zhao <[email protected]>
- Loading branch information
1 parent
d0a377d
commit 5fd012d
Showing
4 changed files
with
91 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2022 Singularity Data | ||
// | ||
// Licensed 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. | ||
|
||
use std::sync::Arc; | ||
|
||
use itertools::Itertools; | ||
use risingwave_common::array::{Array, ArrayBuilder, NaiveDateTimeArray, Utf8ArrayBuilder}; | ||
use risingwave_common::types::{DataType, Datum, ScalarImpl}; | ||
|
||
use super::Expression; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct ExprToCharConstTmplContext { | ||
pub(crate) chrono_tmpl: String, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct ExprToCharConstTmpl { | ||
pub(crate) child: Box<dyn Expression>, | ||
pub(crate) ctx: ExprToCharConstTmplContext, | ||
} | ||
|
||
impl Expression for ExprToCharConstTmpl { | ||
fn return_type(&self) -> DataType { | ||
DataType::Varchar | ||
} | ||
|
||
fn eval( | ||
&self, | ||
input: &risingwave_common::array::DataChunk, | ||
) -> crate::Result<risingwave_common::array::ArrayRef> { | ||
let data_arr = self.child.eval_checked(input)?; | ||
let data_arr: &NaiveDateTimeArray = data_arr.as_ref().into(); | ||
let mut output = Utf8ArrayBuilder::new(input.capacity()); | ||
for (data, vis) in data_arr.iter().zip_eq(input.vis().iter()) { | ||
if !vis { | ||
output.append_null()?; | ||
} else if let Some(data) = data { | ||
let res = data.0.format(&self.ctx.chrono_tmpl).to_string(); | ||
output.append(Some(res.as_str()))?; | ||
} else { | ||
output.append_null()?; | ||
} | ||
} | ||
|
||
Ok(Arc::new((output.finish()?).into())) | ||
} | ||
|
||
fn eval_row(&self, input: &risingwave_common::array::Row) -> crate::Result<Datum> { | ||
let data = self.child.eval_row(input)?; | ||
Ok(if let Some(ScalarImpl::NaiveDateTime(data)) = data { | ||
Some(data.0.format(&self.ctx.chrono_tmpl).to_string().into()) | ||
} else { | ||
None | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters