Skip to content

Commit 51d189d

Browse files
authored
Merge pull request #17556 from d10c/d10c/bigint-docs
BigInt Documentation
2 parents 3d6965a + 328f322 commit 51d189d

File tree

4 files changed

+138
-107
lines changed

4 files changed

+138
-107
lines changed

docs/codeql/codeql-language-guides/extensible-predicates.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ This example of an extensible predicate for a source is taken from the core Java
2727
.. code-block:: ql
2828
2929
extensible predicate sourceModel(
30-
string package, string type, boolean subtypes, string name,
31-
string signature, string ext, string output, string kind,
30+
string package, string type, boolean subtypes, string name,
31+
string signature, string ext, string output, string kind,
3232
string provenance
3333
);
3434
3535
An extensible predicate is a CodeQL predicate with the following restrictions:
3636

3737
- It uses the ``extensible`` keyword.
3838
- It has no body.
39-
- All predicate parameters have primitive types.
39+
- All predicate parameters have type ``string``, ``int``, ``float``, ``boolean``, or ``date``.
4040
- It is not in a module.
4141

4242
Columns shared by all extensible predicates

docs/codeql/ql-language-reference/modules.rst

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
Modules
66
#######
7-
8-
Modules provide a way of organizing QL code by grouping together related types, predicates, and other modules.
97

10-
You can import modules into other files, which avoids duplication, and helps
8+
Modules provide a way of organizing QL code by grouping together related types, predicates, and other modules.
9+
10+
You can import modules into other files, which avoids duplication, and helps
1111
structure your code into more manageable pieces.
1212

1313
.. _defining-module:
@@ -16,7 +16,7 @@ Defining a module
1616
*****************
1717

1818
There are various ways to define modules—here is an example of the simplest way, declaring an
19-
:ref:`explicit module <explicit-modules>` named ``Example`` containing
19+
:ref:`explicit module <explicit-modules>` named ``Example`` containing
2020
a class ``OneTwoThree``:
2121

2222
.. code-block:: ql
@@ -27,17 +27,17 @@ a class ``OneTwoThree``:
2727
this = 1 or this = 2 or this = 3
2828
}
2929
}
30-
}
30+
}
3131
3232
The name of a module can be any `identifier <https://codeql.github.com/docs/ql-language-reference/ql-language-specification/#identifiers>`_
33-
that starts with an uppercase or lowercase letter.
33+
that starts with an uppercase or lowercase letter.
3434

3535
``.ql`` or ``.qll`` files also implicitly define modules.
3636
For more information, see ":ref:`kinds-of-modules`."
3737

3838
You can also annotate a module. For more information, see of ":ref:`annotations-overview`."
3939

40-
Note that you can only annotate :ref:`explicit modules <explicit-modules>`.
40+
Note that you can only annotate :ref:`explicit modules <explicit-modules>`.
4141
File modules cannot be annotated.
4242

4343
.. _kinds-of-modules:
@@ -48,7 +48,7 @@ Kinds of modules
4848
File modules
4949
============
5050

51-
Each query file (extension ``.ql``) and library file (extension ``.qll``) implicitly defines
51+
Each query file (extension ``.ql``) and library file (extension ``.qll``) implicitly defines
5252
a module. The module has the same name as the file, but any spaces in the file name are replaced
5353
by underscores (``_``). The contents of the file form the :ref:`body of the module <module-bodies>`.
5454

@@ -57,7 +57,7 @@ by underscores (``_``). The contents of the file form the :ref:`body of the modu
5757
Library modules
5858
---------------
5959

60-
A library module is defined by a ``.qll`` file. It can contain any of the
60+
A library module is defined by a ``.qll`` file. It can contain any of the
6161
elements listed in :ref:`module-bodies` below, apart from select clauses.
6262

6363
For example, consider the following QL library:
@@ -75,19 +75,19 @@ For example, consider the following QL library:
7575
This file defines a library module named ``OneTwoThreeLib``. The body of this module
7676
defines the class ``OneTwoThree``.
7777

78-
.. _query-modules:
78+
.. _query-modules:
7979

8080
Query modules
8181
-------------
8282

83-
A query module is defined by a ``.ql`` file. It can contain any of the elements listed
84-
in :ref:`module-bodies` below.
83+
A query module is defined by a ``.ql`` file. It can contain any of the elements listed
84+
in :ref:`module-bodies` below.
8585

8686
Query modules are slightly different from other modules:
8787

8888
- A query module can't be imported.
89-
- A query module must have at least one query in its
90-
:ref:`namespace <namespaces>`. This is usually a :ref:`select clause <select-clauses>`,
89+
- A query module must have at least one query in its
90+
:ref:`namespace <namespaces>`. This is usually a :ref:`select clause <select-clauses>`,
9191
but can also be a :ref:`query predicate <query-predicates>`.
9292

9393
For example:
@@ -97,7 +97,7 @@ For example:
9797
.. code-block:: ql
9898
9999
import OneTwoThreeLib
100-
100+
101101
from OneTwoThree ott
102102
where ott = 1 or ott = 2
103103
select ott
@@ -110,13 +110,13 @@ This file defines a query module named ``OneTwoQuery``. The body of this module
110110
Explicit modules
111111
================
112112

113-
You can also define a module within another module. This is an explicit module definition.
113+
You can also define a module within another module. This is an explicit module definition.
114114

115-
An explicit module is defined with the keyword ``module`` followed by
116-
the module name, and then the module body enclosed in braces. It can contain any
117-
of the elements listed in ":ref:`module-bodies`" below, apart from select clauses.
115+
An explicit module is defined with the keyword ``module`` followed by
116+
the module name, and then the module body enclosed in braces. It can contain any
117+
of the elements listed in ":ref:`module-bodies`" below, apart from select clauses.
118118

119-
For example, you could add the following QL snippet to the library file **OneTwoThreeLib.qll**
119+
For example, you could add the following QL snippet to the library file **OneTwoThreeLib.qll**
120120
defined :ref:`above <library-modules>`:
121121

122122
.. code-block:: ql
@@ -129,7 +129,7 @@ defined :ref:`above <library-modules>`:
129129
}
130130
}
131131
}
132-
132+
133133
This defines an explicit module named ``M``. The body of this module defines
134134
the class ``OneTwo``.
135135

@@ -226,7 +226,7 @@ Module bodies
226226
*************
227227

228228
The body of a module is the code inside the module definition, for example
229-
the class ``OneTwo`` in the :ref:`explicit module <explicit-modules>` ``M``.
229+
the class ``OneTwo`` in the :ref:`explicit module <explicit-modules>` ``M``.
230230

231231
In general, the body of a module can contain the following constructs:
232232

@@ -243,11 +243,11 @@ In general, the body of a module can contain the following constructs:
243243
Importing modules
244244
*****************
245245

246-
The main benefit of storing code in a module is that you can reuse it in other modules.
247-
To access the contents of an external module, you can import the module using an
246+
The main benefit of storing code in a module is that you can reuse it in other modules.
247+
To access the contents of an external module, you can import the module using an
248248
:ref:`import statement <import-statements>`.
249249

250-
When you import a module this brings all the names in its namespace, apart from :ref:`private` names,
250+
When you import a module this brings all the names in its namespace, apart from :ref:`private` names,
251251
into the :ref:`namespace <namespaces>` of the current module.
252252

253253
.. _import-statements:
@@ -263,7 +263,7 @@ Import statements are used for importing modules. They are of the form:
263263
import <module_expression2>
264264
265265
Import statements are usually listed at the beginning of the module. Each
266-
import statement imports one module. You can import multiple modules by
266+
import statement imports one module. You can import multiple modules by
267267
including multiple import statements (one for each module you want to import).
268268

269269
An import statement can also be :ref:`annotated <annotations-overview>` with
@@ -272,14 +272,14 @@ An import statement can also be :ref:`annotated <annotations-overview>` with
272272
only reachable through deprecated imports in a given context then usage of the
273273
name in that context will generate deprecation warnings.
274274

275-
You can import a module under a different name using the ``as`` keyword,
275+
You can import a module under a different name using the ``as`` keyword,
276276
for example ``import javascript as js``.
277277

278278
The ``<module_expression>`` itself can be a module name, a selection, or a qualified
279279
reference. For more information, see ":ref:`name-resolution`."
280280

281281
For information about how import statements are looked up, see "`Module resolution <https://codeql.github.com/docs/ql-language-reference/ql-language-specification/#module-resolution>`__"
282-
in the QL language specification.
282+
in the QL language specification.
283283

284284
Built-in modules
285285
****************
@@ -353,7 +353,7 @@ Sets
353353

354354
The built-in ``InternSets`` module is parameterized by ``Key`` and ``Value`` types
355355
and a ``Value getAValue(Key key)`` relation. The module groups the ``Value``
356-
column by ``Key`` and creates a set for each group of values related by a key.
356+
column by ``Key`` and creates a set for each group of values related by a key.
357357

358358
The ``InternSets`` module exports a functional ``Set getSet(Key key)`` relation
359359
that relates keys with the set of value related to the given key by
@@ -424,3 +424,30 @@ The above query therefore evalutes to:
424424
+----+----+
425425
| 4 | 4 |
426426
+----+----+
427+
428+
.. index:: BigInt
429+
.. _bigint:
430+
431+
BigInt
432+
======
433+
434+
The built-in ``QlBuiltins`` module provides an **experimental** type ``BigInt`` of arbitrary-precision integers.
435+
436+
This type is not available in the CodeQL CLI by default, but you can enable it by passing the ``--allow-experimental=bigint``
437+
option to the CodeQL CLI. Consequently, BigInts are currently disallowed in query results and dbscheme columns.
438+
439+
Unlike ``int`` and ``float``, there is no automatic conversion between ``BigInt`` and other numeric types.
440+
Instead, big integers can be constructed using the ``.toBigInt()`` methods of ``int`` and ``string``.
441+
442+
The other built-in operations are:
443+
444+
* comparisons between ``BigInt``\s: ``=``, ``!=``, ``<``, ``<=``, ``>``, ``>=``,
445+
* conversions from ``BigInt``\s to strings or integers (if within range): ``.toString()``, ``.toInt()``,
446+
* ``BigInt`` arithmetic: binary ``+``, ``-``, ``*``, ``/``, ``%``, unary ``-``,
447+
* bitwise operations: ``.bitAnd(BigInt)``, ``.bitOr(BigInt)``,
448+
``.bitXor(BigInt)``, ``.bitShiftLeft(int)``, ``.bitShiftRightSigned(int)``,
449+
``.bitNot()``,
450+
* aggregates: ``min``, ``max``, (``strict``)\ ``sum``, (``strict``)\ ``count``, ``avg``,
451+
``rank``, ``unique``, ``any``.
452+
* other: ``.pow(int)``, ``.abs()``, ``.gcd(BigInt)``, ``.minimum(BigInt)``,
453+
``.maximum(BigInt)``.

docs/codeql/ql-language-reference/ql-language-specification.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ Kinds of types
359359

360360
Types in QL are either *primitive* types, *database* types, *class* types, *character* types, *class domain* types, type *parameters*, or *instantiation-nested* types.
361361

362-
The primitive types are ``boolean``, ``date``, ``float``, ``int``, and ``string``.
362+
The primitive types are ``boolean``, ``date``, ``float``, ``int``, ``string``, and ``QlBuiltins::BigInt``.
363363

364364
Database types are supplied as part of the database. Each database type has a *name*, which is an identifier starting with an at sign (``@``, U+0040) followed by lower-case letter. Database types have some number of *base types*, which are other database types. In a valid database, the base types relation is non-cyclic.
365365

@@ -433,7 +433,7 @@ Values are the fundamental data that QL programs compute over. This section spec
433433
Kinds of values
434434
~~~~~~~~~~~~~~~
435435

436-
There are six kinds of values in QL: one kind for each of the five primitive types, and *entities*. Each value has a type.
436+
There are seven kinds of values in QL: one kind for each of the six primitive types, and *entities*. Each value has a type.
437437

438438
A boolean value is of type ``boolean``, and may have one of two distinct values: ``true`` or ``false``.
439439

@@ -445,6 +445,8 @@ An integer value is of type ``int``. Each value is a 32-bit two's complement int
445445

446446
A string is a finite sequence of 16-bit characters. The characters are interpreted as Unicode code points.
447447

448+
A :ref:`big integer <bigint>` value is of type ``QlBuiltins::BigInt``. Each value is a signed arbitrary-precision integer.
449+
448450
The database includes a number of opaque entity values. Each such value has a type that is one of the database types, and an identifying integer. An entity value is written as the name of its database type followed by its identifying integer in parentheses. For example, ``@tree(12)``, ``@person(16)``, and ``@location(38132)`` are entity values. The identifying integers are left opaque to programmers in this specification, so an implementation of QL is free to use some other set of countable labels to identify its entities.
449451

450452
Ordering
@@ -458,7 +460,7 @@ For dates, the ordering is chronological.
458460

459461
For floats, the ordering is as specified in IEEE 754 when one exists, except that NaN is considered equal to itself and is ordered after all other floats, and negative zero is considered to be strictly less than positive zero.
460462

461-
For integers, the ordering is as for two's complement integers.
463+
For integers (and :ref:`big integers <bigint>`), the ordering is numerical.
462464

463465
For strings, the ordering is lexicographic.
464466

0 commit comments

Comments
 (0)