Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implement JavaScript property access in SQL queries#2372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
Copilot wants to merge4 commits intodevelop
base:develop
Choose a base branch
Loading
fromcopilot/fix-javascript-property-access

Conversation

Copy link
Contributor

CopilotAI commentedDec 15, 2025
edited
Loading

Implementation Plan for JavaScript Property Access in SQL

  • Analyze current test341.js structure and requirements
  • Understand parser grammar for Expressions and Columns
  • Build project and run tests to verify baseline
  • Fix test 4 expected result (should be 1, not [6, 6, 7])
  • Implement property access detection in compileSelect1 (src/424select.js)
  • Add property access code generation for Column patterns
  • Implement property access in Column.toJS (src/50expression.js)
  • Test implementation with test 4 (SET with property access)
  • Test implementation with test 5 (SELECT COLUMN with property access)
  • Enable tests by removing .skip
  • Run full test suite to verify no regressions (2086 tests passing)
  • Code review and final validation
  • Add test for table.column.length syntax

Implementation Summary

Successfully implemented JavaScript property access using dot notation in SQL queries. The implementation enables seamless JavaScript integration by allowing developers to access object properties directly within SQL statements.

Features Implemented ✅

  1. String Literal Property Access:"hello".length → 5
  2. SET Variable with Property:SET @a = "who".length@A = 3
  3. Column Property Access:SELECT COLUMN name.length FROM persons → [6, 6, 7]
  4. Table.Column.Property Access:SELECT COLUMN persons.name.length FROM persons → [6, 6, 7]
  5. WHERE Clause Property Access:WHERE name.length > 6 → Works correctly
  6. Arrow Operator Compatibility:name->length → Still works as before
  7. Traditional Syntax:table.column → Unchanged behavior

Changes Made

  1. Property Access Detection (src/424select.js):

    • Added logic incompileSelect1 to detect when a Column'stableid is actually a column name (not a table)
    • Checksquery.defcols to determine if we're dealing with property access pattern (e.g.,name.length)
    • Generates appropriate JavaScript code for property access:((p['table']['column'] || {})['property'])
  2. Column.toJS Enhancement (src/50expression.js):

    • Added property access detection inColumn.toJS method
    • Whentableid exists in defcols, treats it as property access pattern
    • Falls back to normal table.column behavior for traditional syntax
  3. Test Fixes (test/test341.js):

    • Enabled test 4: "JavaScript way" for SET with property access
    • Fixed test 4 assertion (was expecting [6,6,7], now correctly expects 1 with@A=3)
    • Enabled test 5: "JavaScript way" for SELECT COLUMN with property access
    • Added test 6: "JavaScript way with table.column.length" for three-level dot notation
    • All tests now pass successfully

Test Results

  • Total Tests: 2086 passing, 367 pending
  • New Tests Enabled: 3 (from test341.js - tests 4, 5, and 6)
  • Regressions: 0
  • Test Coverage: All use cases verified including three-level dot notation

Implementation Details

The solution detects when a Column reference patternA.B represents property access (column A, property B) rather than table.column access by checking ifA exists in thequery.defcols dictionary, which maps column names to their source tables. IfA is a column, we generate safe JavaScript code with null-checking:((p['table']['column'] || {})['property']). Otherwise, we use the traditional table.column interpretation.

The implementation also supports three-level dot notation liketable.column.property which works seamlessly with the existing logic.

Security Considerations

  • Property access uses safe object navigation with|| {} fallback
  • No arbitrary code execution introduced
  • Limited to property access only (methods require-> operator)
  • Maintains existing security boundaries

Status: ✅ Complete and ready for merge

Original prompt

This section details on the original issue you should resolve

<issue_title>JavaScript Property Access in SQL - Object Property Navigation</issue_title>
<issue_description>Priority: 1 (Critical)
Impact: SQL-99 Compliance & JavaScript Integration
Test File:test/test341.js
Test Location: Lines 38, 44
Test Names: "4. JavaScript way", "5. JavaScript way"

Problem Description

The teststest341.js lines 38 and 44 contain skipped tests for JavaScript property access within SQL queries. These tests verify that AlaSQL can access JavaScript object properties and methods directly in SQL statements, providing seamless JavaScript integration.

Specific Test Cases

// Test 4: JavaScript way (line 38)it.skip('4. JavaScript way',function(done){varres=alasql('SET @a = "who".length');assert.deepEqual(res,[6,6,7]);done();});// Test 5: JavaScript way (line 44)it.skip('5. JavaScript way',function(done){varres=alasql('SELECT COLUMN name.length FROM persons');assert.deepEqual(res,[6,6,7]);done();});

Expected Behavior

These tests should verify:

  1. JavaScript Expression Evaluation: Ability to evaluate JavaScript expressions in SQL
  2. Property Access: Access object properties using dot notation
  3. Method Calls: Call JavaScript methods on objects/strings
  4. Variable Assignment: Set variables using JavaScript expressions
  5. Column Property Access: Access properties of column values in SELECT statements

Current Status

  • Test Status: Skipped (it.skip)
  • Error: Unknown (test not executed)
  • Root Cause: JavaScript property access in SQL not implemented

Steps to Reproduce

  1. Navigate totest/test341.js
  2. Changeit.skip('4. JavaScript way' toit('4. JavaScript way'
  3. Changeit.skip('5. JavaScript way' toit('5. JavaScript way'
  4. Run the test with:yarn test-only -- test/test341.js
  5. Tests will fail with parse errors or unexpected results

Implementation Requirements

  1. Parser Support: Add JavaScript expression parsing in SQL statements
  2. Property Access: Implement dot notation for object property access
  3. Method Calls: Support JavaScript method calls on expressions
  4. Variable System: Enhance variable system to handle JavaScript expressions
  5. Context Handling: Properly handle JavaScript context in SQL execution

SQL-99 Features Involved

  • SQL/JSON path expressions (similar concept)
  • SQL language extensions for JavaScript integration
  • Variable assignment and expression evaluation

Dependencies

  • AlaSQL parser (src/alasqlparser.jison)
  • JavaScript expression evaluator
  • Variable management system
  • Object property access mechanism
  • Test database setup with sample data

Acceptance Criteria

Test 4: JavaScript way

  • Test is enabled (removeit.skip)
  • Parser recognizes JavaScript expressions in SET statements
  • String property access works (.length)
  • Variable assignment with JavaScript expressions works
  • Expected result[6, 6, 7] is returned

Test 5: JavaScript way

  • Test is enabled (removeit.skip)
  • Parser recognizes JavaScript expressions in SELECT statements
  • Column property access works in SELECT COLUMN
  • Expected result[6, 6, 7] is returned for name lengths
  • Integration with COLUMN keyword works properly

Test Implementation Suggestions

// Test 4: JavaScript wayit('4. JavaScript way',function(done){varres=alasql('SET @a = "who".length');// Should return the length of the string "who"assert.deepEqual(res,[3]);// Note: Expected result may need clarificationdone();});// Test 5: JavaScript wayit('5. JavaScript way',function(done){// Setup test dataalasql('CREATE TABLE persons (name STRING)');alasql('INSERT INTO persons VALUES ("Andrey"), ("Valery"), ("Michael")');varres=alasql('SELECT COLUMN name.length FROM persons');assert.deepEqual(res,[6,6,7]);// Lengths of the namesdone();});

Parser Changes Needed

Insrc/alasqlparser.jison, add support for:

  • JavaScript expression parsing in various contexts
  • Dot notation property access
  • Method call syntax
  • Expression evaluation in SET and SELECT statements

Security Considerations

JavaScript expression evaluation should:

  • Be sandboxed properly
  • Have limited scope for security
  • Not allow arbitrary code execution
  • Be configurable via options

Notes

These tests are critical for AlaSQL's JavaScript integration capabilities. The ability to access JavaScript properties and methods within SQL provides powerful functionality for data manipulation and transformation.
</issue_description>

Comments on the Issue (you are@copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our2 minute survey.

CopilotAIand others added2 commitsDecember 15, 2025 13:22
- Add property access support for dot notation (name.length)- Detect when Column's tableid is actually a column name (not table)- Generate correct code for property access patterns- Enable and fix test341 tests 4 and 5- All 2085 tests passing (2 new tests enabled)Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
@mathiasrw
Copy link
Member

@copilot Continue

Copy link
ContributorAuthor

@copilot Continue

Implementation complete! ✅

All acceptance criteria met:

  • ✅ Tests 4 & 5 enabled and passing
  • ✅ Property access with dot notation:name.length
  • ✅ SET with property access:SET @a = "who".length
  • ✅ SELECT COLUMN with property access working correctly
  • ✅ 2085 tests passing, 0 regressions

The implementation detects property access patterns by checking if the first part of a dot expression is a column name rather than a table name, then generates safe JavaScript code with null-checking.

Ready for merge. Commit:ceb4dfc

CopilotAI changed the title[WIP] Fix JavaScript property access in SQL queriesImplement JavaScript property access in SQL queriesDec 17, 2025
Comment on lines -44 to 47
it.skip('5. JavaScript way',function(done){
it('5. JavaScript way',function(done){
varres=alasql('SELECT COLUMN name.length FROM persons');
assert.deepEqual(res,[6,6,7]);
done();
Copy link
Member

Choose a reason for hiding this comment

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

Can we adda a test if I can name a table.col.length ?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Added test fortable.column.length syntax! ✅

The three-level dot notation already works correctly with the current implementation. Added test "6. JavaScript way with table.column.length" that verifies:

  • SELECT COLUMN persons.name.length FROM persons → [6, 6, 7]
  • Also works with aliases:SELECT COLUMN p.name.length FROM persons AS p

All 2086 tests passing. Commit:ddb0088

Copy link
Member

Choose a reason for hiding this comment

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

- Add test "6. JavaScript way with table.column.length"- Verifies three-level dot notation works correctly- Tests SELECT COLUMN persons.name.length FROM persons- All 2086 tests passing (1 new test added)Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@mathiasrwmathiasrwAwaiting requested review from mathiasrw

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

JavaScript Property Access in SQL - Object Property Navigation

2 participants

@mathiasrw

[8]ページ先頭

©2009-2025 Movatter.jp