Search in sources :

Example 1 with SqlRowMetadata

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

the class PlanExecutor method execute.

SqlResult execute(ShowStatementPlan plan) {
    Stream<String> rows;
    switch(plan.getShowTarget()) {
        case MAPPINGS:
            rows = catalog.getMappingNames().stream();
            break;
        case VIEWS:
            rows = catalog.getViewNames().stream();
            break;
        case JOBS:
            assert plan.getShowTarget() == ShowStatementTarget.JOBS;
            NodeEngine nodeEngine = getNodeEngine(hazelcastInstance);
            JetServiceBackend jetServiceBackend = nodeEngine.getService(JetServiceBackend.SERVICE_NAME);
            rows = jetServiceBackend.getJobRepository().getJobRecords().stream().map(record -> record.getConfig().getName()).filter(Objects::nonNull);
            break;
        default:
            throw new AssertionError("Unsupported SHOW statement target.");
    }
    SqlRowMetadata metadata = new SqlRowMetadata(singletonList(new SqlColumnMetadata("name", VARCHAR, false)));
    InternalSerializationService serializationService = Util.getSerializationService(hazelcastInstance);
    return new SqlResultImpl(QueryId.create(hazelcastInstance.getLocalEndpoint().getUuid()), new StaticQueryResultProducerImpl(rows.sorted().map(name -> new JetSqlRow(serializationService, new Object[] { name })).iterator()), metadata, false);
}
Also used : NodeEngine(com.hazelcast.spi.impl.NodeEngine) Util.getNodeEngine(com.hazelcast.jet.impl.util.Util.getNodeEngine) UpdateSqlResultImpl(com.hazelcast.sql.impl.UpdateSqlResultImpl) Arrays(java.util.Arrays) DropViewPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DropViewPlan) SelectPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.SelectPlan) TimeoutException(java.util.concurrent.TimeoutException) Extractors(com.hazelcast.query.impl.getters.Extractors) SQL_ARGUMENTS_KEY_NAME(com.hazelcast.sql.impl.expression.ExpressionEvalContext.SQL_ARGUMENTS_KEY_NAME) CreateIndexPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateIndexPlan) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) QueryParameterMetadata(com.hazelcast.sql.impl.QueryParameterMetadata) Collections.singletonList(java.util.Collections.singletonList) MapProxyImpl(com.hazelcast.map.impl.proxy.MapProxyImpl) ParameterConverter(com.hazelcast.sql.impl.ParameterConverter) SqlNode(org.apache.calcite.sql.SqlNode) Map(java.util.Map) VARCHAR(com.hazelcast.sql.SqlColumnType.VARCHAR) CreateJobPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateJobPlan) CreateMappingPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateMappingPlan) DropMappingPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DropMappingPlan) CancellationException(java.util.concurrent.CancellationException) JobConfig(com.hazelcast.jet.config.JobConfig) AlterJobPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.AlterJobPlan) QueryResultRegistry(com.hazelcast.sql.impl.state.QueryResultRegistry) IMapUpdatePlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapUpdatePlan) IndexConfig(com.hazelcast.config.IndexConfig) IMapSelectPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapSelectPlan) Objects(java.util.Objects) Util(com.hazelcast.jet.impl.util.Util) List(java.util.List) ExpressionEvalContext(com.hazelcast.sql.impl.expression.ExpressionEvalContext) Stream(java.util.stream.Stream) JobStateSnapshot(com.hazelcast.jet.JobStateSnapshot) EmptyRow(com.hazelcast.sql.impl.row.EmptyRow) DropJobPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DropJobPlan) DmlPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DmlPlan) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) UNIQUE_KEY_TRANSFORMATION(com.hazelcast.jet.sql.impl.parse.SqlCreateIndex.UNIQUE_KEY_TRANSFORMATION) SqlResult(com.hazelcast.sql.SqlResult) Entry(java.util.Map.Entry) SqlErrorCode(com.hazelcast.sql.impl.SqlErrorCode) UniqueKeyTransformation(com.hazelcast.config.BitmapIndexOptions.UniqueKeyTransformation) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) SqlColumnMetadata(com.hazelcast.sql.SqlColumnMetadata) IMapSinkPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapSinkPlan) EntryRemovingProcessor(com.hazelcast.map.impl.EntryRemovingProcessor) ArrayList(java.util.ArrayList) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) IndexType(com.hazelcast.config.IndexType) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) IMapInsertPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapInsertPlan) View(com.hazelcast.sql.impl.schema.view.View) IMapDeletePlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.IMapDeletePlan) KEY_SQL_QUERY_TEXT(com.hazelcast.jet.config.JobConfigArguments.KEY_SQL_QUERY_TEXT) Job(com.hazelcast.jet.Job) QueryException(com.hazelcast.sql.impl.QueryException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) NodeEngine(com.hazelcast.spi.impl.NodeEngine) MapContainer(com.hazelcast.map.impl.MapContainer) CreateViewPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateViewPlan) DropSnapshotPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DropSnapshotPlan) TableResolverImpl(com.hazelcast.jet.sql.impl.schema.TableResolverImpl) MapService(com.hazelcast.map.impl.MapService) QueryId(com.hazelcast.sql.impl.QueryId) RelNode(org.apache.calcite.rel.RelNode) BitmapIndexOptions(com.hazelcast.config.BitmapIndexOptions) ShowStatementTarget(com.hazelcast.jet.sql.impl.parse.SqlShowStatement.ShowStatementTarget) MapServiceContext(com.hazelcast.map.impl.MapServiceContext) HazelcastTypeUtils.toHazelcastType(com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils.toHazelcastType) CreateSnapshotPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.CreateSnapshotPlan) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ShowStatementPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.ShowStatementPlan) UNIQUE_KEY(com.hazelcast.jet.sql.impl.parse.SqlCreateIndex.UNIQUE_KEY) ExplainStatementPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.ExplainStatementPlan) Util.getNodeEngine(com.hazelcast.jet.impl.util.Util.getNodeEngine) KEY_SQL_UNBOUNDED(com.hazelcast.jet.config.JobConfigArguments.KEY_SQL_UNBOUNDED) AbstractJetInstance(com.hazelcast.jet.impl.AbstractJetInstance) JetServiceBackend(com.hazelcast.jet.impl.JetServiceBackend) IMap(com.hazelcast.map.IMap) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) UpdateSqlResultImpl(com.hazelcast.sql.impl.UpdateSqlResultImpl) SqlColumnMetadata(com.hazelcast.sql.SqlColumnMetadata) JetServiceBackend(com.hazelcast.jet.impl.JetServiceBackend)

Example 2 with SqlRowMetadata

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

the class CalciteSqlOptimizer method createRowMetadata.

private SqlRowMetadata createRowMetadata(List<String> columnNames, List<QueryDataType> columnTypes, List<RelDataTypeField> fields) {
    assert columnNames.size() == columnTypes.size();
    assert columnTypes.size() == fields.size();
    List<SqlColumnMetadata> columns = new ArrayList<>(columnNames.size());
    for (int i = 0; i < columnNames.size(); i++) {
        SqlColumnMetadata column = QueryUtils.getColumnMetadata(columnNames.get(i), columnTypes.get(i), fields.get(i).getType().isNullable());
        columns.add(column);
    }
    return new SqlRowMetadata(columns);
}
Also used : ArrayList(java.util.ArrayList) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) SqlColumnMetadata(com.hazelcast.sql.SqlColumnMetadata)

Example 3 with SqlRowMetadata

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

the class SqlBasicTest method testSelect.

@SuppressWarnings("ConstantConditions")
@Test
public void testSelect() {
    if (isPortable()) {
        createMapping(mapName(), PORTABLE_FACTORY_ID, PORTABLE_KEY_CLASS_ID, 0, PORTABLE_FACTORY_ID, PORTABLE_VALUE_CLASS_ID, 0);
    } else {
        createMapping(mapName(), keyClass(), valueClass());
    }
    // Get proper map
    IMap<Object, AbstractPojo> map = getTarget().getMap(mapName());
    // Populate map with values
    Map<Object, AbstractPojo> data = new HashMap<>();
    for (long i = 0; i < dataSetSize; i++) {
        data.put(key(i), value(i));
    }
    map.putAll(data);
    assertEquals(dataSetSize, map.size());
    // Execute query
    boolean portable = serializationMode == SerializationMode.PORTABLE;
    boolean multiPageClient;
    try (SqlResult res = query()) {
        multiPageClient = memberClientCursors() > 0;
        SqlRowMetadata rowMetadata = res.getRowMetadata();
        checkRowMetadata(rowMetadata);
        Set<Long> uniqueKeys = new HashSet<>();
        Iterator<SqlRow> rowIterator = res.iterator();
        while (rowIterator.hasNext()) {
            SqlRow row = rowIterator.next();
            assertEquals(rowMetadata, res.getRowMetadata());
            Long key0 = row.getObject(rowMetadata.findColumn(adjustFieldName("key")));
            assertNotNull(key0);
            AbstractPojoKey key = key(key0);
            AbstractPojo val = map.get(key);
            checkRowValue(SqlColumnType.BIGINT, key.getKey(), row, "key");
            checkRowValue(SqlColumnType.BOOLEAN, val.isBooleanVal(), row, "booleanVal");
            checkRowValue(SqlColumnType.TINYINT, val.getTinyIntVal(), row, "tinyIntVal");
            checkRowValue(SqlColumnType.SMALLINT, val.getSmallIntVal(), row, "smallIntVal");
            checkRowValue(SqlColumnType.INTEGER, val.getIntVal(), row, "intVal");
            checkRowValue(SqlColumnType.BIGINT, val.getBigIntVal(), row, "bigIntVal");
            checkRowValue(SqlColumnType.REAL, val.getRealVal(), row, "realVal");
            checkRowValue(SqlColumnType.DOUBLE, val.getDoubleVal(), row, "doubleVal");
            if (!portable) {
                checkRowValue(SqlColumnType.DECIMAL, new BigDecimal(val.getDecimalBigIntegerVal()), row, "decimalBigIntegerVal");
            }
            checkRowValue(SqlColumnType.DECIMAL, val.getDecimalVal(), row, "decimalVal");
            checkRowValue(SqlColumnType.VARCHAR, Character.toString(val.getCharVal()), row, "charVal");
            checkRowValue(SqlColumnType.VARCHAR, val.getVarcharVal(), row, "varcharVal");
            checkRowValue(SqlColumnType.DATE, val.getDateVal(), row, "dateVal");
            checkRowValue(SqlColumnType.TIME, val.getTimeVal(), row, "timeVal");
            checkRowValue(SqlColumnType.TIMESTAMP, val.getTimestampVal(), row, "timestampVal");
            if (portable) {
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, val.getTsTzOffsetDateTimeVal(), row, "tsTzOffsetDateTimeVal");
                checkRowValue(SqlColumnType.OBJECT, ((PortablePojo) val).getPortableVal(), row, "portableVal");
                checkRowValue(SqlColumnType.VARCHAR, null, row, "nullVal");
            } else {
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, OffsetDateTime.ofInstant(val.getTsTzDateVal().toInstant(), ZoneId.systemDefault()), row, "tsTzDateVal");
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, val.getTsTzCalendarVal().toZonedDateTime().toOffsetDateTime(), row, "tsTzCalendarVal");
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, OffsetDateTime.ofInstant(val.getTsTzInstantVal(), ZoneId.systemDefault()), row, "tsTzInstantVal");
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, val.getTsTzOffsetDateTimeVal(), row, "tsTzOffsetDateTimeVal");
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, val.getTsTzZonedDateTimeVal().toOffsetDateTime(), row, "tsTzZonedDateTimeVal");
                checkRowValue(SqlColumnType.OBJECT, val.getObjectVal(), row, "objectVal");
                checkRowValue(SqlColumnType.OBJECT, null, row, "nullVal");
            }
            uniqueKeys.add(key0);
            assertThrows(IndexOutOfBoundsException.class, () -> row.getObject(-1));
            assertThrows(IndexOutOfBoundsException.class, () -> row.getObject(row.getMetadata().getColumnCount()));
            assertThrows(NullPointerException.class, () -> row.getObject(null));
            assertThrows(IllegalArgumentException.class, () -> row.getObject("unknown_field"));
        }
        assertThrows(NoSuchElementException.class, rowIterator::next);
        assertThrows(IllegalStateException.class, res::iterator);
        assertEquals(dataSetSize, uniqueKeys.size());
    }
    if (multiPageClient) {
        // If this request spawns multiple pages, then:
        // 1) Ensure that results are cleared when the whole result set is fetched
        // 2) Ensure that results are cleared when the result set is closed in the middle.
        assertEquals(0, memberClientCursors());
        try (SqlResult res = query()) {
            assertEquals(1, memberClientCursors());
            res.close();
            assertEquals(0, memberClientCursors());
        }
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) HashSet(java.util.HashSet) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with SqlRowMetadata

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

the class SqlOrderByTest method assertSqlResultOrdered.

private void assertSqlResultOrdered(String sql, List<String> orderFields, List<Boolean> orderDirections, int expectedCount, Integer low, Integer high) {
    try (SqlResult res = query(sql)) {
        SqlRowMetadata rowMetadata = res.getRowMetadata();
        Iterator<SqlRow> rowIterator = res.iterator();
        SqlRow prevRow = null;
        SqlRow lowRow = null;
        SqlRow highRow = null;
        int count = 0;
        while (rowIterator.hasNext()) {
            SqlRow row = rowIterator.next();
            assertOrdered(prevRow, row, orderFields, orderDirections, rowMetadata);
            prevRow = row;
            count++;
            if (count == 1) {
                lowRow = row;
            }
            if (!rowIterator.hasNext()) {
                highRow = row;
            }
        }
        assertEquals(expectedCount, count);
        if (lowRow != null && low != null) {
            String fieldName = orderFields.get(0);
            Object fieldValue = lowRow.getObject(rowMetadata.findColumn(fieldName));
            assertEquals(low, fieldValue);
        }
        if (highRow != null && high != null) {
            String fieldName = orderFields.get(0);
            Object fieldValue = highRow.getObject(rowMetadata.findColumn(fieldName));
            assertEquals(high, fieldValue);
        }
        assertThrows(NoSuchElementException.class, rowIterator::next);
        assertThrows(IllegalStateException.class, res::iterator);
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata)

Example 5 with SqlRowMetadata

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

the class SqlInsertWindowBoundsTest method test_windowBoundsSameTypeAsDescriptor_INT.

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

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