Search in sources :

Example 6 with SqlRowMetadata

use of com.hazelcast.sql.SqlRowMetadata in project hazelcast by hazelcast.

the class SqlInsertWindowBoundsTest method test_windowBoundsSameTypeAsDescriptor_DATE.

@Test
public void test_windowBoundsSameTypeAsDescriptor_DATE() {
    String name = randomName();
    TestStreamSqlConnector.create(sqlService, name, asList("ts", "name"), asList(DATE, VARCHAR), row(LocalDate.of(1970, 1, 1).toString(), "Alice"), row(LocalDate.of(1970, 1, 5).toString(), null));
    String interval = "INTERVAL '1' DAY";
    final SqlRow row = instance().getSql().execute(sql(name, interval)).iterator().next();
    final SqlRowMetadata metadata = row.getMetadata();
    assertThat(metadata.getColumn(0).getType()).isEqualTo(SqlColumnType.DATE);
    assertThat((Object) row.getObject(0)).isInstanceOf(SqlColumnType.DATE.getValueClass());
    assertThat(metadata.getColumn(1).getType()).isEqualTo(SqlColumnType.DATE);
    assertThat((Object) row.getObject(1)).isInstanceOf(SqlColumnType.DATE.getValueClass());
    assertThat(metadata.getColumn(2).getType()).isEqualTo(SqlColumnType.DATE);
    assertThat((Object) row.getObject(2)).isInstanceOf(SqlColumnType.DATE.getValueClass());
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with SqlRowMetadata

use of com.hazelcast.sql.SqlRowMetadata in project hazelcast by hazelcast.

the class SqlInsertWindowBoundsTest method test_windowBoundsSameTypeAsDescriptor_TIMESTAMP_TZ.

@Test
public void test_windowBoundsSameTypeAsDescriptor_TIMESTAMP_TZ() {
    String name = randomName();
    TestStreamSqlConnector.create(sqlService, name, asList("ts", "name"), asList(TIMESTAMP_WITH_TIME_ZONE, VARCHAR), row(timestampTz(0L).toString(), "Alice"), row(timestampTz(10L).toString(), null));
    String interval = "INTERVAL '0.001' SECOND";
    final SqlRow row = instance().getSql().execute(sql(name, interval)).iterator().next();
    final SqlRowMetadata metadata = row.getMetadata();
    Class<?> valueClass = SqlColumnType.TIMESTAMP_WITH_TIME_ZONE.getValueClass();
    assertThat(metadata.getColumn(0).getType()).isEqualTo(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE);
    assertThat((Object) row.getObject(0)).isInstanceOf(valueClass);
    assertThat(metadata.getColumn(1).getType()).isEqualTo(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE);
    assertThat((Object) row.getObject(1)).isInstanceOf(valueClass);
    assertThat(metadata.getColumn(2).getType()).isEqualTo(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE);
    assertThat((Object) row.getObject(2)).isInstanceOf(valueClass);
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with SqlRowMetadata

use of com.hazelcast.sql.SqlRowMetadata in project hazelcast by hazelcast.

the class SqlConsole method executeSqlCmd.

private static void executeSqlCmd(HazelcastInstance hz, String command, Terminal terminal, AtomicReference<SqlResult> activeSqlResult) {
    PrintWriter out = terminal.writer();
    try (SqlResult sqlResult = hz.getSql().execute(command)) {
        activeSqlResult.set(sqlResult);
        // if it's a result with an update count, just print it
        if (sqlResult.updateCount() != -1) {
            String message = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append("OK").toAnsi();
            out.println(message);
            return;
        }
        SqlRowMetadata rowMetadata = sqlResult.getRowMetadata();
        int[] colWidths = determineColumnWidths(rowMetadata);
        Alignment[] alignments = determineAlignments(rowMetadata);
        // this is a result with rows. Print the header and rows, watch for concurrent cancellation
        printMetadataInfo(rowMetadata, colWidths, alignments, out);
        int rowCount = 0;
        for (SqlRow row : sqlResult) {
            rowCount++;
            printRow(row, colWidths, alignments, out);
        }
        // bottom line after all the rows
        printSeparatorLine(sqlResult.getRowMetadata().getColumnCount(), colWidths, out);
        String message = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append(String.valueOf(rowCount)).append(" row(s) selected").toAnsi();
        out.println(message);
    } catch (HazelcastSqlException e) {
        // the query failed to execute with HazelcastSqlException
        String errorPrompt = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append(e.getMessage()).toAnsi();
        out.println(errorPrompt);
    } catch (Exception e) {
        // the query failed to execute with an unexpected exception
        String unexpectedErrorPrompt = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append("Encountered an unexpected exception while executing the query:\n").append(e.getMessage()).toAnsi();
        out.println(unexpectedErrorPrompt);
        e.printStackTrace(out);
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) EndOfFileException(org.jline.reader.EndOfFileException) UserInterruptException(org.jline.reader.UserInterruptException) PrintWriter(java.io.PrintWriter) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException)

Example 9 with SqlRowMetadata

use of com.hazelcast.sql.SqlRowMetadata in project hazelcast by hazelcast.

the class SqlPageCodecTest method check.

private void check(SqlColumnType type, List<Object> values, boolean last) {
    SqlRowMetadata rowMetadata = new SqlRowMetadata(Collections.singletonList(new SqlColumnMetadata("a", type, true)));
    List<SqlRow> rows = new ArrayList<>();
    for (Object value : values) {
        if (SqlPage.convertToData(type) && value != null) {
            value = serializationService.toData(value);
        }
        rows.add(new SqlRowImpl(rowMetadata, new JetSqlRow(TEST_SS, new Object[] { value })));
    }
    SqlPage originalPage = SqlPage.fromRows(Collections.singletonList(type), rows, last, serializationService);
    ClientMessage message = ClientMessage.createForEncode();
    SqlPageCodec.encode(message, originalPage);
    SqlPage restoredPage = SqlPageCodec.decode(message.frameIterator());
    assertEquals(1, restoredPage.getColumnCount());
    assertEquals(values.size(), restoredPage.getRowCount());
    assertEquals(last, restoredPage.isLast());
    assertEquals(1, restoredPage.getColumnTypes().size());
    assertEquals(type, restoredPage.getColumnTypes().get(0));
    for (int i = 0; i < values.size(); i++) {
        Object value = values.get(i);
        Object restoredValue = restoredPage.getColumnValueForClient(0, i);
        if (restoredValue instanceof Data) {
            assertTrue(SqlPage.convertToData(type));
            restoredValue = serializationService.toObject(restoredValue);
        }
        assertEquals(value, restoredValue);
    }
}
Also used : JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) SqlRow(com.hazelcast.sql.SqlRow) SqlRowImpl(com.hazelcast.sql.impl.SqlRowImpl) ArrayList(java.util.ArrayList) Data(com.hazelcast.internal.serialization.Data) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) SqlColumnMetadata(com.hazelcast.sql.SqlColumnMetadata) SqlPage(com.hazelcast.sql.impl.client.SqlPage)

Example 10 with SqlRowMetadata

use of com.hazelcast.sql.SqlRowMetadata in project hazelcast by hazelcast.

the class CalciteSqlOptimizer method toPlan.

private SqlPlanImpl toPlan(PlanKey planKey, QueryParameterMetadata parameterMetadata, RelNode rel, List<String> fieldNames, OptimizerContext context, boolean isInfiniteRows, boolean isCreateJob, String query) {
    PhysicalRel physicalRel = optimize(parameterMetadata, rel, context, isCreateJob);
    List<Permission> permissions = extractPermissions(physicalRel);
    if (physicalRel instanceof SelectByKeyMapPhysicalRel) {
        assert !isCreateJob;
        SelectByKeyMapPhysicalRel select = (SelectByKeyMapPhysicalRel) physicalRel;
        SqlRowMetadata rowMetadata = createRowMetadata(fieldNames, physicalRel.schema(parameterMetadata).getTypes(), rel.getRowType().getFieldList());
        return new IMapSelectPlan(planKey, select.objectKey(), parameterMetadata, select.mapName(), select.keyCondition(parameterMetadata), select.rowProjectorSupplier(parameterMetadata), rowMetadata, planExecutor, permissions);
    } else if (physicalRel instanceof InsertMapPhysicalRel) {
        assert !isCreateJob;
        InsertMapPhysicalRel insert = (InsertMapPhysicalRel) physicalRel;
        return new IMapInsertPlan(planKey, insert.objectKey(), parameterMetadata, insert.mapName(), insert.entriesFn(), planExecutor, permissions);
    } else if (physicalRel instanceof SinkMapPhysicalRel) {
        assert !isCreateJob;
        SinkMapPhysicalRel sink = (SinkMapPhysicalRel) physicalRel;
        return new IMapSinkPlan(planKey, sink.objectKey(), parameterMetadata, sink.mapName(), sink.entriesFn(), planExecutor, permissions);
    } else if (physicalRel instanceof UpdateByKeyMapPhysicalRel) {
        assert !isCreateJob;
        UpdateByKeyMapPhysicalRel update = (UpdateByKeyMapPhysicalRel) physicalRel;
        return new IMapUpdatePlan(planKey, update.objectKey(), parameterMetadata, update.mapName(), update.keyCondition(parameterMetadata), update.updaterSupplier(parameterMetadata), planExecutor, permissions);
    } else if (physicalRel instanceof DeleteByKeyMapPhysicalRel) {
        assert !isCreateJob;
        DeleteByKeyMapPhysicalRel delete = (DeleteByKeyMapPhysicalRel) physicalRel;
        return new IMapDeletePlan(planKey, delete.objectKey(), parameterMetadata, delete.mapName(), delete.keyCondition(parameterMetadata), planExecutor, permissions);
    } else if (physicalRel instanceof TableModify) {
        checkDmlOperationWithView(physicalRel);
        Operation operation = ((TableModify) physicalRel).getOperation();
        CreateDagVisitor visitor = traverseRel(physicalRel, parameterMetadata);
        return new DmlPlan(operation, planKey, parameterMetadata, visitor.getObjectKeys(), visitor.getDag(), query, isInfiniteRows, planExecutor, permissions);
    } else {
        CreateDagVisitor visitor = traverseRel(new RootRel(physicalRel), parameterMetadata);
        SqlRowMetadata rowMetadata = createRowMetadata(fieldNames, physicalRel.schema(parameterMetadata).getTypes(), rel.getRowType().getFieldList());
        return new SelectPlan(planKey, parameterMetadata, visitor.getObjectKeys(), visitor.getDag(), query, isInfiniteRows, rowMetadata, planExecutor, permissions);
    }
}
Also used : IMapDeletePlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapDeletePlan) IMapSinkPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapSinkPlan) Operation(org.apache.calcite.rel.core.TableModify.Operation) RootRel(com.hazelcast.jet.sql.impl.opt.physical.RootRel) InsertMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.InsertMapPhysicalRel) SelectByKeyMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.SelectByKeyMapPhysicalRel) DmlPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DmlPlan) IMapUpdatePlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapUpdatePlan) CreateDagVisitor(com.hazelcast.jet.sql.impl.opt.physical.CreateDagVisitor) SelectByKeyMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.SelectByKeyMapPhysicalRel) SinkMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.SinkMapPhysicalRel) DeleteByKeyMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.DeleteByKeyMapPhysicalRel) InsertMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.InsertMapPhysicalRel) UpdateByKeyMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.UpdateByKeyMapPhysicalRel) PhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.PhysicalRel) Permission(java.security.Permission) MapPermission(com.hazelcast.security.permission.MapPermission) IMapSelectPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapSelectPlan) SelectPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.SelectPlan) IMapSelectPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapSelectPlan) SinkMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.SinkMapPhysicalRel) DeleteByKeyMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.DeleteByKeyMapPhysicalRel) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) TableModify(org.apache.calcite.rel.core.TableModify) IMapInsertPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapInsertPlan) UpdateByKeyMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.UpdateByKeyMapPhysicalRel)

Aggregations

SqlRowMetadata (com.hazelcast.sql.SqlRowMetadata)18 SqlRow (com.hazelcast.sql.SqlRow)13 QuickTest (com.hazelcast.test.annotation.QuickTest)10 Test (org.junit.Test)10 SqlColumnMetadata (com.hazelcast.sql.SqlColumnMetadata)5 ArrayList (java.util.ArrayList)5 SqlResult (com.hazelcast.sql.SqlResult)4 HashMap (java.util.HashMap)4 DmlPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.DmlPlan)3 IMapDeletePlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapDeletePlan)3 IMapInsertPlan (com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapInsertPlan)3 BitmapIndexOptions (com.hazelcast.config.BitmapIndexOptions)2 UniqueKeyTransformation (com.hazelcast.config.BitmapIndexOptions.UniqueKeyTransformation)2 IndexConfig (com.hazelcast.config.IndexConfig)2 IndexType (com.hazelcast.config.IndexType)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)2 Job (com.hazelcast.jet.Job)2 JobStateSnapshot (com.hazelcast.jet.JobStateSnapshot)2 JobConfig (com.hazelcast.jet.config.JobConfig)2