Class SqlOperatorTest

java.lang.Object
org.apache.calcite.test.SqlOperatorTest

@TestInstance(PER_CLASS) public class SqlOperatorTest extends Object
Contains unit tests for all operators. Each of the methods is named after an operator.

To run, you also need an execution mechanism: parse, validate, and execute expressions on the operators. This is left to a SqlTester object which is obtained via the fixture() method. The default tester merely validates calls to operators, but CalciteSqlOperatorTest uses a tester that executes calls and checks that results are valid.

Different implementations of SqlTester are possible, such as:

  • Execute against a JDBC database;
  • Parse and validate but do not evaluate expressions;
  • Generate a SQL script;
  • Analyze which operators are adequately tested.

A typical method will be named after the operator it is testing (say testSubstringFunc). It first calls SqlOperatorFixture.setFor(SqlOperator, VmName...) to declare which operator it is testing.


 public void testSubstringFunc() {
     tester.setFor(SqlStdOperatorTable.substringFunc);
     tester.checkScalar("sin(0)", "0");
     tester.checkScalar("sin(1.5707)", "1");
 }

The rest of the method contains calls to the various checkXxx methods in the SqlTester interface. For an operator to be adequately tested, there need to be tests for:

  • Parsing all of its the syntactic variants.
  • Deriving the type of in all combinations of arguments.
    • Pay particular attention to nullability. For example, the result of the "+" operator is NOT NULL if and only if both of its arguments are NOT NULL.
    • Also pay attention to precision/scale/length. For example, the maximum length of the "||" operator is the sum of the maximum lengths of its arguments.
  • Executing the function. Pay particular attention to corner cases such as null arguments or null results.
  • Field Details

    • TESTER

      public static final SqlOperatorTest.TesterImpl TESTER
    • TODO

      public static final boolean TODO
      See Also:
    • TIME_PATTERN

      public static final Pattern TIME_PATTERN
      Regular expression for a SQL TIME(0) value.
    • TIMESTAMP_PATTERN

      public static final Pattern TIMESTAMP_PATTERN
      Regular expression for a SQL TIMESTAMP(0) value.
    • DATE_PATTERN

      public static final Pattern DATE_PATTERN
      Regular expression for a SQL DATE value.
    • MICROSECOND_VARIANTS

      public static final List<String> MICROSECOND_VARIANTS
    • NANOSECOND_VARIANTS

      public static final List<String> NANOSECOND_VARIANTS
    • SECOND_VARIANTS

      public static final List<String> SECOND_VARIANTS
    • MINUTE_VARIANTS

      public static final List<String> MINUTE_VARIANTS
    • HOUR_VARIANTS

      public static final List<String> HOUR_VARIANTS
    • DAY_VARIANTS

      public static final List<String> DAY_VARIANTS
    • WEEK_VARIANTS

      public static final List<String> WEEK_VARIANTS
    • MONTH_VARIANTS

      public static final List<String> MONTH_VARIANTS
    • QUARTER_VARIANTS

      public static final List<String> QUARTER_VARIANTS
    • YEAR_VARIANTS

      public static final List<String> YEAR_VARIANTS
    • UTC_TZ

      protected static final TimeZone UTC_TZ
    • LOCAL_TZ

      protected static final TimeZone LOCAL_TZ
    • CURRENT_TZ

      protected static final TimeZone CURRENT_TZ
    • DECIMAL

      public static final boolean DECIMAL
      Whether DECIMAL type is implemented.
      See Also:
  • Constructor Details

    • SqlOperatorTest

      public SqlOperatorTest()
  • Method Details

    • fixture

      protected SqlOperatorFixture fixture()
      Sub-classes should override to run tests in a different environment.
    • testConstantCast

      @Test public void testConstantCast()
    • getCalendarNotTooNear

      protected static Calendar getCalendarNotTooNear(int timeUnit)
      Returns a Calendar that is the current time, pausing if we are within 2 minutes of midnight or the top of the hour.
      Parameters:
      timeUnit - Time unit
      Returns:
      calendar
    • testJsonInsert

      @Test public void testJsonInsert()
    • testJsonReplace

      @Test public void testJsonReplace()
    • testJsonSet

      @Test public void testJsonSet()
    • currentTimeString

      protected static Pair<String,Hook.Closeable> currentTimeString(TimeZone tz)
      Returns a time string, in GMT, that will be valid for at least 2 minutes.

      For example, at "2005-01-01 12:34:56 PST", returns "2005-01-01 20:". At "2005-01-01 12:34:59 PST", waits a minute, then returns "2005-01-01 21:".

      Parameters:
      tz - Time zone
      Returns:
      Time string