-
Notifications
You must be signed in to change notification settings - Fork 103
Support SQL Table Valued Functions #3280
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
Conversation
* add post task that copies .token files to fdb-relational-core/src/main/antlr this makes the IDE able to parse the grammar files correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General comment:
I am not sure if this changes the path of INSERT INTO T VALUES (...)
. What is that QGM if it is changed. What is the QGM for SELECT * FROM VALUES(...)
? is it supposed to use an ExplodeValue
?
...c/main/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryTableFunctionPlan.java
Outdated
Show resolved
Hide resolved
...-core/src/main/java/com/apple/foundationdb/record/query/plan/explain/ExplainPlanVisitor.java
Outdated
Show resolved
Hide resolved
...va/com/apple/foundationdb/record/query/plan/cascades/properties/DistinctRecordsProperty.java
Show resolved
Hide resolved
...java/com/apple/foundationdb/record/query/plan/cascades/properties/CardinalitiesProperty.java
Show resolved
Hide resolved
...main/java/com/apple/foundationdb/record/query/plan/cascades/properties/OrderingProperty.java
Show resolved
Hide resolved
Yes, the
I decided to keep it that way anticipating that a switch to using a more general The QGM of Ending up with a single physical |
fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/RecordCursor.java
Outdated
Show resolved
Hide resolved
...a/com/apple/foundationdb/record/query/plan/cascades/expressions/TableFunctionExpression.java
Outdated
Show resolved
Hide resolved
...n/java/com/apple/foundationdb/record/query/plan/cascades/properties/DerivationsProperty.java
Outdated
Show resolved
Hide resolved
public Derivations visitTableFunctionPlan(@Nonnull final RecordQueryTableFunctionPlan tableFunctionPlan) { | ||
final var streamingValue = tableFunctionPlan.getValue(); | ||
final var elementType = streamingValue.getResultType(); | ||
final var values = ImmutableList.<Value>of(new FirstOrDefaultStreamingValue(streamingValue, new ThrowsValue(elementType))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these values are fake anyway, when we spoke I thought we had agreed on using one of these for both arrays and the new stream value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but this can only happen once StreamingValue
is able to handle both arrays (explode) and streamed values (e.g. range). Then we can use FirstOrDefaultStreamingValue
that expects an underlying StreamingValue
. please let me know if this is aligned with your thinking.
By the way, I think there is a different in the empty semantics between FirstOrDefaultStreamingValue
and FirstOrDefaultValue
. We can talk about it offline if you want.
@@ -460,6 +461,11 @@ message PFirstOrDefaultValue { | |||
optional PValue on_empty_result_value = 2; | |||
} | |||
|
|||
message PFirstOrDefaultStreamingValue { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Three options:
FirstOrDefaultStreamingValue
goes away with the suggested changes elsewhere,FirstOrDefaultStreamingValue
properly implements plan serialization together with puttingPFirstOrDefaultStreamingValue
intoPValue
FirstOrDefaultStreamingValue
does not implement plan serialization at all (but I think the constraint on the types will then fail to serialize, so properly two options only).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PFirstOrDefaultStreamingValue
already implements plan serialization, I forgot adding it as a possibility in PValue
. The code works because it is only used currently in DerivationProperty
.
I just added it to PValue
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but the derivation is then put into a constraint that is then put into the continuation. I am not sure why this worked.
...relational-api/src/main/java/com/apple/foundationdb/relational/api/exceptions/ErrorCode.java
Show resolved
Hide resolved
@@ -258,4 +258,9 @@ public void enumTest(YamlTest.Runner runner) throws Exception { | |||
public void uuidTest(YamlTest.Runner runner) throws Exception { | |||
runner.runYamsql("uuid.yamsql"); | |||
} | |||
|
|||
@TestTemplate | |||
public void tableFunctionsTest(YamlTest.Runner runner) throws Exception { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not really sure how this can pass with the plan serialization problem I think exists due to the FirstOrDefaultStreamingValue
problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the plan serialization problem?
the FirstOrDefaultStreamingValue
is not even serialized, it is only used in the DerivationProperty
to represent the semantics of the derivation.
@Nonnull final PRecordQueryTableFunctionPlan recordQueryTableFunctionPlanProto) { | ||
final var value = Value.fromValueProto(serializationContext, | ||
Objects.requireNonNull(recordQueryTableFunctionPlanProto.getValue())); | ||
if (!(value instanceof StreamingValue)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is guaranteed by the class definition to be a StreamingValue
. In the very remote case that we change that without afterthought I think a lot of things would break anyway so why not just cast it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is extra defensive, considering the proto
message is something sent from an external client.
Forgot one more thing: can you please update |
I don't think we should do that manually anymore. There should be a CI job that maintains this file. |
This provides support to table-valued-functions, it introduces the necessary framework support across different components: parsing, semantic analysis, plan generation, planning, and execution, to pre-defined table-valued functions.
Two pre-defined table-functions are introduced in this PR:
1.
values
function:Most major SQL vendors support this function, the relational engine now supports this function as well:
It also enables naming the resulting table along with its columns:
Notice how the value
4
in the first row's nested structureW.Y
was promoted to4.0
, this is because this function analyzes all the rows, and for each leaf in every (nested) tuple, it looks for a type that accommodates for all the individual values of the leaf across all rows (max type) and injects type promotions if necessary.Also note that the type aliasing works with nested array and structs, to some degrees addresses some of the issues reported in #3231, and in the future, the new logic can be leveraged to resolve that ticket as well.
2.
range
function:This is also a very common function, it returns a range between:
0
.The implementation of this TVF has streaming semantics. In other words, it does not pre-materialize all the values in the range, but rather return them on-demand.
With this PR, we can hopefully start adding more advanced TVFs easily.
This resolves #3282.