Interface SqlTester
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
AbstractSqlTester
,SqlOperatorTest.TesterImpl
,SqlValidatorTester
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.
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Parameter checker.static interface
Result checker.static interface
Type checker.static interface
Action that is called after validation.static interface
A function to apply to the result of validation.static enum
Name of a virtual machine that can potentially implement an operator. -
Method Summary
Modifier and TypeMethodDescriptionvoid
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.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.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.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.void
checkAgg
(SqlTestFactory factory, String expr, String[] inputValues, SqlTester.ResultChecker resultChecker) Checks that an aggregate expression returns the expected result.void
checkAggFails
(SqlTestFactory factory, String expr, String[] inputValues, String expectedError, boolean runtime) Tests that an aggregate expression fails at run time.void
checkColumnType
(SqlTestFactory factory, String sql, String type) Checks that a query returns one column of an expected type.default void
checkFails
(SqlTestFactory factory, String expression, String expectedError, boolean runtime) AscheckFails(SqlTestFactory, StringAndPos, String, boolean)
, but with a string that contains carets.void
checkFails
(SqlTestFactory factory, StringAndPos expression, String expectedError, boolean runtime) Tests that a scalar SQL expression fails at run time.void
checkQueryFails
(SqlTestFactory factory, StringAndPos sap, String expectedError) Tests that a SQL query fails at prepare time.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.default RelRoot
convertSqlToRel
(SqlTestFactory factory, String sql, boolean decorrelate, boolean trim) Converts a SQL string to aRelNode
tree.convertSqlToRel2
(SqlTestFactory factory, String sql, boolean decorrelate, boolean trim) Converts a SQL string to a (SqlValidator, RelNode) pair.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.getColumnType
(SqlTestFactory factory, String sql) Returns the data type of the sole column of a SQL query.getResultType
(SqlTestFactory factory, String sql) Returns the data type of the row returned by a SQL query.parseExpression
(SqlTestFactory factory, String expr) Parses an expression.parseQuery
(SqlTestFactory factory, String sql) Parses a query.void
setFor
(SqlOperator operator, SqlTester.VmName... unimplementedVmNames) Declares that this test is for a given operator.trimRelNode
(SqlTestFactory factory, RelNode relNode) Trims a RelNode.<R> R
validateAndApply
(SqlTestFactory factory, StringAndPos sap, SqlTester.ValidatedNodeFunction<R> function) Parses and validates a query, then calls a function on the result.void
validateAndThen
(SqlTestFactory factory, StringAndPos sap, SqlTester.ValidatedNodeConsumer consumer) Parses and validates a query, then calls an action on the result.Methods inherited from interface java.lang.AutoCloseable
close
-
Method Details
-
forEachQuery
Given a scalar expression, generates a sequence of SQL queries that evaluate it, and calls a given action with each.- Parameters:
factory
- Factoryexpression
- Scalar expressionconsumer
- Action to be called for each query
-
parseQuery
Parses a query.- Throws:
SqlParseException
-
parseExpression
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
- Factorysap
- SQL statementexpectedMsgPattern
- If this parameter is null the query must be
-
getColumnType
Returns the data type of the sole column of a SQL query.For example,
getResultType("VALUES (1")
returnsINTEGER
.Fails if query returns more than one column.
- See Also:
-
getResultType
Returns the data type of the row returned by a SQL query.For example,
getResultType("VALUES (1, 'foo')")
returnsRecordType(INTEGER EXPR$0, CHAR(3) EXPR#1)
. -
checkColumnType
Checks that a query returns one column of an expected type. For example,checkType("VALUES (1 + 2)", "INTEGER NOT NULL")
.- Parameters:
factory
- Factorysql
- Query expressiontype
- 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. Ifresult
is aPattern
, the result must match that pattern.- Parameters:
factory
- Factoryquery
- SQL querytypeChecker
- Checks whether the result is the expected typeresultChecker
- 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 usingSqlTester.TypeChecker
andSqlTester.ResultChecker
functors.- Parameters:
factory
- Factoryquery
- 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
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
- Factoryexpr
- 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
- Factoryexpr
- 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
void checkAggFails(SqlTestFactory factory, String expr, String[] inputValues, String expectedError, boolean runtime) Tests that an aggregate expression fails at run time.- Parameters:
factory
- Factoryexpr
- 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
void checkFails(SqlTestFactory factory, StringAndPos expression, String expectedError, boolean runtime) Tests that a scalar SQL expression fails at run time.- Parameters:
factory
- Factoryexpression
- 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
default void checkFails(SqlTestFactory factory, String expression, String expectedError, boolean runtime) AscheckFails(SqlTestFactory, StringAndPos, String, boolean)
, but with a string that contains carets. -
checkQueryFails
Tests that a SQL query fails at prepare time.- Parameters:
factory
- Factorysap
- SQL query and error positionexpectedError
- 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 aRelNode
tree.- Parameters:
factory
- Factorysql
- SQL statementdecorrelate
- Whether to decorrelatetrim
- 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
- FactorydiffRepos
- Diff repositorysql
- SQL query or expressionplan
- Expected plantrim
- Whether to trim columns that are not neededexpression
- True ifsql
is an expression, false if it is a query
-
trimRelNode
Trims a RelNode.
-