Skip to content

wip: rust impl - #194

Merged
evinism merged 61 commits into
mainfrom
ds/rust
Sep 21, 2025
Merged

evinism merged 61 commits into
mainfrom
ds/rust

Conversation

@dshemetov

@dshemetov dshemetov commented Sep 7, 2025 •

Copy link
Copy Markdown
Collaborator

(Note for readers, there is a lot of private conversation around this PR not tracked on GH.)

TODO

  • Fix all failing shared tests
  • Address review comments
  • CLI interface
  • Fix parser discrepancies
    • I think array and object need to be in literal, see parse_simplevalue
    • Add some convenience units in parser.rs (reusable combinator blocks)?
  • Remove unused dependencies
  • Improve serde <-> Rust <-> MistQL type conversions

Future PR stuff in #70

@netlify

netlify Bot commented Sep 7, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for mistql canceled.

Name Link
🔨 Latest commit 9c203ab
🔍 Latest deploy log https://app.netlify.com/projects/mistql/deploys/68cfadb0232c6d00089bff89

@evinism
evinism requested a review from Copilot September 7, 2025 01:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a work-in-progress Rust implementation of MistQL, a miniature query language for JSON-like structures. The implementation provides core language features including a parser, executor, built-in functions, and type system that matches the JavaScript reference implementation's behavior.

Key changes include:

  • Complete runtime type system with 8 core MistQL types (null, boolean, number, string, object, array, function, regex)
  • Recursive descent parser using nom combinators that handles operator precedence and MistQL syntax
  • Execution engine with context management and built-in function support

Reviewed Changes

Copilot reviewed 15 out of 18 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
rust/src/types.rs Implements RuntimeValue enum and MistQLRegex wrapper with conversion, equality, and serialization
rust/src/parser.rs Nom-based parser converting MistQL syntax to AST with proper operator precedence
rust/src/executor.rs Expression execution engine with context stack management and built-in function calls
rust/src/builtins.rs Comprehensive built-in function library including array, object, string, and math operations
rust/src/lib.rs Main library interface providing the public query function
rust/src/tests.rs Test suite loader that runs language-independent tests from shared directory
rust/src/lexer.rs Lexical analyzer for tokenizing MistQL source code with position tracking
rust/src/instance.rs Placeholder for future MistQL instance management (not implemented)
rust/src/errors.rs Basic error type definitions using thiserror
Comments suppressed due to low confidence (2)

rust/src/parser.rs:1

  • The comment on line 371 indicates that unknown escape sequences are passed through literally. This differs from the more comprehensive Unicode handling in the nom parser version (lines 304-332). Consider consolidating escape sequence handling for consistency.
//! Parser implementation for MistQL syntax

rust/src/builtins.rs:1

  • Using fract() == 0.0 to check for integers can fail for very large floating-point numbers where the fractional part cannot be represented precisely. Consider using n.trunc() == n or checking if the number is within the valid integer range before casting.
//! Built-in functions for MistQL

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread rust/src/types.rs Resolved
Comment thread rust/src/executor.rs Resolved
Comment thread rust/src/builtins.rs Resolved
Comment thread rust/src/tests.rs Resolved

@evinism evinism left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously this is a WIP branch, but I'm gonna go through and give my in progress thoughts anyways.

Comment thread rust/src/instance.rs Resolved
Comment thread rust/src/lib.rs Resolved
Comment thread rust/src/lib.rs Resolved
Comment thread rust/src/parser.rs Resolved
Comment thread rust/src/parser.rs Resolved
Comment thread rust/src/parser.rs Resolved
Comment thread rust/src/types.rs Resolved

@evinism evinism left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

howdy dmitry, lots of scattered thoughts, all of which can be argued. this is killer, thank you so much.

Comment thread rust/src/builtins.rs Resolved
Comment thread rust/src/builtins.rs Resolved
Comment thread rust/src/executor.rs Resolved
Comment thread rust/src/executor.rs
let mut context = Self {
stack: Vec::new(),
builtins,
root_data: data.clone(),

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a little wild to have two full copies of the initial data around. Maybe there's some reorg we can do to reduce this?

We can save this for a performance pass, which I'll label as "Performance:" comments.

Comment thread rust/src/executor.rs
let mut data = execute_expression(&stages[0], context)?;

for stage in &stages[1..] {
context.push_context(data.clone());

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance: Okay I'm gonna stop pointing out excessive clones because i'm realizing they're everywhere but it'd also be silly to block merge on them.

Comment thread rust/src/types.rs Resolved
Comment thread rust/src/types.rs Resolved
Comment thread rust/src/types.rs Outdated

impl RuntimeValue {
// Convert from serde_json::Value to RuntimeValue
pub fn from_serde_value(value: &serde_json::Value) -> Result<Self, ExecutionError> {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of me wants to separate out the serde_json translation layer from being in impl RuntimeValue.

serde_json is certainly going to be the primary way users use the rust interface, but separating it out from the main impl block into a separate trait feels right. In a perfect world, we can run and install MistQL without requiring serde_json, although that's more a vibe thing than something that I actually think people would use.

@dshemetov dshemetov Sep 21, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled this out into another function, it doesn't look very Rusty though. I think my challenge here is (a) I'm still new to the Rusty way of doing things, (b) I don't have good vision of what downstream usage of MistQL Rust as a client library is going to look like (almost entirely thinking about CLI usage).

Comment thread rust/src/types.rs
}

// Convert to serde_json::Value
pub fn to_serde_value(&self, permissive: bool) -> Result<serde_json::Value, ExecutionError> {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An interesting question is that the mistql docs mark functions and regexes as non-external types, but the query method as it is written does allow them to be passed back without filtering, and we only error on conversion to serde_json. Should we explicitly block outputting those values?

My vote would be "yes, explicitly filter them out, because it's easier from a backwards compatibility perspective." but I can be swayed. Could also imagine returning a QueryResult rather than a RuntimeValue. At that point, we can pretty much guarantee an Into<> trait rather than a TryInto<> trait for QueryResult -> serde_json::Value

@dshemetov dshemetov Sep 21, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So query returns RuntimeValue, but the new cli can print both RuntimeValue (for debug purposes), which is happy to return non-extern stuff, and result.to_serde(permissive=False), which isn't (and I think that's more compatible with the other libraries' behavior?). Again, feeling like I can imagine the CLI use cases of this, but client library uses feel murky, so I don't have strong opinions on that. You might have a better sense here, in which case I'll just defer to you, otherwise we can advertise this as CLI only for now and buy some time to think through the client library requirements?

Comment thread rust/src/bin/mq.rs Resolved
@dshemetov dshemetov mentioned this pull request Sep 21, 2025
* consolidate builtin handling
* consolidate contextualized expression execution among builtins
* add CustomFunction support
* simplify ExecutionContext builtins and custom function handling
* run through clippy and lint
Comment thread rust/src/types.rs Resolved
@evinism

evinism commented Sep 21, 2025

Copy link
Copy Markdown
Owner

As mentioned in the tracking issue, we should add a little note to the readme saying what the rust directory is and have a link to the issue. Other than that, I'm gonna say this is good to merge.

@evinism
evinism merged commit 23d58ac into main Sep 21, 2025
@dshemetov
dshemetov deleted the ds/rust branch September 21, 2025 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants