lvyanquan commented on code in PR #4227: URL: https://github.com/apache/flink-cdc/pull/4227#discussion_r2704025829
########## flink-cdc-runtime/src/main/java/org/apache/calcite/sql/type/SqlTypeFamily.java: ########## @@ -0,0 +1,287 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.calcite.sql.type; + +import org.apache.flink.shaded.guava31.com.google.common.collect.ImmutableList; +import org.apache.flink.shaded.guava31.com.google.common.collect.ImmutableMap; + +import org.apache.calcite.avatica.util.TimeUnit; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.rel.type.RelDataTypeFamily; +import org.apache.calcite.sql.SqlIntervalQualifier; +import org.apache.calcite.sql.SqlWindow; +import org.apache.calcite.sql.parser.SqlParserPos; + +import javax.annotation.Nullable; + +import java.sql.Types; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * SqlTypeFamily provides SQL type categorization. + * + * <p>The <em>primary</em> family categorization is a complete disjoint partitioning of SQL types + * into families, where two types are members of the same primary family iff instances of the two + * types can be the operands of an SQL equality predicate such as <code>WHERE v1 = v2</code>. + * Primary families are returned by RelDataType.getFamily(). + * + * <p>There is also a <em>secondary</em> family categorization which overlaps with the primary + * categorization. It is used in type strategies for more specific or more general categorization + * than the primary families. Secondary families are never returned by RelDataType.getFamily(). + * + * <p>This class was copied over from Calcite to support variant type(CALCITE-4918). When upgrading + * to Calcite 1.39.0 version, please remove the entire class. + * + * <p>changeList: 1. Add variant type: Line 84, Line 120, Line 218~219, Line 277~278. Review Comment: Done. ########## flink-cdc-composer/src/test/java/org/apache/flink/cdc/composer/flink/FlinkPipelineTransformITCase.java: ########## @@ -1137,6 +1137,96 @@ void testTransformWithTimestamps(String timezone) throws Exception { "DataChangeEvent{tableId=default_namespace.default_schema.mytable1, before=[], after=[3, null, null, null, null, null, null], op=INSERT, meta=()}"); } + /** This tests if transform variant functions works as expected. */ + @Test + void testTransformWithVariantFunctions() throws Exception { + FlinkPipelineComposer composer = FlinkPipelineComposer.ofMiniCluster(); + + // Setup value source + Configuration sourceConfig = new Configuration(); + sourceConfig.set( + ValuesDataSourceOptions.EVENT_SET_ID, + ValuesDataSourceHelper.EventSetId.CUSTOM_SOURCE_EVENTS); + + TableId myTable = TableId.tableId("default_namespace", "default_schema", "mytable1"); + Schema tableSchema = + Schema.newBuilder() + .physicalColumn("id", DataTypes.INT().notNull()) + .physicalColumn("message", DataTypes.STRING()) + .primaryKey("id") + .build(); + + BinaryRecordDataGenerator generator = + new BinaryRecordDataGenerator( + tableSchema.getColumnDataTypes().toArray(new DataType[0])); + List<Event> events = + Arrays.asList( + new CreateTableEvent(myTable, tableSchema), + DataChangeEvent.insertEvent( + myTable, + generator.generate( + new Object[] { + 1, + BinaryStringData.fromString( + "{\"name\":\"张三\",\"age\":30,\"is_active\":true,\"email\":\"[email protected]\",\"hobbies\":[\"reading\",\"coding\",\"traveling\"],\"address\":{\"street\":\"MainSt\",\"city\":\"Beijing\",\"zip\":\"100000\"}}"), + })), + DataChangeEvent.insertEvent( + myTable, + generator.generate( + new Object[] { + 2, + BinaryStringData.fromString( + "{\"name\":\"李四\",\"age\":40,\"is_active\":true,\"email\":\"[email protected]\",\"hobbies\":[\"reading\",\"coding\",\"traveling\"],\"address\":{\"street\":\"MainSt\",\"city\":\"Beijing\",\"zip\":\"100000\"}}"), + })), + DataChangeEvent.insertEvent( + myTable, generator.generate(new Object[] {3, null}))); + + ValuesDataSourceHelper.setSourceEvents(Collections.singletonList(events)); + + SourceDef sourceDef = + new SourceDef(ValuesDataFactory.IDENTIFIER, "Value Source", sourceConfig); + + // Setup value sink + Configuration sinkConfig = new Configuration(); + sinkConfig.set(ValuesDataSinkOptions.MATERIALIZED_IN_MEMORY, true); + SinkDef sinkDef = new SinkDef(ValuesDataFactory.IDENTIFIER, "Value Sink", sinkConfig); + + // Setup pipeline + Configuration pipelineConfig = new Configuration(); + pipelineConfig.set(PipelineOptions.PIPELINE_PARALLELISM, 1); + PipelineDef pipelineDef = + new PipelineDef( + sourceDef, + sinkDef, + Collections.emptyList(), + Collections.singletonList( + new TransformDef( + "default_namespace.default_schema.\\.*", + "id, parse_json(message) as variantVal", Review Comment: Done. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
