Search in sources :

Example 11 with SqlOperatorFixture

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

the class SqlOperatorTest method testLastDayFunc.

@Test
void testLastDayFunc() {
    final SqlOperatorFixture f = fixture();
    f.setFor(SqlStdOperatorTable.LAST_DAY, VmName.EXPAND);
    f.checkScalar("last_day(DATE '2019-02-10')", "2019-02-28", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '2019-06-10')", "2019-06-30", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '2019-07-10')", "2019-07-31", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '2019-09-10')", "2019-09-30", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '2019-12-10')", "2019-12-31", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '9999-12-10')", "9999-12-31", "DATE NOT NULL");
    // Edge tests
    f.checkScalar("last_day(DATE '1900-01-01')", "1900-01-31", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '1935-02-01')", "1935-02-28", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '1965-09-01')", "1965-09-30", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '1970-01-01')", "1970-01-31", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '2019-02-28')", "2019-02-28", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '2019-12-31')", "2019-12-31", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '2019-01-01')", "2019-01-31", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '2019-06-30')", "2019-06-30", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '2020-02-20')", "2020-02-29", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '2020-02-29')", "2020-02-29", "DATE NOT NULL");
    f.checkScalar("last_day(DATE '9999-12-31')", "9999-12-31", "DATE NOT NULL");
    f.checkNull("last_day(cast(null as date))");
    f.checkScalar("last_day(TIMESTAMP '2019-02-10 02:10:12')", "2019-02-28", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '2019-06-10 06:10:16')", "2019-06-30", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '2019-07-10 07:10:17')", "2019-07-31", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '2019-09-10 09:10:19')", "2019-09-30", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '2019-12-10 12:10:22')", "2019-12-31", "DATE NOT NULL");
    // Edge tests
    f.checkScalar("last_day(TIMESTAMP '1900-01-01 01:01:02')", "1900-01-31", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '1935-02-01 02:01:03')", "1935-02-28", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '1970-01-01 01:01:02')", "1970-01-31", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '2019-02-28 02:28:30')", "2019-02-28", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '2019-12-31 12:31:43')", "2019-12-31", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '2019-01-01 01:01:02')", "2019-01-31", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '2019-06-30 06:30:36')", "2019-06-30", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '2020-02-20 02:20:33')", "2020-02-29", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '2020-02-29 02:29:31')", "2020-02-29", "DATE NOT NULL");
    f.checkScalar("last_day(TIMESTAMP '9999-12-31 12:31:43')", "9999-12-31", "DATE NOT NULL");
    f.checkNull("last_day(cast(null as timestamp))");
}
Also used : SqlOperatorFixture(org.apache.calcite.sql.test.SqlOperatorFixture) Test(org.junit.jupiter.api.Test)

Example 12 with SqlOperatorFixture

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

the class SqlOperatorTest method testLastValueFunc.

@Test
void testLastValueFunc() {
    final SqlOperatorFixture f = fixture();
    f.setFor(SqlStdOperatorTable.LAST_VALUE, VM_EXPAND);
    final String[] values = { "0", "CAST(null AS INTEGER)", "3", "3" };
    if (!f.brokenTestsEnabled()) {
        return;
    }
    f.checkWinAgg("last_value(x)", values, "ROWS 3 PRECEDING", "INTEGER", isSet("3", "0"));
    final String[] values2 = { "1.6", "1.2" };
    f.checkWinAgg("last_value(x)", values2, "ROWS 3 PRECEDING", "DECIMAL(2, 1) NOT NULL", isSet("1.6", "1.2"));
    final String[] values3 = { "'foo'", "'bar'", "'name'" };
    f.checkWinAgg("last_value(x)", values3, "ROWS 3 PRECEDING", "CHAR(4) NOT NULL", isSet("foo ", "bar ", "name"));
}
Also used : SqlOperatorFixture(org.apache.calcite.sql.test.SqlOperatorFixture) TimestampString(org.apache.calcite.util.TimestampString) SqlString(org.apache.calcite.sql.util.SqlString) Test(org.junit.jupiter.api.Test)

Example 13 with SqlOperatorFixture

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

the class SqlOperatorTest method testCastDateTime.

@Test
void testCastDateTime() {
    // Test cast for date/time/timestamp
    final SqlOperatorFixture f = fixture();
    f.setFor(SqlStdOperatorTable.CAST, VmName.EXPAND);
    f.checkScalar("cast(TIMESTAMP '1945-02-24 12:42:25.34' as TIMESTAMP)", "1945-02-24 12:42:25", "TIMESTAMP(0) NOT NULL");
    f.checkScalar("cast(TIME '12:42:25.34' as TIME)", "12:42:25", "TIME(0) NOT NULL");
    // test rounding
    if (f.brokenTestsEnabled()) {
        f.checkScalar("cast(TIME '12:42:25.9' as TIME)", "12:42:26", "TIME(0) NOT NULL");
    }
    if (Bug.FRG282_FIXED) {
        // test precision
        f.checkScalar("cast(TIME '12:42:25.34' as TIME(2))", "12:42:25.34", "TIME(2) NOT NULL");
    }
    f.checkScalar("cast(DATE '1945-02-24' as DATE)", "1945-02-24", "DATE NOT NULL");
    // timestamp <-> time
    f.checkScalar("cast(TIMESTAMP '1945-02-24 12:42:25.34' as TIME)", "12:42:25", "TIME(0) NOT NULL");
    // time <-> string
    f.checkCastToString("TIME '12:42:25'", null, "12:42:25");
    if (TODO) {
        f.checkCastToString("TIME '12:42:25.34'", null, "12:42:25.34");
    }
    // Generate the current date as a string, e.g. "2007-04-18". The value
    // is guaranteed to be good for at least 2 minutes, which should give
    // us time to run the rest of the tests.
    final String today = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT).format(getCalendarNotTooNear(Calendar.DAY_OF_MONTH).getTime());
    f.checkScalar("cast(DATE '1945-02-24' as TIMESTAMP)", "1945-02-24 00:00:00", "TIMESTAMP(0) NOT NULL");
    // Note: Casting to time(0) should lose date info and fractional
    // seconds, then casting back to timestamp should initialize to
    // current_date.
    f.checkScalar("cast(cast(TIMESTAMP '1945-02-24 12:42:25.34' as TIME) as TIMESTAMP)", today + " 12:42:25", "TIMESTAMP(0) NOT NULL");
    f.checkScalar("cast(TIME '12:42:25.34' as TIMESTAMP)", today + " 12:42:25", "TIMESTAMP(0) NOT NULL");
    // timestamp <-> date
    f.checkScalar("cast(TIMESTAMP '1945-02-24 12:42:25.34' as DATE)", "1945-02-24", "DATE NOT NULL");
    // Note: casting to Date discards Time fields
    f.checkScalar("cast(cast(TIMESTAMP '1945-02-24 12:42:25.34' as DATE) as TIMESTAMP)", "1945-02-24 00:00:00", "TIMESTAMP(0) NOT NULL");
}
Also used : SqlOperatorFixture(org.apache.calcite.sql.test.SqlOperatorFixture) TimestampString(org.apache.calcite.util.TimestampString) SqlString(org.apache.calcite.sql.util.SqlString) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.jupiter.api.Test)

Example 14 with SqlOperatorFixture

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

the class SqlOperatorTest method testCastToApproxNumeric.

@Test
void testCastToApproxNumeric() {
    final SqlOperatorFixture f = fixture();
    f.setFor(SqlStdOperatorTable.CAST, VmName.EXPAND);
    f.checkCastToApproxOkay("1", "DOUBLE", isExactly(1));
    f.checkCastToApproxOkay("1.0", "DOUBLE", isExactly(1));
    f.checkCastToApproxOkay("-2.3", "FLOAT", isWithin(-2.3, 0.000001));
    f.checkCastToApproxOkay("'1'", "DOUBLE", isExactly(1));
    f.checkCastToApproxOkay("'  -1e-37  '", "DOUBLE", isExactly("-1.0E-37"));
    f.checkCastToApproxOkay("1e0", "DOUBLE", isExactly(1));
    f.checkCastToApproxOkay("0e0", "REAL", isExactly(0));
}
Also used : SqlOperatorFixture(org.apache.calcite.sql.test.SqlOperatorFixture) Test(org.junit.jupiter.api.Test)

Example 15 with SqlOperatorFixture

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

the class SqlOperatorTest method testSpaceFunc.

@Test
void testSpaceFunc() {
    final SqlOperatorFixture f = fixture().setFor(SqlLibraryOperators.SPACE).withLibrary(SqlLibrary.MYSQL);
    f.checkString("SPACE(-100)", "", "VARCHAR(2000) NOT NULL");
    f.checkString("SPACE(-1)", "", "VARCHAR(2000) NOT NULL");
    f.checkString("SPACE(0)", "", "VARCHAR(2000) NOT NULL");
    f.checkString("SPACE(2)", "  ", "VARCHAR(2000) NOT NULL");
    f.checkString("SPACE(5)", "     ", "VARCHAR(2000) NOT NULL");
    f.checkNull("SPACE(cast(null as integer))");
}
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