Skip to content

Clean up string_view usage in metadata_modifier#646

Open
wipheg wants to merge 1 commit into
villagesql:mainfrom
wipheg:main
Open

Clean up string_view usage in metadata_modifier#646
wipheg wants to merge 1 commit into
villagesql:mainfrom
wipheg:main

Conversation

@wipheg

@wipheg wipheg commented Jun 10, 2026

Copy link
Copy Markdown

Summary

Fixes #642.

This updates metadata_modifier and related helper APIs to use std::string_view for string inputs that are only read.

Changes

  • Changed VillageSQL name normalization helpers to accept std::string_view
  • Updated metadata_modifier helper functions to avoid unnecessary std::string copies
  • Updated descriptor prefix constructors to accept std::string_view
  • Kept owned std::string copies where metadata is stored beyond the current call
  • Removed the TODO about migrating metadata_modifier string parameters to std::string_view

Validation

  • git diff --check passed
  • Not run: scripts/villint.sh due to local CRLF line-ending issue

Closes #642

@github-actions

github-actions Bot commented Jun 10, 2026

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@wipheg

wipheg commented Jun 10, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@villagestevers villagestevers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please look at the comment in helpers.cc FIRST. I did other files first and then realized the big problem with even doing this.

I think we should just look and see if some of these places can use string_view, but really the normalization functions cannot use string_views. Sorry for this being a little bit of a wild goose chase.

if (matches.empty()) {
villagesql_error("Unknown %s '%s'", MYF(0), entity_type, name);
const std::string name_for_error(name);
villagesql_error("Unknown %s '%s'", MYF(0), entity_type,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can use %.*s to use name directly with its length:

static_cast(name.length()), name.data() instead of name_for_error creation and .c_str()

const std::string name_for_error(name);
villagesql_error("%s '%s' is ambiguous; qualify as %s", MYF(0), entity_type,
name, qualify_hint);
name_for_error.c_str(), qualify_hint);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same

" index type '%s' (extension '%s')",
MYF(0), col_type_name.c_str(), col_ext_name.c_str(),
index_type_name.c_str(), index_type_ext_name.c_str());
index_type_name_for_error.c_str(), index_type_ext_name_for_error.c_str());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same

: "";
const std::string type_name(kci.custom_index_type.str,
kci.custom_index_type.length);
std::string_view ext_name = kci.custom_index_extension.str

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can't we just set this to to_string_view(kci.custom_index_extension)? it should be set to length 0 if not present.

LEX_CSTRING prof = kp->get_index_profile();
profile_name = std::string(prof.str, prof.length);
const std::string prof_extension =
std::string_view prof_name = to_string_view(prof);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why introduce prof_name? if we have a string anyway, we can use that where string_view is being passed it somewhere.

? std::string(kp->get_index_profile_extension().str,
kp->get_index_profile_extension().length)
: "";
? to_string_view(kp->get_index_profile_extension())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should similarly be initialized properly. I don't like that it is initialized with {} but that should be equivalent to {nullptr, 0}. So it feels like the original call to has_index_profile_extension is probably extraneous.

THD *thd, const std::string &extension_name, enum_mdl_duration duration) {
ExtensionKey ext_key(extension_name);
THD *thd, std::string_view extension_name, enum_mdl_duration duration) {
ExtensionKey ext_key{std::string(extension_name)};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It feels like we could do better here (but not in the scope of this change). Since the Key is essentially short-lived, we could just use string_views internally (though it might be tricky if we need them to own their memory in some scenarios). Maybe add a TODO(villagesql): comment here like:

// TODO(villagesql): consider supporting key "views" that use string_views instead of owning their strings.

}

std::string normalize_database_name(const std::string &name) {
static std::string casedn_string(const CHARSET_INFO *cs, std::string_view name) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No, this is not what we want to do. I am realizing now that the downstream have to be strings. Notice this just wraps casedn and makes a string. The reason is that casedn needs to be able to modify the underlying string (possibly even resizing it - though that really shouldn't be happening for us, as the charset should already be in the utf8_mb4 family). This makes me now doubt that we even should do this change.

@villagestevers

Copy link
Copy Markdown
Member

If it is easier to discuss over discord, you can join us there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Server]: string_view cleanup in metadata_modifier

2 participants