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
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumTypes for cast.static enumName of a virtual machine that can potentially implement an operator. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns whether to run tests that are considered 'broken'.default voiddefault voidcheck(String query, SqlTester.TypeChecker typeChecker, Object result) Tests that a SQL query returns a single column with the given type.default voidcheck(String query, SqlTester.TypeChecker typeChecker, SqlTester.ParameterChecker parameterChecker, SqlTester.ResultChecker resultChecker) Tests that a SQL query returns a result of expected type and value.voidcheckAgg(String expr, String[] inputValues, String type, SqlTester.ResultChecker checker) Checks that an aggregate expression returns the expected result.voidcheckAgg(String expr, String[] inputValues, SqlTester.ResultChecker checker) Checks that an aggregate expression returns the expected result.voidcheckAggFails(String expr, String[] inputValues, String expectedError, boolean runtime) Tests that an aggregate expression fails at run time.default SqlOperatorFixturecheckAggType(String expr, String type) Very similar tocheckType(java.lang.String, java.lang.String), but generates inside a SELECT with a non-empty GROUP BY.voidcheckAggWithMultipleArgs(String expr, String[][] inputValues, SqlTester.ResultChecker resultChecker) Checks that an aggregate expression with multiple args returns the expected result.voidcheckBoolean(String expression, @Nullable Boolean result) Tests that a scalar SQL expression returns the expected boolean result.default voidcheckCastFails(String value, String targetType, String expectedError, boolean runtime, SqlOperatorFixture.CastType castType) default voidcheckCastToApproxOkay(String value, String targetType, Object expected, SqlOperatorFixture.CastType castType) default voidcheckCastToScalarOkay(String value, String targetType, String expected, SqlOperatorFixture.CastType castType) default voidcheckCastToScalarOkay(String value, String targetType, SqlOperatorFixture.CastType castType) default voidcheckCastToString(String value, @Nullable String type, @Nullable String expected, SqlOperatorFixture.CastType castType) default voidcheckCastToStringOkay(String value, String targetType, String expected, SqlOperatorFixture.CastType castType) voidcheckColumnType(String sql, String type) Checks that a query returns one column of an expected type.default voidcheckFails(String expression, String expectedError, boolean runtime) AscheckFails(StringAndPos, String, boolean), but with a string that contains carets.voidcheckFails(StringAndPos expression, String expectedError, boolean runtime) Tests that a scalar SQL expression fails at run time.voidTests that a SQL expression returns the SQL NULL value.voidcheckQuery(String sql) Tests that a SQL query succeeds at prepare time.voidcheckQueryFails(StringAndPos sap, String expectedError) Tests that a SQL query fails at prepare time.default voidcheckScalar(String expression, Object result, String resultType) Tests that a scalar SQL expression returns the expected result and the expected type.voidcheckScalar(String expression, SqlTester.TypeChecker typeChecker, SqlTester.ResultChecker resultChecker) voidcheckScalarApprox(String expression, String expectedType, Object result) Tests that a scalar SQL expression returns expected approximate numeric result.default voidcheckScalarExact(String expression, int result) Tests that a scalar SQL expression returns the expected exact numeric result as an integer.default voidcheckScalarExact(String expression, String expectedType, String result) Tests that a scalar SQL expression returns the expected exact numeric result.voidcheckScalarExact(String expression, String expectedType, SqlTester.ResultChecker resultChecker) voidcheckString(String expression, String result, String resultType) Tests that a scalar SQL expression returns the expected string result.voidTests that a SQL expression has a given type.voidcheckWinAgg(String expr, String[] inputValues, String windowSpec, String type, SqlTester.ResultChecker resultChecker) Checks that a windowed aggregate expression returns the expected result.default SqlConformanceReturns the conformance.default SqlOperatorFixtureenableTypeCoercion(boolean enabled) Returns a fixture that tests with implicit type coercion on/off.default voidforEachConformance(Iterable<? extends SqlConformanceEnum> conformances, Consumer<SqlOperatorFixture> consumer) Applies this fixture to some code for each of the given conformances.default voidforEachLibrary(Iterable<? extends SqlLibrary> libraries, Consumer<SqlOperatorFixture> consumer) Applies this fixture to some code for each of the given libraries.default SqlOperatorFixtureforOracle(SqlConformance conformance) default StringgetCastString(String value, String targetType, boolean errorLoc, SqlOperatorFixture.CastType castType) Returns the test factory.default StringgetTargetType(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 SqlOperatorFixturewithCaseSensitive(boolean sensitive) Returns a fixture that matches identifiers by case-sensitive or case-insensitive.default SqlOperatorFixturewithConformance(SqlConformance conformance) Returns a fixture that tests conformance to a particular SQL language version.default SqlOperatorFixturewithConnectionFactory(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 SqlOperatorFixturewithLenientOperatorLookup(boolean lenient) Returns a fixture that does not fail validation if it encounters an unknown function.default SqlOperatorFixtureReturns a fixture that follows a given lexical policy.default SqlOperatorFixturewithLibraries(SqlLibrary... libraries) default SqlOperatorFixturewithLibrary(SqlLibrary library) default SqlOperatorFixturewithOperatorTable(SqlOperatorTable operatorTable) Returns a fixture that uses a given operator table.default SqlOperatorFixturewithParserConfig(UnaryOperator<SqlParser.Config> transform) Creates a copy of this fixture with a new parser configuration.default SqlOperatorFixturewithQuotedCasing(org.apache.calcite.avatica.util.Casing casing) Returns a fixture that applies a given casing policy to quoted identifiers.default SqlOperatorFixturewithQuoting(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 SqlOperatorFixturewithUnquotedCasing(org.apache.calcite.avatica.util.Casing casing) Returns a fixture that applies a given casing policy to unquoted identifiers.default SqlOperatorFixturewithValidatorConfig(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:
-
INTEGER_OVERFLOW
- See Also:
-
LONG_OVERFLOW
- See Also:
-
DECIMAL_OVERFLOW
- 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 emphas type "INTEGER",SELECT sum(1) FROM emp GROUP BY deptnohas 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
resultis null, the expression must yield the SQL NULL value. Ifresultis 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.TypeCheckerandSqlTester.ResultCheckerfunctors.- 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)
-