Unverified Commit bc09f8fc authored by aiwenmo's avatar aiwenmo Committed by GitHub

[Feature-1842][client] Analyze lineage from the original relNode, not the…

[Feature-1842][client] Analyze lineage from the original relNode, not the optimized relNode, and add unit tests (#2488) * [Feature-1842][client] Analyze lineage from the original relNode, not the optimized relNode, and add unit tests * spotless --------- Co-authored-by: 's avatarwenmo <32723967+wenmo@users.noreply.github.com>
parent 2b67317c
......@@ -65,6 +65,11 @@
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
/*
*
* 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.rel.metadata;
import org.apache.calcite.plan.RelOptTable;
/**
* Modified based on calcite's source code org.apache.calcite.rel.metadata.RelColumnOrigin
* <p>
* Modification point:
* <ol>
* <li>add transform field and related code.
* </ol>
*
* @description: RelColumnOrigin is a data structure describing one of the origins of an
* output column produced by a relational expression.
* @author: HamaWhite
*/
public class RelColumnOrigin {
// ~ Instance fields --------------------------------------------------------
private final RelOptTable originTable;
private final int iOriginColumn;
private final boolean isDerived;
/**
* Stores the expression for data conversion,
* which source table fields are transformed by which expression the target field
*/
private String transform;
// ~ Constructors -----------------------------------------------------------
public RelColumnOrigin(RelOptTable originTable, int iOriginColumn, boolean isDerived) {
this.originTable = originTable;
this.iOriginColumn = iOriginColumn;
this.isDerived = isDerived;
}
public RelColumnOrigin(RelOptTable originTable, int iOriginColumn, boolean isDerived, String transform) {
this.originTable = originTable;
this.iOriginColumn = iOriginColumn;
this.isDerived = isDerived;
this.transform = transform;
}
// ~ Methods ----------------------------------------------------------------
/**
* Returns table of origin.
*/
public RelOptTable getOriginTable() {
return originTable;
}
/**
* Returns the 0-based index of column in origin table; whether this ordinal
* is flattened or unflattened depends on whether UDT flattening has already
* been performed on the relational expression which produced this
* description.
*/
public int getOriginColumnOrdinal() {
return iOriginColumn;
}
/**
* Consider the query <code>select a+b as c, d as e from t</code>. The
* output column c has two origins (a and b), both of them derived. The
* output column d as one origin (c), which is not derived.
*
* @return false if value taken directly from column in origin table; true
* otherwise
*/
public boolean isDerived() {
return isDerived;
}
public String getTransform() {
return transform;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof RelColumnOrigin)) {
return false;
}
RelColumnOrigin other = (RelColumnOrigin) obj;
return originTable.getQualifiedName().equals(other.originTable.getQualifiedName())
&& (iOriginColumn == other.iOriginColumn)
&& (isDerived == other.isDerived);
}
@Override
public int hashCode() {
return originTable.getQualifiedName().hashCode() + iOriginColumn + (isDerived ? 313 : 0);
}
}
......@@ -22,7 +22,6 @@ package org.dinky.executor;
import org.dinky.assertion.Asserts;
import org.dinky.data.model.LineageRel;
import org.dinky.data.result.SqlExplainResult;
import org.dinky.utils.FlinkStreamProgramWithoutPhysical;
import org.dinky.utils.LineageContext;
import org.apache.flink.api.common.RuntimeExecutionMode;
......@@ -68,7 +67,6 @@ import org.apache.flink.table.operations.command.SetOperation;
import org.apache.flink.table.operations.ddl.CreateTableASOperation;
import org.apache.flink.table.operations.ddl.CreateTableOperation;
import org.apache.flink.table.planner.delegation.DefaultExecutor;
import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram;
import org.apache.flink.table.typeutils.FieldInfoUtils;
import org.apache.flink.types.Row;
......@@ -94,8 +92,6 @@ import cn.hutool.core.util.ReflectUtil;
*/
public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
private final FlinkChainedProgram flinkChainedProgram;
public CustomTableEnvironmentImpl(
CatalogManager catalogManager,
ModuleManager moduleManager,
......@@ -117,8 +113,6 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
isStreamingMode,
userClassLoader));
this.executor = executor;
this.flinkChainedProgram =
FlinkStreamProgramWithoutPhysical.buildProgram((Configuration) executionEnvironment.getConfiguration());
}
public static CustomTableEnvironmentImpl create(StreamExecutionEnvironment executionEnvironment) {
......@@ -365,8 +359,7 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
@Override
public List<LineageRel> getLineage(String statement) {
LineageContext lineageContext =
new LineageContext(flinkChainedProgram, (TableEnvironmentImpl) streamTableEnvironment);
LineageContext lineageContext = new LineageContext((TableEnvironmentImpl) streamTableEnvironment);
return lineageContext.getLineage(statement);
}
......
......@@ -27,22 +27,13 @@ import org.apache.calcite.rel.metadata.RelColumnOrigin;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.commons.collections.CollectionUtils;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.table.api.TableConfig;
import org.apache.flink.table.api.TableException;
import org.apache.flink.table.api.ValidationException;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import org.apache.flink.table.catalog.CatalogManager;
import org.apache.flink.table.catalog.FunctionCatalog;
import org.apache.flink.table.operations.CatalogSinkModifyOperation;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.planner.calcite.FlinkRelBuilder;
import org.apache.flink.table.planner.calcite.SqlExprToRexConverterFactory;
import org.apache.flink.table.planner.delegation.PlannerBase;
import org.apache.flink.table.planner.operations.PlannerQueryOperation;
import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram;
import org.apache.flink.table.planner.plan.optimize.program.StreamOptimizeContext;
import org.apache.flink.table.planner.plan.schema.TableSourceTable;
import org.apache.flink.table.planner.plan.trait.MiniBatchInterval;
import java.util.ArrayList;
import java.util.List;
......@@ -55,11 +46,9 @@ import java.util.Set;
*/
public class LineageContext {
private final FlinkChainedProgram flinkChainedProgram;
private final TableEnvironmentImpl tableEnv;
public LineageContext(FlinkChainedProgram flinkChainedProgram, TableEnvironmentImpl tableEnv) {
this.flinkChainedProgram = flinkChainedProgram;
public LineageContext(TableEnvironmentImpl tableEnv) {
this.tableEnv = tableEnv;
}
......@@ -69,11 +58,8 @@ public class LineageContext {
String sinkTable = parsed.getField(0);
RelNode oriRelNode = parsed.getField(1);
// 2. Optimize original relNode to generate Optimized Logical Plan
RelNode optRelNode = optimize(oriRelNode);
// 3. Build lineage based from RelMetadataQuery
return buildFiledLineageResult(sinkTable, optRelNode);
// 2. Build lineage based from RelMetadataQuery
return buildFiledLineageResult(sinkTable, oriRelNode);
}
private Tuple2<String, RelNode> parseStatement(String sql) {
......@@ -94,66 +80,6 @@ public class LineageContext {
}
}
/** Calling each program's optimize method in sequence. */
private RelNode optimize(RelNode relNode) {
return flinkChainedProgram.optimize(relNode, new StreamOptimizeContext() {
@Override
public boolean isBatchMode() {
return false;
}
@Override
public TableConfig getTableConfig() {
return tableEnv.getConfig();
}
@Override
public FunctionCatalog getFunctionCatalog() {
return getPlanner().getFlinkContext().getFunctionCatalog();
}
@Override
public CatalogManager getCatalogManager() {
return tableEnv.getCatalogManager();
}
@Override
public SqlExprToRexConverterFactory getSqlExprToRexConverterFactory() {
return getPlanner().getFlinkContext().getSqlExprToRexConverterFactory();
}
@Override
public <C> C unwrap(Class<C> clazz) {
return getPlanner().getFlinkContext().unwrap(clazz);
}
@Override
public FlinkRelBuilder getFlinkRelBuilder() {
return getPlanner().getRelBuilder();
}
@Override
public boolean needFinalTimeIndicatorConversion() {
return true;
}
@Override
public boolean isUpdateBeforeRequired() {
return false;
}
@Override
public MiniBatchInterval getMiniBatchInterval() {
return MiniBatchInterval.NONE;
}
private PlannerBase getPlanner() {
return (PlannerBase) tableEnv.getPlanner();
}
});
}
/** Check the size of query and sink fields match */
private void validateSchema(String sinkTable, RelNode relNode, List<String> sinkFieldList) {
List<String> queryFieldList = relNode.getRowType().getFieldNames();
......@@ -197,7 +123,8 @@ public class LineageContext {
String sourceColumn = fieldNames.get(ordinal);
// add record
resultList.add(LineageRel.build(sourceTable, sourceColumn, sinkTable, targetColumn));
resultList.add(LineageRel.build(
sourceTable, sourceColumn, sinkTable, targetColumn, relColumnOrigin.getTransform()));
}
}
}
......
/*
*
* 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.dinky.utils;
import static org.junit.Assert.assertEquals;
import org.dinky.data.model.LineageRel;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @description: LineageContextTest
* @author: HamaWhite
*/
public class LineageContextTest {
private static TableEnvironmentImpl tableEnv;
private static LineageContext context;
@BeforeClass
public static void setUp() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment(new Configuration());
EnvironmentSettings settings =
EnvironmentSettings.newInstance().inStreamingMode().build();
tableEnv = (TableEnvironmentImpl) StreamTableEnvironment.create(env, settings);
context = new LineageContext(tableEnv);
}
@Before
public void init() {
// create table ST
tableEnv.executeSql("DROP TABLE IF EXISTS ST");
tableEnv.executeSql("CREATE TABLE ST ( " + " a STRING ,"
+ " b STRING ,"
+ " c STRING "
+ ") WITH ( "
+ " 'connector' = 'datagen' ,"
+ " 'rows-per-second' = '1' "
+ ")");
// create table TT
tableEnv.executeSql("DROP TABLE IF EXISTS TT");
tableEnv.executeSql("CREATE TABLE TT ( " + " A STRING ,"
+ " B STRING "
+ ") WITH ( "
+ " 'connector' = 'print' "
+ ")");
}
@Test
public void testGetLineage() {
List<LineageRel> actualList = context.getLineage("INSERT INTO TT select a||c A ,b||c B from ST");
String[][] expectedArray = {
{"ST", "a", "TT", "A", "||(a, c)"},
{"ST", "c", "TT", "A", "||(a, c)"},
{"ST", "b", "TT", "B", "||(b, c)"},
{"ST", "c", "TT", "B", "||(b, c)"}
};
List<LineageRel> expectedList = buildResult(expectedArray);
assertEquals(expectedList, actualList);
}
private List<LineageRel> buildResult(String[][] expectedArray) {
return Stream.of(expectedArray)
.map(e -> {
String transform = e.length == 5 ? e[4] : null;
return new LineageRel(
"default_catalog",
"default_database",
e[0],
e[1],
"default_catalog",
"default_database",
e[2],
e[3],
transform);
})
.collect(Collectors.toList());
}
}
......@@ -67,6 +67,11 @@
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
/*
*
* 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.rel.metadata;
import org.apache.calcite.plan.RelOptTable;
/**
* Modified based on calcite's source code org.apache.calcite.rel.metadata.RelColumnOrigin
* <p>
* Modification point:
* <ol>
* <li>add transform field and related code.
* </ol>
*
* @description: RelColumnOrigin is a data structure describing one of the origins of an
* output column produced by a relational expression.
* @author: HamaWhite
*/
public class RelColumnOrigin {
// ~ Instance fields --------------------------------------------------------
private final RelOptTable originTable;
private final int iOriginColumn;
private final boolean isDerived;
/**
* Stores the expression for data conversion,
* which source table fields are transformed by which expression the target field
*/
private String transform;
// ~ Constructors -----------------------------------------------------------
public RelColumnOrigin(RelOptTable originTable, int iOriginColumn, boolean isDerived) {
this.originTable = originTable;
this.iOriginColumn = iOriginColumn;
this.isDerived = isDerived;
}
public RelColumnOrigin(RelOptTable originTable, int iOriginColumn, boolean isDerived, String transform) {
this.originTable = originTable;
this.iOriginColumn = iOriginColumn;
this.isDerived = isDerived;
this.transform = transform;
}
// ~ Methods ----------------------------------------------------------------
/**
* Returns table of origin.
*/
public RelOptTable getOriginTable() {
return originTable;
}
/**
* Returns the 0-based index of column in origin table; whether this ordinal
* is flattened or unflattened depends on whether UDT flattening has already
* been performed on the relational expression which produced this
* description.
*/
public int getOriginColumnOrdinal() {
return iOriginColumn;
}
/**
* Consider the query <code>select a+b as c, d as e from t</code>. The
* output column c has two origins (a and b), both of them derived. The
* output column d as one origin (c), which is not derived.
*
* @return false if value taken directly from column in origin table; true
* otherwise
*/
public boolean isDerived() {
return isDerived;
}
public String getTransform() {
return transform;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof RelColumnOrigin)) {
return false;
}
RelColumnOrigin other = (RelColumnOrigin) obj;
return originTable.getQualifiedName().equals(other.originTable.getQualifiedName())
&& (iOriginColumn == other.iOriginColumn)
&& (isDerived == other.isDerived);
}
@Override
public int hashCode() {
return originTable.getQualifiedName().hashCode() + iOriginColumn + (isDerived ? 313 : 0);
}
}
......@@ -24,7 +24,6 @@ import static org.apache.flink.table.api.bridge.internal.AbstractStreamTableEnvi
import org.dinky.assertion.Asserts;
import org.dinky.data.model.LineageRel;
import org.dinky.data.result.SqlExplainResult;
import org.dinky.utils.FlinkStreamProgramWithoutPhysical;
import org.dinky.utils.LineageContext;
import org.apache.flink.api.common.RuntimeExecutionMode;
......@@ -64,7 +63,6 @@ import org.apache.flink.table.operations.command.ResetOperation;
import org.apache.flink.table.operations.command.SetOperation;
import org.apache.flink.table.operations.ddl.CreateTableASOperation;
import org.apache.flink.table.operations.ddl.CreateTableOperation;
import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram;
import org.apache.flink.types.Row;
import java.util.ArrayList;
......@@ -85,8 +83,6 @@ import cn.hutool.core.collection.CollUtil;
*/
public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
private final FlinkChainedProgram flinkChainedProgram;
public CustomTableEnvironmentImpl(
CatalogManager catalogManager,
ModuleManager moduleManager,
......@@ -107,8 +103,6 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
executor,
isStreamingMode,
userClassLoader));
this.flinkChainedProgram = FlinkStreamProgramWithoutPhysical.buildProgram(
(Configuration) getStreamExecutionEnvironment().getConfiguration());
}
public static CustomTableEnvironmentImpl create(StreamExecutionEnvironment executionEnvironment) {
......@@ -324,8 +318,7 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
@Override
public List<LineageRel> getLineage(String statement) {
LineageContext lineageContext =
new LineageContext(flinkChainedProgram, (TableEnvironmentImpl) streamTableEnvironment);
LineageContext lineageContext = new LineageContext((TableEnvironmentImpl) streamTableEnvironment);
return lineageContext.getLineage(statement);
}
......
......@@ -27,23 +27,13 @@ import org.apache.calcite.rel.metadata.RelColumnOrigin;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.commons.collections.CollectionUtils;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.table.api.TableConfig;
import org.apache.flink.table.api.TableException;
import org.apache.flink.table.api.ValidationException;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import org.apache.flink.table.catalog.CatalogManager;
import org.apache.flink.table.catalog.FunctionCatalog;
import org.apache.flink.table.module.ModuleManager;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.operations.SinkModifyOperation;
import org.apache.flink.table.planner.calcite.FlinkRelBuilder;
import org.apache.flink.table.planner.calcite.SqlExprToRexConverterFactory;
import org.apache.flink.table.planner.delegation.PlannerBase;
import org.apache.flink.table.planner.operations.PlannerQueryOperation;
import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram;
import org.apache.flink.table.planner.plan.optimize.program.StreamOptimizeContext;
import org.apache.flink.table.planner.plan.schema.TableSourceTable;
import org.apache.flink.table.planner.plan.trait.MiniBatchInterval;
import java.util.ArrayList;
import java.util.List;
......@@ -56,11 +46,9 @@ import java.util.Set;
*/
public class LineageContext {
private final FlinkChainedProgram flinkChainedProgram;
private final TableEnvironmentImpl tableEnv;
public LineageContext(FlinkChainedProgram flinkChainedProgram, TableEnvironmentImpl tableEnv) {
this.flinkChainedProgram = flinkChainedProgram;
public LineageContext(TableEnvironmentImpl tableEnv) {
this.tableEnv = tableEnv;
}
......@@ -70,11 +58,8 @@ public class LineageContext {
String sinkTable = parsed.getField(0);
RelNode oriRelNode = parsed.getField(1);
// 2. Optimize original relNode to generate Optimized Logical Plan
RelNode optRelNode = optimize(oriRelNode);
// 3. Build lineage based from RelMetadataQuery
return buildFiledLineageResult(sinkTable, optRelNode);
// 2. Build lineage based from RelMetadataQuery
return buildFiledLineageResult(sinkTable, oriRelNode);
}
private Tuple2<String, RelNode> parseStatement(String sql) {
......@@ -96,76 +81,6 @@ public class LineageContext {
}
}
/** Calling each program's optimize method in sequence. */
private RelNode optimize(RelNode relNode) {
return flinkChainedProgram.optimize(relNode, new StreamOptimizeContext() {
@Override
public boolean isBatchMode() {
return false;
}
@Override
public TableConfig getTableConfig() {
return tableEnv.getConfig();
}
@Override
public FunctionCatalog getFunctionCatalog() {
return getPlanner().getFlinkContext().getFunctionCatalog();
}
@Override
public CatalogManager getCatalogManager() {
return tableEnv.getCatalogManager();
}
@Override
public ModuleManager getModuleManager() {
return getPlanner().getFlinkContext().getModuleManager();
}
@Override
public SqlExprToRexConverterFactory getSqlExprToRexConverterFactory() {
return null;
}
@Override
public <C> C unwrap(Class<C> clazz) {
return getPlanner().getFlinkContext().unwrap(clazz);
}
@Override
public FlinkRelBuilder getFlinkRelBuilder() {
return getPlanner().getRelBuilder();
}
@Override
public boolean isUpdateBeforeRequired() {
return false;
}
@Override
public MiniBatchInterval getMiniBatchInterval() {
return MiniBatchInterval.NONE;
}
@Override
public boolean needFinalTimeIndicatorConversion() {
return true;
}
@Override
public ClassLoader getClassLoader() {
return getPlanner().getFlinkContext().getClassLoader();
}
private PlannerBase getPlanner() {
return (PlannerBase) tableEnv.getPlanner();
}
});
}
/** Check the size of query and sink fields match */
private void validateSchema(String sinkTable, RelNode relNode, List<String> sinkFieldList) {
List<String> queryFieldList = relNode.getRowType().getFieldNames();
......@@ -209,7 +124,8 @@ public class LineageContext {
String sourceColumn = fieldNames.get(ordinal);
// add record
resultList.add(LineageRel.build(sourceTable, sourceColumn, sinkTable, targetColumn));
resultList.add(LineageRel.build(
sourceTable, sourceColumn, sinkTable, targetColumn, relColumnOrigin.getTransform()));
}
}
}
......
/*
*
* 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.dinky.utils;
import static org.junit.Assert.assertEquals;
import org.dinky.data.model.LineageRel;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @description: LineageContextTest
* @author: HamaWhite
*/
public class LineageContextTest {
private static TableEnvironmentImpl tableEnv;
private static LineageContext context;
@BeforeClass
public static void setUp() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment(new Configuration());
EnvironmentSettings settings =
EnvironmentSettings.newInstance().inStreamingMode().build();
tableEnv = (TableEnvironmentImpl) StreamTableEnvironment.create(env, settings);
context = new LineageContext(tableEnv);
}
@Before
public void init() {
// create table ST
tableEnv.executeSql("DROP TABLE IF EXISTS ST");
tableEnv.executeSql("CREATE TABLE ST ( " + " a STRING ,"
+ " b STRING ,"
+ " c STRING "
+ ") WITH ( "
+ " 'connector' = 'datagen' ,"
+ " 'rows-per-second' = '1' "
+ ")");
// create table TT
tableEnv.executeSql("DROP TABLE IF EXISTS TT");
tableEnv.executeSql("CREATE TABLE TT ( " + " A STRING ,"
+ " B STRING "
+ ") WITH ( "
+ " 'connector' = 'print' "
+ ")");
}
@Test
public void testGetLineage() {
List<LineageRel> actualList = context.getLineage("INSERT INTO TT select a||c A ,b||c B from ST");
String[][] expectedArray = {
{"ST", "a", "TT", "A", "||(a, c)"},
{"ST", "c", "TT", "A", "||(a, c)"},
{"ST", "b", "TT", "B", "||(b, c)"},
{"ST", "c", "TT", "B", "||(b, c)"}
};
List<LineageRel> expectedList = buildResult(expectedArray);
assertEquals(expectedList, actualList);
}
private List<LineageRel> buildResult(String[][] expectedArray) {
return Stream.of(expectedArray)
.map(e -> {
String transform = e.length == 5 ? e[4] : null;
return new LineageRel(
"default_catalog",
"default_database",
e[0],
e[1],
"default_catalog",
"default_database",
e[2],
e[3],
transform);
})
.collect(Collectors.toList());
}
}
......@@ -41,6 +41,11 @@
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
......
/*
*
* 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.rel.metadata;
import org.apache.calcite.plan.RelOptTable;
/**
* Modified based on calcite's source code org.apache.calcite.rel.metadata.RelColumnOrigin
* <p>
* Modification point:
* <ol>
* <li>add transform field and related code.
* </ol>
*
* @description: RelColumnOrigin is a data structure describing one of the origins of an
* output column produced by a relational expression.
* @author: HamaWhite
*/
public class RelColumnOrigin {
// ~ Instance fields --------------------------------------------------------
private final RelOptTable originTable;
private final int iOriginColumn;
private final boolean isDerived;
/**
* Stores the expression for data conversion,
* which source table fields are transformed by which expression the target field
*/
private String transform;
// ~ Constructors -----------------------------------------------------------
public RelColumnOrigin(RelOptTable originTable, int iOriginColumn, boolean isDerived) {
this.originTable = originTable;
this.iOriginColumn = iOriginColumn;
this.isDerived = isDerived;
}
public RelColumnOrigin(RelOptTable originTable, int iOriginColumn, boolean isDerived, String transform) {
this.originTable = originTable;
this.iOriginColumn = iOriginColumn;
this.isDerived = isDerived;
this.transform = transform;
}
// ~ Methods ----------------------------------------------------------------
/**
* Returns table of origin.
*/
public RelOptTable getOriginTable() {
return originTable;
}
/**
* Returns the 0-based index of column in origin table; whether this ordinal
* is flattened or unflattened depends on whether UDT flattening has already
* been performed on the relational expression which produced this
* description.
*/
public int getOriginColumnOrdinal() {
return iOriginColumn;
}
/**
* Consider the query <code>select a+b as c, d as e from t</code>. The
* output column c has two origins (a and b), both of them derived. The
* output column d as one origin (c), which is not derived.
*
* @return false if value taken directly from column in origin table; true
* otherwise
*/
public boolean isDerived() {
return isDerived;
}
public String getTransform() {
return transform;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof RelColumnOrigin)) {
return false;
}
RelColumnOrigin other = (RelColumnOrigin) obj;
return originTable.getQualifiedName().equals(other.originTable.getQualifiedName())
&& (iOriginColumn == other.iOriginColumn)
&& (isDerived == other.isDerived);
}
@Override
public int hashCode() {
return originTable.getQualifiedName().hashCode() + iOriginColumn + (isDerived ? 313 : 0);
}
}
......@@ -23,7 +23,6 @@ import org.dinky.assertion.Asserts;
import org.dinky.context.DinkyClassLoaderContextHolder;
import org.dinky.data.model.LineageRel;
import org.dinky.data.result.SqlExplainResult;
import org.dinky.utils.FlinkStreamProgramWithoutPhysical;
import org.dinky.utils.LineageContext;
import org.apache.flink.api.dag.Transformation;
......@@ -50,7 +49,6 @@ import org.apache.flink.table.operations.SinkModifyOperation;
import org.apache.flink.table.operations.command.ResetOperation;
import org.apache.flink.table.operations.command.SetOperation;
import org.apache.flink.table.operations.ddl.CreateTableOperation;
import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram;
import org.apache.flink.types.Row;
import java.util.ArrayList;
......@@ -78,13 +76,10 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
private static final Logger log = LoggerFactory.getLogger(CustomTableEnvironmentImpl.class);
private final FlinkChainedProgram flinkChainedProgram;
private static final ObjectMapper mapper = new ObjectMapper();
public CustomTableEnvironmentImpl(StreamTableEnvironment streamTableEnvironment) {
super(streamTableEnvironment);
this.flinkChainedProgram = FlinkStreamProgramWithoutPhysical.buildProgram(
(Configuration) getStreamExecutionEnvironment().getConfiguration());
}
public static CustomTableEnvironmentImpl create(StreamExecutionEnvironment executionEnvironment) {
......@@ -260,8 +255,7 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
@Override
public List<LineageRel> getLineage(String statement) {
LineageContext lineageContext =
new LineageContext(flinkChainedProgram, (TableEnvironmentImpl) streamTableEnvironment);
LineageContext lineageContext = new LineageContext((TableEnvironmentImpl) streamTableEnvironment);
return lineageContext.getLineage(statement);
}
......
......@@ -27,23 +27,13 @@ import org.apache.calcite.rel.metadata.RelColumnOrigin;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.commons.collections.CollectionUtils;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.table.api.TableConfig;
import org.apache.flink.table.api.TableException;
import org.apache.flink.table.api.ValidationException;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import org.apache.flink.table.catalog.CatalogManager;
import org.apache.flink.table.catalog.FunctionCatalog;
import org.apache.flink.table.module.ModuleManager;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.operations.SinkModifyOperation;
import org.apache.flink.table.planner.calcite.FlinkRelBuilder;
import org.apache.flink.table.planner.calcite.RexFactory;
import org.apache.flink.table.planner.delegation.PlannerBase;
import org.apache.flink.table.planner.operations.PlannerQueryOperation;
import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram;
import org.apache.flink.table.planner.plan.optimize.program.StreamOptimizeContext;
import org.apache.flink.table.planner.plan.schema.TableSourceTable;
import org.apache.flink.table.planner.plan.trait.MiniBatchInterval;
import java.util.ArrayList;
import java.util.List;
......@@ -56,11 +46,9 @@ import java.util.Set;
*/
public class LineageContext {
private final FlinkChainedProgram flinkChainedProgram;
private final TableEnvironmentImpl tableEnv;
public LineageContext(FlinkChainedProgram flinkChainedProgram, TableEnvironmentImpl tableEnv) {
this.flinkChainedProgram = flinkChainedProgram;
public LineageContext(TableEnvironmentImpl tableEnv) {
this.tableEnv = tableEnv;
}
......@@ -70,11 +58,8 @@ public class LineageContext {
String sinkTable = parsed.getField(0);
RelNode oriRelNode = parsed.getField(1);
// 2. Optimize original relNode to generate Optimized Logical Plan
RelNode optRelNode = optimize(oriRelNode);
// 3. Build lineage based from RelMetadataQuery
return buildFiledLineageResult(sinkTable, optRelNode);
// 2. Build lineage based from RelMetadataQuery
return buildFiledLineageResult(sinkTable, oriRelNode);
}
private Tuple2<String, RelNode> parseStatement(String sql) {
......@@ -96,71 +81,6 @@ public class LineageContext {
}
}
/** Calling each program's optimize method in sequence. */
private RelNode optimize(RelNode relNode) {
return flinkChainedProgram.optimize(relNode, new StreamOptimizeContext() {
@Override
public boolean isBatchMode() {
return false;
}
@Override
public TableConfig getTableConfig() {
return tableEnv.getConfig();
}
@Override
public FunctionCatalog getFunctionCatalog() {
return getPlanner().getFlinkContext().getFunctionCatalog();
}
@Override
public CatalogManager getCatalogManager() {
return tableEnv.getCatalogManager();
}
@Override
public ModuleManager getModuleManager() {
return getPlanner().getFlinkContext().getModuleManager();
}
@Override
public RexFactory getRexFactory() {
return getPlanner().getFlinkContext().getRexFactory();
}
@Override
public FlinkRelBuilder getFlinkRelBuilder() {
return getPlanner().createRelBuilder();
}
@Override
public boolean isUpdateBeforeRequired() {
return false;
}
@Override
public MiniBatchInterval getMiniBatchInterval() {
return MiniBatchInterval.NONE;
}
@Override
public boolean needFinalTimeIndicatorConversion() {
return true;
}
@Override
public ClassLoader getClassLoader() {
return getPlanner().getFlinkContext().getClassLoader();
}
private PlannerBase getPlanner() {
return (PlannerBase) tableEnv.getPlanner();
}
});
}
/** Check the size of query and sink fields match */
private void validateSchema(String sinkTable, RelNode relNode, List<String> sinkFieldList) {
List<String> queryFieldList = relNode.getRowType().getFieldNames();
......@@ -204,7 +124,8 @@ public class LineageContext {
String sourceColumn = fieldNames.get(ordinal);
// add record
resultList.add(LineageRel.build(sourceTable, sourceColumn, sinkTable, targetColumn));
resultList.add(LineageRel.build(
sourceTable, sourceColumn, sinkTable, targetColumn, relColumnOrigin.getTransform()));
}
}
}
......
/*
*
* 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.dinky.utils;
import static org.junit.Assert.assertEquals;
import org.dinky.data.model.LineageRel;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @description: LineageContextTest
* @author: HamaWhite
*/
public class LineageContextTest {
private static TableEnvironmentImpl tableEnv;
private static LineageContext context;
@BeforeClass
public static void setUp() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment(new Configuration());
EnvironmentSettings settings =
EnvironmentSettings.newInstance().inStreamingMode().build();
tableEnv = (TableEnvironmentImpl) StreamTableEnvironment.create(env, settings);
context = new LineageContext(tableEnv);
}
@Before
public void init() {
// create table ST
tableEnv.executeSql("DROP TABLE IF EXISTS ST");
tableEnv.executeSql("CREATE TABLE ST ( " + " a STRING ,"
+ " b STRING ,"
+ " c STRING "
+ ") WITH ( "
+ " 'connector' = 'datagen' ,"
+ " 'rows-per-second' = '1' "
+ ")");
// create table TT
tableEnv.executeSql("DROP TABLE IF EXISTS TT");
tableEnv.executeSql("CREATE TABLE TT ( " + " A STRING ,"
+ " B STRING "
+ ") WITH ( "
+ " 'connector' = 'print' "
+ ")");
}
@Test
public void testGetLineage() {
List<LineageRel> actualList = context.getLineage("INSERT INTO TT select a||c A ,b||c B from ST");
String[][] expectedArray = {
{"ST", "a", "TT", "A", "||(a, c)"},
{"ST", "c", "TT", "A", "||(a, c)"},
{"ST", "b", "TT", "B", "||(b, c)"},
{"ST", "c", "TT", "B", "||(b, c)"}
};
List<LineageRel> expectedList = buildResult(expectedArray);
assertEquals(expectedList, actualList);
}
private List<LineageRel> buildResult(String[][] expectedArray) {
return Stream.of(expectedArray)
.map(e -> {
String transform = e.length == 5 ? e[4] : null;
return new LineageRel(
"default_catalog",
"default_database",
e[0],
e[1],
"default_catalog",
"default_database",
e[2],
e[3],
transform);
})
.collect(Collectors.toList());
}
}
......@@ -41,6 +41,11 @@
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
......
/*
*
* 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.rel.metadata;
import org.apache.calcite.plan.RelOptTable;
/**
* Modified based on calcite's source code org.apache.calcite.rel.metadata.RelColumnOrigin
* <p>
* Modification point:
* <ol>
* <li>add transform field and related code.
* </ol>
*
* @description: RelColumnOrigin is a data structure describing one of the origins of an
* output column produced by a relational expression.
* @author: HamaWhite
*/
public class RelColumnOrigin {
// ~ Instance fields --------------------------------------------------------
private final RelOptTable originTable;
private final int iOriginColumn;
private final boolean isDerived;
/**
* Stores the expression for data conversion,
* which source table fields are transformed by which expression the target field
*/
private String transform;
// ~ Constructors -----------------------------------------------------------
public RelColumnOrigin(RelOptTable originTable, int iOriginColumn, boolean isDerived) {
this.originTable = originTable;
this.iOriginColumn = iOriginColumn;
this.isDerived = isDerived;
}
public RelColumnOrigin(RelOptTable originTable, int iOriginColumn, boolean isDerived, String transform) {
this.originTable = originTable;
this.iOriginColumn = iOriginColumn;
this.isDerived = isDerived;
this.transform = transform;
}
// ~ Methods ----------------------------------------------------------------
/**
* Returns table of origin.
*/
public RelOptTable getOriginTable() {
return originTable;
}
/**
* Returns the 0-based index of column in origin table; whether this ordinal
* is flattened or unflattened depends on whether UDT flattening has already
* been performed on the relational expression which produced this
* description.
*/
public int getOriginColumnOrdinal() {
return iOriginColumn;
}
/**
* Consider the query <code>select a+b as c, d as e from t</code>. The
* output column c has two origins (a and b), both of them derived. The
* output column d as one origin (c), which is not derived.
*
* @return false if value taken directly from column in origin table; true
* otherwise
*/
public boolean isDerived() {
return isDerived;
}
public String getTransform() {
return transform;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof RelColumnOrigin)) {
return false;
}
RelColumnOrigin other = (RelColumnOrigin) obj;
return originTable.getQualifiedName().equals(other.originTable.getQualifiedName())
&& (iOriginColumn == other.iOriginColumn)
&& (isDerived == other.isDerived);
}
@Override
public int hashCode() {
return originTable.getQualifiedName().hashCode() + iOriginColumn + (isDerived ? 313 : 0);
}
}
......@@ -23,7 +23,6 @@ import org.dinky.assertion.Asserts;
import org.dinky.context.DinkyClassLoaderContextHolder;
import org.dinky.data.model.LineageRel;
import org.dinky.data.result.SqlExplainResult;
import org.dinky.utils.FlinkStreamProgramWithoutPhysical;
import org.dinky.utils.LineageContext;
import org.apache.flink.api.dag.Transformation;
......@@ -51,7 +50,6 @@ import org.apache.flink.table.operations.SinkModifyOperation;
import org.apache.flink.table.operations.command.ResetOperation;
import org.apache.flink.table.operations.command.SetOperation;
import org.apache.flink.table.operations.ddl.CreateTableOperation;
import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram;
import org.apache.flink.types.Row;
import java.util.ArrayList;
......@@ -79,13 +77,10 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
private static final Logger log = LoggerFactory.getLogger(CustomTableEnvironmentImpl.class);
private final FlinkChainedProgram flinkChainedProgram;
private static final ObjectMapper mapper = new ObjectMapper();
public CustomTableEnvironmentImpl(StreamTableEnvironment streamTableEnvironment) {
super(streamTableEnvironment);
this.flinkChainedProgram = FlinkStreamProgramWithoutPhysical.buildProgram(
(Configuration) getStreamExecutionEnvironment().getConfiguration());
}
public static CustomTableEnvironmentImpl create(StreamExecutionEnvironment executionEnvironment) {
......@@ -256,8 +251,7 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
@Override
public List<LineageRel> getLineage(String statement) {
LineageContext lineageContext =
new LineageContext(flinkChainedProgram, (TableEnvironmentImpl) streamTableEnvironment);
LineageContext lineageContext = new LineageContext((TableEnvironmentImpl) streamTableEnvironment);
return lineageContext.getLineage(statement);
}
......
......@@ -27,23 +27,13 @@ import org.apache.calcite.rel.metadata.RelColumnOrigin;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.commons.collections.CollectionUtils;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.table.api.TableConfig;
import org.apache.flink.table.api.TableException;
import org.apache.flink.table.api.ValidationException;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import org.apache.flink.table.catalog.CatalogManager;
import org.apache.flink.table.catalog.FunctionCatalog;
import org.apache.flink.table.module.ModuleManager;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.operations.SinkModifyOperation;
import org.apache.flink.table.planner.calcite.FlinkRelBuilder;
import org.apache.flink.table.planner.calcite.RexFactory;
import org.apache.flink.table.planner.delegation.PlannerBase;
import org.apache.flink.table.planner.operations.PlannerQueryOperation;
import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram;
import org.apache.flink.table.planner.plan.optimize.program.StreamOptimizeContext;
import org.apache.flink.table.planner.plan.schema.TableSourceTable;
import org.apache.flink.table.planner.plan.trait.MiniBatchInterval;
import java.util.ArrayList;
import java.util.List;
......@@ -56,11 +46,9 @@ import java.util.Set;
*/
public class LineageContext {
private final FlinkChainedProgram flinkChainedProgram;
private final TableEnvironmentImpl tableEnv;
public LineageContext(FlinkChainedProgram flinkChainedProgram, TableEnvironmentImpl tableEnv) {
this.flinkChainedProgram = flinkChainedProgram;
public LineageContext(TableEnvironmentImpl tableEnv) {
this.tableEnv = tableEnv;
}
......@@ -70,11 +58,8 @@ public class LineageContext {
String sinkTable = parsed.getField(0);
RelNode oriRelNode = parsed.getField(1);
// 2. Optimize original relNode to generate Optimized Logical Plan
RelNode optRelNode = optimize(oriRelNode);
// 3. Build lineage based from RelMetadataQuery
return buildFiledLineageResult(sinkTable, optRelNode);
// 2. Build lineage based from RelMetadataQuery
return buildFiledLineageResult(sinkTable, oriRelNode);
}
private Tuple2<String, RelNode> parseStatement(String sql) {
......@@ -96,71 +81,6 @@ public class LineageContext {
}
}
/** Calling each program's optimize method in sequence. */
private RelNode optimize(RelNode relNode) {
return flinkChainedProgram.optimize(relNode, new StreamOptimizeContext() {
@Override
public boolean isBatchMode() {
return false;
}
@Override
public TableConfig getTableConfig() {
return tableEnv.getConfig();
}
@Override
public FunctionCatalog getFunctionCatalog() {
return getPlanner().getFlinkContext().getFunctionCatalog();
}
@Override
public CatalogManager getCatalogManager() {
return tableEnv.getCatalogManager();
}
@Override
public ModuleManager getModuleManager() {
return getPlanner().getFlinkContext().getModuleManager();
}
@Override
public RexFactory getRexFactory() {
return getPlanner().getFlinkContext().getRexFactory();
}
@Override
public FlinkRelBuilder getFlinkRelBuilder() {
return getPlanner().createRelBuilder();
}
@Override
public boolean isUpdateBeforeRequired() {
return false;
}
@Override
public MiniBatchInterval getMiniBatchInterval() {
return MiniBatchInterval.NONE;
}
@Override
public boolean needFinalTimeIndicatorConversion() {
return true;
}
@Override
public ClassLoader getClassLoader() {
return getPlanner().getFlinkContext().getClassLoader();
}
private PlannerBase getPlanner() {
return (PlannerBase) tableEnv.getPlanner();
}
});
}
/** Check the size of query and sink fields match */
private void validateSchema(String sinkTable, RelNode relNode, List<String> sinkFieldList) {
List<String> queryFieldList = relNode.getRowType().getFieldNames();
......@@ -204,7 +124,8 @@ public class LineageContext {
String sourceColumn = fieldNames.get(ordinal);
// add record
resultList.add(LineageRel.build(sourceTable, sourceColumn, sinkTable, targetColumn));
resultList.add(LineageRel.build(
sourceTable, sourceColumn, sinkTable, targetColumn, relColumnOrigin.getTransform()));
}
}
}
......
/*
*
* 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.dinky.utils;
import static org.junit.Assert.assertEquals;
import org.dinky.data.model.LineageRel;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @description: LineageContextTest
* @author: HamaWhite
*/
public class LineageContextTest {
private static TableEnvironmentImpl tableEnv;
private static LineageContext context;
@BeforeClass
public static void setUp() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment(new Configuration());
EnvironmentSettings settings =
EnvironmentSettings.newInstance().inStreamingMode().build();
tableEnv = (TableEnvironmentImpl) StreamTableEnvironment.create(env, settings);
context = new LineageContext(tableEnv);
}
@Before
public void init() {
// create table ST
tableEnv.executeSql("DROP TABLE IF EXISTS ST");
tableEnv.executeSql("CREATE TABLE ST ( " + " a STRING ,"
+ " b STRING ,"
+ " c STRING "
+ ") WITH ( "
+ " 'connector' = 'datagen' ,"
+ " 'rows-per-second' = '1' "
+ ")");
// create table TT
tableEnv.executeSql("DROP TABLE IF EXISTS TT");
tableEnv.executeSql("CREATE TABLE TT ( " + " A STRING ,"
+ " B STRING "
+ ") WITH ( "
+ " 'connector' = 'print' "
+ ")");
}
@Test
public void testGetLineage() {
List<LineageRel> actualList = context.getLineage("INSERT INTO TT select a||c A ,b||c B from ST");
String[][] expectedArray = {
{"ST", "a", "TT", "A", "||(a, c)"},
{"ST", "c", "TT", "A", "||(a, c)"},
{"ST", "b", "TT", "B", "||(b, c)"},
{"ST", "c", "TT", "B", "||(b, c)"}
};
List<LineageRel> expectedList = buildResult(expectedArray);
assertEquals(expectedList, actualList);
}
private List<LineageRel> buildResult(String[][] expectedArray) {
return Stream.of(expectedArray)
.map(e -> {
String transform = e.length == 5 ? e[4] : null;
return new LineageRel(
"default_catalog",
"default_database",
e[0],
e[1],
"default_catalog",
"default_database",
e[2],
e[3],
transform);
})
.collect(Collectors.toList());
}
}
......@@ -41,6 +41,11 @@
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
......
/*
*
* 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.rel.metadata;
import org.apache.calcite.plan.RelOptTable;
/**
* Modified based on calcite's source code org.apache.calcite.rel.metadata.RelColumnOrigin
* <p>
* Modification point:
* <ol>
* <li>add transform field and related code.
* </ol>
*
* @description: RelColumnOrigin is a data structure describing one of the origins of an
* output column produced by a relational expression.
* @author: HamaWhite
*/
public class RelColumnOrigin {
// ~ Instance fields --------------------------------------------------------
private final RelOptTable originTable;
private final int iOriginColumn;
private final boolean isDerived;
/**
* Stores the expression for data conversion,
* which source table fields are transformed by which expression the target field
*/
private String transform;
// ~ Constructors -----------------------------------------------------------
public RelColumnOrigin(RelOptTable originTable, int iOriginColumn, boolean isDerived) {
this.originTable = originTable;
this.iOriginColumn = iOriginColumn;
this.isDerived = isDerived;
}
public RelColumnOrigin(RelOptTable originTable, int iOriginColumn, boolean isDerived, String transform) {
this.originTable = originTable;
this.iOriginColumn = iOriginColumn;
this.isDerived = isDerived;
this.transform = transform;
}
// ~ Methods ----------------------------------------------------------------
/**
* Returns table of origin.
*/
public RelOptTable getOriginTable() {
return originTable;
}
/**
* Returns the 0-based index of column in origin table; whether this ordinal
* is flattened or unflattened depends on whether UDT flattening has already
* been performed on the relational expression which produced this
* description.
*/
public int getOriginColumnOrdinal() {
return iOriginColumn;
}
/**
* Consider the query <code>select a+b as c, d as e from t</code>. The
* output column c has two origins (a and b), both of them derived. The
* output column d as one origin (c), which is not derived.
*
* @return false if value taken directly from column in origin table; true
* otherwise
*/
public boolean isDerived() {
return isDerived;
}
public String getTransform() {
return transform;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof RelColumnOrigin)) {
return false;
}
RelColumnOrigin other = (RelColumnOrigin) obj;
return originTable.getQualifiedName().equals(other.originTable.getQualifiedName())
&& (iOriginColumn == other.iOriginColumn)
&& (isDerived == other.isDerived);
}
@Override
public int hashCode() {
return originTable.getQualifiedName().hashCode() + iOriginColumn + (isDerived ? 313 : 0);
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment