This is an automated email from the ASF dual-hosted git repository.

gyeongtae pushed a commit to branch branch-0.12
in repository https://gitbox.apache.org/repos/asf/zeppelin.git


The following commit(s) were added to refs/heads/branch-0.12 by this push:
     new 664c878105 [ZEPPELIN-6234] fix: table() function not working
664c878105 is described below

commit 664c878105c62bbbbe009949d9e548a07f736d7d
Author: Yuijin Kim(yuikim) <[email protected]>
AuthorDate: Thu Aug 28 22:09:07 2025 +0900

    [ZEPPELIN-6234] fix: table() function not working
    
    ### What is this PR for?
    This PR introduces the ensureTableFunction() method to enhance 
compatibility with mongosh (MongoDB Shell) in the Apache Zeppelin Mongo 
interpreter context. Previously, the .table() function was attached to 
DBQuery.prototype, which may not be sufficient in newer MongoDB environments 
where command cursors or altered prototypes are used.
    The new method safely injects the .table() utility function into relevant 
cursor prototypes at runtime, ensuring table rendering works as expected even 
under mongosh.
    
    Additionally, the changes:
    
    - Introduce safer prototype extension using a dummy cursor check.
    - Preserve the original behavior for backwards compatibility.
    - Avoid redundant registration in unsupported environments.
    
    ### What type of PR is it?
    Bug Fix
    
    ### Todos
    * [x] - Add ensureTableFunction() for dynamic prototype injection
    
    ### What is the Jira issue?
    * [ZEPPELIN-6234](https://issues.apache.org/jira/browse/ZEPPELIN-6234): 
MongoDB interpreter: .table() function not working
    
    ### How should this be tested?
    1. Use Apache Zeppelin with the MongoDB interpreter.
    2. Connect to a MongoDB 8.x instance using mongosh.
    3. Execute a standard query with .table():
    ```js
    db.collection.find().table()
    ```
    
    4. Verify:
    - `%table` format renders properly in Zeppelin UI.
    - No registration or prototype errors occur in shell.
    - Legacy Mongo Shells continue to function as before.
    
    ### Screenshots (if appropriate)
    <img width="510" height="270" alt="Pasted Graphic" 
src="https://github.com/user-attachments/assets/9da70f3e-739a-46fa-819a-b8877de213cc";
 />
    
    ### Questions:
    * Does the license files need to update? No
    * Is there breaking changes for older versions? No. The solution maintains 
backward compatibility.
    * Does this needs documentation? Optional. Could be mentioned in Mongo 
interpreter tips or mongosh compatibility notes.
    
    Closes #4987 from kmularise/ZEPPELIN-6234.
    
    Signed-off-by: ParkGyeongTae <[email protected]>
    (cherry picked from commit ade854e2e82fb2834a138485a5d4316edec0c83f)
    Signed-off-by: ParkGyeongTae <[email protected]>
---
 mongodb/src/main/resources/shell_extension.js | 37 ++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 7 deletions(-)

diff --git a/mongodb/src/main/resources/shell_extension.js 
b/mongodb/src/main/resources/shell_extension.js
index ddaf2fc767..14a0d19b8c 100644
--- a/mongodb/src/main/resources/shell_extension.js
+++ b/mongodb/src/main/resources/shell_extension.js
@@ -97,15 +97,38 @@ function printTable(dbquery, fields, flattenArray) {
     });
 }
 
-(DBQuery.prototype || DBQuery).table = function (fields, flattenArray) {
-    if (this._limit > tableLimit) {
-        this.limit(tableLimit);
+function ensureTableFunction() {
+    (DBQuery.prototype || DBQuery).table = function (fields, flattenArray) {
+        if (this._limit > tableLimit) {
+            this.limit(tableLimit);
+        }
+        printTable(this, fields, flattenArray);
+    };
+
+    if (globalThis.DBCommandCursor)
+        (DBCommandCursor.prototype || DBCommandCursor).table = 
(DBQuery.prototype || DBQuery).table;
+
+    var tableFunc = function(fields, flattenArray) {
+        if (this._limit > tableLimit) {
+            this.limit(tableLimit);
+        }
+        printTable(this, fields, flattenArray);
+        return this;
+    };
+
+    try {
+        var sampleCursor = db.getCollection('__dummy__').find({}).limit(0);
+        var proto = Object.getPrototypeOf(sampleCursor);
+
+        if (proto && !proto.table) {
+            proto.table = tableFunc;
+        }
+    } catch (e) {
+        print("Registration Error: prototype registration failed:", e.message);
     }
-    printTable(this, fields, flattenArray);
-};
+}
 
-if (globalThis.DBCommandCursor)
-    (DBCommandCursor.prototype || DBCommandCursor).table = (DBQuery.prototype 
|| DBQuery).table;
+ensureTableFunction();
 
 var userName = "USER_NAME_PLACEHOLDER";
 var password = "PASSWORD_PLACEHOLDER";

Reply via email to