Interface SqlOperatorFixture
- All Superinterfaces:
AutoCloseable
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.
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic enum
Types for cast.static enum
Name of a virtual machine that can potentially implement an operator. -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns whether to run tests that are considered 'broken'.default void
default void
check
(String query, SqlTester.TypeChecker typeChecker, Object result) Tests that a SQL query returns a single column with the given type.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.void
checkAgg
(String expr, String[] inputValues, String type, SqlTester.ResultChecker checker) Checks that an aggregate expression returns the expected result.void
checkAgg
(String expr, String[] inputValues, SqlTester.ResultChecker checker) Checks that an aggregate expression returns the expected result.void
checkAggFails
(String expr, String[] inputValues, String expectedError, boolean runtime) Tests that an aggregate expression fails at run time.default SqlOperatorFixture
checkAggType
(String expr, String type) Very similar tocheckType(java.lang.String, java.lang.String)
, but generates inside a SELECT with a non-empty GROUP BY.void
checkAggWithMultipleArgs
(String expr, String[][] inputValues, SqlTester.ResultChecker resultChecker) Checks that an aggregate expression with multiple args returns the expected result.void
checkBoolean
(String expression, @Nullable Boolean result) Tests that a scalar SQL expression returns the expected boolean result.default void
checkCastFails
(String value, String targetType, String expectedError, boolean runtime, SqlOperatorFixture.CastType castType) default void
checkCastToApproxOkay
(String value, String targetType, Object expected, SqlOperatorFixture.CastType castType) default void
checkCastToScalarOkay
(String value, String targetType, String expected, SqlOperatorFixture.CastType castType) default void
checkCastToScalarOkay
(String value, String targetType, SqlOperatorFixture.CastType castType) default void
checkCastToString
(String value, @Nullable String type, @Nullable String expected, SqlOperatorFixture.CastType castType) default void
checkCastToStringOkay
(String value, String targetType, String expected, SqlOperatorFixture.CastType castType) void
checkColumnType
(String sql, String type) Checks that a query returns one column of an expected type.default void
checkFails
(String expression, String expectedError, boolean runtime) AscheckFails(StringAndPos, String, boolean)
, but with a string that contains carets.void
checkFails
(StringAndPos expression, String expectedError, boolean runtime) Tests that a scalar SQL expression fails at run time.void
Tests that a SQL expression returns the SQL NULL value.void
checkQuery
(String sql) Tests that a SQL query succeeds at prepare time.void
checkQueryFails
(StringAndPos sap, String expectedError) Tests that a SQL query fails at prepare time.default void
checkScalar
(String expression, Object result, String resultType) Tests that a scalar SQL expression returns the expected result and the expected type.void
checkScalar
(String expression, SqlTester.TypeChecker typeChecker, SqlTester.ResultChecker resultChecker) void
checkScalarApprox
(String expression, String expectedType, Object result) Tests that a scalar SQL expression returns expected approximate numeric result.default void
checkScalarExact
(String expression, int result) Tests that a scalar SQL expression returns the expected exact numeric result as an integer.default void
checkScalarExact
(String expression, String expectedType, String result) Tests that a scalar SQL expression returns the expected exact numeric result.void
checkScalarExact
(String expression, String expectedType, SqlTester.ResultChecker resultChecker) void
checkString
(String expression, String result, String resultType) Tests that a scalar SQL expression returns the expected string result.void
Tests that a SQL expression has a given type.void
checkWinAgg
(String expr, String[] inputValues, String windowSpec, String type, SqlTester.ResultChecker resultChecker) Checks that a windowed aggregate expression returns the expected result.default SqlConformance
Returns the conformance.default SqlOperatorFixture
enableTypeCoercion
(boolean enabled) Returns a fixture that tests with implicit type coercion on/off.default void
forEachConformance
(Iterable<? extends SqlConformanceEnum> conformances, Consumer<SqlOperatorFixture> consumer) Applies this fixture to some code for each of the given conformances.default void
forEachLibrary
(Iterable<? extends SqlLibrary> libraries, Consumer<SqlOperatorFixture> consumer) Applies this fixture to some code for each of the given libraries.default SqlOperatorFixture
forOracle
(SqlConformance conformance) default String
getCastString
(String value, String targetType, boolean errorLoc, SqlOperatorFixture.CastType castType) Returns the test factory.default String
getTargetType
(String targetType, SqlOperatorFixture.CastType castType) Returns the tester.setFor
(SqlOperator operator, SqlOperatorFixture.VmName... unimplementedVmNames) Declares that this test is for a given operator.withBrokenTestsEnabled
(boolean enableBrokenTests) SetsbrokenTestsEnabled()
.default SqlOperatorFixture
withCaseSensitive
(boolean sensitive) Returns a fixture that matches identifiers by case-sensitive or case-insensitive.default SqlOperatorFixture
withConformance
(SqlConformance conformance) Returns a fixture that tests conformance to a particular SQL language version.default SqlOperatorFixture
withConnectionFactory
(UnaryOperator<ConnectionFactory> transform) Returns a fixture that gets connections from a given factory.withFactory
(UnaryOperator<SqlTestFactory> transform) Creates a copy of this fixture with a new test factory.default SqlOperatorFixture
withLenientOperatorLookup
(boolean lenient) Returns a fixture that does not fail validation if it encounters an unknown function.default SqlOperatorFixture
Returns a fixture that follows a given lexical policy.default SqlOperatorFixture
withLibraries
(SqlLibrary... libraries) default SqlOperatorFixture
withLibrary
(SqlLibrary library) default SqlOperatorFixture
withOperatorTable
(SqlOperatorTable operatorTable) Returns a fixture that uses a given operator table.default SqlOperatorFixture
withParserConfig
(UnaryOperator<SqlParser.Config> transform) Creates a copy of this fixture with a new parser configuration.default SqlOperatorFixture
withQuotedCasing
(org.apache.calcite.avatica.util.Casing casing) Returns a fixture that applies a given casing policy to quoted identifiers.default SqlOperatorFixture
withQuoting
(org.apache.calcite.avatica.util.Quoting quoting) Returns a fixture that tests a given SQL quoting style.withTester
(UnaryOperator<SqlTester> transform) Creates a copy of this fixture with a new tester.default SqlOperatorFixture
withUnquotedCasing
(org.apache.calcite.avatica.util.Casing casing) Returns a fixture that applies a given casing policy to unquoted identifiers.default SqlOperatorFixture
withValidatorConfig
(UnaryOperator<SqlValidator.Config> transform) Returns a fixture with a given validator configuration.Methods inherited from interface java.lang.AutoCloseable
close
-
Field Details
-
INVALID_CHAR_MESSAGE
- See Also:
-
OUT_OF_RANGE_MESSAGE
- See Also:
-
WRONG_FORMAT_MESSAGE
- See Also:
-
DIVISION_BY_ZERO_MESSAGE
- See Also:
-
STRING_TRUNC_MESSAGE
- See Also:
-
BAD_DATETIME_MESSAGE
- See Also:
-
LITERAL_OUT_OF_RANGE_MESSAGE
- See Also:
-
INVALID_ARGUMENTS_NUMBER
- See Also:
-
INVALID_ARGUMENTS_TYPE_VALIDATION_ERROR
- See Also:
-
-
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
Creates a copy of this fixture with a new tester. -
withParserConfig
Creates a copy of this fixture with a new parser configuration. -
withQuoting
Returns a fixture that tests a given SQL quoting style. -
withQuotedCasing
Returns a fixture that applies a given casing policy to quoted identifiers. -
withUnquotedCasing
Returns a fixture that applies a given casing policy to unquoted identifiers. -
withCaseSensitive
Returns a fixture that matches identifiers by case-sensitive or case-insensitive. -
withLex
Returns a fixture that follows a given lexical policy. -
withConformance
Returns a fixture that tests conformance to a particular SQL language version. -
conformance
Returns the conformance. -
withValidatorConfig
Returns a fixture with a given validator configuration. -
enableTypeCoercion
Returns a fixture that tests with implicit type coercion on/off. -
withLenientOperatorLookup
Returns a fixture that does not fail validation if it encounters an unknown function. -
withConnectionFactory
Returns a fixture that gets connections from a given factory. -
withOperatorTable
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
SetsbrokenTestsEnabled()
. -
checkScalar
void checkScalar(String expression, SqlTester.TypeChecker typeChecker, SqlTester.ResultChecker resultChecker) -
checkScalar
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 expressionresult
- Expected resultresultType
- Expected result type
-
checkScalarExact
Tests that a scalar SQL expression returns the expected exact numeric result as an integer. For example,checkScalarExact("1 + 2", 3);
- Parameters:
expression
- Scalar expressionresult
- Expected result
-
checkScalarExact
Tests that a scalar SQL expression returns the expected exact numeric result. For example,checkScalarExact("1 + 2", "3");
- Parameters:
expression
- Scalar expressionexpectedType
- Type we expect the result to have, including nullability, precision and scale, for exampleDECIMAL(2, 1) NOT NULL
.result
- Expected result
-
checkScalarExact
void checkScalarExact(String expression, String expectedType, SqlTester.ResultChecker resultChecker) -
checkScalarApprox
Tests that a scalar SQL expression returns expected approximate numeric result. For example,checkScalarApprox("1.0 + 2.1", "3.1");
- Parameters:
expression
- Scalar expressionexpectedType
- Type we expect the result to have, including nullability, precision and scale, for exampleDECIMAL(2, 1) NOT NULL
.result
- Expected result, or a matcher- See Also:
-
checkBoolean
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 expressionresult
- Expected result (null signifies NULL).
-
checkString
Tests that a scalar SQL expression returns the expected string result. For example,checkScalarExact("'ab' || 'c'", "abc");
- Parameters:
expression
- Scalar expressionresult
- Expected resultresultType
- Expected result type
-
checkNull
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
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 expressiontype
- Type string
-
checkAggType
Very similar tocheckType(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
Checks that a query returns one column of an expected type. For example,checkType("VALUES (1 + 2)", "INTEGER NOT NULL")
.- Parameters:
sql
- Query expressiontype
- Type string
-
check
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. Ifresult
is aPattern
, the result must match that pattern.- Parameters:
query
- SQL querytypeChecker
- Checks whether the result is the expected type; must not be nullresult
- Expected result, or matcher
-
check
-
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 usingSqlTester.TypeChecker
andSqlTester.ResultChecker
functors.- Parameters:
query
- SQL querytypeChecker
- Checks whether the result is the expected typeparameterChecker
- Checks whether the parameters are of expected typesresultChecker
- Checks whether the result has the expected value
-
setFor
Declares that this test is for a given operator. So we can check that all operators are tested.- Parameters:
operator
- OperatorunimplementedVmNames
- Names of virtual machines for which this
-
checkAgg
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
-
checkAgg
Checks that an aggregate expression returns the expected result.For example,
checkAgg("AVG(DISTINCT x)", new String[] {"2", "3", null, "3" }, "INTEGER", isSingle([2, 3]));
- Parameters:
expr
- Aggregate expression, e.g.SUM(DISTINCT x)
inputValues
- Array of input values, e.g.["1", null, "2"]
.type
- Expected result typechecker
- 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 typeresultChecker
- Checks whether the result has the expected value
-
checkAggFails
Tests that an aggregate expression fails at run time.- Parameters:
expr
- An aggregate expressioninputValues
- Array of input valuesexpectedError
- Pattern for expected errorruntime
- If true, must fail at runtime; if false, must fail at validate time
-
checkFails
Tests that a scalar SQL expression fails at run time.- Parameters:
expression
- SQL scalar expressionexpectedError
- 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
AscheckFails(StringAndPos, String, boolean)
, but with a string that contains carets. -
checkQueryFails
Tests that a SQL query fails at prepare time.- Parameters:
sap
- SQL query and error positionexpectedError
- Pattern for expected error. Must include an error location.
-
checkQuery
Tests that a SQL query succeeds at prepare time.- Parameters:
sql
- SQL query
-
withLibrary
-
withLibraries
-
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
-
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
-
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)
-