Skip to content

Commit

Permalink
Add multirange support (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaclarke authored Nov 9, 2023
1 parent b5d2143 commit 50b841f
Show file tree
Hide file tree
Showing 24 changed files with 280 additions and 70 deletions.
2 changes: 1 addition & 1 deletion shared/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"peerDependencies": {
"edgedb": "^1.2.0",
"edgedb": "^1.4.0",
"react": "^17.0.0",
"react-dom": "^17.0.0"
},
Expand Down
13 changes: 13 additions & 0 deletions shared/common/schemaData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export interface SchemaMultirangeType {
name: string;
escapedName: string;
elementType: SchemaType;
rangeType: {
schemaType: "Range";
name: string;
elementType: SchemaType;
};
}

interface _SchemaPointer {
Expand Down Expand Up @@ -750,6 +755,14 @@ export function buildTypesGraph(data: RawIntrospectionResult): {
);
}
(types.get(type.id) as SchemaRangeType).elementType = elementType;

if (type.type === "schema::MultiRange") {
(types.get(type.id) as SchemaMultirangeType).rangeType = {
schemaType: "Range",
name: `range<${elementType.name}>`,
elementType,
};
}
break;
}
case "schema::ObjectType": {
Expand Down
8 changes: 6 additions & 2 deletions shared/inspector/buildItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export function expandItem(
}

const itemTypes: {
[key in Exclude<CodecKind, "range" | "sparse_object">]: {
[key in Exclude<CodecKind, "range" | "multirange" | "sparse_object">]: {
type: ItemType;
brackets: string;
};
Expand Down Expand Up @@ -390,7 +390,11 @@ export function buildItem(
? "set"
: base.codec.getKind();

if (codecKind === "scalar" || codecKind === "range") {
if (
codecKind === "scalar" ||
codecKind === "range" ||
codecKind === "multirange"
) {
return buildScalarItem(base, data, index, comma, noMultiline);
}

Expand Down
33 changes: 29 additions & 4 deletions shared/inspector/buildScalar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {PropsWithChildren, useState} from "react";
import {_ICodec, Range} from "edgedb";
import {PropsWithChildren, useState, Fragment} from "react";
import {_ICodec, Range, MultiRange} from "edgedb";

import cn from "@edgedb/common/utils/classNames";

import {EnumCodec} from "edgedb/dist/codecs/enum";
import {RangeCodec} from "edgedb/dist/codecs/range";
import {RangeCodec, MultiRangeCodec} from "edgedb/dist/codecs/range";

import {Item, ItemType} from "./buildItem";

Expand All @@ -28,7 +28,7 @@ export function buildScalarItem(
data,
base.codec.getKnownTypeName(),
base.codec instanceof EnumCodec,
base.codec instanceof RangeCodec
base.codec instanceof RangeCodec || base.codec instanceof MultiRangeCodec
? base.codec.getSubcodecs()[0].getKnownTypeName()
: undefined,
undefined,
Expand Down Expand Up @@ -197,6 +197,31 @@ export function renderValue(
),
};
}
if (value instanceof MultiRange) {
const ranges = [...value];

return {
body: (
<span>
multirange(
{ranges.map((range, i) => (
<Fragment key={i}>
{
renderValue(
range,
`multirange<${rangeKnownTypeName!}>`,
false,
rangeKnownTypeName!
).body
}
{i < ranges.length - 1 ? ", " : null}
</Fragment>
))}
)
</span>
),
};
}

if (value instanceof Uint8Array) {
return {
Expand Down
2 changes: 1 addition & 1 deletion shared/inspector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "UNLICENSED",
"peerDependencies": {
"@types/react": "^16.9.34",
"edgedb": "^1.3.0",
"edgedb": "^1.4.0",
"react": "^17.0.0",
"react-dom": "^17.0.0"
},
Expand Down
48 changes: 31 additions & 17 deletions shared/inspector/renderJsonResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,12 @@ export function _renderToJson(
}
}
case "range": {
if (val.isEmpty) {
return `{"empty": true}`;
}
const subcodec = codec.getSubcodecs()[0];
return `{"lower": ${_renderToJson(
val.lower,
subcodec,
depth,
implicitLimit
)}, "upper": ${_renderToJson(
val.upper,
subcodec,
depth,
implicitLimit
)}, "inc_lower": ${val.incLower ? "true" : "false"}, "inc_upper": ${
val.incUpper ? "true" : "false"
}}`;
return _renderRangeToJSON(val, codec, depth, implicitLimit);
}
case "multirange": {
return `[${[...val]
.map((range) => _renderRangeToJSON(range, codec, depth, implicitLimit))
.join(", ")}]`;
}
case "set":
case "array":
Expand Down Expand Up @@ -129,3 +118,28 @@ export function _renderToJson(
throw new Error("unexpected sparse_object");
}
}

function _renderRangeToJSON(
val: any,
codec: ICodec,
depth: string,
implicitLimit: number | null
) {
if (val.isEmpty) {
return `{"empty": true}`;
}
const subcodec = codec.getSubcodecs()[0];
return `{"lower": ${_renderToJson(
val.lower,
subcodec,
depth,
implicitLimit
)}, "upper": ${_renderToJson(
val.upper,
subcodec,
depth,
implicitLimit
)}, "inc_lower": ${val.incLower ? "true" : "false"}, "inc_upper": ${
val.incUpper ? "true" : "false"
}}`;
}
2 changes: 2 additions & 0 deletions shared/lang-edgeql/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ const code = `select {
d := <optional std::str>$d,
e := <optional>$e,
f := <array<str>>$f,
g := <range<int32>>$g,
h := <multirange<int32>>$h
}`;

const tree = parser.parse(code);
Expand Down
7 changes: 6 additions & 1 deletion shared/lang-edgeql/lang.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ element {
Punctuation |
Cast |
compoundType |
fns |
QueryParameter |
Parens { "(" element* ")" } |
Braces { "{" (element | ";")* "}" } |
Expand All @@ -38,11 +39,15 @@ TupleType {
}

RangeType {
BuiltinName { @specialize<shortName, "range"> } castStart Name ">"
BuiltinName { @specialize<shortName, "range" | "multirange"> } castStart Name ">"
}

compoundType { ArrayType | TupleType }

fns {
BuiltinName { @specialize<shortName, "range" | "multirange"> }
}

Cast { castStart ( Name | compoundType | RangeType ) ~cast ">" }

QueryParameter[@dynamicPrecedence=1] {
Expand Down
24 changes: 12 additions & 12 deletions shared/lang-edgeql/lang.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions shared/lang-edgeql/lang.terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
export const
RawStringPrefix = 1,
ByteStringPrefix = 2,
dollarString = 51,
opHack = 52,
dollarString = 52,
opHack = 53,
BigNumberPostfix = 3,
reservedKeyword = 53,
unreservedKeyword = 54,
reservedKeyword = 54,
unreservedKeyword = 55,
BuiltinName = 4,
Bool = 5,
Comment = 6,
Expand All @@ -25,5 +25,5 @@ export const
ArrayType = 20,
TupleType = 22,
RangeType = 25,
QueryParameter = 28,
QueryParameterName = 30
QueryParameter = 29,
QueryParameterName = 31
3 changes: 2 additions & 1 deletion shared/lang-edgeql/meta.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const type_builtins: string[];
export const module_builtins: string[];
export const constraint_builtins: string[];
export const fn_builtins: string[];
export const index_builtins: string[];
export const operators: string[];
export const navigation: string[];
export const navigation: string[];
29 changes: 26 additions & 3 deletions shared/lang-edgeql/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const reserved_keywords = [
"analyze",
"and",
"anyarray",
"anyobject",
"anytuple",
"anytype",
"begin",
Expand Down Expand Up @@ -196,10 +197,16 @@ export const unreserved_keywords = [
export const bool_literals = ["false", "true"];

export const type_builtins = [
"Base64Alphabet",
"BaseObject",
"ElasticLanguage",
"FreeObject",
"JsonEmpty",
"Language",
"LuceneLanguage",
"Object",
"PGLanguage",
"Weight",
"anycontiguous",
"anydiscrete",
"anyenum",
Expand All @@ -216,6 +223,7 @@ export const type_builtins = [
"date_duration",
"datetime",
"decimal",
"document",
"duration",
"enum",
"float32",
Expand All @@ -227,7 +235,8 @@ export const type_builtins = [
"local_date",
"local_datetime",
"local_time",
"range",
// "multirange",
// "range",
"relative_duration",
"sequence",
"str",
Expand All @@ -238,6 +247,7 @@ export const type_builtins = [
export const module_builtins = [
"cal",
"cfg",
"enc",
"ext",
"fts",
"math",
Expand Down Expand Up @@ -321,11 +331,11 @@ export const fn_builtins = [
"max",
"mean",
"min",
"multirange",
// "multirange",
"multirange_unpack",
"overlaps",
"random",
"range",
// "range",
"range_get_lower",
"range_get_upper",
"range_is_empty",
Expand All @@ -337,6 +347,7 @@ export const fn_builtins = [
"re_replace",
"re_test",
"round",
"search",
"sequence_next",
"sequence_reset",
"sqrt",
Expand All @@ -363,6 +374,7 @@ export const fn_builtins = [
"sum",
"time_get",
"to_bigint",
"to_bytes",
"to_date_duration",
"to_datetime",
"to_decimal",
Expand All @@ -382,6 +394,17 @@ export const fn_builtins = [
"uuid_generate_v4",
"var",
"var_pop",
"with_options",
];

export const index_builtins = [
"brin",
"btree",
"gin",
"gist",
"hash",
"index",
"spgist",
];

export const operators = [
Expand Down
Loading

0 comments on commit 50b841f

Please sign in to comment.