You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@storm.apache.org by haohui <gi...@git.apache.org> on 2015/10/23 08:30:41 UTC

[GitHub] storm pull request: STORM-1097. Compile logical plans to Java sour...

GitHub user haohui opened a pull request:

    https://github.com/apache/storm/pull/814

    STORM-1097. Compile logical plans to Java source code.

    This PR implements the compilers that compile logical plans from Calcite to Java source code. It also removes the compiler implemented in native code per discussions in https://issues.apache.org/jira/browse/STORM-1105?focusedCommentId=14970164&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-14970164.
    


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/haohui/storm STORM-1097

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/storm/pull/814.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #814
    
----
commit 024c146cc5daa6506354847f2364f2ef12e19491
Author: Haohui Mai <wh...@apache.org>
Date:   2015-10-23T00:05:18Z

    Remove the native code compiler.

commit ff607555ce4067c2ff2944c59f3684d52625441a
Author: Haohui Mai <wh...@apache.org>
Date:   2015-10-22T23:42:24Z

    Implement compiler for expressions.

commit 3fb0b825d40423a2edb8b00cca09df821385900d
Author: Haohui Mai <wh...@apache.org>
Date:   2015-10-22T23:45:20Z

    Implement compiler for relation nodes.

commit 4e18e717687229a19092ea000df138eb99ba088f
Author: Haohui Mai <wh...@apache.org>
Date:   2015-10-22T23:52:59Z

    Compile logical plans to Java code.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: STORM-1097. Compile logical plans to Java sour...

Posted by harshach <gi...@git.apache.org>.
Github user harshach commented on a diff in the pull request:

    https://github.com/apache/storm/pull/814#discussion_r42936906
  
    --- Diff: external/sql/storm-sql-core/src/jvm/org/apache/storm/sql/compiler/ExprCompiler.java ---
    @@ -0,0 +1,210 @@
    +/**
    + * 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.storm.sql.compiler;
    +
    +import com.google.common.collect.ImmutableMap;
    +import org.apache.calcite.adapter.java.JavaTypeFactory;
    +import org.apache.calcite.rel.type.RelDataType;
    +import org.apache.calcite.rex.RexCall;
    +import org.apache.calcite.rex.RexCorrelVariable;
    +import org.apache.calcite.rex.RexDynamicParam;
    +import org.apache.calcite.rex.RexFieldAccess;
    +import org.apache.calcite.rex.RexInputRef;
    +import org.apache.calcite.rex.RexLiteral;
    +import org.apache.calcite.rex.RexLocalRef;
    +import org.apache.calcite.rex.RexNode;
    +import org.apache.calcite.rex.RexOver;
    +import org.apache.calcite.rex.RexRangeRef;
    +import org.apache.calcite.rex.RexVisitor;
    +import org.apache.calcite.sql.SqlOperator;
    +import org.apache.calcite.util.NlsString;
    +import org.apache.calcite.util.Util;
    +
    +import java.io.PrintWriter;
    +import java.lang.reflect.Type;
    +import java.math.BigDecimal;
    +import java.util.AbstractMap;
    +import java.util.IdentityHashMap;
    +import java.util.Map;
    +
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.DIVIDE;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.DIVIDE_INTEGER;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.GREATER_THAN;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.GREATER_THAN_OR_EQUAL;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LESS_THAN;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LESS_THAN_OR_EQUAL;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.MINUS;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.MULTIPLY;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.PLUS;
    +
    +/**
    + * Compile RexNode on top of the Tuple abstraction.
    + */
    +class ExprCompiler implements RexVisitor<String> {
    +  private final PrintWriter pw;
    +  private final Map<RexNode, String> expr = new IdentityHashMap<>();
    +  private final JavaTypeFactory typeFactory;
    +  private static final ImpTable IMP_TABLE = new ImpTable();
    +
    +  ExprCompiler(PrintWriter pw, JavaTypeFactory typeFactory) {
    +    this.pw = pw;
    +    this.typeFactory = typeFactory;
    +  }
    +
    +  @Override
    +  public String visitInputRef(RexInputRef rexInputRef) {
    +    if (expr.containsKey(rexInputRef)) {
    +      return expr.get(rexInputRef);
    +    }
    +    String name = printExpr(rexInputRef, String.format("_data.get(%d)",
    +                            rexInputRef.getIndex()));
    +    expr.put(rexInputRef, name);
    +    return name;
    +  }
    +
    +  @Override
    +  public String visitLocalRef(RexLocalRef rexLocalRef) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  @Override
    +  public String visitLiteral(RexLiteral rexLiteral) {
    +    Object v = rexLiteral.getValue();
    +    RelDataType ty = rexLiteral.getType();
    +    switch(rexLiteral.getTypeName()) {
    +      case BOOLEAN:
    +        return v.toString();
    +      case CHAR:
    +        return CompilerUtil.escapeJavaString(((NlsString) v).getValue(), true);
    +      case NULL:
    +        return "null";
    +      case DOUBLE:
    +      case BIGINT:
    +      case DECIMAL:
    +        switch (ty.getSqlTypeName()) {
    +          case TINYINT:
    +          case SMALLINT:
    +          case INTEGER:
    +            return Long.toString(((BigDecimal) v).longValueExact());
    +          case BIGINT:
    +            return Long.toString(((BigDecimal)v).longValueExact()) + 'L';
    +          case DECIMAL:
    +          case FLOAT:
    +          case REAL:
    +          case DOUBLE:
    +            return Util.toScientificNotation((BigDecimal) v);
    +        }
    +        break;
    +      default:
    +        throw new UnsupportedOperationException();
    +    }
    +    return null;
    +  }
    +
    +  @Override
    +  public String visitCall(RexCall rexCall) {
    +    return IMP_TABLE.compile(this, rexCall);
    +  }
    +
    +  @Override
    +  public String visitOver(RexOver rexOver) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  @Override
    +  public String visitCorrelVariable(
    +      RexCorrelVariable rexCorrelVariable) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  @Override
    +  public String visitDynamicParam(
    +      RexDynamicParam rexDynamicParam) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  @Override
    +  public String visitRangeRef(RexRangeRef rexRangeRef) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  @Override
    +  public String visitFieldAccess(
    +      RexFieldAccess rexFieldAccess) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  private String printExpr(RexNode node, String definition) {
    +    String name = "t" + expr.size();
    +    Type ty = typeFactory.getJavaClass(node.getType());
    +    String typeName = ((Class<?>)ty).getCanonicalName();
    +    pw.append(
    +        String.format("%s %s = (%s)(%s);\n", typeName, name, typeName, definition));
    +    return name;
    +  }
    +
    +  private interface CallExprPrinter {
    +    String translate(ExprCompiler compiler, RexCall call);
    +  }
    +
    +  private static class ImpTable {
    --- End diff --
    
    what is ImpTable can you add some details here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: STORM-1097. Compile logical plans to Java sour...

Posted by harshach <gi...@git.apache.org>.
Github user harshach commented on the pull request:

    https://github.com/apache/storm/pull/814#issuecomment-151293711
  
    +1


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: STORM-1097. Compile logical plans to Java sour...

Posted by harshach <gi...@git.apache.org>.
Github user harshach commented on a diff in the pull request:

    https://github.com/apache/storm/pull/814#discussion_r42936898
  
    --- Diff: external/sql/storm-sql-core/src/jvm/org/apache/storm/sql/compiler/ExprCompiler.java ---
    @@ -0,0 +1,210 @@
    +/**
    + * 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.storm.sql.compiler;
    +
    +import com.google.common.collect.ImmutableMap;
    +import org.apache.calcite.adapter.java.JavaTypeFactory;
    +import org.apache.calcite.rel.type.RelDataType;
    +import org.apache.calcite.rex.RexCall;
    +import org.apache.calcite.rex.RexCorrelVariable;
    +import org.apache.calcite.rex.RexDynamicParam;
    +import org.apache.calcite.rex.RexFieldAccess;
    +import org.apache.calcite.rex.RexInputRef;
    +import org.apache.calcite.rex.RexLiteral;
    +import org.apache.calcite.rex.RexLocalRef;
    +import org.apache.calcite.rex.RexNode;
    +import org.apache.calcite.rex.RexOver;
    +import org.apache.calcite.rex.RexRangeRef;
    +import org.apache.calcite.rex.RexVisitor;
    +import org.apache.calcite.sql.SqlOperator;
    +import org.apache.calcite.util.NlsString;
    +import org.apache.calcite.util.Util;
    +
    +import java.io.PrintWriter;
    +import java.lang.reflect.Type;
    +import java.math.BigDecimal;
    +import java.util.AbstractMap;
    +import java.util.IdentityHashMap;
    +import java.util.Map;
    +
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.DIVIDE;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.DIVIDE_INTEGER;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.GREATER_THAN;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.GREATER_THAN_OR_EQUAL;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LESS_THAN;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.LESS_THAN_OR_EQUAL;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.MINUS;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.MULTIPLY;
    +import static org.apache.calcite.sql.fun.SqlStdOperatorTable.PLUS;
    +
    +/**
    + * Compile RexNode on top of the Tuple abstraction.
    + */
    +class ExprCompiler implements RexVisitor<String> {
    +  private final PrintWriter pw;
    +  private final Map<RexNode, String> expr = new IdentityHashMap<>();
    +  private final JavaTypeFactory typeFactory;
    +  private static final ImpTable IMP_TABLE = new ImpTable();
    +
    +  ExprCompiler(PrintWriter pw, JavaTypeFactory typeFactory) {
    +    this.pw = pw;
    +    this.typeFactory = typeFactory;
    +  }
    +
    +  @Override
    +  public String visitInputRef(RexInputRef rexInputRef) {
    +    if (expr.containsKey(rexInputRef)) {
    +      return expr.get(rexInputRef);
    +    }
    +    String name = printExpr(rexInputRef, String.format("_data.get(%d)",
    +                            rexInputRef.getIndex()));
    +    expr.put(rexInputRef, name);
    +    return name;
    +  }
    +
    +  @Override
    +  public String visitLocalRef(RexLocalRef rexLocalRef) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  @Override
    +  public String visitLiteral(RexLiteral rexLiteral) {
    +    Object v = rexLiteral.getValue();
    +    RelDataType ty = rexLiteral.getType();
    +    switch(rexLiteral.getTypeName()) {
    +      case BOOLEAN:
    +        return v.toString();
    +      case CHAR:
    +        return CompilerUtil.escapeJavaString(((NlsString) v).getValue(), true);
    +      case NULL:
    +        return "null";
    +      case DOUBLE:
    +      case BIGINT:
    +      case DECIMAL:
    +        switch (ty.getSqlTypeName()) {
    +          case TINYINT:
    +          case SMALLINT:
    +          case INTEGER:
    +            return Long.toString(((BigDecimal) v).longValueExact());
    +          case BIGINT:
    +            return Long.toString(((BigDecimal)v).longValueExact()) + 'L';
    +          case DECIMAL:
    +          case FLOAT:
    +          case REAL:
    +          case DOUBLE:
    +            return Util.toScientificNotation((BigDecimal) v);
    +        }
    +        break;
    +      default:
    +        throw new UnsupportedOperationException();
    +    }
    +    return null;
    +  }
    +
    +  @Override
    +  public String visitCall(RexCall rexCall) {
    +    return IMP_TABLE.compile(this, rexCall);
    +  }
    +
    +  @Override
    +  public String visitOver(RexOver rexOver) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  @Override
    +  public String visitCorrelVariable(
    +      RexCorrelVariable rexCorrelVariable) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  @Override
    +  public String visitDynamicParam(
    +      RexDynamicParam rexDynamicParam) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  @Override
    +  public String visitRangeRef(RexRangeRef rexRangeRef) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  @Override
    +  public String visitFieldAccess(
    +      RexFieldAccess rexFieldAccess) {
    +    throw new UnsupportedOperationException();
    +  }
    +
    +  private String printExpr(RexNode node, String definition) {
    +    String name = "t" + expr.size();
    +    Type ty = typeFactory.getJavaClass(node.getType());
    +    String typeName = ((Class<?>)ty).getCanonicalName();
    +    pw.append(
    +        String.format("%s %s = (%s)(%s);\n", typeName, name, typeName, definition));
    +    return name;
    +  }
    +
    +  private interface CallExprPrinter {
    +    String translate(ExprCompiler compiler, RexCall call);
    +  }
    +
    +  private static class ImpTable {
    +    private final Map<SqlOperator, CallExprPrinter> translators;
    +
    +    private ImpTable() {
    +      ImmutableMap.Builder<SqlOperator, CallExprPrinter> builder =
    +          ImmutableMap.builder();
    +      builder.put(infixBinary(LESS_THAN, "<"))
    +          .put(infixBinary(LESS_THAN_OR_EQUAL, "<="))
    +          .put(infixBinary(GREATER_THAN, ">"))
    +          .put(infixBinary(GREATER_THAN_OR_EQUAL, ">="))
    +          .put(infixBinary(PLUS, "+"))
    +          .put(infixBinary(MINUS, "-"))
    +          .put(infixBinary(MULTIPLY, "*"))
    +          .put(infixBinary(DIVIDE, "/"))
    +          .put(infixBinary(DIVIDE_INTEGER, "/"));
    +      this.translators = builder.build();
    +    }
    +
    +    private String compile(ExprCompiler compiler, RexCall call) {
    +      SqlOperator op = call.getOperator();
    +      CallExprPrinter printer = translators.get(op);
    +      if (printer == null) {
    +        throw new UnsupportedOperationException();
    +      } else {
    +        return printer.translate(compiler, call);
    +      }
    +    }
    +    private Map.Entry<SqlOperator, CallExprPrinter> infixBinary
    --- End diff --
    
    new line here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: STORM-1097. Compile logical plans to Java sour...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/storm/pull/814


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: STORM-1097. Compile logical plans to Java sour...

Posted by haohui <gi...@git.apache.org>.
Github user haohui commented on the pull request:

    https://github.com/apache/storm/pull/814#issuecomment-151210974
  
    Thanks @harshach for the reviews.
    
    ImpTable maps the SQL operators to the classes that understand the SQL operators and generate corresponding Java source code.
    
    I updated the patch and added comments for the class.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] storm pull request: STORM-1097. Compile logical plans to Java sour...

Posted by harshach <gi...@git.apache.org>.
Github user harshach commented on the pull request:

    https://github.com/apache/storm/pull/814#issuecomment-150840662
  
    @haohui overall looks good to me. couple of minor nitpicks.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---