Search in sources :

Example 21 with SqlOperatorFixture

use of org.apache.calcite.sql.test.SqlOperatorFixture in project calcite by apache.

the class SqlOperatorTest method testConcatOperator.

@Test
void testConcatOperator() {
    final SqlOperatorFixture f = fixture();
    f.setFor(SqlStdOperatorTable.CONCAT, VmName.EXPAND);
    f.checkString(" 'a'||'b' ", "ab", "CHAR(2) NOT NULL");
    f.checkNull(" 'a' || cast(null as char(2)) ");
    f.checkNull(" cast(null as char(2)) || 'b' ");
    f.checkNull(" cast(null as char(1)) || cast(null as char(2)) ");
    f.checkString(" x'fe'||x'df' ", "fedf", "BINARY(2) NOT NULL");
    f.checkString(" cast('fe' as char(2)) || cast('df' as varchar)", "fedf", "VARCHAR NOT NULL");
    // Precision is larger than VARCHAR allows, so result is unbounded
    f.checkString(" cast('fe' as char(2)) || cast('df' as varchar(65535))", "fedf", "VARCHAR NOT NULL");
    f.checkString(" cast('fe' as char(2)) || cast('df' as varchar(33333))", "fedf", "VARCHAR(33335) NOT NULL");
    f.checkNull("x'ff' || cast(null as varbinary)");
    f.checkNull(" cast(null as ANY) || cast(null as ANY) ");
    f.checkString("cast('a' as varchar) || cast('b' as varchar) " + "|| cast('c' as varchar)", "abc", "VARCHAR NOT NULL");
}
Also used : SqlOperatorFixture(org.apache.calcite.sql.test.SqlOperatorFixture) Test(org.junit.jupiter.api.Test)

Example 22 with SqlOperatorFixture

use of org.apache.calcite.sql.test.SqlOperatorFixture in project calcite by apache.

the class SqlOperatorTest method testJsonPredicate.

@Test
void testJsonPredicate() {
    final SqlOperatorFixture f = fixture();
    f.checkBoolean("'{}' is json value", true);
    f.checkBoolean("'{]' is json value", false);
    f.checkBoolean("'{}' is json object", true);
    f.checkBoolean("'[]' is json object", false);
    f.checkBoolean("'{}' is json array", false);
    f.checkBoolean("'[]' is json array", true);
    f.checkBoolean("'100' is json scalar", true);
    f.checkBoolean("'[]' is json scalar", false);
    f.checkBoolean("'{}' is not json value", false);
    f.checkBoolean("'{]' is not json value", true);
    f.checkBoolean("'{}' is not json object", false);
    f.checkBoolean("'[]' is not json object", true);
    f.checkBoolean("'{}' is not json array", true);
    f.checkBoolean("'[]' is not json array", false);
    f.checkBoolean("'100' is not json scalar", false);
    f.checkBoolean("'[]' is not json scalar", true);
}
Also used : SqlOperatorFixture(org.apache.calcite.sql.test.SqlOperatorFixture) Test(org.junit.jupiter.api.Test)

Example 23 with SqlOperatorFixture

use of org.apache.calcite.sql.test.SqlOperatorFixture in project calcite by apache.

the class SqlOperatorTest method testCardinalityFunc.

@Test
void testCardinalityFunc() {
    final SqlOperatorFixture f = fixture();
    f.setFor(SqlStdOperatorTable.CARDINALITY, VM_FENNEL, VM_JAVA);
    f.checkScalarExact("cardinality(multiset[cast(null as integer),2])", 2);
    if (!f.brokenTestsEnabled()) {
        return;
    }
    // applied to array
    f.checkScalarExact("cardinality(array['foo', 'bar'])", 2);
    // applied to map
    f.checkScalarExact("cardinality(map['foo', 1, 'bar', 2])", 2);
}
Also used : SqlOperatorFixture(org.apache.calcite.sql.test.SqlOperatorFixture) Test(org.junit.jupiter.api.Test)

Example 24 with SqlOperatorFixture

use of org.apache.calcite.sql.test.SqlOperatorFixture in project calcite by apache.

the class SqlOperatorTest method testYear.

@Test
void testYear() {
    final SqlOperatorFixture f = fixture();
    f.setFor(SqlStdOperatorTable.YEAR, VM_FENNEL, VM_JAVA);
    f.checkScalar("year(date '2008-1-23')", "2008", "BIGINT NOT NULL");
    f.checkNull("year(cast(null as date))");
}
Also used : SqlOperatorFixture(org.apache.calcite.sql.test.SqlOperatorFixture) Test(org.junit.jupiter.api.Test)

Example 25 with SqlOperatorFixture

use of org.apache.calcite.sql.test.SqlOperatorFixture in project calcite by apache.

the class SqlOperatorTest method testTruncateFunc.

@Test
void testTruncateFunc() {
    final SqlOperatorFixture f = fixture();
    f.setFor(SqlStdOperatorTable.TRUNCATE, VmName.EXPAND);
    f.checkType("truncate(42, -1)", "INTEGER NOT NULL");
    f.checkType("truncate(cast(42 as float), 1)", "FLOAT NOT NULL");
    f.checkType("truncate(case when false then 42 else null end, -1)", "INTEGER");
    f.enableTypeCoercion(false).checkFails("^truncate('abc', 'def')^", "Cannot apply 'TRUNCATE' to arguments of type " + "'TRUNCATE\\(<CHAR\\(3\\)>, <CHAR\\(3\\)>\\)'\\. Supported " + "form\\(s\\): 'TRUNCATE\\(<NUMERIC>, <INTEGER>\\)'", false);
    f.checkType("truncate('abc', 'def')", "DECIMAL(19, 9) NOT NULL");
    f.checkScalar("truncate(42, -1)", 40, "INTEGER NOT NULL");
    f.checkScalar("truncate(cast(42.345 as decimal(2, 3)), 2)", BigDecimal.valueOf(4234, 2), "DECIMAL(2, 3) NOT NULL");
    f.checkScalar("truncate(cast(-42.345 as decimal(2, 3)), 2)", BigDecimal.valueOf(-4234, 2), "DECIMAL(2, 3) NOT NULL");
    f.checkNull("truncate(cast(null as integer), 1)");
    f.checkNull("truncate(cast(null as double), 1)");
    f.checkNull("truncate(43.21, cast(null as integer))");
    f.checkScalar("truncate(42)", 42, "INTEGER NOT NULL");
    f.checkScalar("truncate(42.324)", BigDecimal.valueOf(42, 0), "DECIMAL(5, 3) NOT NULL");
    f.checkScalar("truncate(cast(42.324 as float))", 42F, "FLOAT NOT NULL");
    f.checkScalar("truncate(cast(42.345 as decimal(2, 3)))", BigDecimal.valueOf(42, 0), "DECIMAL(2, 3) NOT NULL");
    f.checkNull("truncate(cast(null as integer))");
    f.checkNull("truncate(cast(null as double))");
}
Also used : SqlOperatorFixture(org.apache.calcite.sql.test.SqlOperatorFixture) Test(org.junit.jupiter.api.Test)

Aggregations

SqlOperatorFixture (org.apache.calcite.sql.test.SqlOperatorFixture)297 Test (org.junit.jupiter.api.Test)292 SqlString (org.apache.calcite.sql.util.SqlString)43 TimestampString (org.apache.calcite.util.TimestampString)42 RelDataType (org.apache.calcite.rel.type.RelDataType)2 SqlLiteral (org.apache.calcite.sql.SqlLiteral)2 Disabled (org.junit.jupiter.api.Disabled)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Strong (org.apache.calcite.plan.Strong)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 Hook (org.apache.calcite.runtime.Hook)1 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)1 SqlCall (org.apache.calcite.sql.SqlCall)1 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)1 SqlJdbcFunctionCall (org.apache.calcite.sql.SqlJdbcFunctionCall)1 SqlNodeList (org.apache.calcite.sql.SqlNodeList)1 SqlOperandCountRange (org.apache.calcite.sql.SqlOperandCountRange)1