deephaven / deephaven-core

Deephaven Community Core
Other
259 stars 80 forks source link

Support Switch Expressions in Language Parser #1545

Open cpwright opened 3 years ago

cpwright commented 3 years ago

As a developer, I want to use switch expressions inside of DB language (update) clauses. The current method of doing this involves many layers of nested ternary operators, which is difficult to read and error prone. For example:

.update("Result = Source == `A` ? A : Source == `B` ? B ? Source = `C` ? C : null")

Could better be represented as:

.update("Result = switch (Source) { case 'A' ->  A; case `B` -> B; case `C` -> C; default -> null;"}
niloc132 commented 3 years ago

There is a hidden risk in this - any case in the new switch constructs can be composed of not just an expression, as in the example, but a yield statement or a block ending in a yield statement. Right now that would fail in our parser, as we only allow a subset of java expressions (no class declarations, etc). Making it a language feature could also let a user offer type unions for a column instead of just "find the most specific type that allows Double, String, SmartKey" etc...

I can't see an obvious reason that it would be necessarily unsafe for grpc calls, but our existing protections also make the explicit assumption that only certain expressions (unary, binary, method call) can appear, no statements.

--

With that said, this could be excellent for filters too, and we might even want to expose the experimental/preview pattern match feature as well?