org.apache.calcite.prepare.CalcitePrepareImpl Java Examples

The following examples show how to use org.apache.calcite.prepare.CalcitePrepareImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HiveEnumerableInterpretable.java    From marble with Apache License 2.0 6 votes vote down vote up
static Bindable getBindable(ClassDeclaration expr, String s, int fieldCount)
    throws CompileException, IOException {
  ICompilerFactory compilerFactory;
  try {
    compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
  } catch (Exception e) {
    throw new IllegalStateException(
        "Unable to instantiate java compiler", e);
  }
  IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
  cbe.setClassName(expr.name);
  cbe.setExtendedClass(Utilities.class);
  cbe.setImplementedInterfaces(
      fieldCount == 1
          ? new Class[]{Bindable.class, Typed.class}
          : new Class[]{ArrayBindable.class});
  cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader());
  if (CalcitePrepareImpl.DEBUG) {
    // Add line numbers to the generated janino class
    cbe.setDebuggingInformation(true, true, true);
  }
  return (Bindable) cbe.createInstance(new StringReader(s));
}
 
Example #2
Source File: Frameworks.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes a container then calls user-specified code with a planner
 * and statement.
 *
 * @param action Callback containing user-specified code
 * @return Return value from action
 */
public static <R> R withPrepare(FrameworkConfig config,
    BasePrepareAction<R> action) {
  try {
    final Properties info = new Properties();
    if (config.getTypeSystem() != RelDataTypeSystem.DEFAULT) {
      info.setProperty(CalciteConnectionProperty.TYPE_SYSTEM.camelName(),
          config.getTypeSystem().getClass().getName());
    }
    Connection connection =
        DriverManager.getConnection("jdbc:calcite:", info);
    final CalciteServerStatement statement =
        connection.createStatement()
            .unwrap(CalciteServerStatement.class);
    return new CalcitePrepareImpl().perform(statement, config, action);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example #3
Source File: HiveEnumerableInterpretable.java    From marble with Apache License 2.0 5 votes vote down vote up
public static Bindable toBindable(Map<String, Object> parameters,
    CalcitePrepare.SparkHandler spark, EnumerableRel rel,
    EnumerableRel.Prefer prefer) {
  HiveEnumerableRelImplementor relImplementor =
      new HiveEnumerableRelImplementor(rel.getCluster().getRexBuilder(),
          parameters);

  final ClassDeclaration expr = relImplementor.implementRoot(rel, prefer);
  String s = Expressions.toString(expr.memberDeclarations, "\n", false);

  if (CalcitePrepareImpl.DEBUG) {
    Util.debugCode(System.out, s);
  }

  Hook.JAVA_PLAN.run(s);

  try {
    if (spark != null && spark.enabled()) {
      return spark.compile(expr, s);
    } else {
      return getBindable(expr, s,
          rel.getRowType().getFieldCount());
    }
  } catch (Exception e) {
    throw Helper.INSTANCE.wrap("Error while compiling generated Java code:\n"
        + s, e);
  }
}
 
Example #4
Source File: HiveRexExecutorImpl.java    From marble with Apache License 2.0 5 votes vote down vote up
private String compile(RexBuilder rexBuilder, List<RexNode> constExps,
    RexToLixTranslator.InputGetter getter, RelDataType rowType) {
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(rowType, rexBuilder);
  for (RexNode node : constExps) {
    programBuilder.addProject(
        node, "c" + programBuilder.getProjectList().size());
  }
  final JavaTypeFactoryImpl javaTypeFactory =
      new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
  final BlockBuilder blockBuilder = new BlockBuilder();
  final ParameterExpression root0_ =
      Expressions.parameter(Object.class, "root0");
  final ParameterExpression root_ = DataContext.ROOT;
  blockBuilder.add(
      Expressions.declare(
          Modifier.FINAL, root_,
          Expressions.convert_(root0_, DataContext.class)));
  final SqlConformance conformance = SqlConformanceEnum.HIVE;
  final RexProgram program = programBuilder.getProgram();
  final List<Expression> expressions =
      RexToLixTranslator.translateProjects(program, javaTypeFactory,
          conformance, blockBuilder, null, root_, getter, null);
  blockBuilder.add(
      Expressions.return_(null,
          Expressions.newArrayInit(Object[].class, expressions)));
  final MethodDeclaration methodDecl =
      Expressions.methodDecl(Modifier.PUBLIC, Object[].class,
          BuiltInMethod.FUNCTION1_APPLY.method.getName(),
          ImmutableList.of(root0_), blockBuilder.toBlock());
  String code = Expressions.toString(methodDecl);
  if (CalcitePrepareImpl.DEBUG) {
    Util.debugCode(System.out, code);
  }
  return code;
}
 
Example #5
Source File: CalcitePrepare.java    From calcite with Apache License 2.0 5 votes vote down vote up
public ParseResult(CalcitePrepareImpl prepare, SqlValidator validator,
    String sql,
    SqlNode sqlNode, RelDataType rowType) {
  super();
  this.prepare = prepare;
  this.sql = sql;
  this.sqlNode = sqlNode;
  this.rowType = rowType;
  this.typeFactory = validator.getTypeFactory();
}
 
Example #6
Source File: CalcitePrepare.java    From calcite with Apache License 2.0 5 votes vote down vote up
public AnalyzeViewResult(CalcitePrepareImpl prepare,
    SqlValidator validator, String sql, SqlNode sqlNode,
    RelDataType rowType, RelRoot root, Table table,
    ImmutableList<String> tablePath, RexNode constraint,
    ImmutableIntList columnMapping, boolean modifiable) {
  super(prepare, validator, sql, sqlNode, rowType, root);
  this.table = table;
  this.tablePath = tablePath;
  this.constraint = constraint;
  this.columnMapping = columnMapping;
  this.modifiable = modifiable;
  Preconditions.checkArgument(modifiable == (table != null));
}
 
Example #7
Source File: OLAPTableScan.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public void register(RelOptPlanner planner) {
    // force clear the query context before traversal relational operators
    OLAPContext.clearThreadLocalContexts();

    // register OLAP rules
    addRules(planner, kylinConfig.getCalciteAddRule());

    planner.addRule(OLAPToEnumerableConverterRule.INSTANCE);
    planner.addRule(OLAPFilterRule.INSTANCE);
    planner.addRule(OLAPProjectRule.INSTANCE);
    planner.addRule(OLAPAggregateRule.INSTANCE);
    planner.addRule(OLAPJoinRule.INSTANCE);
    planner.addRule(OLAPLimitRule.INSTANCE);
    planner.addRule(OLAPSortRule.INSTANCE);
    planner.addRule(OLAPUnionRule.INSTANCE);
    planner.addRule(OLAPWindowRule.INSTANCE);
    planner.addRule(OLAPValuesRule.INSTANCE);

    planner.addRule(AggregateProjectReduceRule.INSTANCE);

    // CalcitePrepareImpl.CONSTANT_REDUCTION_RULES
    if (kylinConfig.isReduceExpressionsRulesEnabled()) {
        planner.addRule(ReduceExpressionsRule.PROJECT_INSTANCE);
        planner.addRule(ReduceExpressionsRule.FILTER_INSTANCE);
        planner.addRule(ReduceExpressionsRule.CALC_INSTANCE);
        planner.addRule(ReduceExpressionsRule.JOIN_INSTANCE);
    }
    // the ValuesReduceRule breaks query test somehow...
    //        planner.addRule(ValuesReduceRule.FILTER_INSTANCE);
    //        planner.addRule(ValuesReduceRule.PROJECT_FILTER_INSTANCE);
    //        planner.addRule(ValuesReduceRule.PROJECT_INSTANCE);

    removeRules(planner, kylinConfig.getCalciteRemoveRule());
    if (!kylinConfig.isEnumerableRulesEnabled()) {
        for (RelOptRule rule : CalcitePrepareImpl.ENUMERABLE_RULES) {
            planner.removeRule(rule);
        }
    }
    // since join is the entry point, we can't push filter past join
    planner.removeRule(FilterJoinRule.FILTER_ON_JOIN);
    planner.removeRule(FilterJoinRule.JOIN);

    // since we don't have statistic of table, the optimization of join is too cost
    planner.removeRule(JoinCommuteRule.INSTANCE);
    planner.removeRule(JoinPushThroughJoinRule.LEFT);
    planner.removeRule(JoinPushThroughJoinRule.RIGHT);

    // keep tree structure like filter -> aggregation -> project -> join/table scan, implementOLAP() rely on this tree pattern
    planner.removeRule(AggregateJoinTransposeRule.INSTANCE);
    planner.removeRule(AggregateProjectMergeRule.INSTANCE);
    planner.removeRule(FilterProjectTransposeRule.INSTANCE);
    planner.removeRule(SortJoinTransposeRule.INSTANCE);
    planner.removeRule(JoinPushExpressionsRule.INSTANCE);
    planner.removeRule(SortUnionTransposeRule.INSTANCE);
    planner.removeRule(JoinUnionTransposeRule.LEFT_UNION);
    planner.removeRule(JoinUnionTransposeRule.RIGHT_UNION);
    planner.removeRule(AggregateUnionTransposeRule.INSTANCE);
    planner.removeRule(DateRangeRules.FILTER_INSTANCE);
    planner.removeRule(SemiJoinRule.JOIN);
    planner.removeRule(SemiJoinRule.PROJECT);
    // distinct count will be split into a separated query that is joined with the left query
    planner.removeRule(AggregateExpandDistinctAggregatesRule.INSTANCE);

    // see Dec 26th email @ http://mail-archives.apache.org/mod_mbox/calcite-dev/201412.mbox/browser
    planner.removeRule(ExpandConversionRule.INSTANCE);
}
 
Example #8
Source File: QueryService.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
private SQLResponse getPrepareOnlySqlResponse(String projectName, String correctedSql, Connection conn,
        Boolean isPushDown, List<List<String>> results, List<SelectedColumnMeta> columnMetas) throws SQLException {

    CalcitePrepareImpl.KYLIN_ONLY_PREPARE.set(true);

    PreparedStatement preparedStatement = null;
    try {
        preparedStatement = conn.prepareStatement(correctedSql);
        throw new IllegalStateException("Should have thrown OnlyPrepareEarlyAbortException");
    } catch (Exception e) {
        Throwable rootCause = ExceptionUtils.getRootCause(e);
        if (rootCause != null && rootCause instanceof OnlyPrepareEarlyAbortException) {
            OnlyPrepareEarlyAbortException abortException = (OnlyPrepareEarlyAbortException) rootCause;
            CalcitePrepare.Context context = abortException.getContext();
            CalcitePrepare.ParseResult preparedResult = abortException.getPreparedResult();
            List<RelDataTypeField> fieldList = preparedResult.rowType.getFieldList();

            CalciteConnectionConfig config = context.config();

            // Fill in selected column meta
            for (int i = 0; i < fieldList.size(); ++i) {

                RelDataTypeField field = fieldList.get(i);
                String columnName = field.getKey();

                if (columnName.startsWith("_KY_")) {
                    continue;
                }
                BasicSqlType basicSqlType = (BasicSqlType) field.getValue();

                columnMetas.add(new SelectedColumnMeta(false, config.caseSensitive(), false, false,
                        basicSqlType.isNullable() ? 1 : 0, true, basicSqlType.getPrecision(), columnName,
                        columnName, null, null, null, basicSqlType.getPrecision(),
                        basicSqlType.getScale() < 0 ? 0 : basicSqlType.getScale(),
                        basicSqlType.getSqlTypeName().getJdbcOrdinal(), basicSqlType.getSqlTypeName().getName(),
                        true, false, false));
            }

        } else {
            throw e;
        }
    } finally {
        CalcitePrepareImpl.KYLIN_ONLY_PREPARE.set(false);
        DBUtils.closeQuietly(preparedStatement);
    }

    return buildSqlResponse(projectName, isPushDown, results, columnMetas);
}
 
Example #9
Source File: OLAPTableScan.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public void register(RelOptPlanner planner) {
    // force clear the query context before traversal relational operators
    OLAPContext.clearThreadLocalContexts();

    // register OLAP rules
    addRules(planner, kylinConfig.getCalciteAddRule());

    planner.addRule(OLAPToEnumerableConverterRule.INSTANCE);
    planner.addRule(OLAPFilterRule.INSTANCE);
    planner.addRule(OLAPProjectRule.INSTANCE);
    planner.addRule(OLAPAggregateRule.INSTANCE);
    planner.addRule(OLAPJoinRule.INSTANCE);
    planner.addRule(OLAPLimitRule.INSTANCE);
    planner.addRule(OLAPSortRule.INSTANCE);
    planner.addRule(OLAPUnionRule.INSTANCE);
    planner.addRule(OLAPWindowRule.INSTANCE);
    planner.addRule(OLAPValuesRule.INSTANCE);

    planner.addRule(AggregateProjectReduceRule.INSTANCE);

    // CalcitePrepareImpl.CONSTANT_REDUCTION_RULES
    if (kylinConfig.isReduceExpressionsRulesEnabled()) {
        planner.addRule(ReduceExpressionsRule.PROJECT_INSTANCE);
        planner.addRule(ReduceExpressionsRule.FILTER_INSTANCE);
        planner.addRule(ReduceExpressionsRule.CALC_INSTANCE);
        planner.addRule(ReduceExpressionsRule.JOIN_INSTANCE);
    }
    // the ValuesReduceRule breaks query test somehow...
    //        planner.addRule(ValuesReduceRule.FILTER_INSTANCE);
    //        planner.addRule(ValuesReduceRule.PROJECT_FILTER_INSTANCE);
    //        planner.addRule(ValuesReduceRule.PROJECT_INSTANCE);

    removeRules(planner, kylinConfig.getCalciteRemoveRule());
    if (!kylinConfig.isEnumerableRulesEnabled()) {
        for (RelOptRule rule : CalcitePrepareImpl.ENUMERABLE_RULES) {
            planner.removeRule(rule);
        }
    }
    // since join is the entry point, we can't push filter past join
    planner.removeRule(FilterJoinRule.FILTER_ON_JOIN);
    planner.removeRule(FilterJoinRule.JOIN);

    // since we don't have statistic of table, the optimization of join is too cost
    planner.removeRule(JoinCommuteRule.INSTANCE);
    planner.removeRule(JoinPushThroughJoinRule.LEFT);
    planner.removeRule(JoinPushThroughJoinRule.RIGHT);

    // keep tree structure like filter -> aggregation -> project -> join/table scan, implementOLAP() rely on this tree pattern
    planner.removeRule(AggregateJoinTransposeRule.INSTANCE);
    planner.removeRule(AggregateProjectMergeRule.INSTANCE);
    planner.removeRule(FilterProjectTransposeRule.INSTANCE);
    planner.removeRule(SortJoinTransposeRule.INSTANCE);
    planner.removeRule(JoinPushExpressionsRule.INSTANCE);
    planner.removeRule(SortUnionTransposeRule.INSTANCE);
    planner.removeRule(JoinUnionTransposeRule.LEFT_UNION);
    planner.removeRule(JoinUnionTransposeRule.RIGHT_UNION);
    planner.removeRule(AggregateUnionTransposeRule.INSTANCE);
    planner.removeRule(DateRangeRules.FILTER_INSTANCE);
    planner.removeRule(SemiJoinRule.JOIN);
    planner.removeRule(SemiJoinRule.PROJECT);
    // distinct count will be split into a separated query that is joined with the left query
    planner.removeRule(AggregateExpandDistinctAggregatesRule.INSTANCE);

    // see Dec 26th email @ http://mail-archives.apache.org/mod_mbox/calcite-dev/201412.mbox/browser
    planner.removeRule(ExpandConversionRule.INSTANCE);

    // KYLIN-4464 do not pushdown sort when there is a window function in projection
    planner.removeRule(SortProjectTransposeRule.INSTANCE);
    planner.addRule(KylinSortProjectTransposeRule.INSTANCE);
}
 
Example #10
Source File: QueryService.java    From kylin with Apache License 2.0 4 votes vote down vote up
private SQLResponse getPrepareOnlySqlResponse(String projectName, String correctedSql, Connection conn,
        Boolean isPushDown, List<List<String>> results, List<SelectedColumnMeta> columnMetas) throws SQLException {

    CalcitePrepareImpl.KYLIN_ONLY_PREPARE.set(true);

    PreparedStatement preparedStatement = null;
    try {
        preparedStatement = conn.prepareStatement(correctedSql);
        throw new IllegalStateException("Should have thrown OnlyPrepareEarlyAbortException");
    } catch (Exception e) {
        Throwable rootCause = ExceptionUtils.getRootCause(e);
        if (rootCause != null && rootCause instanceof OnlyPrepareEarlyAbortException) {
            OnlyPrepareEarlyAbortException abortException = (OnlyPrepareEarlyAbortException) rootCause;
            CalcitePrepare.Context context = abortException.getContext();
            CalcitePrepare.ParseResult preparedResult = abortException.getPreparedResult();
            List<RelDataTypeField> fieldList = preparedResult.rowType.getFieldList();

            CalciteConnectionConfig config = context.config();

            // Fill in selected column meta
            for (int i = 0; i < fieldList.size(); ++i) {

                RelDataTypeField field = fieldList.get(i);
                String columnName = field.getKey();

                if (columnName.startsWith("_KY_")) {
                    continue;
                }
                BasicSqlType basicSqlType = (BasicSqlType) field.getValue();

                columnMetas.add(new SelectedColumnMeta(false, config.caseSensitive(), false, false,
                        basicSqlType.isNullable() ? 1 : 0, true, basicSqlType.getPrecision(), columnName,
                        columnName, null, null, null, basicSqlType.getPrecision(),
                        basicSqlType.getScale() < 0 ? 0 : basicSqlType.getScale(),
                        basicSqlType.getSqlTypeName().getJdbcOrdinal(), basicSqlType.getSqlTypeName().getName(),
                        true, false, false));
            }

        } else {
            throw e;
        }
    } finally {
        CalcitePrepareImpl.KYLIN_ONLY_PREPARE.set(false);
        DBUtils.closeQuietly(preparedStatement);
    }

    return buildSqlResponse(projectName, isPushDown, results, columnMetas);
}
 
Example #11
Source File: CalcitePrepare.java    From calcite with Apache License 2.0 4 votes vote down vote up
public ConvertResult(CalcitePrepareImpl prepare, SqlValidator validator,
    String sql, SqlNode sqlNode, RelDataType rowType, RelRoot root) {
  super(prepare, validator, sql, sqlNode, rowType);
  this.root = root;
}