/
CodeGeneratorContext.scala
1072 lines (929 loc) · 37.4 KB
/
CodeGeneratorContext.scala
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.flink.table.planner.codegen
import org.apache.flink.api.common.functions.Function
import org.apache.flink.api.common.typeutils.TypeSerializer
import org.apache.flink.configuration.ReadableConfig
import org.apache.flink.table.data.GenericRowData
import org.apache.flink.table.data.conversion.{DataStructureConverter, DataStructureConverters}
import org.apache.flink.table.functions.{FunctionContext, TableFunction, UserDefinedFunction}
import org.apache.flink.table.planner.codegen.CodeGenUtils._
import org.apache.flink.table.planner.codegen.GenerateUtils.generateRecordStatement
import org.apache.flink.table.planner.utils.{InternalConfigOptions, TableConfigUtils}
import org.apache.flink.table.runtime.operators.TableStreamOperator
import org.apache.flink.table.runtime.typeutils.{ExternalSerializer, InternalSerializers}
import org.apache.flink.table.runtime.util.collections._
import org.apache.flink.table.types.DataType
import org.apache.flink.table.types.logical._
import org.apache.flink.table.types.logical.LogicalTypeRoot._
import org.apache.flink.table.utils.DateTimeUtils
import org.apache.flink.util.InstantiationUtil
import java.time.ZoneId
import java.util.TimeZone
import java.util.concurrent.atomic.AtomicLong
import java.util.function.{Supplier => JSupplier}
import scala.collection.mutable
/**
* The context for code generator, maintaining various reusable statements that could be insert into
* different code sections in the final generated class.
*
* Caution: As we use nameCounter in each CodeGeneratorContext, we must ensure that a unique
* CodeGeneratorContext(or contexts share the same ancestors) is used throughout the entire class to
* avoid naming conflicts. So when we create a context for a class, we can set parentCtx to null.
* However, when we create a context for a code block, we must ensure that all contexts for code
* blocks in a class share a common ancestor by setting parentCtx.
*/
class CodeGeneratorContext(
val tableConfig: ReadableConfig,
val classLoader: ClassLoader,
val parentCtx: CodeGeneratorContext) {
def this(tableConfig: ReadableConfig, classLoader: ClassLoader) =
this(tableConfig, classLoader, null)
// holding a list of objects that could be used passed into generated class
val references: mutable.ArrayBuffer[AnyRef] = new mutable.ArrayBuffer[AnyRef]()
// set of strings (lines) that will be concatenated into a single class header comment
private val reusableHeaderComments: mutable.LinkedHashSet[String] =
mutable.LinkedHashSet[String]()
// set of member statements that will be added only once
// we use a LinkedHashSet to keep the insertion order
private val reusableMemberStatements: mutable.LinkedHashSet[String] =
mutable.LinkedHashSet[String]()
// set of constructor statements that will be added only once
// we use a LinkedHashSet to keep the insertion order
private val reusableInitStatements: mutable.LinkedHashSet[String] =
mutable.LinkedHashSet[String]()
// set of open statements for RichFunction that will be added only once
// we use a LinkedHashSet to keep the insertion order
private val reusableOpenStatements: mutable.LinkedHashSet[String] =
mutable.LinkedHashSet[String]()
// set of finish statements for RichFunction that will be added only once
// we use a LinkedHashSet to keep the insertion order
private val reusableFinishStatements: mutable.LinkedHashSet[String] =
mutable.LinkedHashSet[String]()
// set of close statements for RichFunction that will be added only once
// we use a LinkedHashSet to keep the insertion order
private val reusableCloseStatements: mutable.LinkedHashSet[String] =
mutable.LinkedHashSet[String]()
// set of statements for cleanup dataview that will be added only once
// we use a LinkedHashSet to keep the insertion order
private val reusableCleanupStatements = mutable.LinkedHashSet[String]()
// set of statements that will be added only once per record;
// code should only update member variables because local variables are not accessible if
// the code needs to be split;
// we use a LinkedHashSet to keep the insertion order
private val reusablePerRecordStatements: mutable.LinkedHashSet[String] =
mutable.LinkedHashSet[String]()
// set of statements that will be added only for operator fusion codegen process method
private val reusableFusionCodegenProcessStatements: mutable.TreeMap[Int, String] =
mutable.TreeMap[Int, String]()
// set of statements that will be added only for operator fusion codegen endInput method
private val reusableFusionCodegenEndInputStatements: mutable.TreeMap[Int, String] =
mutable.TreeMap[Int, String]()
// map of initial input unboxing expressions that will be added only once
// (inputTerm, index) -> expr
val reusableInputUnboxingExprs: mutable.Map[(String, Int), GeneratedExpression] =
mutable.Map[(String, Int), GeneratedExpression]()
// set of constructor statements that will be added only once
// we use a LinkedHashSet to keep the insertion order
private val reusableConstructorStatements: mutable.LinkedHashSet[(String, String)] =
mutable.LinkedHashSet[(String, String)]()
// set of inner class definition statements that will be added only once
private val reusableInnerClassDefinitionStatements: mutable.Map[String, String] =
mutable.Map[String, String]()
// map of string constants that will be added only once
// string_constant -> reused_term
private val reusableStringConstants: mutable.Map[String, String] = mutable.Map[String, String]()
// map of type serializer that will be added only once
// LogicalType -> reused_term
private val reusableTypeSerializers: mutable.Map[LogicalType, String] =
mutable.Map[LogicalType, String]()
// map of data structure converters that will be added only once
// DataType -> reused_term
private val reusableConverters: mutable.Map[DataType, String] =
mutable.Map[DataType, String]()
// map of external serializer that will be added only once
// DataType -> reused_term
private val reusableExternalSerializers: mutable.Map[DataType, String] =
mutable.Map[DataType, String]()
/**
* The current method name for [[reusableLocalVariableStatements]]. You can start a new local
* variable statements for another method using [[startNewLocalVariableStatement()]]
*/
private var currentMethodNameForLocalVariables = "DEFAULT"
// map of local variable statements. It will be placed in method if method code not excess
// max code length, otherwise will be placed in member area of the class. The statements
// are maintained for multiple methods, so that it's a map from method_name to variables.
//
// method_name -> local_variable_statements
private val reusableLocalVariableStatements = mutable.Map[String, mutable.LinkedHashSet[String]](
(currentMethodNameForLocalVariables, mutable.LinkedHashSet[String]()))
/** the class is used as the generated operator code's base class. */
private var operatorBaseClass: Class[_] = classOf[TableStreamOperator[_]]
private val nameCounter = new AtomicLong
// ---------------------------------------------------------------------------------
// Getter
// ---------------------------------------------------------------------------------
def getReusableInputUnboxingExprs(inputTerm: String, index: Int): Option[GeneratedExpression] =
reusableInputUnboxingExprs.get((inputTerm, index))
/** Prioritize using the nameCounter of the ancestor. */
def getNameCounter: AtomicLong = if (parentCtx == null) nameCounter else parentCtx.getNameCounter
/**
* Add a line comment to [[reusableHeaderComments]] list which will be concatenated into a single
* class header comment.
*
* @param comment
* The comment to add for class header
*/
def addReusableHeaderComment(comment: String): Unit = {
reusableHeaderComments.add(comment)
}
// ---------------------------------------------------------------------------------
// Local Variables for Code Split
// ---------------------------------------------------------------------------------
/**
* Starts a new local variable statements for a generated class with the given method name.
*
* @param methodName
* the method name which the fields will be placed into if code is not split.
*/
def startNewLocalVariableStatement(methodName: String): Unit = {
currentMethodNameForLocalVariables = methodName
reusableLocalVariableStatements(methodName) = mutable.LinkedHashSet[String]()
}
/**
* Adds a reusable local variable statement with the given type term and field name. The local
* variable statements will be placed in methods or class member area depends on whether the
* method length excess max code length.
*
* @param fieldName
* the field name prefix
* @param fieldTypeTerm
* the field type term
* @return
* a new generated unique field name
*/
def addReusableLocalVariable(fieldTypeTerm: String, fieldName: String): String = {
val fieldTerm = newName(this, fieldName)
reusableLocalVariableStatements
.getOrElse(currentMethodNameForLocalVariables, mutable.LinkedHashSet[String]())
.add(s"$fieldTypeTerm $fieldTerm;")
fieldTerm
}
/**
* Adds multiple pairs of local variables. The local variable statements will be placed in methods
* or class member area depends on whether the method length excess max code length.
*
* @param fieldTypeAndNames
* pairs of local variables with left is field type term and right is field name
* @return
* the new generated unique field names for each variable pairs
*/
def addReusableLocalVariables(fieldTypeAndNames: (String, String)*): Seq[String] = {
val fieldTerms = newNames(this, fieldTypeAndNames.map(_._2): _*)
fieldTypeAndNames.map(_._1).zip(fieldTerms).foreach {
case (fieldTypeTerm, fieldTerm) =>
reusableLocalVariableStatements
.getOrElse(currentMethodNameForLocalVariables, mutable.LinkedHashSet[String]())
.add(s"$fieldTypeTerm $fieldTerm;")
}
fieldTerms
}
// ---------------------------------------------------------------------------------
// generate reuse code methods
// ---------------------------------------------------------------------------------
/** @return Comment to be added as a header comment on the generated class */
def getClassHeaderComment: String = {
s"""
|// ${reusableHeaderComments.mkString("\n// ")}
""".stripMargin
}
/**
* @return
* code block of statements that need to be placed in the member area of the class (e.g. inner
* class definition)
*/
def reuseInnerClassDefinitionCode(): String = {
reusableInnerClassDefinitionStatements.values.mkString("\n")
}
/**
* @return
* code block of statements that need to be placed in the member area of the class (e.g. member
* variables and their initialization)
*/
def reuseMemberCode(): String = {
reusableMemberStatements.mkString("\n")
}
/**
* @return
* code block of statements that will be placed in the member area of the class if generated
* code is split or in local variables of method
*/
def reuseLocalVariableCode(methodName: String = currentMethodNameForLocalVariables): String = {
if (methodName == null) {
reusableLocalVariableStatements(currentMethodNameForLocalVariables).mkString("\n")
} else {
reusableLocalVariableStatements(methodName).mkString("\n")
}
}
/** @return code block of statements that need to be placed in the constructor */
def reuseInitCode(): String = {
reusableInitStatements.mkString("\n")
}
/**
* @return
* code block of statements that need to be placed in the per recode process block (e.g.
* Function or StreamOperator's processElement)
*/
def reusePerRecordCode(): String = {
reusablePerRecordStatements.mkString("\n")
}
/**
* @return
* code block of statements that need to be placed in the open() method (e.g. RichFunction or
* StreamOperator)
*/
def reuseOpenCode(): String = {
reusableOpenStatements.mkString("\n")
}
/**
* @return
* code block of statements that need to be placed in the finish() method (e.g. RichFunction or
* StreamOperator)
*/
def reuseFinishCode(): String = {
reusableFinishStatements.mkString("\n")
}
/**
* @return
* code block of statements that need to be placed in the close() method (e.g. RichFunction or
* StreamOperator)
*/
def reuseCloseCode(): String = {
reusableCloseStatements.mkString("\n")
}
/**
* @return
* code block of statements that need to be placed in the cleanup() method of
* [AggregationsFunction]
*/
def reuseCleanupCode(): String = {
reusableCleanupStatements.mkString("", "\n", "\n")
}
/**
* @return
* code block of statements that need to be placed in the getInputs() method of
* [FusionStreamOperator]
*/
def reuseFusionProcessCode(): String = {
reusableFusionCodegenProcessStatements.values.mkString(",\n")
}
/**
* @return
* code block of statements that need to be placed in the endInput() method of
* [BoundedMultiInput]
*/
def reuseFusionEndInputCode(inputId: String): String = {
val endInputCode = reusableFusionCodegenEndInputStatements
.map {
case (id, code) => s"""
|case $id:
| $code
| break;
|""".stripMargin
}
.mkString("\n")
s"""
|switch($inputId) {
| $endInputCode
|}
|""".stripMargin
}
/**
* @return
* code block of statements that unbox input variables to a primitive variable and a
* corresponding null flag variable
*/
def reuseInputUnboxingCode(): String = {
reusableInputUnboxingExprs.values.map(_.code).mkString("\n")
}
/** Returns code block of unboxing input variables which belongs to the given inputTerm. */
def reuseInputUnboxingCode(inputTerm: String): String = {
val exprs = reusableInputUnboxingExprs.filter {
case ((term, _), _) =>
inputTerm.equals(term)
}
val codes = for (((_, _), expr) <- exprs) yield expr.code
codes.mkString("\n").trim
}
/** @return code block of constructor statements */
def reuseConstructorCode(className: String): String = {
reusableConstructorStatements
.map {
case (params, body) =>
s"""
|public $className($params) throws Exception {
| this();
| $body
|}
|""".stripMargin
}
.mkString("\n")
}
def setOperatorBaseClass(operatorBaseClass: Class[_]): CodeGeneratorContext = {
this.operatorBaseClass = operatorBaseClass
this
}
def getOperatorBaseClass: Class[_] = this.operatorBaseClass
// ---------------------------------------------------------------------------------
// add reusable code blocks
// ---------------------------------------------------------------------------------
/** Adds a reusable inner class statement with the given class name and class code */
def addReusableInnerClass(className: String, statements: String): Unit = {
reusableInnerClassDefinitionStatements(className) = statements
}
/**
* Adds a reusable member field statement to the member area.
*
* @param memberStatement
* the member field declare statement
*/
def addReusableMember(memberStatement: String): Unit = {
reusableMemberStatements.add(memberStatement)
}
/** Adds a reusable init statement which will be placed in constructor. */
def addReusableInitStatement(s: String): Unit = reusableInitStatements.add(s)
/** Adds a reusable per record statement */
def addReusablePerRecordStatement(s: String): Unit = reusablePerRecordStatements.add(s)
/** Adds a reusable open statement */
def addReusableOpenStatement(s: String): Unit = reusableOpenStatements.add(s)
/** Adds a reusable finish statement */
def addReusableFinishStatement(s: String): Unit = reusableFinishStatements.add(s)
/** Adds a reusable close statement */
def addReusableCloseStatement(s: String): Unit = reusableCloseStatements.add(s)
/** Adds a reusable cleanup statement */
def addReusableCleanupStatement(s: String): Unit = reusableCleanupStatements.add(s)
/** Adds a reusable fusion codegen process statement */
def addReusableFusionCodegenProcessStatement(inputId: Int, s: String): Unit =
reusableFusionCodegenProcessStatements.put(inputId, s)
/** Adds a reusable fusion codegen endInput statement */
def addReusableFusionCodegenEndInputStatement(inputId: Int, s: String): Unit =
reusableFusionCodegenEndInputStatements.put(inputId, s)
/** Adds a reusable input unboxing expression */
def addReusableInputUnboxingExprs(
inputTerm: String,
index: Int,
expr: GeneratedExpression): Unit = reusableInputUnboxingExprs((inputTerm, index)) = expr
/** Adds a reusable output record statement to member area. */
def addReusableOutputRecord(
t: LogicalType,
clazz: Class[_],
outRecordTerm: String,
outRecordWriterTerm: Option[String] = None): Unit = {
generateRecordStatement(t, clazz, outRecordTerm, outRecordWriterTerm, this)
}
/** Adds a reusable null [[GenericRowData]] to the member area. */
def addReusableNullRow(rowTerm: String, arity: Int): Unit = {
addReusableOutputRecord(
RowType.of((0 until arity).map(_ => new IntType()): _*),
classOf[GenericRowData],
rowTerm)
}
/** Adds a reusable internal hash set to the member area of the generated class. */
def addReusableHashSet(elements: Seq[GeneratedExpression], elementType: LogicalType): String = {
val fieldTerm = newName(this, "set")
val setTypeTerm = elementType.getTypeRoot match {
case TINYINT => className[ByteHashSet]
case SMALLINT => className[ShortHashSet]
case INTEGER => className[IntHashSet]
case BIGINT => className[LongHashSet]
case FLOAT => className[FloatHashSet]
case DOUBLE => className[DoubleHashSet]
case _ => className[ObjectHashSet[_]]
}
val addElementsCode = elements
.map {
element =>
if (element.literalValue.isDefined) {
// Don't generate the null check in case the element is a literal expression
if (element.literalValue.get != null) {
s"""
|${element.code}
|$fieldTerm.add(${element.resultTerm});
|""".stripMargin
} else if (element.literalValue.get == null) {
s"$fieldTerm.addNull();"
}
} else {
s"""
|${element.code}
|if (${element.nullTerm}) {
| $fieldTerm.addNull();
|} else {
| $fieldTerm.add(${element.resultTerm});
|}
|""".stripMargin
}
}
.mkString("\n")
val setBuildingFunctionName = newName(this, "buildSet")
val setBuildingFunctionCode =
s"""
|private void $setBuildingFunctionName() {
| $addElementsCode
| $fieldTerm.optimize();
|}
|""".stripMargin
addReusableMember(s"""
|final $setTypeTerm $fieldTerm = new $setTypeTerm(${elements.size});
|$setBuildingFunctionCode
|""".stripMargin)
reusableInitStatements.add(s"$setBuildingFunctionName();")
fieldTerm
}
/**
* Adds a reusable record-level timestamp to the beginning of the SAM of the generated class.
*
* <p> The timestamp value is evaluated for per record, this function is generally used in stream
* job.
*/
def addReusableRecordLevelCurrentTimestamp(): String = {
val fieldTerm = s"timestamp"
reusableMemberStatements.add(s"private $TIMESTAMP_DATA $fieldTerm;")
val field =
s"""
|$fieldTerm =
| $TIMESTAMP_DATA.fromEpochMillis(java.lang.System.currentTimeMillis());
|""".stripMargin
reusablePerRecordStatements.add(field)
fieldTerm
}
/**
* Adds a reusable query-level timestamp to the beginning of the SAM of the generated class.
*
* <p> The timestamp value is evaluated once at query-start, this function is generally used in
* batch job.
*/
def addReusableQueryLevelCurrentTimestamp(): String = {
val fieldTerm = s"queryStartTimestamp"
val queryStartEpoch = tableConfig
.getOptional(InternalConfigOptions.TABLE_QUERY_START_EPOCH_TIME)
.orElseThrow(
new JSupplier[Throwable] {
override def get() = new CodeGenException(
"Try to obtain epoch time of query-start fail." +
" This is a bug, please file an issue.")
}
)
reusableMemberStatements.add(s"""
|private static final $TIMESTAMP_DATA $fieldTerm =
|$TIMESTAMP_DATA.fromEpochMillis(${queryStartEpoch}L);
|""".stripMargin)
fieldTerm
}
/**
* Adds a reusable record-level local date time to the beginning of the SAM of the generated
* class.
*
* <p> The timestamp value is evaluated for per record, this function is generally used in stream
* job.
*/
def addReusableRecordLevelLocalDateTime(): String = {
val fieldTerm = s"localTimestamp"
val sessionTimeZone = addReusableSessionTimeZone()
val timestamp = addReusableRecordLevelCurrentTimestamp()
// declaration
reusableMemberStatements.add(s"private $TIMESTAMP_DATA $fieldTerm;")
// assignment
val field =
s"""
|$fieldTerm = $TIMESTAMP_DATA.fromEpochMillis(
| $timestamp.getMillisecond() +
| $sessionTimeZone.getOffset($timestamp.getMillisecond()));
|""".stripMargin
reusablePerRecordStatements.add(field)
fieldTerm
}
/**
* Adds a reusable query-level local date time to the beginning of the SAM of the generated class.
*
* <p> The timestamp value is evaluated once at query-start, this function is generally used in
* batch job.
*/
def addReusableQueryLevelLocalDateTime(): String = {
val fieldTerm = s"queryStartLocaltimestamp"
val queryStartLocalTimestamp = tableConfig
.getOptional(InternalConfigOptions.TABLE_QUERY_START_LOCAL_TIME)
.orElseThrow(
new JSupplier[Throwable] {
override def get() = new CodeGenException(
"Try to obtain local time of query-start fail." +
" This is a bug, please file an issue.")
}
)
reusableMemberStatements.add(s"""
|private static final $TIMESTAMP_DATA $fieldTerm =
|$TIMESTAMP_DATA.fromEpochMillis(${queryStartLocalTimestamp}L);
|""".stripMargin)
fieldTerm
}
/**
* Adds a reusable query-level current database to the beginning of the SAM of the generated
* class.
*
* <p> The current database value is evaluated once at query-start.
*/
def addReusableQueryLevelCurrentDatabase(): String = {
val fieldTerm = s"queryCurrentDatabase"
val queryStartCurrentDatabase = tableConfig
.getOptional(InternalConfigOptions.TABLE_QUERY_CURRENT_DATABASE)
.orElseThrow(new JSupplier[Throwable] {
override def get() = new CodeGenException(
"Try to obtain current database of query-start fail." +
" This is a bug, please file an issue.")
})
reusableMemberStatements.add(s"""
|private static final $BINARY_STRING $fieldTerm =
|$BINARY_STRING.fromString("$queryStartCurrentDatabase");
|""".stripMargin)
fieldTerm
}
/** Adds a reusable record-level local time to the beginning of the SAM of the generated class. */
def addReusableRecordLevelLocalTime(): String = {
val fieldTerm = s"localTime"
val localtimestamp = addReusableRecordLevelLocalDateTime()
// declaration
reusableMemberStatements.add(s"private int $fieldTerm;")
val utilsName = classOf[DateTimeUtils].getCanonicalName
// assignment
val field =
s"""
|$fieldTerm = $utilsName.timestampMillisToTime($localtimestamp.getMillisecond());
|""".stripMargin
reusablePerRecordStatements.add(field)
fieldTerm
}
/** Adds a reusable query-level local time to the beginning of the SAM of the generated class. */
def addReusableQueryLevelLocalTime(): String = {
val fieldTerm = s"queryStartLocaltime"
val queryStartLocalTimestamp = addReusableQueryLevelLocalDateTime()
val utilsName = classOf[DateTimeUtils].getCanonicalName
// declaration
reusableMemberStatements.add(
s"""
|private static final int $fieldTerm =
| $utilsName.timestampMillisToTime($queryStartLocalTimestamp.getMillisecond());
| """.stripMargin)
fieldTerm
}
/** Adds a reusable record-level date to the beginning of the SAM of the generated class. */
def addReusableRecordLevelCurrentDate(): String = {
val fieldTerm = s"date"
val timestamp = addReusableRecordLevelLocalDateTime()
val utilsName = classOf[DateTimeUtils].getCanonicalName
// declaration
reusableMemberStatements.add(s"private int $fieldTerm;")
// assignment
val field = s"$fieldTerm = $utilsName.timestampMillisToDate($timestamp.getMillisecond());"
reusablePerRecordStatements.add(field)
fieldTerm
}
/** Adds a reusable query-level date to the beginning of the SAM of the generated class. */
def addReusableQueryLevelCurrentDate(): String = {
val fieldTerm = s"queryStartDate"
val utilsName = classOf[DateTimeUtils].getCanonicalName
val timestamp = addReusableQueryLevelLocalDateTime()
reusableMemberStatements.add(
s"""
|private static final int $fieldTerm =
| $fieldTerm = $utilsName.timestampMillisToDate($timestamp.getMillisecond());
|""".stripMargin)
fieldTerm
}
/** Adds a reusable TimeZone to the member area of the generated class. */
def addReusableSessionTimeZone(): String = {
val zoneID = TimeZone.getTimeZone(TableConfigUtils.getLocalTimeZone(tableConfig)).getID
val stmt =
s"""private static final java.util.TimeZone $DEFAULT_TIMEZONE_TERM =
| java.util.TimeZone.getTimeZone("$zoneID");""".stripMargin
addReusableMember(stmt)
DEFAULT_TIMEZONE_TERM
}
/** Adds a reusable shift TimeZone of window to the member area of the generated class. */
def addReusableShiftTimeZone(zoneId: ZoneId): String = {
val fieldTerm = s"shiftTimeZone"
val stmt =
s"""private static final java.time.ZoneId $fieldTerm =
| java.time.ZoneId.of("${zoneId.toString}");""".stripMargin
addReusableMember(stmt)
fieldTerm
}
/**
* Adds a reusable [[java.util.Random]] to the member area of the generated class.
*
* The seed parameter must be a literal/constant expression.
*
* @return
* member variable term
*/
def addReusableRandom(seedExpr: Option[GeneratedExpression]): String = {
val fieldTerm = newName(this, "random")
val field =
s"""
|final java.util.Random $fieldTerm;
|""".stripMargin
val fieldInit = seedExpr match {
case Some(s) =>
s"""
|${s.code}
|if (!${s.nullTerm}) {
| $fieldTerm = new java.util.Random(${s.resultTerm});
|}
|else {
| $fieldTerm = new java.util.Random();
|}
|""".stripMargin
case _ =>
s"""
|$fieldTerm = new java.util.Random();
|""".stripMargin
}
reusableMemberStatements.add(field)
reusableInitStatements.add(fieldInit)
fieldTerm
}
/**
* Adds a reusable Object to the member area of the generated class
* @param obj
* the object to be added to the generated class
* @param fieldNamePrefix
* prefix field name of the generated member field term
* @param fieldTypeTerm
* field type class name
* @return
* the generated unique field term
*/
def addReusableObject(
obj: AnyRef,
fieldNamePrefix: String,
fieldTypeTerm: String = null): String = {
addReusableObjectWithName(obj, newName(this, fieldNamePrefix), fieldTypeTerm)
}
def addReusableObjectWithName(
obj: AnyRef,
fieldTerm: String,
fieldTypeTerm: String = null): String = {
val clsName = Option(fieldTypeTerm).getOrElse(obj.getClass.getCanonicalName)
addReusableObjectInternal(obj, fieldTerm, clsName)
fieldTerm
}
private def addReusableObjectInternal(
obj: AnyRef,
fieldTerm: String,
fieldTypeTerm: String): Unit = {
val idx = references.length
// make a deep copy of the object
val byteArray = InstantiationUtil.serializeObject(obj)
val objCopy: AnyRef =
InstantiationUtil.deserializeObject(byteArray, classLoader)
references += objCopy
reusableMemberStatements.add(s"private transient $fieldTypeTerm $fieldTerm;")
reusableInitStatements.add(s"$fieldTerm = ((($fieldTypeTerm) references[$idx]));")
}
/**
* Adds a reusable [[UserDefinedFunction]] to the member area of the generated [[Function]].
*
* @param function
* [[UserDefinedFunction]] object to be instantiated during runtime
* @param functionContextClass
* class of [[FunctionContext]]
* @param contextArgs
* additional list of arguments for [[FunctionContext]]
* @return
* member variable term
*/
def addReusableFunction(
function: UserDefinedFunction,
functionContextClass: Class[_ <: FunctionContext] = classOf[FunctionContext],
contextArgs: Seq[String] = null): String = {
val classQualifier = function.getClass.getName
val fieldTerm = CodeGenUtils.udfFieldName(function)
addReusableObjectInternal(function, fieldTerm, classQualifier)
val openFunction = if (contextArgs != null) {
s"""
|$fieldTerm.open(new ${functionContextClass.getCanonicalName}(${contextArgs.mkString(", ")}));
""".stripMargin
} else {
s"""
|$fieldTerm.open(new ${functionContextClass.getCanonicalName}(getRuntimeContext()));
""".stripMargin
}
reusableOpenStatements.add(openFunction)
if (function.isInstanceOf[TableFunction[_]]) {
val finishFunction = s"$fieldTerm.finish();"
reusableFinishStatements.add(finishFunction)
}
val closeFunction = s"$fieldTerm.close();"
reusableCloseStatements.add(closeFunction)
fieldTerm
}
/**
* Adds a reusable [[DataStructureConverter]] to the member area of the generated class.
*
* @param dataType
* converter to be added
* @param classLoaderTerm
* term to access the [[ClassLoader]] for user-defined classes
*/
def addReusableConverter(dataType: DataType, classLoaderTerm: String = null): String = {
reusableConverters.get(dataType) match {
case Some(term) =>
term
case None =>
val converter = DataStructureConverters.getConverter(dataType)
val converterTerm = addReusableObject(converter, "converter")
val openConverter = if (classLoaderTerm != null) {
s"""
|$converterTerm.open($classLoaderTerm);
""".stripMargin
} else {
s"""
|$converterTerm.open(getRuntimeContext().getUserCodeClassLoader());
""".stripMargin
}
reusableOpenStatements.add(openConverter)
reusableConverters(dataType) = converterTerm
converterTerm
}
}
/**
* Adds a reusable [[TypeSerializer]] to the member area of the generated class.
*
* @param t
* the internal type which used to generate internal type serializer
* @return
* member variable term
*/
def addReusableTypeSerializer(t: LogicalType): String = {
// if type serializer has been used before, we can reuse the code that
// has already been generated
reusableTypeSerializers.get(t) match {
case Some(term) => term
case None =>
val term = newName(this, "typeSerializer")
val ser = InternalSerializers.create(t)
addReusableObjectInternal(ser, term, ser.getClass.getCanonicalName)
reusableTypeSerializers(t) = term
term
}
}
/**
* Adds a reusable [[ExternalSerializer]] to the member area of the generated class.
*
* @param t
* the internal type which used to generate internal type serializer
* @return
* member variable term
*/
def addReusableExternalSerializer(t: DataType): String = {
reusableExternalSerializers.get(t) match {
case Some(term) =>
term
case None =>
val serializer = ExternalSerializer.of(t)
val serializerTerm = addReusableObject(serializer, "externalSerializer")
reusableExternalSerializers(t) = serializerTerm
serializerTerm
}
}
/** Adds a reusable static SLF4J Logger to the member area of the generated class. */
def addReusableLogger(logTerm: String, clazzTerm: String): Unit = {
val stmt =
s"""
|private static final org.slf4j.Logger $logTerm =
| org.slf4j.LoggerFactory.getLogger("$clazzTerm");
|""".stripMargin
reusableMemberStatements.add(stmt)
}
/**
* Adds a reusable constant to the member area of the generated class.
*
* @param constant
* constant expression
* @return
* generated expression with the fieldTerm and nullTerm
*/
def addReusableConstant(constant: GeneratedExpression): GeneratedExpression = {
require(constant.literal, "Literal expected")
val fieldTerm = newName(this, "constant")
val nullTerm = fieldTerm + "isNull"
val fieldType = primitiveTypeTermForType(constant.resultType)
val field =
s"""
|private final $fieldType $fieldTerm;
|private final boolean $nullTerm;
|""".stripMargin
reusableMemberStatements.add(field)
val init =
s"""
|${constant.code}
|$fieldTerm = ${constant.resultTerm};
|$nullTerm = ${constant.nullTerm};
|""".stripMargin
reusableInitStatements.add(init)
GeneratedExpression(fieldTerm, nullTerm, "", constant.resultType)
}
/**
* Adds a reusable string constant to the member area of the generated class.
*
* The string must be already escaped with
* [[org.apache.flink.table.utils.EncodingUtils.escapeJava()]].
*/
def addReusableEscapedStringConstant(value: String): String = {
reusableStringConstants.get(value) match {
case Some(field) => field
case None =>
val field = newName(this, "str")
val stmt =
s"""
|private final $BINARY_STRING $field = $BINARY_STRING.fromString("$value");
""".stripMargin
reusableMemberStatements.add(stmt)
reusableStringConstants(value) = field