Skip to content

Commit

Permalink
repo-sqale: m_ext_item item name and type are mapped as TEXT, not URI id
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Jun 7, 2021
1 parent 36c83ae commit df7fb99
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 14 deletions.
50 changes: 50 additions & 0 deletions repo/repo-sqale/sql/pgnew-experiments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,56 @@ where
order by oid
;

-- more JSONB experiments with matching various types or nested structures (e.g. poly-strings)
/*
insert into m_user (nameorig, namenorm, ext)
values ('usera', 'usera', '{"polys":[{"o":"orig1", "n":"norm1"}, {"o":"orig2", "n":"norm2"}]}');
insert into m_user (nameorig, namenorm, ext)
values ('userb', 'userb', '{"polys":[]}'); -- hypothetic, we will NOT save the key at all
insert into m_user (nameorig, namenorm, ext)
values ('userc', 'userc', '{"polys":[{"o":"CORIG1", "n":"CNORM1"}, {"o":"CORIG2", "n":"CNORM2"}]}');
insert into m_user (nameorig, namenorm, ext)
values ('userd', 'userd', '{"polys":[{"o":"dORIG1", "n":"dNORM1"}, {"o":"ORIG2", "n":"NORM2"}]}');
insert into m_user (nameorig, namenorm, ext)
values ('userx', 'userx', '{"number":123456789012345678901234567890, "float": 1234567890.1234567890}');
insert into m_user (nameorig, namenorm, ext)
values ('usery', 'usery', '{"number":23456789012345678901234567890, "float": 234567890.1234567890}');
*/

select ext->'number' from m_object
where (ext->'number')::numeric > 2345678901234567890
order by ext->'number';

select nameorig, ext from m_object
-- where (ext->'number')::numeric > 2345678901234567890
order by ext->'float';

-- set enable_seqscan=false
-- set enable_seqscan=true
explain
select oid, nameorig, ext
from m_user
-- where ext @> '{"float": 1234567890.1234567890}' -- can use index
-- where ext @> '{"float": [1234567890.1234567890]}' -- does not match
-- where (ext->'float')::numeric = 1234567890.1234567890 -- works, doesn't using GIN index
-- where (ext->'float'->0)::numeric = 1234567890.1234567890 -- works for arrays too, doesn't using GIN index
-- the last two can benefit from adding: and ext ? 'float' -- that actually utilizes the index
;

-- matching poly-strings
explain
-- select oid, nameorig, ext, jsonb_array_elements_text(ext->'polys')
select oid, nameorig, ext
from m_user
-- where ext @> '{"polys": []}' -- this matches ANY polys, empty or not
-- where ext @> '{"polys": [{"o":"orig1"}]}' -- exact match is easy
-- where ext->'polys'->'n' is not null -- doesn't match anything, 'n' is nested in array
-- where ext->'polys'->0->>'n' ilike 'NO%' -- works but only for first value, we want also "userd"
-- ^^above the last ->> (double >) is critical, if -> ::text is used, it returns values with " around
-- where ext->'polys'->0->'n'#>>'{}' ilike 'NO%' -- alternative with #>>'{}', see https://dba.stackexchange.com/a/234047/157622
where exists (select 1 from jsonb_array_elements(ext->'polys') val WHERE val->>'n' ilike 'NO%')
;

-- MANAGEMENT queries

-- See: https://wiki.postgresql.org/wiki/Disk_Usage
Expand Down
8 changes: 6 additions & 2 deletions repo/repo-sqale/sql/pgnew-repo.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1607,10 +1607,14 @@ CREATE INDEX m_operation_execution_timestampValue_idx ON m_operation_execution (
-- endregion

-- region Extension support
-- Catalog table of known indexed extension items.
-- While itemName and valueType are both Q-names they are not cached via m_uri because this
-- table is small, itemName does not repeat (valueType does) and readability is also better.
-- This has similar function as m_uri - it translates something to IDs, no need to nest it.
CREATE TABLE m_ext_item (
id SERIAL NOT NULL PRIMARY KEY,
itemNameId INTEGER NOT NULL REFERENCES m_uri(id),
valueTypeId INTEGER NOT NULL REFERENCES m_uri(id),
itemName TEXT NOT NULL,
valueType TEXT NOT NULL,
holderType ExtItemHolderType NOT NULL,
cardinality ExtItemCardinality NOT NULL
-- information about storage mechanism (JSON common/separate, column, table separate/common, etc.)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,72 @@
*/
package com.evolveum.midpoint.repo.sqale.qmodel.ext;

import com.evolveum.midpoint.repo.sqale.qmodel.common.QUri;
import java.util.Objects;

/**
* Querydsl "row bean" type related to {@link QUri}.
* Querydsl "row bean" type related to {@link QExtItem}.
*/
public class MExtItem {

public Integer id;
public Integer itemNameId;
public Integer valueTypeId; // references use ObjectReferenceType#COMPLEX_TYPE
public String itemName;
public String valueType; // references use ObjectReferenceType#COMPLEX_TYPE
public MExtItemHolderType holderType;
public MExtItemCardinality cardinality;

public static MExtItem of(Integer id, Key key) {
MExtItem row = new MExtItem();
row.id = id;
row.itemName = key.itemName;
row.valueType = key.valueType;
row.holderType = key.holderType;
row.cardinality = key.cardinality;
return row;
}

public Key key() {
Key key = new Key();
key.itemName = this.itemName;
key.valueType = this.valueType;
key.holderType = this.holderType;
key.cardinality = this.cardinality;
return key;
}

public static class Key {
public String itemName;
public String valueType;
public MExtItemHolderType holderType;
public MExtItemCardinality cardinality;

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Key key = (Key) o;

return Objects.equals(itemName, key.itemName)
&& Objects.equals(valueType, key.valueType)
&& holderType == key.holderType
&& cardinality == key.cardinality;
}

@Override
public int hashCode() {
return Objects.hash(itemName, valueType, holderType, cardinality);
}
}

@Override
public String toString() {
return "MExtItem{" +
"id=" + id +
", itemNameId=" + itemNameId +
", valueTypeId=" + valueTypeId +
", itemName=" + itemName +
", valueType=" + valueType +
", holderType=" + holderType +
", cardinality=" + cardinality +
'}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.querydsl.core.types.dsl.EnumPath;
import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.core.types.dsl.StringPath;
import com.querydsl.sql.ColumnMetadata;
import com.querydsl.sql.PrimaryKey;

Expand All @@ -30,18 +31,18 @@ public class QExtItem extends FlexibleRelationalPathBase<MExtItem> {

public static final ColumnMetadata ID =
ColumnMetadata.named("id").ofType(Types.INTEGER).notNull();
public static final ColumnMetadata ITEM_NAME_ID =
ColumnMetadata.named("itemNameId").ofType(Types.INTEGER).notNull();
public static final ColumnMetadata VALUE_TYPE_ID =
ColumnMetadata.named("valueTypeId").ofType(Types.INTEGER).notNull();
public static final ColumnMetadata ITEM_NAME =
ColumnMetadata.named("itemName").ofType(Types.VARCHAR).notNull();
public static final ColumnMetadata VALUE_TYPE =
ColumnMetadata.named("valueType").ofType(Types.VARCHAR).notNull();
public static final ColumnMetadata HOLDER_TYPE =
ColumnMetadata.named("holderType").ofType(Types.OTHER).notNull();
public static final ColumnMetadata CARDINALITY =
ColumnMetadata.named("cardinality").ofType(Types.OTHER).notNull();

public final NumberPath<Integer> id = createInteger("id", ID);
public final NumberPath<Integer> itemNameId = createInteger("itemNameId", ITEM_NAME_ID);
public final NumberPath<Integer> valueTypeId = createInteger("valueTypeId", VALUE_TYPE_ID);
public final StringPath itemName = createString("itemName", ITEM_NAME);
public final StringPath valueType = createString("valueType", VALUE_TYPE);
public final EnumPath<MExtItemHolderType> holderType =
createEnum("holderType", MExtItemHolderType.class, HOLDER_TYPE);
public final EnumPath<MExtItemCardinality> cardinality =
Expand Down

0 comments on commit df7fb99

Please sign in to comment.