Skip to content

Conversation

@comphead
Copy link
Contributor

@comphead comphead commented May 29, 2023

Which issue does this PR close?

Related #6448.

Closes #6462

Rationale for this change

Reduce search complexity when mapping BuiltinScalarFunction and its alias. Currently it is linear search and the place becoming a hotspot. The change is to make the search O(1) using static maps

What changes are included in this PR?

Are these changes tested?

Yes

Are there any user-facing changes?

No

@github-actions github-actions bot added logical-expr Logical plan and expressions sql SQL Planner labels May 29, 2023
@comphead
Copy link
Contributor Author

@alamb @2010YOUY01

Copy link
Contributor

@2010YOUY01 2010YOUY01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation makes it very clear what code to change when adding new functions. Suggested some minor changes, but overall looks good to me, thank you!

static ref FUNCTION_TO_NAME: HashMap<BuiltinScalarFunction, &'static str> = {
let mut map: HashMap<BuiltinScalarFunction, &'static str> = HashMap::new();
BuiltinScalarFunction::iter().for_each(|func| {
map.insert(func, aliases(&func).iter().next().unwrap_or(&"NO_ALIAS"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.iter().next() == .first()

}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/// First alias in the array is used to display function names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is that?

if func == self {
return write!(f, "{}", func_name);
}
fn aliases(func: &BuiltinScalarFunction) -> &'static [&'static str] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I commented on the wrong line previously @comphead , should be here.

Adding a comment for aliases():
/// First alias in the array is used to display function names

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @comphead and @2010YOUY01

BuiltinScalarFunction::CurrentDate => &["current_date"],
BuiltinScalarFunction::CurrentTime => &["current_time"],
BuiltinScalarFunction::DateBin => &["date_bin"],
BuiltinScalarFunction::DateTrunc => &["date_trunc", "datetrunc"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how this formulation makes it easier to understand what functions have aliases and which do not 👍

@alamb
Copy link
Contributor

alamb commented May 31, 2023

I think this PR looks good to go and all outstanding comments have been addressed, so merging it in 🚀

@alamb alamb merged commit 201a2a9 into apache:main May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logical-expr Logical plan and expressions sql SQL Planner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants