[ 
https://issues.apache.org/jira/browse/BEAM-5112?focusedWorklogId=179572&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-179572
 ]

ASF GitHub Bot logged work on BEAM-5112:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 28/Dec/18 21:33
            Start Date: 28/Dec/18 21:33
    Worklog Time Spent: 10m 
      Work Description: apilloud commented on pull request #6417: [BEAM-5112] 
Use Calcite codegen to implement BeamCalcRel
URL: https://github.com/apache/beam/pull/6417#discussion_r244411820
 
 

 ##########
 File path: 
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/rel/BeamCalcRel.java
 ##########
 @@ -96,43 +224,243 @@ public boolean isInputSortRelAndLimitOnly() {
 
   /** {@code CalcFn} is the executor for a {@link BeamCalcRel} step. */
   private static class CalcFn extends DoFn<Row, Row> {
-    private BeamSqlExpressionExecutor executor;
-    private Schema outputSchema;
+    private final String processElementBlock;
+    private final Schema outputSchema;
+    private transient @Nullable ScriptEvaluator se = null;
 
-    public CalcFn(BeamSqlExpressionExecutor executor, Schema outputSchema) {
-      super();
-      this.executor = executor;
+    public CalcFn(String processElementBlock, Schema outputSchema) {
+      this.processElementBlock = processElementBlock;
       this.outputSchema = outputSchema;
     }
 
+    ScriptEvaluator compile() {
+      ScriptEvaluator se = new ScriptEvaluator();
+      se.setParameters(
+          new String[] {outputSchemaParam.name, processContextParam.name, 
DataContext.ROOT.name},
+          new Class[] {
+            (Class) outputSchemaParam.getType(),
+            (Class) processContextParam.getType(),
+            (Class) DataContext.ROOT.getType()
+          });
+      try {
+        se.cook(processElementBlock);
+      } catch (CompileException e) {
+        throw new RuntimeException("Could not compile CalcFn: " + 
processElementBlock, e);
+      }
+      return se;
+    }
+
     @Setup
     public void setup() {
-      executor.prepare();
+      this.se = compile();
     }
 
     @ProcessElement
     public void processElement(ProcessContext c) {
-      Row inputRow = c.element();
-      @Nullable
-      List<Object> rawResultValues =
-          executor.execute(inputRow, null, 
BeamSqlExpressionEnvironments.forRow(inputRow, null));
-
-      if (rawResultValues != null) {
-        List<Object> castResultValues =
-            IntStream.range(0, outputSchema.getFieldCount())
-                .mapToObj(i -> castField(rawResultValues, i))
-                .collect(toList());
-        
c.output(Row.withSchema(outputSchema).addValues(castResultValues).build());
+      assert se != null;
+      try {
+        se.evaluate(new Object[] {outputSchema, c, CONTEXT_INSTANCE});
+      } catch (InvocationTargetException e) {
+        throw new RuntimeException(
+            "CalcFn failed to evaluate: " + processElementBlock, e.getCause());
+      }
+    }
+  }
+
+  private static final Map<TypeName, Type> rawTypeMap =
+      ImmutableMap.<TypeName, Type>builder()
+          .put(TypeName.BYTE, Byte.class)
+          .put(TypeName.INT16, Short.class)
+          .put(TypeName.INT32, Integer.class)
+          .put(TypeName.INT64, Long.class)
+          .put(TypeName.FLOAT, Float.class)
+          .put(TypeName.DOUBLE, Double.class)
+          .build();
+
+  private Expression castOutput(Expression value, FieldType toType) {
+    if (value.getType() == Object.class) {
+      // fast copy path, just pass object through
+      return value;
+    } else if (toType.getTypeName().isDateType()
+        && value.getType() instanceof Class
+        && !Types.isAssignableFrom(ReadableInstant.class, (Class) 
value.getType())) {
+      Expression valueDateTime = value;
+      if (toType.getMetadata() == null) {
+        if (value.getType() == java.sql.Timestamp.class) {
 
 Review comment:
   The code generator only converts to the `java.sql` types if there are UDFs, 
otherwise they will remain primitive types. So we don't always need to convert 
back.
   I am comparing metadata because `equals` compares things I don't care about 
here, like nullability. Looks like there is a new `typesEqual` helper I can use 
here.
   I don't think this logic is compatible with a `switch/case` format.
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 179572)
    Time Spent: 4h 50m  (was: 4h 40m)

> Investigate if Calcite can generate functions that we need
> ----------------------------------------------------------
>
>                 Key: BEAM-5112
>                 URL: https://issues.apache.org/jira/browse/BEAM-5112
>             Project: Beam
>          Issue Type: Sub-task
>          Components: dsl-sql
>            Reporter: Rui Wang
>            Assignee: Andrew Pilloud
>            Priority: Major
>          Time Spent: 4h 50m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to