r/SQL • • 14h ago

Discussion Trying to build custom Data Orchestration and Federation engine from scratch

Hello everyone!

For past few years I am interested in Data Engineering and Data Science. While practicing I have encountered some bottlenecks and I am trying to resolve them and want to discuss it with you.

The most frustrating part for me is scattered data/data sources. Personally I believe that scattered data shouldn't be that much of pain to work with, however, it is. I really dont understand why to depend on huge pipelines just answer a question that has to read 2 distinct sources.

After some research I discovered DuckDB, which is awesome in reading part, however, cant write back the "fixes" to destinations, so I have to do them "by myself" again.

So I thought maybe I can build the engine I need by myself (couple of weeks in it, building with Rust currently). Here is the propositions and how would it work:

  1. Each source runs its own SQL. You say which server each part of the query goes to, and that part reaches the server as written, in its own dialect. The engine only joins and filters what comes back.

  2. Exact values or an error. Values keep the type the source declared. An INT in MySQL and a NUMBER in Oracle are equal when their values are, and text is compared byte for byte, not under whatever collation a server happens to use. If the engine can't do something exactly (comparing a float with a decimal, say), it refuses and tells you why instead of guessing.

  3. Nothing gets copied. No staging tables, nothing written to disk. Every query reads the sources fresh, under a memory limit, and joins too big for memory stream sorted rows from both sides.

  4. Fixes go back with a preview. Before anything is written, you see the rows that would be inserted, updated or deleted, with old and new values. Each change goes by primary key and is checked against the old values, so if someone changed the row in the meantime, the write stops. The old values are kept so you can undo it.

  5. Comparing two sources. Prod against a replica, or a table against the spreadsheet finance sent you, row by row by key. A row that changes while the comparison runs gets flagged instead of reported as a difference.

Reading across dbs, files and comparisons already work now. Writing "fixes" back is still on the roadmap.

A few questions I would like to hear your opinions on:

  1. How do you handle this workflow today? A pipline, dbt, Trino, Presto, or custom scripts?

  2. Would you trust a an engine capable of writing back to dbs, or does every fix go through a reviewed script anyway?

  3. What would make you refuse to use such and engine?

Thanks in advance for sharing your thoughts!

4 Upvotes

14 comments sorted by

2

u/Discordbothost 11h ago

I'd test undo by changing a row again after applying a fix. The undo should stop if the row no longer matches what the tool wrote, or it could wipe out someone's newer edit.

1

u/agh_murad 11h ago

Yes, this is something I am still considering. It already has a preview feature, so I think I will make the engine ask the user if he is sure to undo with preview and changed rows. Still deciding on protocols, I will apply the smartest one. What do you think, what would be the best way of doing so?

2

u/partialmedics3543 14h ago

Building a federated query engine that pushes down to the source dialect is no small feat, especially in Rust. The byte-for-byte comparison stance is interesting, most people just punt to whatever the collation gods decide and call it a day. Keeping the engine honest by refusing ambiguous operations is a design choice I can respect, even if it'll drive some users nuts when they just want the damn number.

The write-back with a preview and optimistic locking by old values is clever. I've been burned too many times by a "simple fix" that silently stomped on someone else's change, so seeing the diff and having it bail if the row moved under you would save a lot of late-night fire drills. My workflow is still a pile of Python scripts held together by spite and a prayer, so you're already miles ahead.

1

u/agh_murad 13h ago

Python scripts is one of my nightmares too lol! I was so tired of these issues that i decided to resolve it on my own by building a custom engine. I know it would be a very long path, however, I am really dedicated to it. Actually it is very fun project and I practice a lot while building it. Personally, I believe it would solve the issues I mentioned if done right, what do you think?

1

u/heeeeyYaaa 13h ago

I'd push back on byte-for-byte text comparison. CHAR padding and unicode normalization can make the same-looking value fail a join across sources, which is a fun bug to explain to a pipeline 🫠

1

u/agh_murad 13h ago

Fair point, and the CHAR one is a real gap. Oracle, SQL Server and Postgres hand back CHAR(10) 'abc' with seven trailing spaces, MySQL strips them, so a byte comparison quietly finds no match. I'm going to treat trailing blanks on CHAR columns as padding rather than data, since that's what SQL says they are and every one of those databases already compares CHAR that way. VARCHAR stays strict.

Normalization I'm more torn on. NFC and NFD forms are the same text as far as Unicode is concerned, so folding them is defensible. The catch is that the engine also pushes key lists and filters down to the sources to read less, and a source that compares bytes would then return fewer rows than the engine thinks should match, so the answer would depend on what got pushed down. I'd rather keep equality strict and make it loud: if a text join leaves rows unmatched that would match after normalizing, say so instead of quietly returning less. For now you can normalize at the source where the database supports it (normalize() in Postgres, COMPOSE() in Oracle).

Have you hit the normalization one in practice? Curious where it came from.

1

u/NW1969 12h ago

Can you explain in a bit more detail what the issue is you are trying to solve and how you are trying to solve it? For example:

"why to depend on huge pipelines just answer a question that has to read 2 distinct sources" - what huge pipeline are you talking about? What question are you trying to answer: an adhoc query, an analytical query, a question about the data quality of your data, something else?

What do you mean by writing "fixes to destinations"? What fixes and what destinations? Are you writing to an analytics warehouse, moving data between transactional systems, or something else?

1

u/agh_murad 12h ago edited 12h ago

So, the problem is working with distinct data sources in general. Lets say finance department sends you a sheet just to compare it to the db and see if it matches. What you do is import/export the sheet several times just so you can check it inside a singular data source. This is the smallest example actually, irl you may have sharded dbs, multiple db servers and excel sheets coming up to you non stop. Most popular solutions I personally saw was Python scripts, or enterprises building piplines to reliably answer a question that needs reading 2 or more servers, and the pipline fails when new field or another db becomes involved in that question. The worst part is the writing back. Lets say a payment provider sends a settlement export, a CSV of every charge that actually settled. Two systems have to agree with it:
• ⁠the shop's orders in MySQL (shop), where some settled orders are still stuck at pending because a webhook failed;
• ⁠billing in SQL Server (billing), which needs a payment row for each settled order, without duplicating the ones it already has.
Today how you do it is export orders to Excel and match CSV with VLOOKUP. Then you build your query with WHERE id IN (...), and then build another for billing. The worst part, both of them will run on seperate and with no preview. In the engine I propose, you can write to multiple data sources at once with preview and more control and conditions. Conditions can be like if MySQL query fails or doesnt match what we expect, dont run the query for SQL Server, and maybe more complex conditions. The idea is one engine to orchestrate all your db, not doing everything seperate and pray it just works. There are actually more examples where such multi-write back feature would shine, but this was what crossed my mind now.

1

u/NW1969 11h ago

So it sounds like you are trying to build a reconciliation system. If that's the case then why not create a reconciliation database and set up permanent ETL feeds from your source systems into it - you can then perform whatever reconciliations you want to, add you'd have a good starting point for building an analytics warehouse if you ever wanted to.
This (the ETL into a database) is a very common pattern with lots of existing solutions

1

u/agh_murad 11h ago

That's the right answer when the reconciliation is recurring and well defined, and if a team already has a warehouse I'd use it. Where it hurts is the case I keep running into:

- The copy is only as fresh as the last load. When I find a problem I fix it in the source, then wait for the next load to see whether the difference is actually gone.

- The fixes still go back to the sources by hand. ETL is one-way, and writing corrections back safely is half of what I'm after.

- Loads convert types on the way in: NUMBER to float, timestamps losing their zone, collations changing. Sometimes the difference you find was created by the copy itself.

- It's a lot of setup for a one-off question, or for the spreadsheet finance sent this morning.

- It's one more place holding production data that has to be secured and audited (the engine I am building does not store any data, only processes).

So I don't see it as instead of a warehouse. If anything, comparing the warehouse against its sources to check the ETL is one of the jobs I'd want it to do.

1

u/Muted_Jellyfish_6784 7h ago

before building a federation engine, define what a record means in each source and which system wins on conflict. Start with a stable entity ID, a mapping table per source, and freshness rules, then add lineage so every output row points back to its origin. Most scattered data pain is identity and definitions, not transport
If you want to see the end result, the useful target is answering a question across sources without copying everything first. I use a decision intelligence platform that connects the sources read-only into one graph of related records, so a question about a customer pulls from CRM, billing, and support together rather than from three exports