Interface SqlTester

All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
AbstractSqlTester, SqlOperatorTest.TesterImpl, SqlValidatorTester

public interface SqlTester extends AutoCloseable
Callback for testing SQL queries and expressions.

The idea is that when you define an operator (or another piece of SQL functionality), you can define the logical behavior of that operator once, as part of that operator. Later you can define one or more physical implementations of that operator, and test them all using the same set of tests.

Specific implementations of SqlTester might evaluate the queries in different ways, for example, using a C++ versus Java calculator. An implementation might even ignore certain calls altogether.

  • Method Details

    • forEachQuery

      void forEachQuery(SqlTestFactory factory, String expression, Consumer<String> consumer)
      Given a scalar expression, generates a sequence of SQL queries that evaluate it, and calls a given action with each.
      Parameters:
      factory - Factory
      expression - Scalar expression
      consumer - Action to be called for each query
    • parseQuery

      SqlNode parseQuery(SqlTestFactory factory, String sql) throws SqlParseException
      Parses a query.
      Throws:
      SqlParseException
    • parseExpression

      SqlNode parseExpression(SqlTestFactory factory, String expr) throws SqlParseException
      Parses an expression.
      Throws:
      SqlParseException
    • validateAndThen

      void validateAndThen(SqlTestFactory factory, StringAndPos sap, SqlTester.ValidatedNodeConsumer consumer)
      Parses and validates a query, then calls an action on the result.
    • validateAndApply

      <R> R validateAndApply(SqlTestFactory factory, StringAndPos sap, SqlTester.ValidatedNodeFunction<R> function)
      Parses and validates a query, then calls a function on the result.
    • assertExceptionIsThrown

      void assertExceptionIsThrown(SqlTestFactory factory, StringAndPos sap, @Nullable String expectedMsgPattern)
      Checks that a query is valid, or, if invalid, throws the right message at the right location.

      If expectedMsgPattern is null, the query must succeed.

      If expectedMsgPattern is not null, the query must fail, and give an error location of (expectedLine, expectedColumn) through (expectedEndLine, expectedEndColumn).

      Parameters:
      factory - Factory
      sap - SQL statement
      expectedMsgPattern - If this parameter is null the query must be
    • getColumnType

      RelDataType getColumnType(SqlTestFactory factory, String sql)
      Returns the data type of the sole column of a SQL query.

      For example, getResultType("VALUES (1") returns INTEGER.

      Fails if query returns more than one column.

      See Also:
    • getResultType

      RelDataType getResultType(SqlTestFactory factory, String sql)
      Returns the data type of the row returned by a SQL query.

      For example, getResultType("VALUES (1, 'foo')") returns RecordType(INTEGER EXPR$0, CHAR(3) EXPR#1).

    • checkColumnType

      void checkColumnType(SqlTestFactory factory, String sql, String type)
      Checks that a query returns one column of an expected type. For example, checkType("VALUES (1 + 2)", "INTEGER NOT NULL").
      Parameters:
      factory - Factory
      sql - Query expression
      type - Type string
    • check

      default void check(SqlTestFactory factory, String query, SqlTester.TypeChecker typeChecker, SqlTester.ResultChecker resultChecker)
      Tests that a SQL query returns a single column with the given type. For example,
      check("VALUES (1 + 2)", "3", SqlTypeName.Integer);

      If result is null, the expression must yield the SQL NULL value. If result is a Pattern, the result must match that pattern.

      Parameters:
      factory - Factory
      query - SQL query
      typeChecker - Checks whether the result is the expected type
      resultChecker - Checks whether the result has the expected value
    • check

      void check(SqlTestFactory factory, String query, SqlTester.TypeChecker typeChecker, SqlTester.ParameterChecker parameterChecker, SqlTester.ResultChecker resultChecker)
      Tests that a SQL query returns a result of expected type and value. Checking of type and value are abstracted using SqlTester.TypeChecker and SqlTester.ResultChecker functors.
      Parameters:
      factory - Factory
      query - SQL query
      typeChecker - Checks whether the result is the expected type
      parameterChecker - Checks whether the parameters are of expected types
      resultChecker - Checks whether the result has the expected value
    • setFor

      void setFor(SqlOperator operator, SqlTester.VmName... unimplementedVmNames)
      Declares that this test is for a given operator. So we can check that all operators are tested.
      Parameters:
      operator - Operator
      unimplementedVmNames - Names of virtual machines for which this
    • checkAgg

      void checkAgg(SqlTestFactory factory, String expr, String[] inputValues, SqlTester.ResultChecker resultChecker)
      Checks that an aggregate expression returns the expected result.

      For example, checkAgg("AVG(DISTINCT x)", new String[] {"2", "3", null, "3" }, new Double(2.5), 0);

      Parameters:
      factory - Factory
      expr - Aggregate expression, e.g. SUM(DISTINCT x)
      inputValues - Array of input values, e.g. ["1", null, "2"]
      resultChecker - Checks whether the result has the expected value
    • checkWinAgg

      void checkWinAgg(SqlTestFactory factory, String expr, String[] inputValues, String windowSpec, String type, SqlTester.ResultChecker resultChecker)
      Checks that a windowed aggregate expression returns the expected result.

      For example, checkWinAgg("FIRST_VALUE(x)", new String[] {"2", "3", null, "3" }, "INTEGER NOT NULL", 2, 0d);

      Parameters:
      factory - Factory
      expr - Aggregate expression, e.g. SUM(DISTINCT x)
      inputValues - Array of input values, e.g. ["1", null, "2"]
      type - Expected result type
      resultChecker - Checks whether the result has the expected value
    • checkAggFails

      void checkAggFails(SqlTestFactory factory, String expr, String[] inputValues, String expectedError, boolean runtime)
      Tests that an aggregate expression fails at run time.
      Parameters:
      factory - Factory
      expr - An aggregate expression
      inputValues - Array of input values
      expectedError - Pattern for expected error
      runtime - If true, must fail at runtime; if false, must fail at validate time
    • checkFails

      void checkFails(SqlTestFactory factory, StringAndPos expression, String expectedError, boolean runtime)
      Tests that a scalar SQL expression fails at run time.
      Parameters:
      factory - Factory
      expression - SQL scalar expression
      expectedError - Pattern for expected error. If !runtime, must include an error location.
      runtime - If true, must fail at runtime; if false, must fail at validate time
    • checkFails

      default void checkFails(SqlTestFactory factory, String expression, String expectedError, boolean runtime)
      As checkFails(SqlTestFactory, StringAndPos, String, boolean), but with a string that contains carets.
    • checkQueryFails

      void checkQueryFails(SqlTestFactory factory, StringAndPos sap, String expectedError)
      Tests that a SQL query fails at prepare time.
      Parameters:
      factory - Factory
      sap - SQL query and error position
      expectedError - Pattern for expected error. Must include an error location.
    • convertSqlToRel

      default RelRoot convertSqlToRel(SqlTestFactory factory, String sql, boolean decorrelate, boolean trim)
      Converts a SQL string to a RelNode tree.
      Parameters:
      factory - Factory
      sql - SQL statement
      decorrelate - Whether to decorrelate
      trim - Whether to trim
      Returns:
      Relational expression, never null
    • convertSqlToRel2

      Pair<SqlValidator,RelRoot> convertSqlToRel2(SqlTestFactory factory, String sql, boolean decorrelate, boolean trim)
      Converts a SQL string to a (SqlValidator, RelNode) pair.
    • assertConvertsTo

      void assertConvertsTo(SqlTestFactory factory, DiffRepository diffRepos, String sql, String plan, boolean trim, boolean expression, boolean decorrelate)
      Checks that a SQL statement converts to a given plan, optionally trimming columns that are not needed.
      Parameters:
      factory - Factory
      diffRepos - Diff repository
      sql - SQL query or expression
      plan - Expected plan
      trim - Whether to trim columns that are not needed
      expression - True if sql is an expression, false if it is a query
    • trimRelNode

      RelNode trimRelNode(SqlTestFactory factory, RelNode relNode)
      Trims a RelNode.