-
Notifications
You must be signed in to change notification settings - Fork 622
[GLUTEN-12306][FLINK][VL] Route print sink options to Velox via reflection (no file path) #12320
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
Changes from 1 commit
bc074f6
e9e68c6
59015d1
adcbeaf
b561569
2d78282
314bdcd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,9 +30,6 @@ | |
| import io.github.zhztheplayer.velox4j.type.RowType; | ||
|
|
||
| import org.apache.flink.api.dag.Transformation; | ||
| import org.apache.flink.configuration.ConfigConstants; | ||
| import org.apache.flink.configuration.Configuration; | ||
| import org.apache.flink.configuration.CoreOptions; | ||
| import org.apache.flink.streaming.api.operators.OneInputStreamOperator; | ||
| import org.apache.flink.streaming.api.operators.SimpleOperatorFactory; | ||
| import org.apache.flink.streaming.api.transformations.LegacySinkTransformation; | ||
|
|
@@ -41,6 +38,7 @@ | |
| import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; | ||
| import org.apache.flink.util.FlinkRuntimeException; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
|
|
@@ -71,33 +69,48 @@ public Transformation<RowData> buildVeloxSource( | |
| throw new FlinkRuntimeException("Unimplemented method 'buildSource'"); | ||
| } | ||
|
|
||
| // Pulls print-identifier/standard-error from RowDataPrintFunction via reflection. | ||
| // Flink 1.19.x field names: sinkIdentifier (print-identifier), target (standard-error, true = | ||
| // stderr). | ||
| // Package-private for direct unit testing. | ||
| static String[] extractPrintOptions(Transformation<RowData> transformation) { | ||
| SimpleOperatorFactory operatorFactory = | ||
| (SimpleOperatorFactory) ((LegacySinkTransformation) transformation).getOperatorFactory(); | ||
| SinkOperator sinkOp = (SinkOperator) operatorFactory.getOperator(); | ||
| Object rowDataPrintFn = sinkOp.getUserFunction(); | ||
| try { | ||
| Field writerField = rowDataPrintFn.getClass().getDeclaredField("writer"); | ||
| writerField.setAccessible(true); | ||
| Object writer = writerField.get(rowDataPrintFn); | ||
| Field idField = writer.getClass().getDeclaredField("sinkIdentifier"); | ||
| idField.setAccessible(true); | ||
| Field stdErrField = writer.getClass().getDeclaredField("target"); | ||
| stdErrField.setAccessible(true); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| String printIdentifier = (String) idField.get(writer); | ||
| boolean isStdErr = stdErrField.getBoolean(writer); | ||
| return new String[] { | ||
| printIdentifier == null ? "" : printIdentifier, Boolean.toString(isStdErr) | ||
| }; | ||
| } catch (NoSuchFieldException | IllegalAccessException e) { | ||
| throw new FlinkRuntimeException("Failed to extract print sink options", e); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings({"rawtypes", "unchecked"}) | ||
| @Override | ||
| public Transformation buildVeloxSink( | ||
| Transformation<RowData> transformation, Map<String, Object> parameters) { | ||
| Transformation inputTrans = (Transformation) transformation.getInputs().get(0); | ||
| InternalTypeInfo inputTypeInfo = (InternalTypeInfo) inputTrans.getOutputType(); | ||
| Configuration config = (Configuration) parameters.get(Configuration.class.getName()); | ||
| String logDir = config.get(CoreOptions.FLINK_LOG_DIR); | ||
| String printPath; | ||
| if (logDir != null) { | ||
| printPath = String.format("file://%s/%s", logDir, "taskmanager.out"); | ||
| } else { | ||
| String flinkHomeDir = System.getenv(ConfigConstants.ENV_FLINK_HOME_DIR); | ||
| if (flinkHomeDir == null) { | ||
| String flinkConfDir = System.getenv(ConfigConstants.ENV_FLINK_CONF_DIR); | ||
| if (flinkConfDir == null) { | ||
| throw new FlinkRuntimeException( | ||
| "Can not get flink home directory, please set FLINK_HOME."); | ||
| } | ||
| printPath = String.format("file://%s/../log/%s", flinkConfDir, "taskmanager.out"); | ||
| } else { | ||
| printPath = String.format("file://%s/log/%s", flinkHomeDir, "taskmanager.out"); | ||
| } | ||
| } | ||
|
|
||
| String[] printOpts = extractPrintOptions(transformation); | ||
| String printIdentifier = printOpts[0]; | ||
| boolean isStdErr = Boolean.parseBoolean(printOpts[1]); | ||
|
|
||
| RowType inputColumns = (RowType) LogicalTypeConverter.toVLType(inputTypeInfo.toLogicalType()); | ||
| RowType ignore = new RowType(List.of("num"), List.of(new BigIntType())); | ||
| PrintTableHandle tableHandle = new PrintTableHandle("print-table", inputColumns, printPath); | ||
| PrintTableHandle tableHandle = | ||
| new PrintTableHandle("print-table", inputColumns, printIdentifier, isStdErr); | ||
| TableWriteNode tableWriteNode = | ||
| new TableWriteNode( | ||
| PlanNodeIdGenerator.newId(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| * 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.gluten.velox; | ||
|
|
||
| import org.apache.flink.api.dag.Transformation; | ||
| import org.apache.flink.streaming.api.functions.sink.RichSinkFunction; | ||
| import org.apache.flink.streaming.api.functions.sink.SinkFunction; | ||
| import org.apache.flink.streaming.api.operators.SimpleOperatorFactory; | ||
| import org.apache.flink.streaming.api.transformations.LegacySinkTransformation; | ||
| import org.apache.flink.table.data.RowData; | ||
| import org.apache.flink.table.runtime.operators.sink.SinkOperator; | ||
| import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; | ||
| import org.apache.flink.table.types.logical.IntType; | ||
| import org.apache.flink.table.types.logical.RowType; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class PrintSinkFactoryTest { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| private static final String ROWDATA_PRINT_FUNCTION_CN = | ||
| "org.apache.flink.connector.print.table.PrintTableSinkFactory$RowDataPrintFunction"; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static SinkFunction<RowData> newRowDataPrintFunction(String identifier, boolean isStdErr) | ||
| throws Exception { | ||
| Class<?> cls = Class.forName(ROWDATA_PRINT_FUNCTION_CN); | ||
| Constructor<?> ctor = | ||
| cls.getDeclaredConstructor( | ||
| org.apache.flink.table.connector.sink.DynamicTableSink.DataStructureConverter.class, | ||
| String.class, | ||
| boolean.class); | ||
| ctor.setAccessible(true); | ||
| return (SinkFunction<RowData>) ctor.newInstance(null, identifier, isStdErr); | ||
| } | ||
|
|
||
| private static LegacySinkTransformation<RowData> buildSinkTransformation( | ||
| SinkFunction<RowData> userFunction) { | ||
| SinkOperator sinkOp = new SinkOperator(userFunction, -1); | ||
| SimpleOperatorFactory<Object> factory = SimpleOperatorFactory.of(sinkOp); | ||
| Transformation<RowData> input = new StubTransformation(); | ||
| return new LegacySinkTransformation<>(input, "print-sink", factory, 1); | ||
| } | ||
|
|
||
| private static final class StubTransformation extends Transformation<RowData> { | ||
| StubTransformation() { | ||
| super("stub", InternalTypeInfo.of(RowType.of(new IntType())), 1); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Transformation<?>> getInputs() { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| @Override | ||
| protected List<Transformation<?>> getTransitivePredecessorsInternal() { | ||
| return Collections.emptyList(); | ||
| } | ||
| } | ||
|
|
||
| private static final class OtherSinkFunction extends RichSinkFunction<RowData> {} | ||
|
|
||
| @Test | ||
| void testMatchAcceptsRowDataPrintFunction() throws Exception { | ||
| PrintSinkFactory factory = new PrintSinkFactory(); | ||
| assertTrue(factory.match(buildSinkTransformation(newRowDataPrintFunction("foo", false)))); | ||
| } | ||
|
|
||
| @Test | ||
| void testMatchRejectsNonPrintSinkFunction() { | ||
| PrintSinkFactory factory = new PrintSinkFactory(); | ||
| assertFalse(factory.match(buildSinkTransformation(new OtherSinkFunction()))); | ||
| } | ||
|
|
||
| @Test | ||
| void testMatchRejectsNonLegacySinkTransformation() { | ||
| PrintSinkFactory factory = new PrintSinkFactory(); | ||
| assertFalse(factory.match(new StubTransformation())); | ||
| } | ||
|
|
||
| @Test | ||
| void testExtractPrintOptionsReadsIdentifierAndStderr() throws Exception { | ||
| LegacySinkTransformation<RowData> tx = | ||
| buildSinkTransformation(newRowDataPrintFunction("foo", true)); | ||
| String[] opts = PrintSinkFactory.extractPrintOptions(tx); | ||
| assertEquals("foo", opts[0]); | ||
| assertEquals("true", opts[1]); | ||
| } | ||
|
|
||
| @Test | ||
| void testExtractPrintOptionsDefaultsWhenUnset() throws Exception { | ||
| LegacySinkTransformation<RowData> tx = | ||
| buildSinkTransformation(newRowDataPrintFunction(null, false)); | ||
| String[] opts = PrintSinkFactory.extractPrintOptions(tx); | ||
| assertEquals("", opts[0]); | ||
| assertEquals("false", opts[1]); | ||
| } | ||
| } | ||
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.
the readability of
String[]is bad, may we can useclass PringOptionsinstead?