1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
| SQL是如何转换为DataStream操作的 当直接使用Table时,会发现Table的API并没有类似打印,输出数据的功能 只能使用toAppendStream/toRetractStream将Table转换为DataStream进行输出
这里我使用的剖析入口是insertInto
public void insertInto(String tablePath) { tableEnvironment.insertInto(tablePath, this); }
void insertInto(String targetPath, Table table);
public void insertInto(String targetPath, Table table) { UnresolvedIdentifier unresolvedIdentifier = parser.parseIdentifier(targetPath); insertIntoInternal(unresolvedIdentifier, table); } private void insertIntoInternal(UnresolvedIdentifier unresolvedIdentifier, Table table) { ObjectIdentifier objectIdentifier = catalogManager.qualifyIdentifier(unresolvedIdentifier); List<ModifyOperation列表> modifyOperations = Collections.singletonList( new CatalogSinkModifyOperation( objectIdentifier, table.getQueryOperation())); if (isEagerOperationTranslation()) { translate(modifyOperations); } else { buffer(modifyOperations); } } private void translate(List<ModifyOperation> modifyOperations) { List<Transformation<?>> transformations = planner.translate(modifyOperations); execEnv.apply(transformations); }
override def translate(tableOperations: util.List[ModifyOperation]) : util.List[Transformation[_]] = { tableOperations.asScala.map(translate).filter(Objects.nonNull).asJava }
private def translate(tableOperation: ModifyOperation) : Transformation[_] = { tableOperation match { case s : UnregisteredSinkModifyOperation[_] => writeToSink(s.getChild, s.getSink, unwrapQueryConfig) case catalogSink: CatalogSinkModifyOperation => getTableSink(catalogSink.getTableIdentifier) .map(sink => { TableSinkUtils.validateSink( catalogSink.getStaticPartitions, catalogSink.getChild, catalogSink.getTableIdentifier, sink) sink match { case partitionableSink: PartitionableTableSink => partitionableSink.setStaticPartition(catalogSink.getStaticPartitions) case _ => } sink match { case overwritableTableSink: OverwritableTableSink => overwritableTableSink.setOverwrite(catalogSink.isOverwrite) case _ => assert(!catalogSink.isOverwrite, "INSERT OVERWRITE requires " + s"${classOf[OverwritableTableSink].getSimpleName} but actually got " + sink.getClass.getName) } writeToSink(catalogSink.getChild, sink, unwrapQueryConfig) }) match { case Some(t) => t case None => throw new TableException(s"Sink ${catalogSink.getTableIdentifier} does not exists") } case outputConversion: OutputConversionModifyOperation => val (isRetract, withChangeFlag) = outputConversion.getUpdateMode match { case UpdateMode.RETRACT => (true, true) case UpdateMode.APPEND => (false, false) case UpdateMode.UPSERT => (false, true) } translateToType( tableOperation.getChild, unwrapQueryConfig, isRetract, withChangeFlag, TypeConversions.fromDataTypeToLegacyInfo(outputConversion.getType)).getTransformation case _ => throw new TableException(s"Unsupported ModifyOperation: $tableOperation") } }
private def writeToSink[T]( tableOperation: QueryOperation, sink: TableSink[T], queryConfig: StreamQueryConfig) : Transformation[_] = { val resultSink = sink match { case retractSink: RetractStreamTableSink[T] => retractSink match { case _: PartitionableTableSink => throw new TableException("Partitionable sink in retract stream mode " + "is not supported yet!") case _ => } writeToRetractSink(retractSink, tableOperation, queryConfig) case upsertSink: UpsertStreamTableSink[T] => upsertSink match { case _: PartitionableTableSink => throw new TableException("Partitionable sink in upsert stream mode " + "is not supported yet!") case _ => } writeToUpsertSink(upsertSink, tableOperation, queryConfig) case appendSink: AppendStreamTableSink[T] => writeToAppendSink(appendSink, tableOperation, queryConfig) case _ => throw new ValidationException("Stream Tables can only be emitted by AppendStreamTableSink, " + "RetractStreamTableSink, or UpsertStreamTableSink.") } if (resultSink != null) { resultSink.getTransformation } else { null } }
private def translateToType[A]( table: QueryOperation, queryConfig: StreamQueryConfig, updatesAsRetraction: Boolean, withChangeFlag: Boolean, tpe: TypeInformation[A]) : DataStream[A] = { val relNode = getRelBuilder.tableOperation(table).build() val dataStreamPlan = optimizer.optimize(relNode, updatesAsRetraction, getRelBuilder) val rowType = getTableSchema(table.getTableSchema.getFieldNames, dataStreamPlan) if (!withChangeFlag && !UpdatingPlanChecker.isAppendOnly(dataStreamPlan)) { throw new ValidationException( "Table is not an append-only table. " + "Use the toRetractStream() in order to handle add and retract messages.") } translateOptimized(dataStreamPlan, rowType, tpe, queryConfig, withChangeFlag) }
private def translateOptimized[A]( optimizedPlan: RelNode, logicalSchema: TableSchema, tpe: TypeInformation[A], queryConfig: StreamQueryConfig, withChangeFlag: Boolean) : DataStream[A] = { val dataStream = translateToCRow(optimizedPlan, queryConfig) DataStreamConversions.convert(dataStream, logicalSchema, withChangeFlag, tpe, config) }
private def translateToCRow( logicalPlan: RelNode, queryConfig: StreamQueryConfig): DataStream[CRow] = { logicalPlan match { case node: DataStreamRel => getExecutionEnvironment.configure( config.getConfiguration, Thread.currentThread().getContextClassLoader) node.translateToPlan(this, queryConfig) case _ => throw new TableException("Cannot generate DataStream due to an invalid logical plan. " + "This is a bug and should not happen. Please file an issue.") } }
override def translateToPlan( planner: StreamPlanner, queryConfig: StreamQueryConfig): DataStream[CRow] = { val config = planner.getConfig val inputDataStream = getInput.asInstanceOf[DataStreamRel].translateToPlan(planner, queryConfig) val condition = if (calcProgram.getCondition != null) { val materializedCondition = RelTimeIndicatorConverter.convertExpression( calcProgram.expandLocalRef(calcProgram.getCondition), inputSchema.relDataType, cluster.getRexBuilder) Some(materializedCondition) } else { None } val projection = calcProgram.getProjectList.asScala .map(calcProgram.expandLocalRef) val generator = new FunctionCodeGenerator(config, false, inputSchema.typeInfo) val genFunction = generateFunction( generator, ruleDescription, schema, projection, condition, config, classOf[ProcessFunction[CRow, CRow]]) val inputParallelism = inputDataStream.getParallelism val processFunc = new CRowProcessRunner( genFunction.name, genFunction.code, CRowTypeInfo(schema.typeInfo)) inputDataStream .process(processFunc) .name(calcOpName(calcProgram, getExpressionString)) .setParallelism(inputParallelism) }
|