diffdb-core 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. diffdb/__init__.py +179 -0
  2. diffdb/concurrency/__init__.py +10 -0
  3. diffdb/concurrency/pool.py +88 -0
  4. diffdb/concurrency/stream.py +111 -0
  5. diffdb/core/__init__.py +38 -0
  6. diffdb/core/differ.py +136 -0
  7. diffdb/core/hash_differ.py +390 -0
  8. diffdb/core/segment.py +323 -0
  9. diffdb/core/sql.py +81 -0
  10. diffdb/core/summary.py +179 -0
  11. diffdb/databases/__init__.py +59 -0
  12. diffdb/databases/_connect.py +210 -0
  13. diffdb/databases/base.py +266 -0
  14. diffdb/databases/bigquery.py +54 -0
  15. diffdb/databases/clickhouse.py +35 -0
  16. diffdb/databases/databricks.py +40 -0
  17. diffdb/databases/dialects/_base.yaml +182 -0
  18. diffdb/databases/dialects/bigquery.yaml +39 -0
  19. diffdb/databases/dialects/clickhouse.yaml +42 -0
  20. diffdb/databases/dialects/databricks.yaml +36 -0
  21. diffdb/databases/dialects/duckdb.yaml +48 -0
  22. diffdb/databases/dialects/mysql.yaml +45 -0
  23. diffdb/databases/dialects/oracle.yaml +44 -0
  24. diffdb/databases/dialects/postgresql.yaml +44 -0
  25. diffdb/databases/dialects/presto.yaml +37 -0
  26. diffdb/databases/dialects/redshift.yaml +11 -0
  27. diffdb/databases/dialects/snowflake.yaml +55 -0
  28. diffdb/databases/dialects/trino.yaml +5 -0
  29. diffdb/databases/dialects/vertica.yaml +39 -0
  30. diffdb/databases/duckdb.py +40 -0
  31. diffdb/databases/mysql.py +38 -0
  32. diffdb/databases/oracle.py +99 -0
  33. diffdb/databases/postgresql.py +34 -0
  34. diffdb/databases/presto.py +41 -0
  35. diffdb/databases/redshift.py +12 -0
  36. diffdb/databases/snowflake.py +55 -0
  37. diffdb/databases/trino.py +41 -0
  38. diffdb/databases/vertica.py +43 -0
  39. diffdb/databases/yaml_dialect.py +428 -0
  40. diffdb/geometry/__init__.py +42 -0
  41. diffdb/geometry/arithmetic.py +141 -0
  42. diffdb/geometry/bisection.py +100 -0
  43. diffdb/geometry/vector.py +42 -0
  44. diffdb/telemetry/__init__.py +11 -0
  45. diffdb/telemetry/info_tree.py +52 -0
  46. diffdb/telemetry/query_tracker.py +53 -0
  47. diffdb/types.py +302 -0
  48. diffdb/utils.py +121 -0
  49. diffdb_core-0.1.0.dist-info/METADATA +315 -0
  50. diffdb_core-0.1.0.dist-info/RECORD +53 -0
  51. diffdb_core-0.1.0.dist-info/WHEEL +5 -0
  52. diffdb_core-0.1.0.dist-info/licenses/LICENSE.md +87 -0
  53. diffdb_core-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,182 @@
1
+ # diffdb/databases/dialects/_base.yaml
2
+ # =====================================================================================================
3
+ # Abstract ANSI SQL base dialect -- and the reference for every key a dialect file can contain.
4
+ #
5
+ # HOW DIALECT FILES WORK
6
+ # * One file per database engine (oracle.yaml, postgresql.yaml, ...). The Python driver class
7
+ # loads it by name: `dialect = YamlDialect.load("oracle")` (diffdb/databases/<db>.py).
8
+ # * `extends:` names a parent file. The child is deep-merged over the parent: mappings
9
+ # (syntax, capabilities, type_classes, normalization, ...) merge key by key, while scalars
10
+ # and lists are replaced. So a dialect only lists what differs from its parent.
11
+ # * Every file is validated when it loads (validate_dialect_config in yaml_dialect.py). Unknown
12
+ # keys, unknown type classes, unknown template placeholders and missing required templates
13
+ # raise DialectConfigError, so a typo fails fast instead of producing wrong SQL.
14
+ #
15
+ # TEMPLATES
16
+ # * Values with {placeholders} are Python str.format() templates; each key accepts only the
17
+ # placeholders documented next to it. Write a literal brace as {{ or }}.
18
+ # * SQL is full of single quotes, so put templates in double-quoted YAML strings:
19
+ # to_string: "cast({value} as varchar)"
20
+ # For templates that contain both quote kinds or backslashes, use a folded block, which keeps
21
+ # the text literally:
22
+ # some_key: >-
23
+ # regexp_replace({value}, '^(-?)\.', '\10.')
24
+ #
25
+ # THE GOLDEN RULE
26
+ # Rows are compared by hashing one string per row on each database. Source and target must
27
+ # therefore render equal values as byte-identical strings (numbers, timestamps, NULL, booleans).
28
+ # tests/golden/dialect_sql.json snapshots the SQL every dialect generates; review its diff
29
+ # whenever you change a template.
30
+ # =====================================================================================================
31
+
32
+ # Display name, used in log and error messages.
33
+ name: "_base"
34
+
35
+ # true = template only, never used directly, so the required `normalization` templates are not
36
+ # enforced. Not inherited: every file that `extends:` this one is concrete unless it says otherwise.
37
+ abstract: true
38
+
39
+ # (Not set here.) extends: "<parent>.yaml" -- inherit from another dialect, e.g. redshift -> postgresql.
40
+ #
41
+ # (Not set here.) hash_charset: "<charset>" -- character set the concatenated row string is converted
42
+ # to before hashing, applied through `normalization.hash_charset_wrap` (required when this is set).
43
+ # Oracle sets it per connection instead (oracle.py detects NLS_CHARACTERSET), so it is rarely needed in YAML.
44
+
45
+
46
+ # -----------------------------------------------------------------------------------------------------
47
+ # syntax: SQL fragments diffdb/core/sql.py uses to build the four diff queries
48
+ # -----------------------------------------------------------------------------------------------------
49
+ syntax:
50
+ # Character wrapped around identifiers (table and column names): "col" here, `col` on MySQL.
51
+ quote_char: '"'
52
+
53
+ # How the per-column strings of a row are joined into ONE string before hashing, with '|'
54
+ # between columns. An operator such as " || " gives a || '|' || b; the special value "concat"
55
+ # gives concat(a, '|', b) for engines without a concatenation operator (MySQL).
56
+ concat: " || "
57
+
58
+ # Cast any expression to text. Used for types that have no normalization of their own
59
+ # (Integer, unknown types) and for non-normalized expressions inside the row string.
60
+ # Placeholders: {value}
61
+ to_string: "cast({value} as varchar)"
62
+
63
+ # NULL replacement. Each column in the row string is wrapped in this, so NULL hashes as the
64
+ # marker '<null>' (passed in {default}) and stays different from an empty string.
65
+ # Placeholders: {value}, {default}
66
+ coalesce: "coalesce({value}, {default})"
67
+
68
+ # Statement run right after connecting, so both sides read and render timestamps in UTC.
69
+ # null = the engine can't do this: the step is skipped (debug log only) and the session time
70
+ # zone is left as it is, so make sure it is UTC on the server.
71
+ set_timezone_to_utc: null
72
+
73
+ # How a Python datetime is written as a SQL literal.
74
+ # Placeholders: {iso} = 2024-01-02T03:04:05.123456, {iso_space} = 2024-01-02 03:04:05.123456
75
+ timestamp_literal: "'{iso}'"
76
+
77
+ # (Optional, default 0.) Digits added to the decimal precision derived from a float column's
78
+ # binary precision (floor(log10(2 ** precision))), for engines that print more or fewer digits.
79
+ # float_precision_offset: 0
80
+
81
+
82
+ # -----------------------------------------------------------------------------------------------------
83
+ # capabilities: engine behaviour the core needs to know about (the core only reads; it never writes tables).
84
+ # -----------------------------------------------------------------------------------------------------
85
+ capabilities:
86
+ # true = the engine ROUNDS when a value loses precision (e.g. a timestamp cast to fewer
87
+ # fractional digits); false = it TRUNCATES. Stored on timestamp columns: rounding columns are
88
+ # normalized with `normalization.timestamp_rounded`, so both sides cut digits the same way.
89
+ rounds_on_prec_loss: true
90
+
91
+
92
+ # -----------------------------------------------------------------------------------------------------
93
+ # type_classes: native type name -> diffdb type class
94
+ #
95
+ # The caller declares each column's native type in TableSegment `columns` ("datatype"), e.g.
96
+ # NUMBER(12,2), VARCHAR2(100), TIMESTAMP(3) WITH TIME ZONE, character varying(100). The core never
97
+ # reads the catalog. YamlDialect.parse_type() splits the name as NAME(args) SUFFIX, looks up
98
+ # "NAME SUFFIX" here (case- and whitespace-insensitive), and the class reads the args, e.g.
99
+ # NUMBER(12,2) -> 2 fractional digits, TIMESTAMP(3) -> 3. The class decides how a column is
100
+ # compared and hashed (see the type registry in diffdb/types.py):
101
+ # Integer whole numbers, hashed as plain text
102
+ # Decimal fixed-point: (p,s) -> s fractional digits compared, (p) -> 0
103
+ # (no declared scale: compared as a whole number, with a warning)
104
+ # Float floating point, rounded to its decimal precision
105
+ # Text strings, hashed as-is (also usable as bisection keys)
106
+ # Timestamp / TimestampTZ / Datetime / Date
107
+ # timestamps, formatted 'YYYY-MM-DD HH24:MI:SS.FFFFFF'
108
+ # Boolean hashed as integer 1 / 0
109
+ # Native_UUID / String_UUID / ColType_UUID
110
+ # UUIDs (native type / stored as text)
111
+ # A type not listed here (and not matched by type_regexps) is rejected when the TableSegment is
112
+ # created. Core diffdb handles the basic types (number, string, timestamp, boolean); reduce other
113
+ # types (LOBs, binary, ...) in the caller with a transform_rule and declare the result's datatype.
114
+ # A dialect's own type_classes are merged with these; a name it repeats in any case or spacing
115
+ # (DATE vs date) replaces the inherited entry.
116
+ # -----------------------------------------------------------------------------------------------------
117
+ type_classes:
118
+ int: "Integer"
119
+ integer: "Integer"
120
+ smallint: "Integer"
121
+ bigint: "Integer"
122
+ decimal: "Decimal"
123
+ numeric: "Decimal"
124
+ float: "Float(53)"
125
+ real: "Float(24)"
126
+ double: "Float(53)"
127
+ char: "Text"
128
+ varchar: "Text"
129
+ text: "Text"
130
+ date: "Date"
131
+ timestamp: "Timestamp"
132
+ timestamptz: "TimestampTZ"
133
+ boolean: "Boolean"
134
+ bool: "Boolean"
135
+
136
+
137
+ # -----------------------------------------------------------------------------------------------------
138
+ # type_regexps: regex on the full type name -> diffdb type class
139
+ #
140
+ # Only needed for names the NAME(args) SUFFIX split above can't handle. Checked BEFORE type_classes,
141
+ # in file order; the first regex that matches wins (re.search, case-insensitive). Options per entry:
142
+ # type: the type class name (same names as type_classes)
143
+ # has_precision: true = capture group 1 is the precision
144
+ # has_scale: true = capture group 2 is the scale (fractional digits) of a number type
145
+ # Example:
146
+ # 'INTERVAL DAY\((\d)\) TO SECOND':
147
+ # type: "..."
148
+ # has_precision: true
149
+ # -----------------------------------------------------------------------------------------------------
150
+ type_regexps: {}
151
+
152
+
153
+ # -----------------------------------------------------------------------------------------------------
154
+ # normalization: SQL templates that turn a column into its canonical string before hashing
155
+ #
156
+ # Every concrete dialect must define md5_as_int, number, boolean, uuid and at least one timestamp
157
+ # template. They are deliberately NOT defined here, so a new dialect can't silently inherit another
158
+ # engine's SQL.
159
+ #
160
+ # md5_as_int Hash the row string to an integer: MD5, keep the LAST 15 hex digits
161
+ # (60 bits), convert to a number. Both sides must produce the same integer.
162
+ # Placeholders: {s} (the row string), {CHECKSUM_MASK} (2**60 - 1)
163
+ # number Decimal/Float -> text with exactly {precision} fractional digits.
164
+ # Placeholders: {value}, {precision},
165
+ # {format_str} (Oracle-style 'FM999...0.90' mask for that precision)
166
+ # boolean Boolean -> '1' / '0'. Placeholders: {value}
167
+ # uuid UUID -> trimmed text. Placeholders: {value}
168
+ # timestamp_* Timestamp -> 'YYYY-MM-DD HH24:MI:SS.FFFFFF' (26 characters) at {precision}
169
+ # fractional digits. The first template that applies is used:
170
+ # timestamp_rounded the column rounds (capabilities.rounds_on_prec_loss)
171
+ # timestamp_zero_precision precision = 0
172
+ # timestamp_six_precision precision = 6
173
+ # timestamp_precision any precision
174
+ # timestamp_unrounded fallback
175
+ # Placeholders: {value}, {precision}
176
+ # hash_charset_wrap Only when hash_charset is set: convert the row string before hashing.
177
+ # Placeholders: {s}, {charset}
178
+ #
179
+ # A NULL never reaches these templates: the row concat wraps each result in syntax.coalesce.
180
+ # -----------------------------------------------------------------------------------------------------
181
+ normalization:
182
+ uuid: "CAST(TRIM({value}) AS VARCHAR(36))"
@@ -0,0 +1,39 @@
1
+ # diffdb/databases/dialects/bigquery.yaml
2
+ # Google BigQuery Dialect Configuration
3
+
4
+ name: "BigQuery"
5
+ extends: "_base.yaml"
6
+
7
+ syntax:
8
+ quote_char: "`"
9
+ concat: "concat"
10
+ to_string: "cast({value} as string)"
11
+ set_timezone_to_utc: null
12
+
13
+ capabilities:
14
+ rounds_on_prec_loss: false
15
+
16
+ type_classes:
17
+ TIMESTAMP: "Timestamp"
18
+ DATETIME: "Datetime"
19
+ INT64: "Integer"
20
+ INT32: "Integer"
21
+ NUMERIC: "Decimal"
22
+ BIGNUMERIC: "Decimal"
23
+ FLOAT64: "Float(53)"
24
+ FLOAT32: "Float(24)"
25
+ STRING: "Text"
26
+ BOOL: "Boolean"
27
+ DATE: "Date"
28
+
29
+ type_regexps: {}
30
+
31
+ normalization:
32
+ md5_as_int: "cast(conv(substr(to_hex(md5({s})), 18), 16, 10) as int64)"
33
+ timestamp_rounded: "FORMAT_TIMESTAMP('%F %H:%M:%E6S', timestamp_micros(cast(round(unix_micros(cast({value} as timestamp))/1000000, {precision})*1000000 as int)))"
34
+ timestamp_zero_precision: "FORMAT_TIMESTAMP('%F %H:%M:%S.000000', {value})"
35
+ timestamp_six_precision: "FORMAT_TIMESTAMP('%F %H:%M:%E6S', {value})"
36
+ timestamp_precision: "RPAD(LEFT(FORMAT_TIMESTAMP('%F %H:%M:%E6S', {value}), 20+{precision}), 26, '0')"
37
+ number: "format('%.{precision}f', {value})"
38
+ boolean: "cast(cast({value} as int) as string)"
39
+ uuid: "CAST(TRIM({value}) AS STRING)"
@@ -0,0 +1,42 @@
1
+ # diffdb/databases/dialects/clickhouse.yaml
2
+ # ClickHouse Dialect Configuration
3
+
4
+ name: "Clickhouse"
5
+ extends: "_base.yaml"
6
+
7
+ syntax:
8
+ quote_char: '"'
9
+ concat: "concat"
10
+ to_string: "toString({value})"
11
+
12
+ capabilities:
13
+ rounds_on_prec_loss: true
14
+
15
+ type_classes:
16
+ DateTime: "Timestamp"
17
+ DateTime64: "Timestamp"
18
+ Date: "Date"
19
+ Int8: "Integer"
20
+ Int16: "Integer"
21
+ Int32: "Integer"
22
+ Int64: "Integer"
23
+ UInt8: "Integer"
24
+ UInt16: "Integer"
25
+ UInt32: "Integer"
26
+ UInt64: "Integer"
27
+ Float32: "Float(24)"
28
+ Float64: "Float(53)"
29
+ Decimal: "Decimal"
30
+ String: "Text"
31
+ FixedString: "Text"
32
+ UUID: "Native_UUID"
33
+
34
+ type_regexps: {}
35
+
36
+ normalization:
37
+ md5_as_int: "reinterpretAsUInt64(reverse(unhex(substring(lower(hex(MD5({s}))), 17))))"
38
+ timestamp_rounded: "formatDateTime({value}, '%Y-%m-%d %H:%M:%S')"
39
+ timestamp_precision: "formatDateTime({value}, '%Y-%m-%d %H:%M:%S')"
40
+ number: "toString({value})"
41
+ boolean: "cast({value} as UInt8)"
42
+ uuid: "toString({value})"
@@ -0,0 +1,36 @@
1
+ # diffdb/databases/dialects/databricks.yaml
2
+ # Databricks / Spark SQL Dialect Configuration
3
+
4
+ name: "Databricks"
5
+ extends: "_base.yaml"
6
+
7
+ syntax:
8
+ quote_char: "`"
9
+ concat: "concat"
10
+ to_string: "cast({value} as string)"
11
+
12
+ capabilities:
13
+ rounds_on_prec_loss: true
14
+
15
+ type_classes:
16
+ TIMESTAMP: "Timestamp"
17
+ DATE: "Date"
18
+ BIGINT: "Integer"
19
+ INT: "Integer"
20
+ SMALLINT: "Integer"
21
+ TINYINT: "Integer"
22
+ DECIMAL: "Decimal"
23
+ FLOAT: "Float(24)"
24
+ DOUBLE: "Float(53)"
25
+ STRING: "Text"
26
+ BOOLEAN: "Boolean"
27
+
28
+ type_regexps: {}
29
+
30
+ normalization:
31
+ md5_as_int: "conv(substr(md5({s}), 18), 16, 10)"
32
+ timestamp_rounded: "date_format({value}, 'yyyy-MM-dd HH:mm:ss.SSSSSS')"
33
+ timestamp_precision: "date_format({value}, 'yyyy-MM-dd HH:mm:ss.SSSSSS')"
34
+ number: "format_number({value}, {precision})"
35
+ boolean: "cast({value} as int)"
36
+ uuid: "CAST(TRIM({value}) AS STRING)"
@@ -0,0 +1,48 @@
1
+ # diffdb/databases/dialects/duckdb.yaml
2
+ # DuckDB Dialect Configuration
3
+
4
+ name: "DuckDB"
5
+ extends: "_base.yaml"
6
+
7
+ syntax:
8
+ quote_char: '"'
9
+ concat: " || "
10
+ to_string: "cast({value} as varchar)"
11
+ set_timezone_to_utc: "SET GLOBAL TimeZone='UTC'"
12
+
13
+ capabilities:
14
+ rounds_on_prec_loss: false
15
+
16
+ type_classes:
17
+ TIMESTAMP: "Timestamp"
18
+ "TIMESTAMP WITH TIME ZONE": "TimestampTZ"
19
+ TIMESTAMPTZ: "TimestampTZ"
20
+ DATE: "Date"
21
+ TIME: "Timestamp"
22
+ HUGEINT: "Integer"
23
+ BIGINT: "Integer"
24
+ INTEGER: "Integer"
25
+ SMALLINT: "Integer"
26
+ TINYINT: "Integer"
27
+ UBIGINT: "Integer"
28
+ UINTEGER: "Integer"
29
+ USMALLINT: "Integer"
30
+ UTINYINT: "Integer"
31
+ DECIMAL: "Decimal"
32
+ DOUBLE: "Float(53)"
33
+ FLOAT: "Float(24)"
34
+ VARCHAR: "Text"
35
+ TEXT: "Text"
36
+ BOOLEAN: "Boolean"
37
+ UUID: "Native_UUID"
38
+
39
+ type_regexps: {}
40
+
41
+ normalization:
42
+ # Last 15 hex digits of the MD5 as an integer (same value as postgresql.yaml); DuckDB casts '0x..' strings
43
+ md5_as_int: "('0x' || substring(md5({s}), 18))::BIGINT"
44
+ timestamp_rounded: "strftime({value}::timestamp, '%Y-%m-%d %H:%M:%S.%f')"
45
+ timestamp_precision: "rpad(strftime({value}::timestamp, '%Y-%m-%d %H:%M:%S.%f'), 26, '0')"
46
+ number: "{value}::decimal(38, {precision})::varchar"
47
+ boolean: "{value}::int::varchar"
48
+ uuid: "CAST(TRIM({value}) AS VARCHAR(36))"
@@ -0,0 +1,45 @@
1
+ # diffdb/databases/dialects/mysql.yaml
2
+ # MySQL Dialect Configuration
3
+
4
+ name: "MySQL"
5
+ extends: "_base.yaml"
6
+
7
+ syntax:
8
+ quote_char: "`"
9
+ concat: "concat"
10
+ to_string: "cast({value} as char)"
11
+ set_timezone_to_utc: "SET @@session.time_zone='+00:00'"
12
+
13
+ capabilities:
14
+ rounds_on_prec_loss: true
15
+
16
+ type_classes:
17
+ timestamp: "Timestamp"
18
+ datetime: "Datetime"
19
+ date: "Date"
20
+ int: "Integer"
21
+ smallint: "Integer"
22
+ tinyint: "Integer"
23
+ mediumint: "Integer"
24
+ bigint: "Integer"
25
+ decimal: "Decimal"
26
+ numeric: "Decimal"
27
+ float: "Float(24)"
28
+ double: "Float(53)"
29
+ varchar: "Text"
30
+ char: "Text"
31
+ text: "Text"
32
+ tinytext: "Text"
33
+ mediumtext: "Text"
34
+ longtext: "Text"
35
+ boolean: "Boolean"
36
+
37
+ type_regexps: {}
38
+
39
+ normalization:
40
+ md5_as_int: "conv(substring(md5({s}), 18), 16, 10)"
41
+ timestamp_rounded: "date_format({value}, '%Y-%m-%d %H:%i:%S.%f')"
42
+ timestamp_precision: "rpad(date_format({value}, '%Y-%m-%d %H:%i:%S.%f'), 26, '0')"
43
+ number: "format({value}, {precision})"
44
+ boolean: "cast({value} as unsigned)"
45
+ uuid: "CAST(TRIM({value}) AS CHAR(36))"
@@ -0,0 +1,44 @@
1
+ # diffdb/databases/dialects/oracle.yaml
2
+ # Oracle Database Dialect Configuration
3
+
4
+ name: "Oracle"
5
+ extends: "_base.yaml"
6
+
7
+ syntax:
8
+ quote_char: '"'
9
+ concat: " || "
10
+ to_string: "to_char({value})"
11
+ coalesce: "NVL({value}, {default})"
12
+ set_timezone_to_utc: "ALTER SESSION SET TIME_ZONE = 'UTC'"
13
+ timestamp_literal: "timestamp '{iso_space}'"
14
+
15
+ capabilities:
16
+ rounds_on_prec_loss: true
17
+
18
+ type_classes:
19
+ NUMBER: "Decimal"
20
+ FLOAT: "Float(126)"
21
+ CHAR: "Text"
22
+ NCHAR: "Text"
23
+ NVARCHAR2: "Text"
24
+ VARCHAR2: "Text"
25
+ DATE: "Timestamp"
26
+ # TIMESTAMP(p) [WITH [LOCAL] TIME ZONE]: the parser splits off (p) as the fractional-second precision
27
+ TIMESTAMP: "Timestamp"
28
+ TIMESTAMP WITH TIME ZONE: "TimestampTZ"
29
+ TIMESTAMP WITH LOCAL TIME ZONE: "Timestamp"
30
+ # CLOB / NCLOB / BLOB / RAW / LONG RAW are deliberately not mapped: reduce them to a basic type
31
+ # with a transform_rule (e.g. LENGTH / DBMS_LOB.GETLENGTH declared as NUMBER(10,0)).
32
+
33
+ type_regexps: {}
34
+
35
+ normalization:
36
+ md5_as_int: "to_number(substr(standard_hash({s}, 'MD5'), 18), 'xxxxxxxxxxxxxxx')"
37
+ timestamp_rounded: "to_char(cast({value} as timestamp({precision})), 'YYYY-MM-DD HH24:MI:SS.FF6')"
38
+ timestamp_precision: "RPAD(to_char({value}, 'YYYY-MM-DD HH24:MI:SS.FF{precision}'), 26, '0')"
39
+ timestamp_zero_precision: "RPAD(to_char({value}, 'YYYY-MM-DD HH24:MI:SS.'), 26, '0')"
40
+ number: "to_char({value}, '{format_str}')"
41
+ boolean: "to_char(cast({value} as int))"
42
+ # Applied to the concatenated row string when the instance sets hash_charset (see oracle.py)
43
+ hash_charset_wrap: "CONVERT({s}, '{charset}')"
44
+ uuid: "CAST(TRIM({value}) AS VARCHAR(36))"
@@ -0,0 +1,44 @@
1
+ # diffdb/databases/dialects/postgresql.yaml
2
+ # PostgreSQL Database Dialect Configuration
3
+
4
+ name: "PostgreSQL"
5
+ extends: "_base.yaml"
6
+
7
+ syntax:
8
+ quote_char: '"'
9
+ concat: " || "
10
+ to_string: "{value}::varchar"
11
+ set_timezone_to_utc: "SET TIME ZONE 'UTC'"
12
+
13
+ capabilities:
14
+ rounds_on_prec_loss: true
15
+
16
+ type_classes:
17
+ "timestamp with time zone": "TimestampTZ"
18
+ "timestamp without time zone": "Timestamp"
19
+ "timestamp": "Timestamp"
20
+ "date": "Date"
21
+ "double precision": "Float(53)"
22
+ "real": "Float(24)"
23
+ "decimal": "Decimal"
24
+ "smallint": "Integer"
25
+ "integer": "Integer"
26
+ "numeric": "Decimal"
27
+ "bigint": "Integer"
28
+ "character": "Text"
29
+ "character varying": "Text"
30
+ "varchar": "Text"
31
+ "text": "Text"
32
+ "uuid": "Native_UUID"
33
+ "boolean": "Boolean"
34
+
35
+ type_regexps: {}
36
+
37
+ normalization:
38
+ md5_as_int: "('x' || substring(md5({s}), 18))::bit(60)::bigint"
39
+ timestamp_rounded: "to_char({value}::timestamp({precision}), 'YYYY-mm-dd HH24:MI:SS.US')"
40
+ timestamp_precision: "RPAD(LEFT(to_char({value}::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US'), 20+{precision}), 26, '0')"
41
+ number: "{value}::decimal(38, {precision})::varchar"
42
+ # Booleans hash as 1 / 0; NULL stays NULL and becomes '<null>' in the row concat like every other type
43
+ boolean: "{value}::int::varchar"
44
+ uuid: "CAST(TRIM({value}) AS VARCHAR(36))"
@@ -0,0 +1,37 @@
1
+ # diffdb/databases/dialects/presto.yaml
2
+ # Presto Dialect Configuration
3
+
4
+ name: "Presto"
5
+ extends: "_base.yaml"
6
+
7
+ syntax:
8
+ quote_char: '"'
9
+ concat: " || "
10
+ to_string: "cast({value} as varchar)"
11
+
12
+ capabilities:
13
+ rounds_on_prec_loss: true
14
+
15
+ type_classes:
16
+ timestamp: "Timestamp"
17
+ date: "Date"
18
+ bigint: "Integer"
19
+ integer: "Integer"
20
+ smallint: "Integer"
21
+ tinyint: "Integer"
22
+ decimal: "Decimal"
23
+ real: "Float(24)"
24
+ double: "Float(53)"
25
+ varchar: "Text"
26
+ char: "Text"
27
+ boolean: "Boolean"
28
+
29
+ type_regexps: {}
30
+
31
+ normalization:
32
+ md5_as_int: "from_base(substr(to_hex(md5(to_utf8({s}))), 18), 16)"
33
+ timestamp_rounded: "date_format({value}, '%Y-%m-%d %H:%i:%S.%f')"
34
+ timestamp_precision: "date_format({value}, '%Y-%m-%d %H:%i:%S.%f')"
35
+ number: "cast({value} as varchar)"
36
+ boolean: "cast({value} as integer)"
37
+ uuid: "CAST(TRIM({value}) AS VARCHAR)"
@@ -0,0 +1,11 @@
1
+ # diffdb/databases/dialects/redshift.yaml
2
+ # Amazon Redshift Dialect Configuration
3
+
4
+ name: "Redshift"
5
+ extends: "postgresql.yaml"
6
+
7
+ syntax:
8
+ to_string: "cast({value} as varchar(max))"
9
+
10
+ normalization:
11
+ md5_as_int: "strtol(substring(md5({s}), 17), 16)"
@@ -0,0 +1,55 @@
1
+ # diffdb/databases/dialects/snowflake.yaml
2
+ # Snowflake Database Dialect Configuration
3
+
4
+ name: "Snowflake"
5
+ extends: "_base.yaml"
6
+
7
+ syntax:
8
+ quote_char: '"'
9
+ concat: " || "
10
+ to_string: "cast({value} as varchar)"
11
+ set_timezone_to_utc: "ALTER SESSION SET TIMEZONE = 'UTC'"
12
+
13
+ capabilities:
14
+ rounds_on_prec_loss: true
15
+
16
+ type_classes:
17
+ TIMESTAMP_NTZ: "Timestamp"
18
+ TIMESTAMP_TZ: "TimestampTZ"
19
+ TIMESTAMP_LTZ: "TimestampTZ"
20
+ TIMESTAMP: "Timestamp"
21
+ DATE: "Date"
22
+ NUMBER: "Decimal"
23
+ DECIMAL: "Decimal"
24
+ NUMERIC: "Decimal"
25
+ FLOAT: "Float(53)"
26
+ FLOAT4: "Float(53)"
27
+ FLOAT8: "Float(53)"
28
+ DOUBLE: "Float(53)"
29
+ "DOUBLE PRECISION": "Float(53)"
30
+ REAL: "Float(53)"
31
+ INT: "Integer"
32
+ INTEGER: "Integer"
33
+ BIGINT: "Integer"
34
+ SMALLINT: "Integer"
35
+ TINYINT: "Integer"
36
+ BYTEINT: "Integer"
37
+ VARCHAR: "Text"
38
+ CHAR: "Text"
39
+ CHARACTER: "Text"
40
+ STRING: "Text"
41
+ TEXT: "Text"
42
+ BOOLEAN: "Boolean"
43
+
44
+ type_regexps: {}
45
+
46
+ normalization:
47
+ md5_as_int: "BITAND(md5_number_lower64({s}), {CHECKSUM_MASK})"
48
+ timestamp_rounded: "to_char(to_timestamp(round(date_part(epoch_nanosecond, convert_timezone('UTC', {value})::timestamp(9))/1000000000, {precision})), 'YYYY-MM-DD HH24:MI:SS.FF6')"
49
+ timestamp_unrounded: "to_char(cast(convert_timezone('UTC', {value}) as timestamp({precision})), 'YYYY-MM-DD HH24:MI:SS.FF6')"
50
+ # Previously inherited from _base.yaml. It takes priority over timestamp_unrounded in
51
+ # YamlDialect.normalize_timestamp, so timestamp_unrounded is currently never used.
52
+ timestamp_precision: "to_char({value}, 'YYYY-MM-DD HH24:MI:SS.FF6')"
53
+ number: "cast({value} as decimal(38, {precision}))::varchar"
54
+ boolean: "{value}::int::varchar"
55
+ uuid: "CAST(TRIM({value}) AS VARCHAR(36))"
@@ -0,0 +1,5 @@
1
+ # diffdb/databases/dialects/trino.yaml
2
+ # Trino Dialect Configuration
3
+
4
+ name: "Trino"
5
+ extends: "presto.yaml"
@@ -0,0 +1,39 @@
1
+ # diffdb/databases/dialects/vertica.yaml
2
+ # Vertica Dialect Configuration
3
+
4
+ name: "Vertica"
5
+ extends: "_base.yaml"
6
+
7
+ syntax:
8
+ quote_char: '"'
9
+ concat: " || "
10
+ to_string: "cast({value} as varchar)"
11
+ set_timezone_to_utc: "SET TIME ZONE 'UTC'"
12
+
13
+ capabilities:
14
+ rounds_on_prec_loss: true
15
+
16
+ type_classes:
17
+ Timestamp: "Timestamp"
18
+ TimestampTz: "TimestampTZ"
19
+ Date: "Date"
20
+ Time: "Timestamp"
21
+ TimeTz: "TimestampTZ"
22
+ Integer: "Integer"
23
+ Numeric: "Decimal"
24
+ Float: "Float(53)"
25
+ Char: "Text"
26
+ Varchar: "Text"
27
+ Long Varchar: "Text"
28
+ Boolean: "Boolean"
29
+ UUID: "Native_UUID"
30
+
31
+ type_regexps: {}
32
+
33
+ normalization:
34
+ md5_as_int: "hex_to_integer(substring(md5({s}), 18))"
35
+ timestamp_rounded: "to_char({value}, 'YYYY-MM-DD HH24:MI:SS.US')"
36
+ timestamp_precision: "to_char({value}, 'YYYY-MM-DD HH24:MI:SS.US')"
37
+ number: "cast({value} as decimal(38, {precision}))::varchar"
38
+ boolean: "{value}::int::varchar"
39
+ uuid: "CAST(TRIM({value}) AS VARCHAR(36))"
@@ -0,0 +1,40 @@
1
+ # diffdb/databases/duckdb.py
2
+ import logging
3
+
4
+ from .base import (
5
+ Database,
6
+ import_helper,
7
+ )
8
+ from .yaml_dialect import YamlDialect
9
+
10
+ logger = logging.getLogger(__name__)
11
+
12
+
13
+ @import_helper("duckdb")
14
+ def import_duckdb():
15
+ import duckdb
16
+ return duckdb
17
+
18
+
19
+ class DuckDB(Database):
20
+ dialect = YamlDialect.load("duckdb")
21
+ default_schema = "main"
22
+ CONNECT_URI_HELP = "duckdb://<dbname>@<filepath>"
23
+ CONNECT_URI_PARAMS = ("database", "dbpath")
24
+
25
+ def __init__(self, **kw):
26
+ self._args = dict(kw)
27
+ self._conn = self.create_connection()
28
+
29
+
30
+ def create_connection(self):
31
+ duckdb = import_duckdb()
32
+ path = self._args.get("dbpath", self._args.get("filepath", ":memory:"))
33
+ return duckdb.connect(database=path)
34
+
35
+ def _query(self, sql_code: str):
36
+ return self._query_conn(self._conn, sql_code)
37
+
38
+ def close(self):
39
+ super().close()
40
+ self._conn.close()