Interface SqlOperatorFixture

All Superinterfaces:
AutoCloseable

public interface SqlOperatorFixture extends AutoCloseable
A fixture for testing the SQL operators.

It provides a fluent API so that you can write tests by chaining method calls.

It is immutable. If you have two test cases that require a similar set up (for example, the same SQL expression and parser configuration), it is safe to use the same fixture object as a starting point for both tests.

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.

Depending on the implementation of SqlTester used (see withTester(UnaryOperator)), the fixture may or may not evaluate expressions and check their results.

  • Field Details

  • Method Details

    • getFactory

      SqlTestFactory getFactory()
      Returns the test factory.
    • withFactory

      Creates a copy of this fixture with a new test factory.
    • getTester

      SqlTester getTester()
      Returns the tester.
    • withTester

      SqlOperatorFixture withTester(UnaryOperator<SqlTester> transform)
      Creates a copy of this fixture with a new tester.
    • withParserConfig

      default SqlOperatorFixture withParserConfig(UnaryOperator<SqlParser.Config> transform)
      Creates a copy of this fixture with a new parser configuration.
    • withQuoting

      default SqlOperatorFixture withQuoting(org.apache.calcite.avatica.util.Quoting quoting)
      Returns a fixture that tests a given SQL quoting style.
    • withQuotedCasing

      default SqlOperatorFixture withQuotedCasing(org.apache.calcite.avatica.util.Casing casing)
      Returns a fixture that applies a given casing policy to quoted identifiers.
    • withUnquotedCasing

      default SqlOperatorFixture withUnquotedCasing(org.apache.calcite.avatica.util.Casing casing)
      Returns a fixture that applies a given casing policy to unquoted identifiers.
    • withCaseSensitive

      default SqlOperatorFixture withCaseSensitive(boolean sensitive)
      Returns a fixture that matches identifiers by case-sensitive or case-insensitive.
    • withLex

      default SqlOperatorFixture withLex(Lex lex)
      Returns a fixture that follows a given lexical policy.
    • withConformance

      default SqlOperatorFixture withConformance(SqlConformance conformance)
      Returns a fixture that tests conformance to a particular SQL language version.
    • conformance

      default SqlConformance conformance()
      Returns the conformance.
    • withValidatorConfig

      default SqlOperatorFixture withValidatorConfig(UnaryOperator<SqlValidator.Config> transform)
      Returns a fixture with a given validator configuration.
    • enableTypeCoercion

      default SqlOperatorFixture enableTypeCoercion(boolean enabled)
      Returns a fixture that tests with implicit type coercion on/off.
    • withLenientOperatorLookup

      default SqlOperatorFixture withLenientOperatorLookup(boolean lenient)
      Returns a fixture that does not fail validation if it encounters an unknown function.
    • withConnectionFactory

      default SqlOperatorFixture withConnectionFactory(UnaryOperator<ConnectionFactory> transform)
      Returns a fixture that gets connections from a given factory.
    • withOperatorTable

      default SqlOperatorFixture withOperatorTable(SqlOperatorTable operatorTable)
      Returns a fixture that uses a given operator table.
    • brokenTestsEnabled

      boolean brokenTestsEnabled()
      Returns whether to run tests that are considered 'broken'. Returns false by default, but it is useful to temporarily enable the 'broken' tests to see whether they are still broken.
    • withBrokenTestsEnabled

      SqlOperatorFixture withBrokenTestsEnabled(boolean enableBrokenTests)
    • checkScalar

      void checkScalar(String expression, SqlTester.TypeChecker typeChecker, SqlTester.ResultChecker resultChecker)
    • checkScalar

      default void checkScalar(String expression, Object result, String resultType)
      Tests that a scalar SQL expression returns the expected result and the expected type. For example,
      checkScalar("1.1 + 2.9", "4.0", "DECIMAL(2, 1) NOT NULL");
      Parameters:
      expression - Scalar expression
      result - Expected result
      resultType - Expected result type
    • checkScalarExact

      default void checkScalarExact(String expression, int result)
      Tests that a scalar SQL expression returns the expected exact numeric result as an integer. For example,
      checkScalarExact("1 + 2", 3);
      Parameters:
      expression - Scalar expression
      result - Expected result
    • checkScalarExact

      default void checkScalarExact(String expression, String expectedType, String result)
      Tests that a scalar SQL expression returns the expected exact numeric result. For example,
      checkScalarExact("1 + 2", "3");
      Parameters:
      expression - Scalar expression
      expectedType - Type we expect the result to have, including nullability, precision and scale, for example DECIMAL(2, 1) NOT NULL.
      result - Expected result
    • checkScalarExact

      void checkScalarExact(String expression, String expectedType, SqlTester.ResultChecker resultChecker)
    • checkScalarApprox

      void checkScalarApprox(String expression, String expectedType, Object result)
      Tests that a scalar SQL expression returns expected approximate numeric result. For example,
      checkScalarApprox("1.0 + 2.1", "3.1");
      Parameters:
      expression - Scalar expression
      expectedType - Type we expect the result to have, including nullability, precision and scale, for example DECIMAL(2, 1) NOT NULL.
      result - Expected result, or a matcher
      See Also:
    • checkBoolean

      void checkBoolean(String expression, @Nullable Boolean result)
      Tests that a scalar SQL expression returns the expected boolean result. For example,
      checkScalarExact("TRUE AND FALSE", Boolean.TRUE);

      The expected result can be null:

      checkScalarExact("NOT UNKNOWN", null);
      Parameters:
      expression - Scalar expression
      result - Expected result (null signifies NULL).
    • checkString

      void checkString(String expression, String result, String resultType)
      Tests that a scalar SQL expression returns the expected string result. For example,
      checkScalarExact("'ab' || 'c'", "abc");
      Parameters:
      expression - Scalar expression
      result - Expected result
      resultType - Expected result type
    • checkNull

      void checkNull(String expression)
      Tests that a SQL expression returns the SQL NULL value. For example,
      checkNull("CHAR_LENGTH(CAST(NULL AS VARCHAR(3))");
      Parameters:
      expression - Scalar expression
    • checkType

      void checkType(String expression, String type)
      Tests that a SQL expression has a given type. For example,
      checkType("SUBSTR('hello' FROM 1 FOR 3)", "VARCHAR(3) NOT NULL");

      This method checks length/precision, scale, and whether the type allows NULL values, so is more precise than the type-checking done by methods such as checkScalarExact(java.lang.String, int).

      Parameters:
      expression - Scalar expression
      type - Type string
    • checkAggType

      default SqlOperatorFixture checkAggType(String expr, String type)
      Very similar to checkType(java.lang.String, java.lang.String), but generates inside a SELECT with a non-empty GROUP BY. Aggregate functions may be nullable if executed in a SELECT with an empty GROUP BY.

      Viz: SELECT sum(1) FROM emp has type "INTEGER", SELECT sum(1) FROM emp GROUP BY deptno has type "INTEGER NOT NULL",

    • checkColumnType

      void checkColumnType(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:
      sql - Query expression
      type - Type string
    • check

      default void check(String query, SqlTester.TypeChecker typeChecker, Object result)
      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:
      query - SQL query
      typeChecker - Checks whether the result is the expected type; must not be null
      result - Expected result, or matcher
    • check

      default void check(String query, String expectedType, Object result)
    • check

      default void check(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:
      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

      SqlOperatorFixture setFor(SqlOperator operator, SqlOperatorFixture.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(String expr, String[] inputValues, SqlTester.ResultChecker checker)
      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:
      expr - Aggregate expression, e.g. SUM(DISTINCT x)
      inputValues - Array of input values, e.g. ["1", null, "2"].
      checker - Result checker
    • checkAggWithMultipleArgs

      void checkAggWithMultipleArgs(String expr, String[][] inputValues, SqlTester.ResultChecker resultChecker)
      Checks that an aggregate expression with multiple args returns the expected result.
      Parameters:
      expr - Aggregate expression, e.g. AGG_FUNC(x, x2, x3)
      inputValues - Nested array of input values, e.g. [ ["1", null, "2"] ["3", "4", null] ]
      resultChecker - Checks whether the result has the expected value
    • checkWinAgg

      void checkWinAgg(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:
      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(String expr, String[] inputValues, String expectedError, boolean runtime)
      Tests that an aggregate expression fails at run time.
      Parameters:
      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(StringAndPos expression, String expectedError, boolean runtime)
      Tests that a scalar SQL expression fails at run time.
      Parameters:
      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(String expression, String expectedError, boolean runtime)
      As checkFails(StringAndPos, String, boolean), but with a string that contains carets.
    • checkQueryFails

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

      void checkQuery(String sql)
      Tests that a SQL query succeeds at prepare time.
      Parameters:
      sql - SQL query
    • withLibrary

      default SqlOperatorFixture withLibrary(SqlLibrary library)
    • forEachLibrary

      default void forEachLibrary(Iterable<? extends SqlLibrary> libraries, Consumer<SqlOperatorFixture> consumer)
      Applies this fixture to some code for each of the given libraries.
    • forEachConformance

      default void forEachConformance(Iterable<? extends SqlConformanceEnum> conformances, Consumer<SqlOperatorFixture> consumer)
      Applies this fixture to some code for each of the given conformances.
    • forOracle

      default SqlOperatorFixture forOracle(SqlConformance conformance)
    • getCastString

      default String getCastString(String value, String targetType, boolean errorLoc, SqlOperatorFixture.CastType castType)
    • checkCastToApproxOkay

      default void checkCastToApproxOkay(String value, String targetType, Object expected, SqlOperatorFixture.CastType castType)
    • checkCastToStringOkay

      default void checkCastToStringOkay(String value, String targetType, String expected, SqlOperatorFixture.CastType castType)
    • checkCastToScalarOkay

      default void checkCastToScalarOkay(String value, String targetType, String expected, SqlOperatorFixture.CastType castType)
    • getTargetType

      default String getTargetType(String targetType, SqlOperatorFixture.CastType castType)
    • checkCastToScalarOkay

      default void checkCastToScalarOkay(String value, String targetType, SqlOperatorFixture.CastType castType)
    • checkCastFails

      default void checkCastFails(String value, String targetType, String expectedError, boolean runtime, SqlOperatorFixture.CastType castType)
    • checkCastToString

      default void checkCastToString(String value, @Nullable String type, @Nullable String expected, SqlOperatorFixture.CastType castType)