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.