org.apache.calcite.util.Bug Java Examples

The following examples show how to use org.apache.calcite.util.Bug. 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: RelMdDistinctRowCount.java    From Bats with Apache License 2.0 6 votes vote down vote up
public Double getDistinctRowCount(RelSubset rel, RelMetadataQuery mq,
    ImmutableBitSet groupKey, RexNode predicate) {
  final RelNode best = rel.getBest();
  if (best != null) {
    return mq.getDistinctRowCount(best, groupKey, predicate);
  }
  if (!Bug.CALCITE_1048_FIXED) {
    return getDistinctRowCount((RelNode) rel, mq, groupKey, predicate);
  }
  Double d = null;
  for (RelNode r2 : rel.getRels()) {
    try {
      Double d2 = mq.getDistinctRowCount(r2, groupKey, predicate);
      d = NumberUtil.min(d, d2);
    } catch (CyclicMetadataException e) {
      // Ignore this relational expression; there will be non-cyclic ones
      // in this set.
    }
  }
  return d;
}
 
Example #2
Source File: SqlLineTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Execute a script with "sqlline -f".
 *
 * @throws java.lang.Throwable On error
 * @return The stderr and stdout from running the script
 * @param args Script arguments
 */
private static Pair<SqlLine.Status, String> run(String... args)
    throws Throwable {
  SqlLine sqlline = new SqlLine();
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  PrintStream sqllineOutputStream =
      new PrintStream(os, false, StandardCharsets.UTF_8.name());
  sqlline.setOutputStream(sqllineOutputStream);
  sqlline.setErrorStream(sqllineOutputStream);
  SqlLine.Status status = SqlLine.Status.OK;

  Bug.upgrade("[sqlline-35] Make Sqlline.begin public");
  // TODO: status = sqlline.begin(args, null, false);

  return Pair.of(status, os.toString("UTF8"));
}
 
Example #3
Source File: RelMdPredicates.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** @see RelMetadataQuery#getPulledUpPredicates(RelNode) */
public RelOptPredicateList getPredicates(RelSubset r,
    RelMetadataQuery mq) {
  if (!Bug.CALCITE_1048_FIXED) {
    return RelOptPredicateList.EMPTY;
  }
  final RexBuilder rexBuilder = r.getCluster().getRexBuilder();
  RelOptPredicateList list = null;
  for (RelNode r2 : r.getRels()) {
    RelOptPredicateList list2 = mq.getPulledUpPredicates(r2);
    if (list2 != null) {
      list = list == null ? list2 : list.union(rexBuilder, list2);
    }
  }
  return Util.first(list, RelOptPredicateList.EMPTY);
}
 
Example #4
Source File: QuidemTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static Object getEnv(String varName) {
  switch (varName) {
  case "jdk18":
    return System.getProperty("java.version").startsWith("1.8");
  case "fixed":
    // Quidem requires a Java 8 function
    return (Function<String, Object>) v -> {
      switch (v) {
      case "calcite1045":
        return Bug.CALCITE_1045_FIXED;
      case "calcite1048":
        return Bug.CALCITE_1048_FIXED;
      }
      return null;
    };
  default:
    return null;
  }
}
 
Example #5
Source File: JdbcFrontJdbcBackLinqMiddleTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testJoinGroupByOrderBy() {
  if (CalciteAssert.DB == CalciteAssert.DatabaseInstance.MYSQL
      && !Bug.CALCITE_673_FIXED) {
    return;
  }
  that()
      .with(CalciteAssert.Config.JDBC_FOODMART)
      .query("select count(*), c.\"state_province\",\n"
          + "  sum(s.\"unit_sales\") as s\n"
          + "from \"foodmart\".\"sales_fact_1997\" as s\n"
          + "  join \"foodmart\".\"customer\" as c\n"
          + "  on s.\"customer_id\" = c.\"customer_id\"\n"
          + "group by c.\"state_province\"\n"
          + "order by c.\"state_province\"")
      .returns2("EXPR$0=24442; state_province=CA; S=74748\n"
          + "EXPR$0=21611; state_province=OR; S=67659\n"
          + "EXPR$0=40784; state_province=WA; S=124366\n");
}
 
Example #6
Source File: RelMdPredicates.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** @see RelMetadataQuery#getPulledUpPredicates(RelNode) */
public RelOptPredicateList getPredicates(RelSubset r,
    RelMetadataQuery mq) {
  if (!Bug.CALCITE_1048_FIXED) {
    return RelOptPredicateList.EMPTY;
  }
  final RexBuilder rexBuilder = r.getCluster().getRexBuilder();
  RelOptPredicateList list = null;
  for (RelNode r2 : r.getRels()) {
    RelOptPredicateList list2 = mq.getPulledUpPredicates(r2);
    if (list2 != null) {
      list = list == null ? list2 : list.union(rexBuilder, list2);
    }
  }
  return Util.first(list, RelOptPredicateList.EMPTY);
}
 
Example #7
Source File: TpcdsTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private CalciteAssert.AssertQuery checkQuery(int i) {
  final Query query = Query.of(i);
  String sql = query.sql(new Random(0));
  switch (i) {
  case 58:
    if (Bug.upgrade("new TPC-DS generator")) {
      // Work around bug: Support '<DATE>  = <character literal>'.
      sql = sql.replace(" = '", " = DATE '");
    } else {
      // Until TPC-DS generator can handle date(...).
      sql = sql.replace("'date([YEAR]+\"-01-01\",[YEAR]+\"-07-24\",sales)'",
          "DATE '1998-08-18'");
    }
    break;
  case 72:
    // Work around CALCITE-304: Support '<DATE> + <INTEGER>'.
    sql = sql.replace("+ 5", "+ interval '5' day");
    break;
  case 95:
    sql = sql.replace("60 days", "interval '60' day");
    sql = sql.replace("d_date between '", "d_date between date '");
    break;
  }
  return with()
      .query(sql.replace("tpcds.", "tpcds_01."));
}
 
Example #8
Source File: RelMdDistinctRowCount.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Double getDistinctRowCount(RelSubset rel, RelMetadataQuery mq,
    ImmutableBitSet groupKey, RexNode predicate) {
  final RelNode best = rel.getBest();
  if (best != null) {
    return mq.getDistinctRowCount(best, groupKey, predicate);
  }
  if (!Bug.CALCITE_1048_FIXED) {
    return getDistinctRowCount((RelNode) rel, mq, groupKey, predicate);
  }
  Double d = null;
  for (RelNode r2 : rel.getRels()) {
    try {
      Double d2 = mq.getDistinctRowCount(r2, groupKey, predicate);
      d = NumberUtil.min(d, d2);
    } catch (CyclicMetadataException e) {
      // Ignore this relational expression; there will be non-cyclic ones
      // in this set.
    }
  }
  return d;
}
 
Example #9
Source File: RelMdMaxRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Double getMaxRowCount(RelSubset rel, RelMetadataQuery mq) {
  // FIXME This is a short-term fix for [CALCITE-1018]. A complete
  // solution will come with [CALCITE-1048].
  Util.discard(Bug.CALCITE_1048_FIXED);
  for (RelNode node : rel.getRels()) {
    if (node instanceof Sort) {
      Sort sort = (Sort) node;
      if (sort.fetch != null) {
        return (double) RexLiteral.intValue(sort.fetch);
      }
    }
  }

  return Double.POSITIVE_INFINITY;
}
 
Example #10
Source File: SqlToRelConverterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testInterval() {
  // temporarily disabled per DTbug 1212
  if (!Bug.DT785_FIXED) {
    return;
  }
  final String sql =
      "values(cast(interval '1' hour as interval hour to second))";
  sql(sql).ok();
}
 
Example #11
Source File: JdbcFrontJdbcBackLinqMiddleTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testJoinGroupByEmpty() {
  if (CalciteAssert.DB == CalciteAssert.DatabaseInstance.MYSQL
      && !Bug.CALCITE_673_FIXED) {
    return;
  }
  that()
      .with(CalciteAssert.Config.JDBC_FOODMART)
      .query("select count(*) from (\n"
          + "  select *\n"
          + "  from \"foodmart\".\"sales_fact_1997\" as s\n"
          + "  join \"foodmart\".\"customer\" as c\n"
          + "  on s.\"customer_id\" = c.\"customer_id\")")
      .returns("EXPR$0=86837\n");
}
 
Example #12
Source File: RexSimplify.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Combines predicates AND, optimizes, and returns null if the result is
 * always false.
 *
 * <p>The expression is simplified on the assumption that an UNKNOWN value
 * is always treated as FALSE. Therefore the simplified expression may
 * sometimes evaluate to FALSE where the original expression would evaluate to
 * UNKNOWN.
 *
 * @param predicates Filter condition predicates
 * @return simplified conjunction of predicates for the filter, null if always false
 */
public RexNode simplifyFilterPredicates(Iterable<? extends RexNode> predicates) {
  final RexNode simplifiedAnds =
      withPredicateElimination(Bug.CALCITE_2401_FIXED)
          .simplifyUnknownAsFalse(
              RexUtil.composeConjunction(rexBuilder, predicates));
  if (simplifiedAnds.isAlwaysFalse()) {
    return null;
  }

  // Remove cast of BOOLEAN NOT NULL to BOOLEAN or vice versa. Filter accepts
  // nullable and not-nullable conditions, but a CAST might get in the way of
  // other rewrites.
  return removeNullabilityCast(simplifiedAnds);
}
 
Example #13
Source File: RelMdMinRowCount.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getMinRowCount(RelSubset rel, RelMetadataQuery mq) {
  // FIXME This is a short-term fix for [CALCITE-1018]. A complete
  // solution will come with [CALCITE-1048].
  Util.discard(Bug.CALCITE_1048_FIXED);
  for (RelNode node : rel.getRels()) {
    if (node instanceof Sort) {
      Sort sort = (Sort) node;
      if (sort.fetch != null) {
        return (double) RexLiteral.intValue(sort.fetch);
      }
    }
  }

  return 0D;
}
 
Example #14
Source File: RelMdMaxRowCount.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getMaxRowCount(RelSubset rel, RelMetadataQuery mq) {
  // FIXME This is a short-term fix for [CALCITE-1018]. A complete
  // solution will come with [CALCITE-1048].
  Util.discard(Bug.CALCITE_1048_FIXED);
  for (RelNode node : rel.getRels()) {
    if (node instanceof Sort) {
      Sort sort = (Sort) node;
      if (sort.fetch != null) {
        return (double) RexLiteral.intValue(sort.fetch);
      }
    }
  }

  return Double.POSITIVE_INFINITY;
}
 
Example #15
Source File: MongoAdapterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Disabled
@Test void testFilterSort() {
  // LONGITUDE and LATITUDE are null because of CALCITE-194.
  Util.discard(Bug.CALCITE_194_FIXED);
  assertModel(MODEL)
      .query("select * from zips\n"
          + "where city = 'SPRINGFIELD' and id >= '70000'\n"
          + "order by state, id")
      .returns(""
          + "CITY=SPRINGFIELD; LONGITUDE=null; LATITUDE=null; POP=752; STATE=AR; ID=72157\n"
          + "CITY=SPRINGFIELD; LONGITUDE=null; LATITUDE=null; POP=1992; STATE=CO; ID=81073\n"
          + "CITY=SPRINGFIELD; LONGITUDE=null; LATITUDE=null; POP=5597; STATE=LA; ID=70462\n"
          + "CITY=SPRINGFIELD; LONGITUDE=null; LATITUDE=null; POP=32384; STATE=OR; ID=97477\n"
          + "CITY=SPRINGFIELD; LONGITUDE=null; LATITUDE=null; POP=27521; STATE=OR; ID=97478\n")
      .queryContains(
          mongoChecker(
              "{\n"
                  + "  $match: {\n"
                  + "    city: \"SPRINGFIELD\",\n"
                  + "    _id: {\n"
                  + "      $gte: \"70000\"\n"
                  + "    }\n"
                  + "  }\n"
                  + "}",
              "{$project: {CITY: '$city', LONGITUDE: '$loc[0]', LATITUDE: '$loc[1]', POP: '$pop', STATE: '$state', ID: '$_id'}}",
              "{$sort: {STATE: 1, ID: 1}}"))
      .explainContains("PLAN=MongoToEnumerableConverter\n"
          + "  MongoSort(sort0=[$4], sort1=[$5], dir0=[ASC], dir1=[ASC])\n"
          + "    MongoProject(CITY=[CAST(ITEM($0, 'city')):VARCHAR(20)], LONGITUDE=[CAST(ITEM(ITEM($0, 'loc'), 0)):FLOAT], LATITUDE=[CAST(ITEM(ITEM($0, 'loc'), 1)):FLOAT], POP=[CAST(ITEM($0, 'pop')):INTEGER], STATE=[CAST(ITEM($0, 'state')):VARCHAR(2)], ID=[CAST(ITEM($0, '_id')):VARCHAR(5)])\n"
          + "      MongoFilter(condition=[AND(=(CAST(ITEM($0, 'city')):VARCHAR(20), 'SPRINGFIELD'), >=(CAST(ITEM($0, '_id')):VARCHAR(5), '70000'))])\n"
          + "        MongoTableScan(table=[[mongo_raw, zips]])");
}
 
Example #16
Source File: FlinkRelMdCollation.java    From flink with Apache License 2.0 5 votes vote down vote up
public com.google.common.collect.ImmutableList<RelCollation> collations(RelSubset subset, RelMetadataQuery mq) {
	if (!Bug.CALCITE_1048_FIXED) {
		//if the best node is null, so we can get the collation based original node, due to
		//the original node is logically equivalent as the rel.
		RelNode rel = Util.first(subset.getBest(), subset.getOriginal());
		return mq.collations(rel);
	} else {
		throw new RuntimeException("CALCITE_1048 is fixed, so check this method again!");
	}
}
 
Example #17
Source File: RexSimplify.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Combines predicates AND, optimizes, and returns null if the result is
 * always false.
 *
 * <p>The expression is simplified on the assumption that an UNKNOWN value
 * is always treated as FALSE. Therefore the simplified expression may
 * sometimes evaluate to FALSE where the original expression would evaluate to
 * UNKNOWN.
 *
 * @param predicates Filter condition predicates
 * @return simplified conjunction of predicates for the filter, null if always false
 */
public RexNode simplifyFilterPredicates(Iterable<? extends RexNode> predicates) {
    final RexNode simplifiedAnds = withPredicateElimination(Bug.CALCITE_2401_FIXED)
            .simplifyUnknownAsFalse(RexUtil.composeConjunction(rexBuilder, predicates));
    if (simplifiedAnds.isAlwaysFalse()) {
        return null;
    }

    // Remove cast of BOOLEAN NOT NULL to BOOLEAN or vice versa. Filter accepts
    // nullable and not-nullable conditions, but a CAST might get in the way of
    // other rewrites.
    return removeNullabilityCast(simplifiedAnds);
}
 
Example #18
Source File: DateRangeRules.java    From Bats with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
ExtractShuttle(RexBuilder rexBuilder, TimeUnitRange timeUnit, Map<RexNode, RangeSet<Calendar>> operandRanges,
        ImmutableSortedSet<TimeUnitRange> timeUnitRanges, String timeZone) {
    this.rexBuilder = Objects.requireNonNull(rexBuilder);
    this.timeUnit = Objects.requireNonNull(timeUnit);
    Bug.upgrade("Change type to Map<RexNode, RangeSet<Calendar>> when" + " [CALCITE-1367] is fixed");
    this.operandRanges = Objects.requireNonNull(operandRanges);
    this.timeUnitRanges = Objects.requireNonNull(timeUnitRanges);
    this.timeZone = timeZone;
}
 
Example #19
Source File: RelMdMinRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Double getMinRowCount(RelSubset rel, RelMetadataQuery mq) {
  // FIXME This is a short-term fix for [CALCITE-1018]. A complete
  // solution will come with [CALCITE-1048].
  Util.discard(Bug.CALCITE_1048_FIXED);
  for (RelNode node : rel.getRels()) {
    if (node instanceof Sort) {
      Sort sort = (Sort) node;
      if (sort.fetch != null) {
        return (double) RexLiteral.intValue(sort.fetch);
      }
    }
  }

  return 0D;
}
 
Example #20
Source File: MongoRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public String visitCall(RexCall call) {
  String name = isItem(call);
  if (name != null) {
    return "'$" + name + "'";
  }
  final List<String> strings = visitList(call.operands);
  if (call.getKind() == SqlKind.CAST) {
    return strings.get(0);
  }
  String stdOperator = MONGO_OPERATORS.get(call.getOperator());
  if (stdOperator != null) {
    return "{" + stdOperator + ": [" + Util.commaList(strings) + "]}";
  }
  if (call.getOperator() == SqlStdOperatorTable.ITEM) {
    final RexNode op1 = call.operands.get(1);
    if (op1 instanceof RexLiteral
        && op1.getType().getSqlTypeName() == SqlTypeName.INTEGER) {
      if (!Bug.CALCITE_194_FIXED) {
        return "'" + stripQuotes(strings.get(0)) + "["
            + ((RexLiteral) op1).getValue2() + "]'";
      }
      return strings.get(0) + "[" + strings.get(1) + "]";
    }
  }
  if (call.getOperator() == SqlStdOperatorTable.CASE) {
    StringBuilder sb = new StringBuilder();
    StringBuilder finish = new StringBuilder();
    // case(a, b, c)  -> $cond:[a, b, c]
    // case(a, b, c, d) -> $cond:[a, b, $cond:[c, d, null]]
    // case(a, b, c, d, e) -> $cond:[a, b, $cond:[c, d, e]]
    for (int i = 0; i < strings.size(); i += 2) {
      sb.append("{$cond:[");
      finish.append("]}");

      sb.append(strings.get(i));
      sb.append(',');
      sb.append(strings.get(i + 1));
      sb.append(',');
      if (i == strings.size() - 3) {
        sb.append(strings.get(i + 2));
        break;
      }
      if (i == strings.size() - 2) {
        sb.append("null");
        break;
      }
    }
    sb.append(finish);
    return sb.toString();
  }
  throw new IllegalArgumentException("Translation of " + call.toString()
      + " is not supported by MongoProject");
}
 
Example #21
Source File: TpchTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testQuery07() {
  assumeTrue(Bug.CALCITE_2223_FIXED);
  checkQuery(7);
}
 
Example #22
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
public TreeSet<CorRef> get() {
	Bug.upgrade("use MultimapBuilder when we're on Guava-16");
	return com.google.common.collect.Sets.newTreeSet();
}
 
Example #23
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
public TreeSet<CorRef> get() {
	Bug.upgrade("use MultimapBuilder when we're on Guava-16");
	return com.google.common.collect.Sets.newTreeSet();
}
 
Example #24
Source File: CassandraExtension.java    From calcite with Apache License 2.0 3 votes vote down vote up
/**
 * Whether to run this test.
 * <p>Enabled by default, unless explicitly disabled
 * from command line ({@code -Dcalcite.test.cassandra=false}) or running on incompatible JDK
 * version (see below).
 *
 * <p>As of this wiring Cassandra 4.x is not yet released and we're using 3.x
 * (which fails on JDK11+). All cassandra tests will be skipped if
 * running on JDK11+.
 *
 * @see <a href="https://issues.apache.org/jira/browse/CASSANDRA-9608">CASSANDRA-9608</a>
 * @return {@code true} if test is compatible with current environment,
 *         {@code false} otherwise
 */
@Override public ConditionEvaluationResult evaluateExecutionCondition(
    final ExtensionContext context) {
  boolean enabled = CalciteSystemProperty.TEST_CASSANDRA.value();
  Bug.upgrade("remove JDK version check once current adapter supports Cassandra 4.x");
  boolean compatibleJdk = TestUtil.getJavaMajorVersion() < 11;
  if (enabled && compatibleJdk) {
    return ConditionEvaluationResult.enabled("Cassandra enabled");
  }
  return ConditionEvaluationResult.disabled("Cassandra tests disabled");
}