Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faker: Override generator expression in select statement #24078

Closed

Conversation

brybacki
Copy link
Contributor

@brybacki brybacki commented Nov 8, 2024

Description

Faker: Add a possibility to override the generator expression in the select statement. It is possible to do something like this:

SELECT name FROM person WHERE name='#{Name.first_name}' limit 1

Additional context and related issues

Release notes

( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
( ) Release notes are required, with the following suggested text:

## Section
* Fix some things. ({issue}`issuenumber`)

String testQuery = "SELECT name FROM generators WHERE name='#{Name.first_name} Bond' limit 1";

String resultText = getQueryRunner().execute(testQuery).getMaterializedRows().getFirst().toString();
assertThat(resultText).contains("Bond").matches(s -> s.length() - s.replace(" ", "").length() == 1);
Copy link
Member

Choose a reason for hiding this comment

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

This would be true even without the change in FakerPageSource. Consider using a different expression, maybe #{numerify '####'} - then you can check if the result contains any digits.

Copy link
Member

@martint martint left a comment

Choose a reason for hiding this comment

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

Using a condition in the where clause to specify the pattern to use for generation is an abuse of the semantics of the where clause.

Specifically, when a column is compared to a string, the result should be such that the values of that column match exactly the provided string. This is not the case here.

@brybacki
Copy link
Contributor Author

brybacki commented Nov 8, 2024

Using a condition in the where clause to specify the pattern to use for generation is an abuse of the semantics of the where clause.

Specifically, when a column is compared to a string, the result should be such that the values of that column match exactly the provided string. This is not the case here.

That is true, thanks for the comment. I think we can do a virtual column.
Or maybe we can drop this PR and just improve documentation.

@brybacki
Copy link
Contributor Author

brybacki commented Nov 8, 2024

There is already a way to do what we wanted from SQL with Faker connector function random_string.

SELECT id, random_string('#{Name.first_name}') AS name
FROM faker.default.table
LIMIT 50
UNION ALL
SELECT id random_string('#{Name.first_name} #{Name.first_name}') AS name
FROM faker.default.table
LIMIT 50

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

Successfully merging this pull request may close these issues.

3 participants