DEV Community

Talha Munir πŸ‡΅πŸ‡Έ
Talha Munir πŸ‡΅πŸ‡Έ

Posted on • Edited on

Data types

Data types in Apache age

Simple Data Types

Null

In cypher languages, null is used to represent the missing or undefined values. Conceptually, null means a missing unknown values that is treated somewhat different from other values. In most cases the expressions that take null as input produces null as output.

Agtype NULL and Postgres NULL:

The concept of NULL in Agtype and Postgres is the same as it is in Cypher.

Integer:

The integer type stores whole numbers, i.e. numbers without fractional components. Integer datatype is a 64 bit field that stores values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It will throw error if you try to store the values outside this range. Int is the most commonly used data type as it offers the best balance between range, storage, size and performance.

Query:

The below query returns 1

SELECT *
FROM cypher('graph_name', $$
    RETURN 1
$$) AS (int_result agtype);
Enter fullscreen mode Exit fullscreen mode

Float:

The float data type is an inexact, variable-percision numeric type. Inexact means that some values cannot be converted exactly to the internal format and are stored as approximations. Comparing the two floating point values for equality might not always works as expected. Values that are too large or too small will cause an error. Rounding might take place if the precision of an input number is too high. Numbers too close to zero that are not representable as distinct from zero will cause an underflow error.
Floating point type have some special values:

  • Infinity
  • -Infinity
  • NaN When writing these values as constants in a cypher command, you must put quotes around them and typecast them, for example:

SET x.float_value = '-Infinity'::float

Query:

The below query returns 1.0

SELECT *
FROM cypher('graph_name', $$
    RETURN 1.0
$$) AS (float_result agtype);
Enter fullscreen mode Exit fullscreen mode

Numeric:

The numeric type can store numbers with a very large number of digits. It is mostly recommended to store monetary amounts and other quantities where we require exactness. During the calculations with numeric values, the datatype yields exact results where possible e.g. Addition, subtraction, multiplication. However the calculations are very slow as compared to other datatypes like integer, or floating point values.
Precision:
The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point.
Scale:
The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point.

Let's take an example we have a digit: 2345.7890 has a precision of 8 and scale of 4. Integers have a scale of zero.
Numeric values are physically stored without any extra leading or trailing zeros. Thus declared precision and scale of a column are maximums not fixed allocations.

Query:

When creating a numeric data type, the β€˜::numeric’ data annotation is required.
The query will return 1.0::numeric

SELECT *
FROM cypher('graph_name', $$
    RETURN 1.0::numeric
$$) AS (numeric_result agtype);
Enter fullscreen mode Exit fullscreen mode

Bool:

Age provides the standard cypher type boolean. The boolean data type can have 3 states, either true, false or unknown, which is represented by Agtype null value.
Boolean constants can be represented in Cypher queries by the keywords True, False and Null

Query:

SELECT *
FROM cypher('graph_name', $$
    RETURN TRUE
$$) AS (boolean_result agtype);
Enter fullscreen mode Exit fullscreen mode

String:

String literals can contain some of the escape sequences.
Use single quotes to identify a string. The output will use double quotes.

Query:

The below query will return the string.

SELECT *
FROM cypher('graph_name', $$
    RETURN 'This is a string'
$$) AS (string_result agtype);
Enter fullscreen mode Exit fullscreen mode

\t: used for tab
\b: used to backspace
\n: used for newline
\r: used for carriage return
\f: used for form feed
\': used for single quote
\": used for Double quote
\: used for Backlash

You can follow for more content on age website and github:

  1. Apache age
  2. Apache age Github

Top comments (0)