@@ -232,28 +232,52 @@ public <T, ACC> void registerFunction(
232232
233233 @ Override
234234 public <T > Table fromDataStream (DataStream <T > dataStream ) {
235- return fromDataStreamInternal (dataStream , null , null );
235+ return fromStreamInternal (dataStream , null , null , ChangelogMode . insertOnly () );
236236 }
237237
238238 @ Override
239239 public <T > Table fromDataStream (DataStream <T > dataStream , Schema schema ) {
240240 Preconditions .checkNotNull (schema , "Schema must not be null." );
241- return fromDataStreamInternal (dataStream , schema , null );
241+ return fromStreamInternal (dataStream , schema , null , ChangelogMode .insertOnly ());
242+ }
243+
244+ @ Override
245+ public Table fromChangelogStream (DataStream <Row > dataStream ) {
246+ return fromStreamInternal (dataStream , null , null , ChangelogMode .all ());
247+ }
248+
249+ @ Override
250+ public Table fromChangelogStream (DataStream <Row > dataStream , Schema schema ) {
251+ Preconditions .checkNotNull (schema , "Schema must not be null." );
252+ return fromStreamInternal (dataStream , schema , null , ChangelogMode .all ());
253+ }
254+
255+ @ Override
256+ public Table fromChangelogStream (
257+ DataStream <Row > dataStream , Schema schema , ChangelogMode changelogMode ) {
258+ Preconditions .checkNotNull (schema , "Schema must not be null." );
259+ return fromStreamInternal (dataStream , schema , null , changelogMode );
242260 }
243261
244262 @ Override
245263 public <T > void createTemporaryView (String path , DataStream <T > dataStream ) {
246- createTemporaryView (path , fromDataStreamInternal (dataStream , null , path ));
264+ createTemporaryView (
265+ path , fromStreamInternal (dataStream , null , path , ChangelogMode .insertOnly ()));
247266 }
248267
249268 @ Override
250269 public <T > void createTemporaryView (String path , DataStream <T > dataStream , Schema schema ) {
251- createTemporaryView (path , fromDataStreamInternal (dataStream , schema , path ));
270+ createTemporaryView (
271+ path , fromStreamInternal (dataStream , schema , path , ChangelogMode .insertOnly ()));
252272 }
253273
254- private <T > Table fromDataStreamInternal (
255- DataStream <T > dataStream , @ Nullable Schema schema , @ Nullable String viewPath ) {
274+ private <T > Table fromStreamInternal (
275+ DataStream <T > dataStream ,
276+ @ Nullable Schema schema ,
277+ @ Nullable String viewPath ,
278+ ChangelogMode changelogMode ) {
256279 Preconditions .checkNotNull (dataStream , "Data stream must not be null." );
280+ Preconditions .checkNotNull (changelogMode , "Changelog mode must not be null." );
257281 final CatalogManager catalogManager = getCatalogManager ();
258282 final SchemaResolver schemaResolver = catalogManager .getSchemaResolver ();
259283 final OperationTreeBuilder operationTreeBuilder = getOperationTreeBuilder ();
@@ -281,7 +305,7 @@ private <T> Table fromDataStreamInternal(
281305 dataStream ,
282306 schemaTranslationResult .getPhysicalDataType (),
283307 schemaTranslationResult .isTopLevelRecord (),
284- ChangelogMode . insertOnly () ,
308+ changelogMode ,
285309 resolvedSchema );
286310
287311 final List <String > projections = schemaTranslationResult .getProjections ();
@@ -324,28 +348,69 @@ public <T> DataStream<T> toDataStream(Table table, Class<T> targetClass) {
324348 public <T > DataStream <T > toDataStream (Table table , AbstractDataType <?> targetDataType ) {
325349 Preconditions .checkNotNull (table , "Table must not be null." );
326350 Preconditions .checkNotNull (targetDataType , "Target data type must not be null." );
327- final CatalogManager catalogManager = getCatalogManager ();
328- final SchemaResolver schemaResolver = catalogManager .getSchemaResolver ();
329- final OperationTreeBuilder operationTreeBuilder = getOperationTreeBuilder ();
330351
331352 final ExternalSchemaTranslator .OutputResult schemaTranslationResult =
332353 ExternalSchemaTranslator .fromInternal (
333- catalogManager .getDataTypeFactory (),
354+ getCatalogManager () .getDataTypeFactory (),
334355 table .getResolvedSchema (),
335356 targetDataType );
336357
337- final List <String > projections = schemaTranslationResult .getProjections ();
338- final QueryOperation projectOperation ;
339- if (projections == null ) {
340- projectOperation = table .getQueryOperation ();
341- } else {
342- projectOperation =
343- operationTreeBuilder .project (
344- projections .stream ()
345- .map (ApiExpressionUtils ::unresolvedRef )
346- .collect (Collectors .toList ()),
347- table .getQueryOperation ());
348- }
358+ return toStreamInternal (table , schemaTranslationResult , ChangelogMode .insertOnly ());
359+ }
360+
361+ @ Override
362+ public DataStream <Row > toChangelogStream (Table table ) {
363+ Preconditions .checkNotNull (table , "Table must not be null." );
364+
365+ final ExternalSchemaTranslator .OutputResult schemaTranslationResult =
366+ ExternalSchemaTranslator .fromInternal (table .getResolvedSchema (), null );
367+
368+ return toStreamInternal (table , schemaTranslationResult , null );
369+ }
370+
371+ @ Override
372+ public DataStream <Row > toChangelogStream (Table table , Schema targetSchema ) {
373+ Preconditions .checkNotNull (table , "Table must not be null." );
374+ Preconditions .checkNotNull (targetSchema , "Target schema must not be null." );
375+
376+ final ExternalSchemaTranslator .OutputResult schemaTranslationResult =
377+ ExternalSchemaTranslator .fromInternal (table .getResolvedSchema (), targetSchema );
378+
379+ return toStreamInternal (table , schemaTranslationResult , null );
380+ }
381+
382+ @ Override
383+ public DataStream <Row > toChangelogStream (
384+ Table table , Schema targetSchema , ChangelogMode changelogMode ) {
385+ Preconditions .checkNotNull (table , "Table must not be null." );
386+ Preconditions .checkNotNull (targetSchema , "Target schema must not be null." );
387+ Preconditions .checkNotNull (changelogMode , "Changelog mode must not be null." );
388+
389+ final ExternalSchemaTranslator .OutputResult schemaTranslationResult =
390+ ExternalSchemaTranslator .fromInternal (table .getResolvedSchema (), targetSchema );
391+
392+ return toStreamInternal (table , schemaTranslationResult , changelogMode );
393+ }
394+
395+ private <T > DataStream <T > toStreamInternal (
396+ Table table ,
397+ ExternalSchemaTranslator .OutputResult schemaTranslationResult ,
398+ @ Nullable ChangelogMode changelogMode ) {
399+ final CatalogManager catalogManager = getCatalogManager ();
400+ final SchemaResolver schemaResolver = catalogManager .getSchemaResolver ();
401+ final OperationTreeBuilder operationTreeBuilder = getOperationTreeBuilder ();
402+
403+ final QueryOperation projectOperation =
404+ schemaTranslationResult
405+ .getProjections ()
406+ .map (
407+ projections ->
408+ operationTreeBuilder .project (
409+ projections .stream ()
410+ .map (ApiExpressionUtils ::unresolvedRef )
411+ .collect (Collectors .toList ()),
412+ table .getQueryOperation ()))
413+ .orElseGet (table ::getQueryOperation );
349414
350415 final ResolvedSchema resolvedSchema =
351416 schemaResolver .resolve (schemaTranslationResult .getSchema ());
@@ -361,13 +426,15 @@ public <T> DataStream<T> toDataStream(Table table, AbstractDataType<?> targetDat
361426 objectIdentifier ,
362427 projectOperation ,
363428 resolvedSchema ,
364- ChangelogMode .insertOnly (),
365- schemaTranslationResult .getPhysicalDataType ());
429+ changelogMode ,
430+ schemaTranslationResult
431+ .getPhysicalDataType ()
432+ .orElseGet (resolvedSchema ::toPhysicalRowDataType ));
366433
367- return toDataStreamInternal (table , modifyOperation );
434+ return toStreamInternal (table , modifyOperation );
368435 }
369436
370- private <T > DataStream <T > toDataStreamInternal (Table table , ModifyOperation modifyOperation ) {
437+ private <T > DataStream <T > toStreamInternal (Table table , ModifyOperation modifyOperation ) {
371438 final List <Transformation <?>> transformations =
372439 planner .translate (Collections .singletonList (modifyOperation ));
373440
@@ -441,7 +508,7 @@ public <T> DataStream<T> toAppendStream(Table table, TypeInformation<T> typeInfo
441508 table .getQueryOperation (),
442509 TypeConversions .fromLegacyInfoToDataType (typeInfo ),
443510 OutputConversionModifyOperation .UpdateMode .APPEND );
444- return toDataStreamInternal (table , modifyOperation );
511+ return toStreamInternal (table , modifyOperation );
445512 }
446513
447514 @ Override
@@ -458,7 +525,7 @@ public <T> DataStream<Tuple2<Boolean, T>> toRetractStream(
458525 table .getQueryOperation (),
459526 wrapWithChangeFlag (typeInfo ),
460527 OutputConversionModifyOperation .UpdateMode .RETRACT );
461- return toDataStreamInternal (table , modifyOperation );
528+ return toStreamInternal (table , modifyOperation );
462529 }
463530
464531 @ Override
0 commit comments