org.apache.calcite.config.CalciteSystemProperty Java Examples

The following examples show how to use org.apache.calcite.config.CalciteSystemProperty. 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: JaninoRexCompiler.java    From calcite with Apache License 2.0 6 votes vote down vote up
static Scalar getScalar(ClassDeclaration expr, String s)
    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.setImplementedInterfaces(new Class[]{Scalar.class});
  cbe.setParentClassLoader(JaninoRexCompiler.class.getClassLoader());
  if (CalciteSystemProperty.DEBUG.value()) {
    // Add line numbers to the generated janino class
    cbe.setDebuggingInformation(true, true, true);
  }
  return (Scalar) cbe.createInstance(new StringReader(s));
}
 
Example #2
Source File: DruidAdapter2IT.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testMetadataColumns() throws Exception {
  sql("values 1")
      .withConnection(c -> {
        try {
          final DatabaseMetaData metaData = c.getMetaData();
          final ResultSet r =
              metaData.getColumns(null, null, "foodmart", null);
          Multimap<String, Boolean> map = ArrayListMultimap.create();
          while (r.next()) {
            map.put(r.getString("TYPE_NAME"), true);
          }
          if (CalciteSystemProperty.DEBUG.value()) {
            System.out.println(map);
          }
          // 1 timestamp, 2 float measure, 1 int measure, 88 dimensions
          assertThat(map.keySet().size(), is(4));
          assertThat(map.values().size(), is(92));
          assertThat(map.get("TIMESTAMP(0) NOT NULL").size(), is(1));
          assertThat(map.get("DOUBLE").size(), is(2));
          assertThat(map.get("BIGINT").size(), is(1));
          assertThat(map.get(VARCHAR_TYPE).size(), is(88));
        } catch (SQLException e) {
          throw TestUtil.rethrow(e);
        }
      });
}
 
Example #3
Source File: DruidConnectionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Reads data source names from Druid. */
Set<String> tableNames() {
  final Map<String, String> requestHeaders =
      ImmutableMap.of("Content-Type", "application/json");
  final String data = null;
  final String url = coordinatorUrl + "/druid/coordinator/v1/metadata/datasources";
  if (CalciteSystemProperty.DEBUG.value()) {
    System.out.println("Druid: table names" + data + "; " + url);
  }
  try (InputStream in0 = post(url, data, requestHeaders, 10000, 1800000);
       InputStream in = traceResponse(in0)) {
    final ObjectMapper mapper = new ObjectMapper();
    final CollectionType listType =
        mapper.getTypeFactory().constructCollectionType(List.class,
            String.class);
    final List<String> list = mapper.readValue(in, listType);
    return ImmutableSet.copyOf(list);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #4
Source File: DruidConnectionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Executes a query request.
 *
 * @param queryType Query type
 * @param data Data to post
 * @param sink Sink to which to send the parsed rows
 * @param fieldNames Names of fields
 * @param fieldTypes Types of fields (never null, but elements may be null)
 * @param page Page definition (in/out)
 */
public void request(QueryType queryType, String data, Sink sink,
    List<String> fieldNames, List<ColumnMetaData.Rep> fieldTypes,
    Page page) {
  final String url = this.url + "/druid/v2/?pretty";
  final Map<String, String> requestHeaders =
      ImmutableMap.of("Content-Type", "application/json");
  if (CalciteSystemProperty.DEBUG.value()) {
    System.out.println(data);
  }
  try (InputStream in0 = post(url, data, requestHeaders, 10000, 1800000);
       InputStream in = traceResponse(in0)) {
    parse(queryType, in, sink, fieldNames, fieldTypes, page);
  } catch (IOException e) {
    throw new RuntimeException("Error while processing druid request ["
        + data + "]", e);
  }
}
 
Example #5
Source File: VolcanoPlanner.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Dumps the internal state of this VolcanoPlanner to a writer.
 *
 * @param pw Print writer
 * @see #normalizePlan(String)
 */
public void dump(PrintWriter pw) {
  pw.println("Root: " + root);
  pw.println("Original rel:");

  if (originalRoot != null) {
    originalRoot.explain(
        new RelWriterImpl(pw, SqlExplainLevel.ALL_ATTRIBUTES, false));
  }

  try {
    if (CalciteSystemProperty.DUMP_SETS.value()) {
      pw.println();
      pw.println("Sets:");
      Dumpers.dumpSets(this, pw);
    }
    if (CalciteSystemProperty.DUMP_GRAPHVIZ.value()) {
      pw.println();
      pw.println("Graphviz:");
      Dumpers.dumpGraphviz(this, pw);
    }
  } catch (Exception | AssertionError e) {
    pw.println("Error when dumping plan state: \n"
        + e);
  }
}
 
Example #6
Source File: ProfilerImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
boolean offer(double d) {
  boolean b;
  if (count++ < warmUpCount || d > priorityQueue.peek()) {
    if (priorityQueue.size() >= size) {
      priorityQueue.remove(deque.pop());
    }
    priorityQueue.add(d);
    deque.add(d);
    b = true;
  } else {
    b = false;
  }
  if (CalciteSystemProperty.DEBUG.value()) {
    System.out.println("offer " + d
        + " min " + priorityQueue.peek()
        + " accepted " + b);
  }
  return b;
}
 
Example #7
Source File: RelOptMaterialization.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a relational expression to a form where
 * {@link org.apache.calcite.rel.logical.LogicalJoin}s are
 * as close to leaves as possible.
 */
public static RelNode toLeafJoinForm(RelNode rel) {
  final Program program = Programs.hep(
      ImmutableList.of(
          JoinProjectTransposeRule.RIGHT_PROJECT,
          JoinProjectTransposeRule.LEFT_PROJECT,
          FilterJoinRule.FilterIntoJoinRule.FILTER_ON_JOIN,
          ProjectRemoveRule.INSTANCE,
          ProjectMergeRule.INSTANCE),
      false,
      DefaultRelMetadataProvider.INSTANCE);
  if (CalciteSystemProperty.DEBUG.value()) {
    System.out.println(
        RelOptUtil.dumpPlan("before", rel, SqlExplainFormat.TEXT,
            SqlExplainLevel.DIGEST_ATTRIBUTES));
  }
  final RelNode rel2 = program.run(null, rel, null,
      ImmutableList.of(),
      ImmutableList.of());
  if (CalciteSystemProperty.DEBUG.value()) {
    System.out.println(
        RelOptUtil.dumpPlan("after", rel2, SqlExplainFormat.TEXT,
            SqlExplainLevel.DIGEST_ATTRIBUTES));
  }
  return rel2;
}
 
Example #8
Source File: RelOptMaterialization.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a relational expression to a form where
 * {@link org.apache.calcite.rel.logical.LogicalJoin}s are
 * as close to leaves as possible.
 */
public static RelNode toLeafJoinForm(RelNode rel) {
  final Program program = Programs.hep(
      ImmutableList.of(
          JoinProjectTransposeRule.RIGHT_PROJECT,
          JoinProjectTransposeRule.LEFT_PROJECT,
          FilterJoinRule.FilterIntoJoinRule.FILTER_ON_JOIN,
          ProjectRemoveRule.INSTANCE,
          ProjectMergeRule.INSTANCE),
      false,
      DefaultRelMetadataProvider.INSTANCE);
  if (CalciteSystemProperty.DEBUG.value()) {
    System.out.println(
        RelOptUtil.dumpPlan("before", rel, SqlExplainFormat.TEXT,
            SqlExplainLevel.DIGEST_ATTRIBUTES));
  }
  final RelNode rel2 = program.run(null, rel, null,
      ImmutableList.of(),
      ImmutableList.of());
  if (CalciteSystemProperty.DEBUG.value()) {
    System.out.println(
        RelOptUtil.dumpPlan("after", rel2, SqlExplainFormat.TEXT,
            SqlExplainLevel.DIGEST_ATTRIBUTES));
  }
  return rel2;
}
 
Example #9
Source File: VolcanoPlanner.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Dumps the internal state of this VolcanoPlanner to a writer.
 *
 * @param pw Print writer
 * @see #normalizePlan(String)
 */
public void dump(PrintWriter pw) {
  pw.println("Root: " + root.getDescription());
  pw.println("Original rel:");

  if (originalRoot != null) {
    originalRoot.explain(
        new RelWriterImpl(pw, SqlExplainLevel.ALL_ATTRIBUTES, false));
  }
  if (CalciteSystemProperty.DUMP_SETS.value()) {
    pw.println();
    pw.println("Sets:");
    dumpSets(pw);
  }
  if (CalciteSystemProperty.DUMP_GRAPHVIZ.value()) {
    pw.println();
    pw.println("Graphviz:");
    dumpGraphviz(pw);
  }
}
 
Example #10
Source File: VolcanoPlanner.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void registerAbstractRelationalRules() {
  addRule(FilterJoinRule.FILTER_ON_JOIN);
  addRule(FilterJoinRule.JOIN);
  addRule(AbstractConverter.ExpandConversionRule.INSTANCE);
  addRule(JoinCommuteRule.INSTANCE);
  addRule(SemiJoinRule.PROJECT);
  addRule(SemiJoinRule.JOIN);
  if (CalciteSystemProperty.COMMUTE.value()) {
    addRule(JoinAssociateRule.INSTANCE);
  }
  addRule(AggregateRemoveRule.INSTANCE);
  addRule(UnionToDistinctRule.INSTANCE);
  addRule(ProjectRemoveRule.INSTANCE);
  addRule(AggregateJoinTransposeRule.INSTANCE);
  addRule(AggregateProjectMergeRule.INSTANCE);
  addRule(CalcRemoveRule.INSTANCE);
  addRule(SortRemoveRule.INSTANCE);

  // todo: rule which makes Project({OrdinalRef}) disappear
}
 
Example #11
Source File: CalciteRemoteDriverTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Check that the "get" conversion table looks like Table B-5 in JDBC 4.1
 * specification */
@Test void testTableB6() {
  SqlType[] columns = {
      SqlType.TINYINT, SqlType.SMALLINT, SqlType.INTEGER, SqlType.BIGINT,
      SqlType.REAL, SqlType.FLOAT, SqlType.DOUBLE, SqlType.DECIMAL,
      SqlType.NUMERIC, SqlType.BIT, SqlType.BOOLEAN, SqlType.CHAR,
      SqlType.VARCHAR, SqlType.LONGVARCHAR, SqlType.BINARY, SqlType.VARBINARY,
      SqlType.LONGVARBINARY, SqlType.DATE, SqlType.TIME, SqlType.TIMESTAMP,
      SqlType.CLOB, SqlType.BLOB, SqlType.ARRAY, SqlType.REF,
      SqlType.DATALINK, SqlType.STRUCT, SqlType.JAVA_OBJECT, SqlType.ROWID,
      SqlType.NCHAR, SqlType.NVARCHAR, SqlType.LONGNVARCHAR, SqlType.NCLOB,
      SqlType.SQLXML
  };
  final PrintWriter out =
      CalciteSystemProperty.DEBUG.value()
          ? Util.printWriter(System.out)
          : new PrintWriter(new StringWriter());
  for (SqlType.Method row : SqlType.Method.values()) {
    out.print(pad(row.methodName));
    for (SqlType column : columns) {
      out.print(SqlType.canGet(row, column) ? "x " : ". ");
    }
    out.println();
  }
}
 
Example #12
Source File: FrameworksTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2039">[CALCITE-2039]
 * AssertionError when pushing project to ProjectableFilterableTable</a>
 * using UPDATE via {@link Frameworks}. */
@Test void testUpdate() throws Exception {
  Table table = new TableImpl();
  final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
  SchemaPlus schema = rootSchema.add("x", new AbstractSchema());
  schema.add("MYTABLE", table);
  List<RelTraitDef> traitDefs = new ArrayList<>();
  traitDefs.add(ConventionTraitDef.INSTANCE);
  traitDefs.add(RelDistributionTraitDef.INSTANCE);
  SqlParser.Config parserConfig =
      SqlParser.configBuilder(SqlParser.Config.DEFAULT)
          .setCaseSensitive(false)
          .build();

  final FrameworkConfig config = Frameworks.newConfigBuilder()
      .parserConfig(parserConfig)
      .defaultSchema(schema)
      .traitDefs(traitDefs)
      // define the rules you want to apply
      .ruleSets(
          RuleSets.ofList(AbstractConverter.ExpandConversionRule.INSTANCE))
      .programs(Programs.ofRules(Programs.RULE_SET))
      .build();
  executeQuery(config, " UPDATE MYTABLE set id=7 where id=1",
      CalciteSystemProperty.DEBUG.value());
}
 
Example #13
Source File: MultiJdbcSchemaJoinTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Set<Integer> runQuery(Connection calciteConnection, String query)
    throws SQLException {
  // Print out the plan
  Statement stmt = calciteConnection.createStatement();
  try {
    ResultSet rs;
    if (CalciteSystemProperty.DEBUG.value()) {
      rs = stmt.executeQuery("explain plan for " + query);
      rs.next();
      System.out.println(rs.getString(1));
    }

    // Run the actual query
    rs = stmt.executeQuery(query);
    Set<Integer> ids = new HashSet<>();
    while (rs.next()) {
      ids.add(rs.getInt(1));
    }
    return ids;
  } finally {
    stmt.close();
  }
}
 
Example #14
Source File: RelOptUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Experimental
public static void registerAbstractRelationalRules(RelOptPlanner planner) {
  RelOptRules.ABSTRACT_RELATIONAL_RULES.forEach(planner::addRule);
  if (CalciteSystemProperty.COMMUTE.value()) {
    planner.addRule(JoinAssociateRule.INSTANCE);
  }
  // todo: rule which makes Project({OrdinalRef}) disappear
}
 
Example #15
Source File: EnumerableInterpretable.java    From calcite with Apache License 2.0 5 votes vote down vote up
static Bindable getBindable(ClassDeclaration expr, String s, int fieldCount)
    throws CompileException, IOException, ExecutionException {
  ICompilerFactory compilerFactory;
  try {
    compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
  } catch (Exception e) {
    throw new IllegalStateException(
        "Unable to instantiate java compiler", e);
  }
  final 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 (CalciteSystemProperty.DEBUG.value()) {
    // Add line numbers to the generated janino class
    cbe.setDebuggingInformation(true, true, true);
  }

  if (CalciteSystemProperty.BINDABLE_CACHE_MAX_SIZE.value() != 0) {
    StaticFieldDetector detector = new StaticFieldDetector();
    expr.accept(detector);
    if (!detector.containsStaticField) {
      return BINDABLE_CACHE.get(s, () -> (Bindable) cbe.createInstance(new StringReader(s)));
    }
  }
  return (Bindable) cbe.createInstance(new StringReader(s));
}
 
Example #16
Source File: EnumerableInterpretable.java    From calcite 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) {
  EnumerableRelImplementor relImplementor =
      new EnumerableRelImplementor(rel.getCluster().getRexBuilder(),
          parameters);

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

  if (CalciteSystemProperty.DEBUG.value()) {
    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 #17
Source File: CalciteException.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new CalciteException object.
 *
 * @param message error message
 * @param cause   underlying cause
 */
public CalciteException(
    String message,
    Throwable cause) {
  super(message, cause);

  // TODO: Force the caller to pass in a Logger as a trace argument for
  // better context.  Need to extend ResGen for this.
  LOGGER.trace("CalciteException", this);
  if (CalciteSystemProperty.DEBUG.value()) {
    LOGGER.error(toString());
  }
}
 
Example #18
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * @return the character-set prefix of an sql string literal; returns null
 * if there is none
 */
public static String getCharacterSet(String s) {
  if (s.charAt(0) == '\'') {
    return null;
  }
  if (Character.toUpperCase(s.charAt(0)) == 'N') {
    return CalciteSystemProperty.DEFAULT_NATIONAL_CHARSET.value();
  }
  int i = s.indexOf("'");
  return s.substring(1, i); // skip prefixed '_'
}
 
Example #19
Source File: EnumerableTableScan.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns whether EnumerableTableScan can generate code to handle a
 * particular variant of the Table SPI.
 **/
public static boolean canHandle(RelOptTable relOptTable) {
  Table table = relOptTable.unwrap(Table.class);
  if (table != null && !canHandle(table)) {
    return false;
  }
  boolean supportArray = CalciteSystemProperty.ENUMERABLE_ENABLE_TABLESCAN_ARRAY.value();
  boolean supportMap = CalciteSystemProperty.ENUMERABLE_ENABLE_TABLESCAN_MAP.value();
  boolean supportMultiset = CalciteSystemProperty.ENUMERABLE_ENABLE_TABLESCAN_MULTISET.value();
  if (supportArray && supportMap && supportMultiset) {
    return true;
  }
  // Struct fields are not supported in EnumerableTableScan
  for (RelDataTypeField field : relOptTable.getRowType().getFieldList()) {
    boolean unsupportedType = false;
    switch (field.getType().getSqlTypeName()) {
    case ARRAY:
      unsupportedType = supportArray;
      break;
    case MAP:
      unsupportedType = supportMap;
      break;
    case MULTISET:
      unsupportedType = supportMultiset;
      break;
    default:
      break;
    }
    if (unsupportedType) {
      return false;
    }
  }
  return true;
}
 
Example #20
Source File: SqlValidatorException.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new SqlValidatorException object.
 *
 * @param message error message
 * @param cause   underlying cause
 */
public SqlValidatorException(
    String message,
    Throwable cause) {
  super(message, cause);

  // TODO: see note in CalciteException constructor
  LOGGER.trace("SqlValidatorException", this);
  if (CalciteSystemProperty.DEBUG.value()) {
    LOGGER.error(toString());
  }
}
 
Example #21
Source File: RelOptMaterializations.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of RelNode transformed from all possible lattice uses.
 * @param rel       the original RelNode
 * @param lattices  the lattice list
 * @return the list of transformed RelNode together with their corresponding
 *         lattice used in the transformation.
 */
public static List<Pair<RelNode, RelOptLattice>> useLattices(
    final RelNode rel, List<RelOptLattice> lattices) {
  final Set<RelOptTable> queryTables = RelOptUtil.findTables(rel);
  // Use a lattice if the query uses at least the central (fact) table of the
  // lattice.
  final List<Pair<RelNode, RelOptLattice>> latticeUses = new ArrayList<>();
  final Set<List<String>> queryTableNames =
      Sets.newHashSet(
          Iterables.transform(queryTables, RelOptTable::getQualifiedName));
  // Remember leaf-join form of root so we convert at most once.
  final Supplier<RelNode> leafJoinRoot =
      Suppliers.memoize(() -> RelOptMaterialization.toLeafJoinForm(rel))::get;
  for (RelOptLattice lattice : lattices) {
    if (queryTableNames.contains(lattice.rootTable().getQualifiedName())) {
      RelNode rel2 = lattice.rewrite(leafJoinRoot.get());
      if (rel2 != null) {
        if (CalciteSystemProperty.DEBUG.value()) {
          System.out.println("use lattice:\n"
              + RelOptUtil.toString(rel2));
        }
        latticeUses.add(Pair.of(rel2, lattice));
      }
    }
  }

  return latticeUses;
}
 
Example #22
Source File: EnumerableAggregateBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected List<Type> createAggStateTypes(
    final List<Expression> initExpressions,
    final BlockBuilder initBlock,
    final List<AggImpState> aggs,
    JavaTypeFactory typeFactory) {
  final List<Type> aggStateTypes = new ArrayList<>();
  for (final AggImpState agg : aggs) {
    agg.context = new AggContextImpl(agg, typeFactory);
    final List<Type> state = agg.implementor.getStateType(agg.context);

    if (state.isEmpty()) {
      agg.state = ImmutableList.of();
      continue;
    }

    aggStateTypes.addAll(state);

    final List<Expression> decls = new ArrayList<>(state.size());
    for (int i = 0; i < state.size(); i++) {
      String aggName = "a" + agg.aggIdx;
      if (CalciteSystemProperty.DEBUG.value()) {
        aggName = Util.toJavaId(agg.call.getAggregation().getName(), 0)
            .substring("ID$0$".length()) + aggName;
      }
      Type type = state.get(i);
      ParameterExpression pe =
          Expressions.parameter(type,
              initBlock.newName(aggName + "s" + i));
      initBlock.add(Expressions.declare(0, pe, null));
      decls.add(pe);
    }
    agg.state = decls;
    initExpressions.addAll(decls);
    agg.implementor.implementReset(agg.context,
        new AggResultContextImpl(initBlock, agg.call, decls, null, null));
  }
  return aggStateTypes;
}
 
Example #23
Source File: RexExecutorImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static 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.DEFAULT;
  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 (CalciteSystemProperty.DEBUG.value()) {
    Util.debugCode(System.out, code);
  }
  return code;
}
 
Example #24
Source File: FrameworksTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-3228">[CALCITE-3228]
 * Error while applying rule ProjectScanRule:interpreter</a>
 *
 * <p>This bug appears under the following conditions:
 * 1) have an aggregate with group by and multi aggregate calls.
 * 2) the aggregate can be removed during optimization.
 * 3) all aggregate calls are simplified to the same reference.
 * */
@Test void testPushProjectToScan() throws Exception {
  Table table = new TableImpl();
  final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
  SchemaPlus schema = rootSchema.add("x", new AbstractSchema());
  schema.add("MYTABLE", table);
  List<RelTraitDef> traitDefs = new ArrayList<>();
  traitDefs.add(ConventionTraitDef.INSTANCE);
  traitDefs.add(RelDistributionTraitDef.INSTANCE);
  SqlParser.Config parserConfig =
          SqlParser.configBuilder(SqlParser.Config.DEFAULT)
                  .setCaseSensitive(false)
                  .build();

  final FrameworkConfig config = Frameworks.newConfigBuilder()
          .parserConfig(parserConfig)
          .defaultSchema(schema)
          .traitDefs(traitDefs)
          // define the rules you want to apply
          .ruleSets(
                  RuleSets.ofList(AbstractConverter.ExpandConversionRule.INSTANCE,
                          ProjectTableScanRule.INSTANCE))
          .programs(Programs.ofRules(Programs.RULE_SET))
          .build();

  executeQuery(config, "select min(id) as mi, max(id) as ma from mytable where id=1 group by id",
          CalciteSystemProperty.DEBUG.value());
}
 
Example #25
Source File: TraitPropagationTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testOne() throws Exception {
  RelNode planned = run(new PropAction(), RULES);
  if (CalciteSystemProperty.DEBUG.value()) {
    System.out.println(
        RelOptUtil.dumpPlan("LOGICAL PLAN", planned, SqlExplainFormat.TEXT,
            SqlExplainLevel.ALL_ATTRIBUTES));
  }
  final RelMetadataQuery mq = planned.getCluster().getMetadataQuery();
  assertEquals(3, 0, mq.getCumulativeCost(planned).getRows(),
      "Sortedness was not propagated");
}
 
Example #26
Source File: SparkHandlerImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public ArrayBindable compile(ClassDeclaration expr, String s) {
  final String className = "CalciteProgram" + classId.getAndIncrement();
  final String classFileName = className + ".java";
  String source = "public class " + className + "\n"
      + "    implements " + ArrayBindable.class.getName()
      + ", " + Serializable.class.getName()
      + " {\n"
      + s + "\n"
      + "}\n";

  if (CalciteSystemProperty.DEBUG.value()) {
    Util.debugCode(System.out, source);
  }

  JaninoCompiler compiler = new JaninoCompiler();
  compiler.getArgs().setDestdir(CLASS_DIR.getAbsolutePath());
  compiler.getArgs().setSource(source, classFileName);
  compiler.getArgs().setFullClassName(className);
  compiler.compile();
  try {
    @SuppressWarnings("unchecked")
    final Class<ArrayBindable> clazz =
        (Class<ArrayBindable>) compiler.getClassLoader().loadClass(className);
    final Constructor<ArrayBindable> constructor = clazz.getConstructor();
    return constructor.newInstance();
  } catch (ClassNotFoundException | InstantiationException
      | IllegalAccessException | NoSuchMethodException
      | InvocationTargetException e) {
    throw new RuntimeException(e);
  }
}
 
Example #27
Source File: SqlParserUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * @return the character-set prefix of an sql string literal; returns null
 * if there is none
 */
public static String getCharacterSet(String s) {
  if (s.charAt(0) == '\'') {
    return null;
  }
  if (Character.toUpperCase(s.charAt(0)) == 'N') {
    return CalciteSystemProperty.DEFAULT_NATIONAL_CHARSET.value();
  }
  int i = s.indexOf("'");
  return s.substring(1, i); // skip prefixed '_'
}
 
Example #28
Source File: SqlValidatorException.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new SqlValidatorException object.
 *
 * @param message error message
 * @param cause   underlying cause
 */
public SqlValidatorException(
    String message,
    Throwable cause) {
  super(message, cause);

  // TODO: see note in CalciteException constructor
  LOGGER.trace("SqlValidatorException", this);
  if (CalciteSystemProperty.DEBUG.value()) {
    LOGGER.error(toString());
  }
}
 
Example #29
Source File: CalciteException.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new CalciteException object.
 *
 * @param message error message
 * @param cause   underlying cause
 */
public CalciteException(
    String message,
    Throwable cause) {
  super(message, cause);

  // TODO: Force the caller to pass in a Logger as a trace argument for
  // better context.  Need to extend ResGen for this.
  LOGGER.trace("CalciteException", this);
  if (CalciteSystemProperty.DEBUG.value()) {
    LOGGER.error(toString());
  }
}
 
Example #30
Source File: RelOptMaterializations.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of RelNode transformed from all possible lattice uses.
 * @param rel       the original RelNode
 * @param lattices  the lattice list
 * @return the list of transformed RelNode together with their corresponding
 *         lattice used in the transformation.
 */
public static List<Pair<RelNode, RelOptLattice>> useLattices(
    final RelNode rel, List<RelOptLattice> lattices) {
  final Set<RelOptTable> queryTables = RelOptUtil.findTables(rel);
  // Use a lattice if the query uses at least the central (fact) table of the
  // lattice.
  final List<Pair<RelNode, RelOptLattice>> latticeUses = new ArrayList<>();
  final Set<List<String>> queryTableNames =
      Sets.newHashSet(
          Iterables.transform(queryTables, RelOptTable::getQualifiedName));
  // Remember leaf-join form of root so we convert at most once.
  final Supplier<RelNode> leafJoinRoot =
      Suppliers.memoize(() -> RelOptMaterialization.toLeafJoinForm(rel))::get;
  for (RelOptLattice lattice : lattices) {
    if (queryTableNames.contains(lattice.rootTable().getQualifiedName())) {
      RelNode rel2 = lattice.rewrite(leafJoinRoot.get());
      if (rel2 != null) {
        if (CalciteSystemProperty.DEBUG.value()) {
          System.out.println("use lattice:\n"
              + RelOptUtil.toString(rel2));
        }
        latticeUses.add(Pair.of(rel2, lattice));
      }
    }
  }

  return latticeUses;
}