Search in sources :

Example 11 with SqlException

use of io.questdb.griffin.SqlException in project questdb by bluestreak01.

the class LineTcpConnectionContextTest method testTableParameterRetentionOnAddColumn.

@Test
public void testTableParameterRetentionOnAddColumn() throws Exception {
    String table = "retention";
    runInContext(() -> {
        try (SqlCompiler compiler = new SqlCompiler(engine);
            SqlExecutionContext sqlExecutionContext = new SqlExecutionContextImpl(engine, 1)) {
            compiler.compile("create table " + table + " (location SYMBOL, temperature DOUBLE, timestamp TIMESTAMP) timestamp(timestamp) partition by DAY WITH maxUncommittedRows=3, commitLag=250ms;", sqlExecutionContext);
        } catch (SqlException ex) {
            throw new RuntimeException(ex);
        }
        try (TableReader reader = engine.getReader(AllowAllCairoSecurityContext.INSTANCE, table)) {
            Assert.assertEquals(3, reader.getMetadata().getMaxUncommittedRows());
            Assert.assertEquals(250_000, reader.getMetadata().getCommitLag());
        }
        recvBuffer = table + ",location=us-midwest temperature=82 1465839830100400200\n" + table + ",location=us-midwest temperature=83 1465839830100500200\n" + table + ",location=us-eastcoast,city=york temperature=81 1465839830101400200\n" + table + ",location=us-midwest temperature=85 1465839830102300200\n" + table + ",location=us-eastcoast temperature=89 1465839830102400200\n" + table + ",location=us-eastcoast temperature=80 1465839830102400200\n" + table + ",location=us-westcost temperature=82 1465839830102500200\n";
        do {
            handleContextIO();
            Assert.assertFalse(disconnected);
        } while (recvBuffer.length() > 0);
        closeContext();
        String expected = "location\ttemperature\ttimestamp\tcity\n" + "us-midwest\t82.0\t2016-06-13T17:43:50.100400Z\t\n" + "us-midwest\t83.0\t2016-06-13T17:43:50.100500Z\t\n" + "us-eastcoast\t81.0\t2016-06-13T17:43:50.101400Z\tyork\n" + "us-midwest\t85.0\t2016-06-13T17:43:50.102300Z\t\n" + "us-eastcoast\t89.0\t2016-06-13T17:43:50.102400Z\t\n" + "us-eastcoast\t80.0\t2016-06-13T17:43:50.102400Z\t\n" + "us-westcost\t82.0\t2016-06-13T17:43:50.102500Z\t\n";
        assertTable(expected, table);
        try (TableReader reader = engine.getReader(AllowAllCairoSecurityContext.INSTANCE, table)) {
            Assert.assertEquals(3, reader.getMetadata().getMaxUncommittedRows());
            Assert.assertEquals(250_000, reader.getMetadata().getCommitLag());
        }
    });
}
Also used : SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) SqlCompiler(io.questdb.griffin.SqlCompiler) SqlExecutionContext(io.questdb.griffin.SqlExecutionContext) SqlException(io.questdb.griffin.SqlException) Test(org.junit.Test)

Example 12 with SqlException

use of io.questdb.griffin.SqlException in project questdb by bluestreak01.

the class SampleByTest method testSampleByFirstLastRecordCursorFactoryInvalidNotFirstLast.

@Test
public void testSampleByFirstLastRecordCursorFactoryInvalidNotFirstLast() {
    try {
        GenericRecordMetadata groupByMeta = new GenericRecordMetadata();
        TableColumnMetadata column = new TableColumnMetadata("col1", 1, ColumnType.LONG, false, 0, false, null);
        groupByMeta.add(column);
        GenericRecordMetadata meta = new GenericRecordMetadata();
        meta.add(column);
        ObjList<QueryColumn> columns = new ObjList<>();
        ExpressionNode first = ExpressionNode.FACTORY.newInstance().of(ColumnType.LONG, "min", 0, 0);
        first.rhs = ExpressionNode.FACTORY.newInstance().of(ColumnType.LONG, "col1", 0, 0);
        QueryColumn col = QueryColumn.FACTORY.newInstance().of("col1", first);
        columns.add(col);
        new SampleByFirstLastRecordCursorFactory(null, new MicroTimestampSampler(100L), groupByMeta, columns, meta, null, 0, null, 0, 0, getSymbolFilter(), -1);
        Assert.fail();
    } catch (SqlException e) {
        TestUtils.assertContains(e.getFlyweightMessage(), "expected first() or last() functions but got min");
    }
}
Also used : ObjList(io.questdb.std.ObjList) QueryColumn(io.questdb.griffin.model.QueryColumn) ExpressionNode(io.questdb.griffin.model.ExpressionNode) SqlException(io.questdb.griffin.SqlException) Test(org.junit.Test) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest)

Example 13 with SqlException

use of io.questdb.griffin.SqlException in project questdb by bluestreak01.

the class SampleByTest method testSampleByFirstLastRecordCursorFactoryInvalidColumns.

@Test
public void testSampleByFirstLastRecordCursorFactoryInvalidColumns() {
    try {
        GenericRecordMetadata groupByMeta = new GenericRecordMetadata();
        groupByMeta.add(new TableColumnMetadata("col1", 1, ColumnType.STRING, false, 0, false, null));
        GenericRecordMetadata meta = new GenericRecordMetadata();
        meta.add(new TableColumnMetadata("col1", 2, ColumnType.LONG, false, 0, false, null));
        ObjList<QueryColumn> columns = new ObjList<>();
        ExpressionNode first = ExpressionNode.FACTORY.newInstance().of(ColumnType.LONG, "first", 0, 0);
        first.rhs = ExpressionNode.FACTORY.newInstance().of(ColumnType.LONG, "col1", 0, 0);
        QueryColumn col = QueryColumn.FACTORY.newInstance().of("col1", first);
        columns.add(col);
        new SampleByFirstLastRecordCursorFactory(null, new MicroTimestampSampler(100L), groupByMeta, columns, meta, null, 0, null, 0, 0, getSymbolFilter(), -1);
        Assert.fail();
    } catch (SqlException e) {
        TestUtils.assertContains(e.getFlyweightMessage(), "first(), last() is not supported on data type");
    }
}
Also used : ObjList(io.questdb.std.ObjList) QueryColumn(io.questdb.griffin.model.QueryColumn) ExpressionNode(io.questdb.griffin.model.ExpressionNode) SqlException(io.questdb.griffin.SqlException) Test(org.junit.Test) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest)

Example 14 with SqlException

use of io.questdb.griffin.SqlException in project questdb by bluestreak01.

the class SampleByTest method testIndexSampleByAlignToCalendarBindVariables.

@Test
public void testIndexSampleByAlignToCalendarBindVariables() throws Exception {
    assertMemoryLeak(() -> {
        compiler.compile("create table x as " + "(" + "select" + "   rnd_double(1)*180 lat," + "   rnd_double(1)*180 lon," + "   rnd_symbol('a') s," + "   timestamp_sequence('2021-03-28T00:59:00.00000Z', 60*1000000L) k" + "   from" + "   long_sequence(100)" + "), index(s) timestamp(k) partition by DAY", sqlExecutionContext);
        try (RecordCursorFactory factory = compiler.compile("select k, s, first(lat) lat, last(lon) lon " + "from x " + "where s in ('a') " + "sample by 1h align to calendar time zone $1 with offset $2", sqlExecutionContext).getRecordCursorFactory()) {
            String expectedMoscow = "k\ts\tlat\tlon\n" + "2021-03-28T00:15:00.000000Z\ta\t144.77803379943109\tNaN\n" + "2021-03-28T01:15:00.000000Z\ta\t31.267026583720984\tNaN\n" + "2021-03-28T02:15:00.000000Z\ta\t103.7167928478985\t128.42101395467057\n";
            String expectedPrague = "k\ts\tlat\tlon\n" + "2021-03-28T00:10:00.000000Z\ta\t144.77803379943109\tNaN\n" + "2021-03-28T01:10:00.000000Z\ta\t137.95662156473048\tNaN\n" + "2021-03-28T02:10:00.000000Z\ta\tNaN\t128.42101395467057\n";
            sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Moscow");
            sqlExecutionContext.getBindVariableService().setStr(1, "00:15");
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                assertCursor(expectedMoscow, cursor, factory.getMetadata(), true);
            }
            // invalid timezone
            sqlExecutionContext.getBindVariableService().setStr(0, "Oopsie");
            sqlExecutionContext.getBindVariableService().setStr(1, "00:15");
            try {
                factory.getCursor(sqlExecutionContext);
                Assert.fail();
            } catch (SqlException e) {
                Assert.assertEquals(108, e.getPosition());
                TestUtils.assertContains(e.getFlyweightMessage(), "invalid timezone: Oopsie");
            }
            sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Prague");
            sqlExecutionContext.getBindVariableService().setStr(1, "uggs");
            try {
                factory.getCursor(sqlExecutionContext);
                Assert.fail();
            } catch (SqlException e) {
                Assert.assertEquals(123, e.getPosition());
                TestUtils.assertContains(e.getFlyweightMessage(), "invalid offset: uggs");
            }
            sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Prague");
            sqlExecutionContext.getBindVariableService().setStr(1, "00:10");
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                assertCursor(expectedPrague, cursor, factory.getMetadata(), true);
            }
            sqlExecutionContext.getBindVariableService().setStr(0, null);
            sqlExecutionContext.getBindVariableService().setStr(1, "00:10");
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                assertCursor(expectedPrague, cursor, factory.getMetadata(), true);
            }
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) SqlException(io.questdb.griffin.SqlException) Test(org.junit.Test) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest)

Example 15 with SqlException

use of io.questdb.griffin.SqlException in project questdb by bluestreak01.

the class CaseFunctionFactory method newInstance.

@Override
public Function newInstance(int position, @Transient ObjList<Function> args, @Transient IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) throws SqlException {
    int n = args.size();
    int returnType = -1;
    final ObjList<Function> vars = new ObjList<>(n);
    final ObjList<Function> argsToPoke = new ObjList<>(n);
    Function elseBranch;
    int elseBranchPosition;
    if (n % 2 == 1) {
        elseBranch = args.getQuick(n - 1);
        elseBranchPosition = argPositions.getQuick(n - 1);
        n--;
    } else {
        elseBranch = null;
        elseBranchPosition = 0;
    }
    // compute return type in this loop
    for (int i = 0; i < n; i += 2) {
        Function bool = args.getQuick(i);
        Function value = args.getQuick(i + 1);
        if (!ColumnType.isBoolean(bool.getType())) {
            throw SqlException.position(argPositions.getQuick(i)).put("BOOLEAN expected, found ").put(ColumnType.nameOf(bool.getType()));
        }
        returnType = CaseCommon.getCommonType(returnType, value.getType(), argPositions.getQuick(i + 1));
        vars.add(bool);
        vars.add(value);
        argsToPoke.add(bool);
        argsToPoke.add(value);
    }
    if (elseBranch != null) {
        returnType = CaseCommon.getCommonType(returnType, elseBranch.getType(), elseBranchPosition);
        argsToPoke.add(elseBranch);
    }
    // next calculate cast functions
    for (int i = 1; i < n; i += 2) {
        vars.setQuick(i, CaseCommon.getCastFunction(vars.getQuick(i), argPositions.getQuick(i), returnType, configuration, sqlExecutionContext));
    }
    if (elseBranch != null) {
        elseBranch = CaseCommon.getCastFunction(elseBranch, elseBranchPosition, returnType, configuration, sqlExecutionContext);
    }
    final int argsLen = vars.size();
    final Function elseB = elseBranch != null ? elseBranch : Constants.getNullConstant(returnType);
    final CaseFunctionPicker picker = record -> {
        for (int i = 0; i < argsLen; i += 2) {
            if (vars.getQuick(i).getBool(record)) {
                return vars.getQuick(i + 1);
            }
        }
        return elseB;
    };
    return CaseCommon.getCaseFunction(position, returnType, picker, argsToPoke);
}
Also used : ColumnType(io.questdb.cairo.ColumnType) ObjList(io.questdb.std.ObjList) Transient(io.questdb.std.Transient) Function(io.questdb.cairo.sql.Function) SqlException(io.questdb.griffin.SqlException) IntList(io.questdb.std.IntList) CairoConfiguration(io.questdb.cairo.CairoConfiguration) Constants(io.questdb.griffin.engine.functions.constants.Constants) SqlExecutionContext(io.questdb.griffin.SqlExecutionContext) FunctionFactory(io.questdb.griffin.FunctionFactory) Function(io.questdb.cairo.sql.Function) ObjList(io.questdb.std.ObjList)

Aggregations

SqlException (io.questdb.griffin.SqlException)17 SqlCompiler (io.questdb.griffin.SqlCompiler)9 SqlExecutionContext (io.questdb.griffin.SqlExecutionContext)9 Test (org.junit.Test)9 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)7 CairoConfiguration (io.questdb.cairo.CairoConfiguration)5 Function (io.questdb.cairo.sql.Function)5 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)5 ColumnType (io.questdb.cairo.ColumnType)4 RecordCursor (io.questdb.cairo.sql.RecordCursor)4 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)4 FunctionFactory (io.questdb.griffin.FunctionFactory)4 Constants (io.questdb.griffin.engine.functions.constants.Constants)4 Record (io.questdb.cairo.sql.Record)3 io.questdb.std (io.questdb.std)3 ObjList (io.questdb.std.ObjList)3 ExpressionNode (io.questdb.griffin.model.ExpressionNode)2 QueryColumn (io.questdb.griffin.model.QueryColumn)2 Runner (org.openjdk.jmh.runner.Runner)2 Options (org.openjdk.jmh.runner.options.Options)2