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

SQL: Add RAND() function. #8814

Closed
wants to merge 3 commits into from
Closed

SQL: Add RAND() function. #8814

wants to merge 3 commits into from

Conversation

gianm
Copy link
Contributor

@gianm gianm commented Nov 3, 2019

Fixes #8789, #8661.

@vogievetsky
Copy link
Contributor

Does it fully fix #8661?

@gianm
Copy link
Contributor Author

gianm commented Nov 4, 2019

Does it fully fix #8661?

I believe the other functions already exist (check the docs) so yes.

throw new IAE("Function[%s] needs zero or one arguments", name());
}

class RandExpr extends ExprMacroTable.BaseScalarMacroFunctionExpr
Copy link
Contributor

Choose a reason for hiding this comment

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

👀 I've never seen this pattern before.

dumb guy question: Is there any advantage of using this vs a private static inner class. Do we do this just so we don't have to pass randomGenerator to the constructor of this class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's sort of the same thing as an anonymous class, except it's not anonymous. I use them somewhat interchangeably. I'm not aware of any major advantage except that the class name is nicer in stack traces, debuggers, etc.

final DoubleSupplier randomGenerator;

if (args.isEmpty()) {
randomGenerator = () -> ThreadLocalRandom.current().nextDouble();
Copy link
Contributor

Choose a reason for hiding this comment

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

If you wanted to make the test in ExpressionsTest.java easier, you could inject an annotated Supplier<DoubleSupplier> into this class something like

@Inject
RandExprMacro(@ThreadRandom Supplier<DoubleSupplier> localThreadRandomSupplier) {
    ...
}

As I'm typing this, it feels like overkill, so feel free to ignore

@lgtm-com
Copy link

lgtm-com bot commented Nov 6, 2019

This pull request introduces 2 alerts when merging 97a0780 into 3b602da - view on LGTM.com

new alerts:

  • 2 for Missing format argument

@gianm
Copy link
Contributor Author

gianm commented Nov 7, 2019

Actually, I just realized this is a bad idea, for two reasons.

  1. The result of the random expression will be potentially cached at query time. Fixing this means adding an isCacheable method to all expressions.
  2. At ingest time, if you use this in a transformSpec, it makes the results of ingestion nondeterministic. This will cause haywire with Kafka index task replicas. Fixing it probably requires creating a per-partition seed ahead of time on the supervisor and sharing it across tasks.

These issues can be overcome, but, it doesn't seem worth it at this time. So I will close this PR. Thanks @suneet-amp for taking a look anyway!

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

Successfully merging this pull request may close these issues.

SQL RAND function
3 participants