Skip to content

Commit 4b7210d

Browse files
authored
[FLINK-38768][table] Materialized table fails with validation exception in case of LATERAL TABLE
1 parent b0b6669 commit 4b7210d

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

flink-table/flink-table-planner/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,23 @@
169169
* Default implementation of {@link SqlValidator}, the class was copied over because of
170170
* CALCITE-4554.
171171
*
172-
* <p>Lines 202 ~ 205, Flink improves error message for functions without appropriate arguments in
172+
* <p>Lines 207 ~ 210, Flink improves error message for functions without appropriate arguments in
173173
* handleUnresolvedFunction.
174174
*
175-
* <p>Lines 1270 ~ 1272, CALCITE-7217, should be removed after upgrading Calcite to 1.41.0.
175+
* <p>Lines 1275 ~ 1277, CALCITE-7217, should be removed after upgrading Calcite to 1.41.0.
176176
*
177-
* <p>Lines 2031 ~ 2045, Flink improves error message for functions without appropriate arguments in
177+
* <p>Lines 2036 ~ 2050, Flink improves error message for functions without appropriate arguments in
178178
* handleUnresolvedFunction at {@link SqlValidatorImpl#handleUnresolvedFunction}.
179179
*
180-
* <p>Lines 2571 ~ 2588, CALCITE-7217, should be removed after upgrading Calcite to 1.41.0.
180+
* <p>Lines 2576 ~ 2595, CALCITE-7217, CALCITE-7312 should be removed after upgrading Calcite to
181+
* 1.42.0.
181182
*
182-
* <p>Line 2618 ~2631, set the correct scope for VECTOR_SEARCH.
183+
* <p>Line 2626 ~2644, set the correct scope for VECTOR_SEARCH.
183184
*
184-
* <p>Lines 3920 ~ 3925, 6599 ~ 6606 Flink improves Optimize the retrieval of sub-operands in
185+
* <p>Lines 3923 ~ 3927, 6602 ~ 6608 Flink improves Optimize the retrieval of sub-operands in
185186
* SqlCall when using NamedParameters at {@link SqlValidatorImpl#checkRollUp}.
186187
*
187-
* <p>Lines 5340 ~ 5347, FLINK-24352 Add null check for temporal table check on SqlSnapshot.
188+
* <p>Lines 5343 ~ 5349, FLINK-24352 Add null check for temporal table check on SqlSnapshot.
188189
*/
189190
public class SqlValidatorImpl implements SqlValidatorWithHints {
190191
// ~ Static fields/initializers ---------------------------------------------
@@ -2578,16 +2579,18 @@ private SqlNode registerFrom(
25782579
// in order to make visible the left items
25792580
// of the JOIN tree.
25802581
scopes.put(node, usingScope);
2581-
registerFrom(
2582-
parentScope,
2583-
usingScope,
2584-
register,
2585-
((SqlCall) node).operand(0),
2586-
enclosingNode,
2587-
alias,
2588-
extendList,
2589-
forceNullable,
2590-
true);
2582+
newOperand =
2583+
registerFrom(
2584+
parentScope,
2585+
usingScope,
2586+
register,
2587+
((SqlCall) node).operand(0),
2588+
enclosingNode,
2589+
alias,
2590+
extendList,
2591+
forceNullable,
2592+
true);
2593+
sbc.setOperand(0, newOperand);
25912594
return sbc;
25922595
// ----- FLINK MODIFICATION END -----
25932596

flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/operations/SqlMaterializedTableNodeToOperationConverterTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,31 @@ void testCreateMaterializedTableWithUDTFQuery() {
311311
+ "LATERAL TABLE(`builtin`.`default`.`myFunc`(`b`)) AS `T` (`f1`, `f2`)");
312312
}
313313

314+
@Test
315+
void testCreateMaterializedTableWithUDTFQueryWithoutAlias() {
316+
functionCatalog.registerCatalogFunction(
317+
UnresolvedIdentifier.of(
318+
ObjectIdentifier.of(
319+
catalogManager.getCurrentCatalog(), "default", "myFunc")),
320+
FunctionDescriptor.forFunctionClass(TableFunc0.class).build(),
321+
true);
322+
323+
final String sql =
324+
"CREATE MATERIALIZED TABLE mtbl1 \n"
325+
+ "AS SELECT * FROM t1, LATERAL TABLE(myFunc(b))";
326+
Operation operation = parse(sql);
327+
assertThat(operation).isInstanceOf(CreateMaterializedTableOperation.class);
328+
329+
CreateMaterializedTableOperation createOperation =
330+
(CreateMaterializedTableOperation) operation;
331+
332+
assertThat(createOperation.getCatalogMaterializedTable().getExpandedQuery())
333+
.isEqualTo(
334+
"SELECT `t1`.`a`, `t1`.`b`, `t1`.`c`, `t1`.`d`, `EXPR$0`.`name`, `EXPR$0`.`age`\n"
335+
+ "FROM `builtin`.`default`.`t1` AS `t1`,\n"
336+
+ "LATERAL TABLE(`builtin`.`default`.`myFunc`(`b`)) AS `EXPR$0`");
337+
}
338+
314339
@Test
315340
void testContinuousRefreshMode() {
316341
// test continuous mode derived by specify freshness automatically

0 commit comments

Comments
 (0)